Implemented odr_strdup. Added Reference ID to backend server API.
[yaz-moved-to-github.git] / odr / odr_mem.c
1 /*
2  * Copyright (c) 1995-1998, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_mem.c,v $
7  * Revision 1.15  1999-03-31 11:18:25  adam
8  * Implemented odr_strdup. Added Reference ID to backend server API.
9  *
10  * Revision 1.14  1998/07/20 12:38:15  adam
11  * More LOG_DEBUG-diagnostics.
12  *
13  * Revision 1.13  1998/02/11 11:53:34  adam
14  * Changed code so that it compiles as C++.
15  *
16  * Revision 1.12  1995/11/08 17:41:33  quinn
17  * Smallish.
18  *
19  * Revision 1.11  1995/11/01  13:54:43  quinn
20  * Minor adjustments
21  *
22  * Revision 1.10  1995/10/25  16:58:19  quinn
23  * Stupid bug in odr_malloc
24  *
25  * Revision 1.9  1995/10/13  16:08:08  quinn
26  * Added OID utility
27  *
28  * Revision 1.8  1995/09/29  17:12:24  quinn
29  * Smallish
30  *
31  * Revision 1.7  1995/09/27  15:02:59  quinn
32  * Modified function heads & prototypes.
33  *
34  * Revision 1.6  1995/08/21  09:10:41  quinn
35  * Smallish fixes to suppport new formats.
36  *
37  * Revision 1.5  1995/05/16  08:50:55  quinn
38  * License, documentation, and memory fixes
39  *
40  * Revision 1.4  1995/05/15  11:56:09  quinn
41  * More work on memory management.
42  *
43  * Revision 1.3  1995/04/18  08:15:21  quinn
44  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
45  * neater. We'll make the same change for decoding one day.
46  *
47  * Revision 1.2  1995/03/17  10:17:52  quinn
48  * Added memory management.
49  *
50  * Revision 1.1  1995/03/14  10:27:40  quinn
51  * Modified makefile to use common lib
52  * Beginning to add memory management to odr
53  *
54  */
55
56 #include <stdlib.h>
57 #include <odr.h>
58 #include <xmalloc.h>
59
60 /* ------------------------ NIBBLE MEMORY ---------------------- */
61
62 /*
63  * Extract the memory control block from o.
64  */
65 NMEM odr_extract_mem(ODR o)
66 {
67     NMEM r = o->mem;
68
69     o->mem = 0;
70     return r;
71 }
72
73 void *odr_malloc(ODR o, int size)
74 {
75     if (o && !o->mem)
76         o->mem = nmem_create();
77     return nmem_malloc(o ? o->mem : 0, size);
78 }
79
80 char *odr_strdup(ODR o, const char *str)
81 {
82     return nmem_strdup(o->mem, str);
83 }
84
85 int odr_total(ODR o)
86 {
87     return o->mem ? nmem_total(o->mem) : 0;
88 }
89
90 /* ---------- memory management for data encoding ----------*/
91
92
93 int odr_grow_block(odr_ecblock *b, int min_bytes)
94 {
95     int togrow;
96
97     if (!b->can_grow)
98         return -1;
99     if (!b->size)
100         togrow = 1024;
101     else
102         togrow = b->size;
103     if (togrow < min_bytes)
104         togrow = min_bytes;
105     if (b->size && !(b->buf =(unsigned char *)xrealloc(b->buf, b->size += togrow)))
106         abort();
107     else if (!b->size && !(b->buf = (unsigned char *)xmalloc(b->size = togrow)))
108         abort();
109 #ifdef ODR_DEBUG
110     fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
111 #endif
112     return 0;
113 }
114
115 int odr_write(ODR o, unsigned char *buf, int bytes)
116 {
117     if (o->ecb.pos + bytes >= o->ecb.size && odr_grow_block(&o->ecb, bytes))
118     {
119         o->error = OSPACE;
120         return -1;
121     }
122     memcpy(o->ecb.buf + o->ecb.pos, buf, bytes);
123     o->ecb.pos += bytes;
124     if (o->ecb.pos > o->ecb.top)
125         o->ecb.top = o->ecb.pos;
126     return 0;
127 }
128
129 int odr_seek(ODR o, int whence, int offset)
130 {
131     if (whence == ODR_S_CUR)
132         offset += o->ecb.pos;
133     else if (whence == ODR_S_END)
134         offset += o->ecb.top;
135     if (offset > o->ecb.size && odr_grow_block(&o->ecb, offset - o->ecb.size))
136     {
137         o->error = OSPACE;
138         return -1;
139     }
140     o->ecb.pos = offset;
141     return 0;
142 }