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