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