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