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