2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.12 2007-09-11 08:35:42 adam Exp $
9 * \brief Implements ODR memory management
17 #include <yaz/xmalloc.h>
19 /* ------------------------ NIBBLE MEMORY ---------------------- */
22 * Extract the memory control block from o.
24 NMEM odr_extract_mem(ODR o)
28 o->mem = nmem_create();
32 void *odr_malloc(ODR o, int size)
34 return nmem_malloc(o->mem, size);
37 char *odr_strdup(ODR o, const char *str)
39 return nmem_strdup(o->mem, str);
42 char *odr_strdup_null(ODR o, const char *str)
44 return nmem_strdup_null(o->mem, str);
47 char *odr_strdupn(ODR o, const char *str, size_t n)
49 return nmem_strdupn(o->mem, str, n);
52 int *odr_intdup(ODR o, int v)
54 return nmem_intdup(o->mem, v);
59 return nmem_total(o->mem);
62 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
64 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
65 p->buf = (unsigned char *) odr_malloc(o, sz);
66 memcpy(p->buf, buf, sz);
72 /* ---------- memory management for data encoding ----------*/
75 int odr_grow_block(ODR b, int min_bytes)
85 if (togrow < min_bytes)
87 if (b->size && !(b->buf =
88 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
90 else if (!b->size && !(b->buf = (unsigned char *)
91 xmalloc(b->size = togrow)))
96 int odr_write(ODR o, unsigned char *buf, int bytes)
98 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
100 odr_seterror(o, OSPACE, 40);
103 memcpy(o->buf + o->pos, buf, bytes);
110 int odr_seek(ODR o, int whence, int offset)
112 if (whence == ODR_S_CUR)
114 else if (whence == ODR_S_END)
116 if (offset > o->size && odr_grow_block(o, offset - o->size))
118 odr_seterror(o, OSPACE, 41);
127 * indent-tabs-mode: nil
129 * vim: shiftwidth=4 tabstop=8 expandtab