2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.4 2005-01-15 19:47:14 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 /* ---------- memory management for data encoding ----------*/
66 int odr_grow_block(ODR b, int min_bytes)
76 if (togrow < min_bytes)
78 if (b->size && !(b->buf =
79 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
81 else if (!b->size && !(b->buf = (unsigned char *)
82 xmalloc(b->size = togrow)))
85 fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
90 int odr_write(ODR o, unsigned char *buf, int bytes)
92 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
94 odr_seterror(o, OSPACE, 40);
97 memcpy(o->buf + o->pos, buf, bytes);
104 int odr_seek(ODR o, int whence, int offset)
106 if (whence == ODR_S_CUR)
108 else if (whence == ODR_S_END)
110 if (offset > o->size && odr_grow_block(o, offset - o->size))
112 odr_seterror(o, OSPACE, 41);