1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ODR memory management
15 #include <yaz/xmalloc.h>
17 /* ------------------------ NIBBLE MEMORY ---------------------- */
20 * Extract the memory control block from o.
22 NMEM odr_extract_mem(ODR o)
26 o->mem = nmem_create();
30 void *odr_malloc(ODR o, int size)
32 return nmem_malloc(o->mem, size);
35 char *odr_strdup(ODR o, const char *str)
37 return nmem_strdup(o->mem, str);
40 char *odr_strdup_null(ODR o, const char *str)
42 return nmem_strdup_null(o->mem, str);
45 char *odr_strdupn(ODR o, const char *str, size_t n)
47 return nmem_strdupn(o->mem, str, n);
50 int *odr_intdup(ODR o, int v)
52 return nmem_intdup(o->mem, v);
57 return nmem_total(o->mem);
60 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
62 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
63 p->buf = (unsigned char *) odr_malloc(o, sz);
64 memcpy(p->buf, buf, sz);
70 /* ---------- memory management for data encoding ----------*/
73 int odr_grow_block(ODR b, int min_bytes)
83 if (togrow < min_bytes)
85 if (b->size && !(b->buf =
86 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
88 else if (!b->size && !(b->buf = (unsigned char *)
89 xmalloc(b->size = togrow)))
94 int odr_write(ODR o, unsigned char *buf, int bytes)
96 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
98 odr_seterror(o, OSPACE, 40);
101 memcpy(o->buf + o->pos, buf, bytes);
108 int odr_seek(ODR o, int whence, int offset)
110 if (whence == ODR_S_CUR)
112 else if (whence == ODR_S_END)
114 if (offset > o->size && odr_grow_block(o, offset - o->size))
116 odr_seterror(o, OSPACE, 41);
125 * indent-tabs-mode: nil
127 * vim: shiftwidth=4 tabstop=8 expandtab