YAZ  4.2.57
odr_mem.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <stdlib.h>
14 #include "odr-priv.h"
15 #include <yaz/xmalloc.h>
16 
17 /* ------------------------ NIBBLE MEMORY ---------------------- */
18 
19 /*
20  * Extract the memory control block from o.
21  */
23 {
24  NMEM r = o->mem;
25 
26  o->mem = nmem_create();
27  return r;
28 }
29 
30 void *odr_malloc(ODR o, size_t size)
31 {
32  return nmem_malloc(o->mem, size);
33 }
34 
35 char *odr_strdup(ODR o, const char *str)
36 {
37  return nmem_strdup(o->mem, str);
38 }
39 
40 char *odr_strdup_null(ODR o, const char *str)
41 {
42  return nmem_strdup_null(o->mem, str);
43 }
44 
45 char *odr_strdupn(ODR o, const char *str, size_t n)
46 {
47  return nmem_strdupn(o->mem, str, n);
48 }
49 
51 {
52  return nmem_intdup(o->mem, v);
53 }
54 
56 {
57  return nmem_booldup(o->mem, v);
58 }
59 
60 size_t odr_total(ODR o)
61 {
62  return nmem_total(o->mem);
63 }
64 
65 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
66 {
67  Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
68  p->buf = (unsigned char *) odr_malloc(o, sz);
69  memcpy(p->buf, buf, sz);
70  p->size = sz;
71  p->len = sz;
72  return p;
73 }
74 
75 /* ---------- memory management for data encoding ----------*/
76 
77 
78 int odr_grow_block(ODR b, int min_bytes)
79 {
80  int togrow;
81 
82  if (!b->op->can_grow)
83  return -1;
84  if (!b->size)
85  togrow = 1024;
86  else
87  togrow = b->size;
88  if (togrow < min_bytes)
89  togrow = min_bytes;
90  if (b->size && !(b->buf =
91  (unsigned char *) xrealloc(b->buf, b->size += togrow)))
92  abort();
93  else if (!b->size && !(b->buf = (unsigned char *)
94  xmalloc(b->size = togrow)))
95  abort();
96  return 0;
97 }
98 
99 int odr_write2(ODR o, const char *buf, int bytes)
100 {
101  if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
102  {
103  odr_seterror(o, OSPACE, 40);
104  return -1;
105  }
106  memcpy(o->buf + o->pos, buf, bytes);
107  o->pos += bytes;
108  if (o->pos > o->top)
109  o->top = o->pos;
110  return 0;
111 }
112 
113 int odr_write(ODR o, unsigned char *buf, int bytes)
114 {
115  return odr_write2(o, (char *) buf, bytes);
116 }
117 
118 int odr_seek(ODR o, int whence, int offset)
119 {
120  if (whence == ODR_S_CUR)
121  offset += o->pos;
122  else if (whence == ODR_S_END)
123  offset += o->top;
124  if (offset > o->size && odr_grow_block(o, offset - o->size))
125  {
126  odr_seterror(o, OSPACE, 41);
127  return -1;
128  }
129  o->pos = offset;
130  return 0;
131 }
132 
133 Odr_int odr_strtol(const char *nptr, char **endptr, int base)
134 {
135 #if NMEM_64
136 #if WIN32
137  return _strtoui64(nptr, endptr, base);
138 #else
139  return strtoll(nptr, endptr, base);
140 #endif
141 
142 #else
143  return strtol(nptr, endptr, base);
144 #endif
145 }
146 
147 Odr_int odr_atoi(const char *s)
148 {
149  char *endptr;
150  return odr_strtol(s, &endptr, 10);
151 }
152 
153 /*
154  * Local variables:
155  * c-basic-offset: 4
156  * c-file-style: "Stroustrup"
157  * indent-tabs-mode: nil
158  * End:
159  * vim: shiftwidth=4 tabstop=8 expandtab
160  */
161