Fix leak for odr_print of ZOOM connection.
[yaz-moved-to-github.git] / src / nmem.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file nmem.c
8  * \brief Implements Nibble Memory
9  *
10  * This is a simple and fairly wasteful little module for nibble memory
11  * allocation. Evemtually we'll put in something better.
12  *
13  * FIXME - it also has some semaphore stuff, and stuff to handle errno.
14  *         These should be moved to some other place!
15  */
16 #if HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stddef.h>
25 #include <yaz/xmalloc.h>
26 #include <yaz/nmem.h>
27 #include <yaz/log.h>
28
29 #define NMEM_CHUNK (4*1024)
30
31 struct nmem_block
32 {
33     char *buf;              /* memory allocated in this block */
34     size_t size;            /* size of buf */
35     size_t top;             /* top of buffer */
36     struct nmem_block *next;
37 };
38
39 struct nmem_control
40 {
41     int total;
42     struct nmem_block *blocks;
43     struct nmem_control *next;
44 };
45
46 struct align {
47     char x;
48     union {
49         char c;
50         short s;
51         int i;
52         long l;
53 #if HAVE_LONG_LONG
54         long long ll;
55 #endif
56         float f;
57         double d;
58     } u;
59 };
60
61 #define NMEM_ALIGN (offsetof(struct align, u))
62
63 static int log_level = 0;
64 static int log_level_initialized = 0;
65
66 static void free_block(struct nmem_block *p)
67 {  
68     xfree(p->buf);
69     xfree(p);
70     if (log_level)
71         yaz_log (log_level, "nmem free_block p=%p", p);
72 }
73
74 /*
75  * acquire a block with a minimum of size free bytes.
76  */
77 static struct nmem_block *get_block(size_t size)
78 {
79     struct nmem_block *r;
80     size_t get = NMEM_CHUNK;
81
82     if (log_level)
83         yaz_log (log_level, "nmem get_block size=%ld", (long) size);
84
85     
86     if (get < size)
87         get = size;
88     if(log_level)
89         yaz_log (log_level, "nmem get_block alloc new block size=%ld",
90                  (long) get);
91     
92     r = (struct nmem_block *) xmalloc(sizeof(*r));
93     r->buf = (char *)xmalloc(r->size = get);
94     r->top = 0;
95     return r;
96 }
97
98 void nmem_reset(NMEM n)
99 {
100     struct nmem_block *t;
101     
102     yaz_log (log_level, "nmem_reset p=%p", n);
103     if (!n)
104         return;
105     while (n->blocks)
106     {
107         t = n->blocks;
108         n->blocks = n->blocks->next;
109         free_block(t);
110     }
111     n->total = 0;
112 }
113
114 void *nmem_malloc(NMEM n, int size)
115 {
116     struct nmem_block *p;
117     char *r;
118
119     if (!n)
120     {
121         yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
122         abort ();
123     }
124     p = n->blocks;
125     if (!p || p->size < size + p->top)
126     {
127         p = get_block(size);
128         p->next = n->blocks;
129         n->blocks = p;
130     }
131     r = p->buf + p->top;
132     /* align size */
133     p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
134     n->total += size;
135     return r;
136 }
137
138 int nmem_total(NMEM n)
139 {
140     return n->total;
141 }
142
143 NMEM nmem_create(void)
144 {
145     NMEM r;
146     if (!log_level_initialized)
147     {
148         log_level = yaz_log_module_level("nmem");
149         log_level_initialized = 1;
150     }
151     
152     r = (struct nmem_control *)xmalloc(sizeof(*r));
153
154     r->blocks = 0;
155     r->total = 0;
156     r->next = 0;
157
158     return r;
159 }
160
161 void nmem_destroy(NMEM n)
162 {
163     if (!n)
164         return;
165     
166     nmem_reset(n);
167     xfree(n);
168 }
169
170 void nmem_transfer (NMEM dst, NMEM src)
171 {
172     struct nmem_block *t;
173     while ((t = src->blocks))
174     {
175         src->blocks = t->next;
176         t->next = dst->blocks;
177         dst->blocks = t;
178     }
179     dst->total += src->total;
180     src->total = 0;
181 }
182
183 /*
184  * Local variables:
185  * c-basic-offset: 4
186  * c-file-style: "Stroustrup"
187  * indent-tabs-mode: nil
188  * End:
189  * vim: shiftwidth=4 tabstop=8 expandtab
190  */
191