2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.10 2007-04-12 13:52:57 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_strdupn(ODR o, const char *str, size_t n)
44 return nmem_strdupn(o->mem, str, n);
47 int *odr_intdup(ODR o, int v)
49 return nmem_intdup(o->mem, v);
54 return nmem_total(o->mem);
57 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
59 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
60 p->buf = odr_malloc(o, sz);
61 memcpy(p->buf, buf, sz);
67 /* ---------- memory management for data encoding ----------*/
70 int odr_grow_block(ODR b, int min_bytes)
80 if (togrow < min_bytes)
82 if (b->size && !(b->buf =
83 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
85 else if (!b->size && !(b->buf = (unsigned char *)
86 xmalloc(b->size = togrow)))
91 int odr_write(ODR o, unsigned char *buf, int bytes)
93 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
95 odr_seterror(o, OSPACE, 40);
98 memcpy(o->buf + o->pos, buf, bytes);
105 int odr_seek(ODR o, int whence, int offset)
107 if (whence == ODR_S_CUR)
109 else if (whence == ODR_S_END)
111 if (offset > o->size && odr_grow_block(o, offset - o->size))
113 odr_seterror(o, OSPACE, 41);
122 * indent-tabs-mode: nil
124 * vim: shiftwidth=4 tabstop=8 expandtab