2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.22 2003-03-11 11:03:31 adam Exp $
13 #include <yaz/xmalloc.h>
15 /* ------------------------ NIBBLE MEMORY ---------------------- */
18 * Extract the memory control block from o.
20 NMEM odr_extract_mem(ODR o)
28 void *odr_malloc(ODR o, int size)
31 o->mem = nmem_create();
32 return nmem_malloc(o ? o->mem : 0, size);
35 char *odr_strdup(ODR o, const char *str)
37 return nmem_strdup(o->mem, str);
40 int *odr_intdup(ODR o, int v)
42 return nmem_intdup(o->mem, v);
47 return o->mem ? nmem_total(o->mem) : 0;
50 /* ---------- memory management for data encoding ----------*/
53 int odr_grow_block(ODR b, int min_bytes)
63 if (togrow < min_bytes)
65 if (b->size && !(b->buf =
66 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
68 else if (!b->size && !(b->buf = (unsigned char *)
69 xmalloc(b->size = togrow)))
72 fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
77 int odr_write(ODR o, unsigned char *buf, int bytes)
79 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
81 odr_seterror(o, OSPACE, 40);
84 memcpy(o->buf + o->pos, buf, bytes);
91 int odr_seek(ODR o, int whence, int offset)
93 if (whence == ODR_S_CUR)
95 else if (whence == ODR_S_END)
97 if (offset > o->size && odr_grow_block(o, offset - o->size))
99 odr_seterror(o, OSPACE, 41);