Fixed bug #2068: pkg-config trouble.
[yaz-moved-to-github.git] / src / nmem.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: nmem.c,v 1.30 2007-04-17 20:26:18 adam Exp $
6  */
7
8 /**
9  * \file nmem.c
10  * \brief Implements Nibble Memory
11  *
12  * This is a simple and fairly wasteful little module for nibble memory
13  * allocation. Evemtually we'll put in something better.
14  *
15  * FIXME - it also has some semaphore stuff, and stuff to handle errno.
16  *         These should be moved to some other place!
17  */
18 #if HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stddef.h>
27 #include <yaz/xmalloc.h>
28 #include <yaz/nmem.h>
29 #include <yaz/log.h>
30
31 #ifdef WIN32
32 #include <windows.h>
33 #endif
34
35 #define NMEM_CHUNK (4*1024)
36
37 struct nmem_block
38 {
39     char *buf;              /* memory allocated in this block */
40     size_t size;            /* size of buf */
41     size_t top;             /* top of buffer */
42     struct nmem_block *next;
43 };
44
45 struct nmem_control
46 {
47     int total;
48     struct nmem_block *blocks;
49     struct nmem_control *next;
50 };
51
52 struct align {
53     char x;
54     union {
55         char c;
56         short s;
57         int i;
58         long l;
59 #if HAVE_LONG_LONG
60         long long ll;
61 #endif
62         float f;
63         double d;
64     } u;
65 };
66
67 #define NMEM_ALIGN (offsetof(struct align, u))
68
69 static int log_level = 0;
70 static int log_level_initialized = 0;
71
72 static void free_block(struct nmem_block *p)
73 {  
74     xfree(p->buf);
75     xfree(p);
76     if (log_level)
77         yaz_log (log_level, "nmem free_block p=%p", p);
78 }
79
80 /*
81  * acquire a block with a minimum of size free bytes.
82  */
83 static struct nmem_block *get_block(size_t size)
84 {
85     struct nmem_block *r;
86     size_t get = NMEM_CHUNK;
87
88     if (log_level)
89         yaz_log (log_level, "nmem get_block size=%ld", (long) size);
90
91     
92     if (get < size)
93         get = size;
94     if(log_level)
95         yaz_log (log_level, "nmem get_block alloc new block size=%ld",
96                  (long) get);
97     
98     r = (struct nmem_block *) xmalloc(sizeof(*r));
99     r->buf = (char *)xmalloc(r->size = get);
100     r->top = 0;
101     return r;
102 }
103
104 void nmem_reset(NMEM n)
105 {
106     struct nmem_block *t;
107     
108     yaz_log (log_level, "nmem_reset p=%p", n);
109     if (!n)
110         return;
111     while (n->blocks)
112     {
113         t = n->blocks;
114         n->blocks = n->blocks->next;
115         free_block(t);
116     }
117     n->total = 0;
118 }
119
120 void *nmem_malloc(NMEM n, int size)
121 {
122     struct nmem_block *p;
123     char *r;
124
125     if (!n)
126     {
127         yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
128         abort ();
129     }
130     p = n->blocks;
131     if (!p || p->size < size + p->top)
132     {
133         p = get_block(size);
134         p->next = n->blocks;
135         n->blocks = p;
136     }
137     r = p->buf + p->top;
138     /* align size */
139     p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
140     n->total += size;
141     return r;
142 }
143
144 int nmem_total(NMEM n)
145 {
146     return n->total;
147 }
148
149 NMEM nmem_create(void)
150 {
151     NMEM r;
152     if (!log_level_initialized)
153     {
154         log_level = yaz_log_module_level("nmem");
155         log_level_initialized = 1;
156     }
157     
158     r = (struct nmem_control *)xmalloc(sizeof(*r));
159
160     r->blocks = 0;
161     r->total = 0;
162     r->next = 0;
163
164     return r;
165 }
166
167 void nmem_destroy(NMEM n)
168 {
169     if (!n)
170         return;
171     
172     nmem_reset(n);
173     xfree(n);
174 }
175
176 void nmem_transfer (NMEM dst, NMEM src)
177 {
178     struct nmem_block *t;
179     while ((t = src->blocks))
180     {
181         src->blocks = t->next;
182         t->next = dst->blocks;
183         dst->blocks = t;
184     }
185     dst->total += src->total;
186     src->total = 0;
187 }
188
189 int yaz_errno(void)
190 {
191     return errno;
192 }
193
194 void yaz_set_errno(int v)
195 {
196     errno = v;
197 }
198
199 void yaz_strerror(char *buf, int max)
200 {
201 #ifdef WIN32
202     DWORD err;
203 #endif
204     char *cp;
205     if (!log_level_initialized)
206     {
207         log_level = yaz_log_module_level("nmem");
208         log_level_initialized = 1;
209     }
210     
211 #ifdef WIN32
212     err = GetLastError();
213     if (err)
214     {
215         FormatMessage(
216                 FORMAT_MESSAGE_FROM_SYSTEM,
217                 NULL,
218                 err,
219                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
220                 (LPTSTR) buf,
221                 max-1,
222                 NULL);
223     }
224     else
225         *buf = '\0';
226 #else
227 /* UNIX */
228 #if HAVE_STRERROR_R
229     *buf = '\0';
230     strerror_r(errno, buf, max);
231     /* if buffer is unset - use strerror anyway (GLIBC bug) */
232     if (*buf == '\0')
233         strcpy(buf, strerror(yaz_errno()));
234 #else
235     strcpy(buf, strerror(yaz_errno()));
236 #endif
237 /* UNIX */
238 #endif
239     if ((cp = strrchr(buf, '\n')))
240         *cp = '\0';
241     if ((cp = strrchr(buf, '\r')))
242         *cp = '\0';
243 }
244 /*
245  * Local variables:
246  * c-basic-offset: 4
247  * indent-tabs-mode: nil
248  * End:
249  * vim: shiftwidth=4 tabstop=8 expandtab
250  */
251