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