X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=odr%2Fodr_mem.c;h=f938a687fc590d9026fbc1b6b194416ad91a0f1a;hb=695e6df9fce9b838cb3fe8f49b211ed99943caef;hp=e57730a32ccadcf0b077998475229524c0568102;hpb=892f7f56860a1ee8e8d816eeaec0387c54c1a0a7;p=yaz-moved-to-github.git diff --git a/odr/odr_mem.c b/odr/odr_mem.c index e57730a..f938a68 100644 --- a/odr/odr_mem.c +++ b/odr/odr_mem.c @@ -1,10 +1,63 @@ /* - * Copyright (C) 1994, Index Data I/S - * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss + * Copyright (c) 1995-2001, Index Data + * See the file LICENSE for details. * * $Log: odr_mem.c,v $ - * Revision 1.2 1995-03-17 10:17:52 quinn + * Revision 1.19 2001-03-25 21:55:12 adam + * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate. + * + * Revision 1.18 2000/02/29 13:44:55 adam + * Check for config.h (currently not generated). + * + * Revision 1.17 2000/01/31 13:15:21 adam + * Removed uses of assert(3). Cleanup of ODR. CCL parser update so + * that some characters are not surrounded by spaces in resulting term. + * ILL-code updates. + * + * Revision 1.16 1999/11/30 13:47:11 adam + * Improved installation. Moved header files to include/yaz. + * + * Revision 1.15 1999/03/31 11:18:25 adam + * Implemented odr_strdup. Added Reference ID to backend server API. + * + * Revision 1.14 1998/07/20 12:38:15 adam + * More LOG_DEBUG-diagnostics. + * + * Revision 1.13 1998/02/11 11:53:34 adam + * Changed code so that it compiles as C++. + * + * Revision 1.12 1995/11/08 17:41:33 quinn + * Smallish. + * + * Revision 1.11 1995/11/01 13:54:43 quinn + * Minor adjustments + * + * Revision 1.10 1995/10/25 16:58:19 quinn + * Stupid bug in odr_malloc + * + * Revision 1.9 1995/10/13 16:08:08 quinn + * Added OID utility + * + * Revision 1.8 1995/09/29 17:12:24 quinn + * Smallish + * + * Revision 1.7 1995/09/27 15:02:59 quinn + * Modified function heads & prototypes. + * + * Revision 1.6 1995/08/21 09:10:41 quinn + * Smallish fixes to suppport new formats. + * + * 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 @@ -12,84 +65,101 @@ * Beginning to add memory management to odr * */ +#if HAVE_CONFIG_H +#include +#endif #include -#include +#include +#include -#define ODR_MEM_CHUNK (10*1024) +/* ------------------------ NIBBLE MEMORY ---------------------- */ -typedef struct odr_memblock +/* + * Extract the memory control block from o. + */ +NMEM odr_extract_mem(ODR o) { - char *buf; - int size; - int top; - struct odr_memblock *next; -} odr_memblock; + NMEM r = o->mem; -static odr_memblock *freelist = 0; + o->mem = 0; + return r; +} -static void free_block(odr_memblock *p) +void *odr_malloc(ODR o, int size) { - p->next = freelist; - freelist = p; + if (o && !o->mem) + o->mem = nmem_create(); + return nmem_malloc(o ? o->mem : 0, size); } -static odr_memblock *get_block(int size) +char *odr_strdup(ODR o, const char *str) { - odr_memblock *r, *l; + return nmem_strdup(o->mem, str); +} - 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; +int *odr_intdup(ODR o, int v) +{ + return nmem_intdup(o->mem, v); +} - if (get < size) - get = size; - if (!(r = malloc(sizeof(*r)))) - return 0; - if (!(r->buf = malloc(r->size = get))) - return 0; - } - r->top = 0; - return r; +int odr_total(ODR o) +{ + return o->mem ? nmem_total(o->mem) : 0; } -void odr_release_mem(odr_memblock *p) +/* ---------- memory management for data encoding ----------*/ + + +int odr_grow_block(ODR b, int min_bytes) { - odr_memblock *t; + int togrow; - while (p) + if (!b->can_grow) + return -1; + if (!b->size) + togrow = 1024; + else + togrow = b->size; + if (togrow < min_bytes) + togrow = min_bytes; + if (b->size && !(b->buf = + (unsigned char *) xrealloc(b->buf, b->size += togrow))) + abort(); + 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); +#endif + return 0; +} + +int odr_write(ODR o, unsigned char *buf, int bytes) +{ + if (o->pos + bytes >= o->size && odr_grow_block(o, bytes)) { - t = p; - p = p->next; - free_block(t); + o->error = OSPACE; + return -1; } + memcpy(o->buf + o->pos, buf, bytes); + o->pos += bytes; + if (o->pos > o->top) + o->top = o->pos; + return 0; } -void *odr_malloc(ODR o, int size) +int odr_seek(ODR o, int whence, int offset) { - struct odr_memblock *p = o->mem; - char *r; - - if (!p || p->size - p->top < size) - if (!(p = get_block(size))) - { - o->error = OMEMORY; - return 0; - } - else - { - p->next = o->mem; - o->mem = p; - } - r = p->buf + p->top; - p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1); - return r; + if (whence == ODR_S_CUR) + offset += o->pos; + else if (whence == ODR_S_END) + offset += o->top; + if (offset > o->size && odr_grow_block(o, offset - o->size)) + { + o->error = OSPACE; + return -1; + } + o->pos = offset; + return 0; }