X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=odr%2Fodr_mem.c;h=a032d4b3f152bcccb60efe8e3f787c68657b674c;hp=bc48fcda2e9e6a4dfed749dcd08955604d8a9a9e;hb=4d531a1a9131d69c3b6c27fbac42837e22cff61c;hpb=99268f722648e84bf5c54eb685a1434e100f38a1 diff --git a/odr/odr_mem.c b/odr/odr_mem.c index bc48fcd..a032d4b 100644 --- a/odr/odr_mem.c +++ b/odr/odr_mem.c @@ -1,131 +1,56 @@ /* - * Copyright (c) 1995, Index Data + * Copyright (c) 1995-2003, Index Data * See the file LICENSE for details. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: odr_mem.c,v $ - * Revision 1.5 1995-05-16 08:50:55 quinn - * License, documentation, and memory fixes - * - * Revision 1.4 1995/05/15 11:56:09 quinn - * More work on memory management. - * - * Revision 1.3 1995/04/18 08:15:21 quinn - * Added dynamic memory allocation on encoding (whew). Code is now somewhat - * neater. We'll make the same change for decoding one day. - * - * Revision 1.2 1995/03/17 10:17:52 quinn - * Added memory management. - * - * Revision 1.1 1995/03/14 10:27:40 quinn - * Modified makefile to use common lib - * Beginning to add memory management to odr * + * $Id: odr_mem.c,v 1.21 2003-01-06 08:20:27 adam Exp $ */ +#if HAVE_CONFIG_H +#include +#endif #include -#include -#include +#include "odr-priv.h" +#include /* ------------------------ NIBBLE MEMORY ---------------------- */ -#define ODR_MEM_CHUNK (10*1024) - -typedef struct odr_memblock -{ - char *buf; - int size; - int top; - struct odr_memblock *next; -} odr_memblock; - -static odr_memblock *freelist = 0; /* global freelist */ - -static void free_block(odr_memblock *p) -{ - p->next = freelist; - freelist = p; -} - /* - * acquire a block with a minimum of size free bytes. + * Extract the memory control block from o. */ -static odr_memblock *get_block(int size) +NMEM odr_extract_mem(ODR o) { - odr_memblock *r, *l; + NMEM r = o->mem; - for (r = freelist, l = 0; r; l = r, r = r->next) - if (r->size >= size) - break; - if (r) - if (l) - l->next = r->next; - else - freelist = r->next; - else - { - int get = ODR_MEM_CHUNK; - - if (get < size) - get = size; - if (!(r = malloc(sizeof(*r)))) - abort(); - if (!(r->buf = malloc(r->size = get))) - abort(); - } - r->top = 0; + o->mem = 0; return r; } -/* - * Return p to the global freelist. - */ -void odr_release_mem(ODR_MEM p) +void *odr_malloc(ODR o, int size) { - odr_memblock *t; - - while (p) - { - t = p; - p = p->next; - free_block(t); - } + if (o && !o->mem) + o->mem = nmem_create(); + return nmem_malloc(o ? o->mem : 0, size); } -/* - * Extract the memory control block from o. - */ -ODR_MEM odr_extract_mem(ODR o) +char *odr_strdup(ODR o, const char *str) { - ODR_MEM r = o->mem; - - o->mem = 0; - return r; + return nmem_strdup(o->mem, str); } -void *odr_malloc(ODR o, int size) +int *odr_intdup(ODR o, int v) { - struct odr_memblock *p = o->mem; - char *r; + return nmem_intdup(o->mem, v); +} - if (!p || p->size - p->top < size) - if (!(p = get_block(size))) - abort(); - else - { - p->next = o->mem; - o->mem = p; - } - r = p->buf + p->top; - /* align size */ - p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1); - return r; +int odr_total(ODR o) +{ + return o->mem ? nmem_total(o->mem) : 0; } /* ---------- memory management for data encoding ----------*/ -int odr_grow_block(odr_ecblock *b, int min_bytes) +int odr_grow_block(ODR b, int min_bytes) { int togrow; @@ -137,9 +62,11 @@ int odr_grow_block(odr_ecblock *b, int min_bytes) togrow = b->size; if (togrow < min_bytes) togrow = min_bytes; - if (b->size && !(b->buf = realloc(b->buf, b->size += togrow))) + if (b->size && !(b->buf = + (unsigned char *) xrealloc(b->buf, b->size += togrow))) abort(); - else if (!b->size && !(b->buf = malloc(b->size = togrow))) + else if (!b->size && !(b->buf = (unsigned char *) + xmalloc(b->size = togrow))) abort(); #ifdef ODR_DEBUG fprintf(stderr, "New size for encode_buffer: %d\n", b->size); @@ -149,29 +76,29 @@ int odr_grow_block(odr_ecblock *b, int min_bytes) int odr_write(ODR o, unsigned char *buf, int bytes) { - if (o->ecb.pos + bytes >= o->ecb.size && odr_grow_block(&o->ecb, bytes)) + if (o->pos + bytes >= o->size && odr_grow_block(o, bytes)) { o->error = OSPACE; return -1; } - memcpy(o->ecb.buf + o->ecb.pos, buf, bytes); - o->ecb.pos += bytes; - if (o->ecb.pos > o->ecb.top) - o->ecb.top = o->ecb.pos; + memcpy(o->buf + o->pos, buf, bytes); + o->pos += bytes; + if (o->pos > o->top) + o->top = o->pos; return 0; } int odr_seek(ODR o, int whence, int offset) { if (whence == ODR_S_CUR) - offset += o->ecb.pos; + offset += o->pos; else if (whence == ODR_S_END) - offset += o->ecb.top; - if (offset > o->ecb.size && odr_grow_block(&o->ecb, offset - o->ecb.size)) + offset += o->top; + if (offset > o->size && odr_grow_block(o, offset - o->size)) { o->error = OSPACE; return -1; } - o->ecb.pos = offset; + o->pos = offset; return 0; }