Moved more members of public struct odr (ODR*) to struct Odr_private.
[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.9 2007-03-19 21:08:13 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 = 0;
29     return r;
30 }
31
32 void *odr_malloc(ODR o, int size)
33 {
34     if (o && !o->mem)
35         o->mem = nmem_create();
36     return nmem_malloc(o ? o->mem : 0, size);
37 }
38
39 char *odr_strdup(ODR o, const char *str)
40 {
41     if (o && !o->mem)
42         o->mem = nmem_create();
43     return nmem_strdup(o->mem, str);
44 }
45
46 char *odr_strdupn(ODR o, const char *str, size_t n)
47 {
48     return nmem_strdupn(o->mem, str, n);
49 }
50
51 int *odr_intdup(ODR o, int v)
52 {
53     if (o && !o->mem)
54         o->mem = nmem_create();
55     return nmem_intdup(o->mem, v);
56 }
57
58 int odr_total(ODR o)
59 {
60     return o->mem ? nmem_total(o->mem) : 0;
61 }
62
63 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
64 {
65     Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
66     p->buf = odr_malloc(o, sz);
67     memcpy(p->buf, buf, sz);
68     p->size = sz;
69     p->len = sz;
70     return p;
71 }
72
73 /* ---------- memory management for data encoding ----------*/
74
75
76 int odr_grow_block(ODR b, int min_bytes)
77 {
78     int togrow;
79
80     if (!b->op->can_grow)
81         return -1;
82     if (!b->size)
83         togrow = 1024;
84     else
85         togrow = b->size;
86     if (togrow < min_bytes)
87         togrow = min_bytes;
88     if (b->size && !(b->buf =
89                      (unsigned char *) xrealloc(b->buf, b->size += togrow)))
90         abort();
91     else if (!b->size && !(b->buf = (unsigned char *)
92                            xmalloc(b->size = togrow)))
93         abort();
94     return 0;
95 }
96
97 int odr_write(ODR o, unsigned char *buf, int bytes)
98 {
99     if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
100     {
101         odr_seterror(o, OSPACE, 40);
102         return -1;
103     }
104     memcpy(o->buf + o->pos, buf, bytes);
105     o->pos += bytes;
106     if (o->pos > o->top)
107         o->top = o->pos;
108     return 0;
109 }
110
111 int odr_seek(ODR o, int whence, int offset)
112 {
113     if (whence == ODR_S_CUR)
114         offset += o->pos;
115     else if (whence == ODR_S_END)
116         offset += o->top;
117     if (offset > o->size && odr_grow_block(o, offset - o->size))
118     {
119         odr_seterror(o, OSPACE, 41);
120         return -1;
121     }
122     o->pos = offset;
123     return 0;
124 }
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132