2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.7 2006-02-19 18:33:09 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)
32 void *odr_malloc(ODR o, int size)
35 o->mem = nmem_create();
36 return nmem_malloc(o ? o->mem : 0, size);
39 char *odr_strdup(ODR o, const char *str)
42 o->mem = nmem_create();
43 return nmem_strdup(o->mem, str);
46 char *odr_strdupn(ODR o, const char *str, size_t n)
48 return nmem_strdupn(o->mem, str, n);
51 int *odr_intdup(ODR o, int v)
54 o->mem = nmem_create();
55 return nmem_intdup(o->mem, v);
60 return o->mem ? nmem_total(o->mem) : 0;
63 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
65 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
66 p->buf = odr_malloc(o, sz);
67 memcpy(p->buf, buf, sz);
73 /* ---------- memory management for data encoding ----------*/
76 int odr_grow_block(ODR b, int min_bytes)
86 if (togrow < min_bytes)
88 if (b->size && !(b->buf =
89 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
91 else if (!b->size && !(b->buf = (unsigned char *)
92 xmalloc(b->size = togrow)))
97 int odr_write(ODR o, unsigned char *buf, int bytes)
99 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
101 odr_seterror(o, OSPACE, 40);
104 memcpy(o->buf + o->pos, buf, bytes);
111 int odr_seek(ODR o, int whence, int offset)
113 if (whence == ODR_S_CUR)
115 else if (whence == ODR_S_END)
117 if (offset > o->size && odr_grow_block(o, offset - o->size))
119 odr_seterror(o, OSPACE, 41);
128 * indent-tabs-mode: nil
130 * vim: shiftwidth=4 tabstop=8 expandtab