nmem revert fix: Set nmem_init_flag=0.
[yaz-moved-to-github.git] / util / nmem.c
1 /*
2  * Copyright (c) 1995-2001, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: nmem.c,v 1.34 2001-11-18 21:05:13 adam Exp $
7  */
8
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 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <assert.h>
18 #include <string.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/nmem.h>
21 #include <yaz/log.h>
22 #include <yaz/oid.h>
23
24 #ifdef WIN32
25 #include <windows.h>
26 #endif
27
28 #if YAZ_POSIX_THREADS
29 #include <pthread.h>
30 #endif
31
32 #if YAZ_GNU_THREADS
33 #include <pth.h>
34 #endif
35
36 #define NMEM_CHUNK (4*1024)
37
38 #ifdef WIN32
39 static CRITICAL_SECTION critical_section;
40 #define NMEM_ENTER EnterCriticalSection(&critical_section)
41 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
42 struct nmem_mutex {
43     CRITICAL_SECTION m_handle;
44 };
45 #elif YAZ_POSIX_THREADS
46 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
47 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
48 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
49 struct nmem_mutex {
50     pthread_mutex_t m_handle;
51 };
52 #elif YAZ_GNU_THREADS
53 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
54 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
55 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
56 struct nmem_mutex {
57     pth_mutex_t m_handle;
58 };
59 #else
60 #define NMEM_ENTER
61 #define NMEM_LEAVE
62 struct nmem_mutex {
63     int dummy;
64 };
65 #endif
66
67 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
68 {
69     NMEM_ENTER;
70     if (!*p)
71     {
72         *p = (NMEM_MUTEX) malloc (sizeof(**p));
73 #ifdef WIN32
74         InitializeCriticalSection(&(*p)->m_handle);
75 #elif YAZ_POSIX_THREADS
76         pthread_mutex_init (&(*p)->m_handle, 0);
77 #elif YAZ_GNU_THREADS
78         pth_mutex_init (&(*p)->m_handle);
79 #endif
80     }
81     NMEM_LEAVE;
82 }
83
84 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
85 {
86     if (p)
87     {
88 #ifdef WIN32
89         EnterCriticalSection(&p->m_handle);
90 #elif YAZ_POSIX_THREADS
91         pthread_mutex_lock(&p->m_handle);
92 #endif
93     }
94 }
95
96 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
97 {
98     if (p)
99     {
100 #ifdef WIN32
101         LeaveCriticalSection(&p->m_handle);
102 #elif YAZ_POSIX_THREADS
103         pthread_mutex_unlock(&p->m_handle);
104 #endif
105     }
106 }
107
108 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
109 {
110     NMEM_ENTER;
111     if (*p)
112     {
113 #ifdef WIN32
114         DeleteCriticalSection(&(*p)->m_handle);
115 #endif
116         free (*p);
117         *p = 0;
118     }
119     NMEM_LEAVE;
120 }
121
122 static nmem_block *freelist = NULL;        /* "global" freelists */
123 static nmem_control *cfreelist = NULL;
124 static int nmem_active_no = 0;
125 static int nmem_init_flag = 0;
126
127 #if NMEM_DEBUG
128 struct nmem_debug_info {
129     void *p;
130     char file[40];
131     int line;
132     struct nmem_debug_info *next;
133 };
134   
135 struct nmem_debug_info *nmem_debug_list = 0;  
136 #endif
137
138 static void free_block(nmem_block *p)
139 {  
140     p->next = freelist;
141     freelist = p;
142 #if NMEM_DEBUG
143     yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
144 #endif
145 }
146
147 #if NMEM_DEBUG
148 void nmem_print_list (void)
149 {
150     struct nmem_debug_info *p;
151
152     yaz_log (LOG_DEBUG, "nmem print list");
153     NMEM_ENTER;
154     for (p = nmem_debug_list; p; p = p->next)
155         yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
156                  nmem_total(p->p));
157     NMEM_LEAVE;
158 }
159 #endif
160 /*
161  * acquire a block with a minimum of size free bytes.
162  */
163 static nmem_block *get_block(int size)
164 {
165     nmem_block *r, *l;
166
167 #if NMEM_DEBUG
168     yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
169 #endif
170     for (r = freelist, l = 0; r; l = r, r = r->next)
171         if (r->size >= size)
172             break;
173     if (r)
174     {
175 #if NMEM_DEBUG
176         yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
177 #endif
178         if (l)
179             l->next = r->next;
180         else
181             freelist = r->next;
182     }
183     else
184     {
185         int get = NMEM_CHUNK;
186
187         if (get < size)
188             get = size;
189 #if NMEM_DEBUG
190         yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
191 #endif
192         r = (nmem_block *)xmalloc(sizeof(*r));
193         r->buf = (char *)xmalloc(r->size = get);
194     }
195     r->top = 0;
196     return r;
197 }
198
199 void nmem_reset(NMEM n)
200 {
201     nmem_block *t;
202
203 #if NMEM_DEBUG
204     yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
205 #endif
206     if (!n)
207         return;
208     NMEM_ENTER;
209     while (n->blocks)
210     {
211         t = n->blocks;
212         n->blocks = n->blocks->next;
213         free_block(t);
214     }
215     n->total = 0;
216     NMEM_LEAVE;
217 }
218
219 #if NMEM_DEBUG
220 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
221 #else
222 void *nmem_malloc(NMEM n, int size)
223 #endif
224 {
225     struct nmem_block *p;
226     char *r;
227
228 #if NMEM_DEBUG
229     yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
230                      n, size);
231 #endif
232     if (!n)
233     {
234         abort ();
235         return xmalloc(size);
236     }
237 #ifdef WIN32
238     assert (nmem_init_flag);
239 #endif
240     NMEM_ENTER;
241     p = n->blocks;
242     if (!p || p->size - p->top < size)
243     {
244         p = get_block(size);
245         p->next = n->blocks;
246         n->blocks = p;
247     }
248     r = p->buf + p->top;
249     /* align size */
250     p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
251     n->total += size;
252     NMEM_LEAVE;
253     return r;
254 }
255
256 int nmem_total(NMEM n)
257 {
258     return n->total;
259 }
260
261 #if NMEM_DEBUG
262 NMEM nmem_create_f(const char *file, int line)
263 #else
264 NMEM nmem_create(void)
265 #endif
266 {
267     NMEM r;
268 #if NMEM_DEBUG
269     struct nmem_debug_info *debug_p;
270 #endif
271     
272     NMEM_ENTER;
273     nmem_active_no++;
274     r = cfreelist;
275     if (r)
276         cfreelist = cfreelist->next;
277     else
278         r = (nmem_control *)xmalloc(sizeof(*r));
279     NMEM_LEAVE;
280
281 #if NMEM_DEBUG
282     yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
283                      nmem_active_no, r);
284 #endif
285     r->blocks = 0;
286     r->total = 0;
287     r->next = 0;
288
289 #if NMEM_DEBUG
290     for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
291         if (debug_p->p == r)
292         {
293             yaz_log (LOG_FATAL, "multi used block in nmem");
294             abort ();
295         }
296     debug_p = xmalloc (sizeof(*debug_p));
297     strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
298     debug_p->file[sizeof(debug_p->file)-1] = '\0';
299     debug_p->line = line;
300     debug_p->p = r;
301     debug_p->next = nmem_debug_list;
302     nmem_debug_list = debug_p;
303
304     nmem_print_list();
305 #endif
306     return r;
307 }
308
309 #if NMEM_DEBUG
310 void nmem_destroy_f(const char *file, int line, NMEM n)
311 #else
312 void nmem_destroy(NMEM n)
313 #endif
314 {
315 #if NMEM_DEBUG
316     struct nmem_debug_info **debug_p;
317     int ok = 0;
318 #endif
319     if (!n)
320         return;
321     
322 #if NMEM_DEBUG
323     yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
324                      nmem_active_no-1, n);
325     NMEM_ENTER;
326     for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
327         if ((*debug_p)->p == n)
328         {
329             struct nmem_debug_info *debug_save = *debug_p;
330             *debug_p = (*debug_p)->next;
331             xfree (debug_save);
332             ok = 1;
333             break;
334         }
335     NMEM_LEAVE;
336     nmem_print_list();
337     if (!ok)
338     {
339         yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
340                  file, line, n);
341         return;
342     }
343 #endif
344     nmem_reset(n);
345     NMEM_ENTER;
346     nmem_active_no--;
347     n->next = cfreelist;
348     cfreelist = n;
349     NMEM_LEAVE;
350 }
351
352 void nmem_transfer (NMEM dst, NMEM src)
353 {
354     nmem_block *t;
355     while ((t=src->blocks))
356     {
357         src->blocks = t->next;
358         t->next = dst->blocks;
359         dst->blocks = t;
360     }
361     dst->total += src->total;
362     src->total = 0;
363 }
364
365 void nmem_critical_enter (void)
366 {
367     NMEM_ENTER;
368 }
369
370 void nmem_critical_leave (void)
371 {
372     NMEM_LEAVE;
373 }
374
375 void nmem_init (void)
376 {
377     if (++nmem_init_flag == 1)
378     {
379 #ifdef WIN32
380         InitializeCriticalSection(&critical_section);
381 #elif YAZ_GNU_THREADS
382         yaz_log (LOG_LOG, "pth_init");
383         pth_init ();
384 #endif
385         nmem_active_no = 0;
386         freelist = NULL;
387         cfreelist = NULL;
388     }
389 }
390
391 void nmem_exit (void)
392 {
393     if (--nmem_init_flag == 0)
394     {
395         oid_exit();
396         while (freelist)
397         {
398             struct nmem_block *fl = freelist;
399             freelist = freelist->next;
400             xfree (fl->buf);
401             xfree (fl);
402         }
403         while (cfreelist)
404         {
405             struct nmem_control *cfl = cfreelist;
406             cfreelist = cfreelist->next;
407             xfree (cfl);
408         }
409 #ifdef WIN32
410         DeleteCriticalSection(&critical_section);
411 #endif
412     }
413 }
414
415
416 #ifdef WIN32
417 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
418                      DWORD reason,
419                      LPVOID reserved)
420 {
421     switch (reason)
422     {
423     case DLL_PROCESS_ATTACH:
424         nmem_init ();
425         break;
426     case DLL_PROCESS_DETACH:
427         nmem_exit ();
428     }
429     return TRUE;
430 }
431 #endif