2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr_mem.c,v 1.23 2003-03-18 13:34:35 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 char *odr_strdupn(ODR o, const char *str, size_t n)
42 return nmem_strdupn(o->mem, str, n);
45 int *odr_intdup(ODR o, int v)
47 return nmem_intdup(o->mem, v);
52 return o->mem ? nmem_total(o->mem) : 0;
55 /* ---------- memory management for data encoding ----------*/
58 int odr_grow_block(ODR b, int min_bytes)
68 if (togrow < min_bytes)
70 if (b->size && !(b->buf =
71 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
73 else if (!b->size && !(b->buf = (unsigned char *)
74 xmalloc(b->size = togrow)))
77 fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
82 int odr_write(ODR o, unsigned char *buf, int bytes)
84 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
86 odr_seterror(o, OSPACE, 40);
89 memcpy(o->buf + o->pos, buf, bytes);
96 int odr_seek(ODR o, int whence, int offset)
98 if (whence == ODR_S_CUR)
100 else if (whence == ODR_S_END)
102 if (offset > o->size && odr_grow_block(o, offset - o->size))
104 odr_seterror(o, OSPACE, 41);