New OID database - with public definitions in oid_db.h. Removed old OID
[yaz-moved-to-github.git] / src / odr_mem.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: odr_mem.c,v 1.10 2007-04-12 13:52:57 adam Exp $
6  */
7 /**
8  * \file odr_mem.c
9  * \brief Implements ODR memory management
10  */
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdlib.h>
16 #include "odr-priv.h"
17 #include <yaz/xmalloc.h>
18
19 /* ------------------------ NIBBLE MEMORY ---------------------- */
20
21 /*
22  * Extract the memory control block from o.
23  */
24 NMEM odr_extract_mem(ODR o)
25 {
26     NMEM r = o->mem;
27
28     o->mem = nmem_create();
29     return r;
30 }
31
32 void *odr_malloc(ODR o, int size)
33 {
34     return nmem_malloc(o->mem, size);
35 }
36
37 char *odr_strdup(ODR o, const char *str)
38 {
39     return nmem_strdup(o->mem, str);
40 }
41
42 char *odr_strdupn(ODR o, const char *str, size_t n)
43 {
44     return nmem_strdupn(o->mem, str, n);
45 }
46
47 int *odr_intdup(ODR o, int v)
48 {
49     return nmem_intdup(o->mem, v);
50 }
51
52 int odr_total(ODR o)
53 {
54     return nmem_total(o->mem);
55 }
56
57 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
58 {
59     Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
60     p->buf = odr_malloc(o, sz);
61     memcpy(p->buf, buf, sz);
62     p->size = sz;
63     p->len = sz;
64     return p;
65 }
66
67 /* ---------- memory management for data encoding ----------*/
68
69
70 int odr_grow_block(ODR b, int min_bytes)
71 {
72     int togrow;
73
74     if (!b->op->can_grow)
75         return -1;
76     if (!b->size)
77         togrow = 1024;
78     else
79         togrow = b->size;
80     if (togrow < min_bytes)
81         togrow = min_bytes;
82     if (b->size && !(b->buf =
83                      (unsigned char *) xrealloc(b->buf, b->size += togrow)))
84         abort();
85     else if (!b->size && !(b->buf = (unsigned char *)
86                            xmalloc(b->size = togrow)))
87         abort();
88     return 0;
89 }
90
91 int odr_write(ODR o, unsigned char *buf, int bytes)
92 {
93     if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
94     {
95         odr_seterror(o, OSPACE, 40);
96         return -1;
97     }
98     memcpy(o->buf + o->pos, buf, bytes);
99     o->pos += bytes;
100     if (o->pos > o->top)
101         o->top = o->pos;
102     return 0;
103 }
104
105 int odr_seek(ODR o, int whence, int offset)
106 {
107     if (whence == ODR_S_CUR)
108         offset += o->pos;
109     else if (whence == ODR_S_END)
110         offset += o->top;
111     if (offset > o->size && odr_grow_block(o, offset - o->size))
112     {
113         odr_seterror(o, OSPACE, 41);
114         return -1;
115     }
116     o->pos = offset;
117     return 0;
118 }
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * indent-tabs-mode: nil
123  * End:
124  * vim: shiftwidth=4 tabstop=8 expandtab
125  */
126