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