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