Fixes for GNU threads (not working yet).
[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  * $Log: nmem.c,v $
7  * Revision 1.29  2001-10-04 00:37:58  adam
8  * Fixes for GNU threads (not working yet).
9  *
10  * Revision 1.28  2001/10/03 23:55:18  adam
11  * GNU threads support.
12  *
13  * Revision 1.27  2001/09/27 12:09:18  adam
14  * Function nmem_exit calls oid_exit (when reference is 0).
15  *
16  * Revision 1.26  2001/07/19 19:51:42  adam
17  * Added typecasts to make C++ happy.
18  *
19  * Revision 1.25  2001/06/26 14:11:27  adam
20  * Added MUTEX functions for NMEM module (used by OID utility).
21  *
22  * Revision 1.24  2000/05/11 14:37:55  adam
23  * Minor changes.
24  *
25  * Revision 1.23  2000/05/09 10:55:05  adam
26  * Public nmem_print_list (for debugging).
27  *
28  * Revision 1.22  2000/05/03 22:00:00  adam
29  * Reference counter (if multiple modules are init/freeing nmem).
30  *
31  * Revision 1.21  2000/02/29 13:44:55  adam
32  * Check for config.h (currently not generated).
33  *
34  * Revision 1.20  2000/01/06 14:59:13  adam
35  * Added oid_init/oid_exit. Changed oid_exit.
36  *
37  * Revision 1.19  1999/11/30 13:47:12  adam
38  * Improved installation. Moved header files to include/yaz.
39  *
40  * Revision 1.18  1999/08/27 09:40:32  adam
41  * Renamed logf function to yaz_log. Removed VC++ project files.
42  *
43  * Revision 1.17  1999/07/13 13:28:25  adam
44  * Better debugging for NMEM routines.
45  *
46  * Revision 1.16  1999/03/31 11:18:25  adam
47  * Implemented odr_strdup. Added Reference ID to backend server API.
48  *
49  * Revision 1.15  1999/02/11 09:10:26  adam
50  * Function nmem_init only mandatory on Windows.
51  *
52  * Revision 1.14  1999/02/02 13:57:40  adam
53  * Uses preprocessor define WIN32 instead of WINDOWS to build code
54  * for Microsoft WIN32.
55  *
56  * Revision 1.13  1998/10/19 15:24:21  adam
57  * New nmem utility, nmem_transfer, that transfer blocks from one
58  * NMEM to another.
59  *
60  * Revision 1.12  1998/10/13 16:00:18  adam
61  * Implemented nmem_critical_{enter,leave}.
62  *
63  * Revision 1.11  1998/08/21 14:13:36  adam
64  * Added GNU Configure script to build Makefiles.
65  *
66  * Revision 1.10  1998/07/20 12:35:57  adam
67  * Added more memory diagnostics (when NMEM_DEBUG is 1).
68  *
69  * Revision 1.9  1998/07/07 15:49:01  adam
70  * Reduced chunk size.
71  *
72  * Revision 1.8  1998/07/03 14:21:27  adam
73  * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
74  * Fretwell Downing Informatics.
75  *
76  * Revision 1.7  1998/02/11 11:53:36  adam
77  * Changed code so that it compiles as C++.
78  *
79  * Revision 1.6  1997/10/31 12:20:09  adam
80  * Improved memory debugging for xmalloc/nmem.c. References to NMEM
81  * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
82  * Bug fix: missing fclose in data1_read_espec1.
83  *
84  * Revision 1.5  1997/10/06 09:09:52  adam
85  * Function mmem_exit releases memory used by the freelists.
86  *
87  * Revision 1.4  1997/09/29 07:12:50  adam
88  * NMEM thread safe. NMEM must be initialized before use (sigh) -
89  * routine nmem_init/nmem_exit implemented.
90  *
91  * Revision 1.3  1997/07/21 12:47:38  adam
92  * Moved definition of nmem_control and nmem_block.
93  *
94  * Revision 1.2  1995/12/13 13:44:37  quinn
95  * Modified Data1-system to use nmem
96  *
97  * Revision 1.1  1995/11/13  09:27:52  quinn
98  * Fiddling with the variant stuff.
99  *
100  *
101  */
102
103 /*
104  * This is a simple and fairly wasteful little module for nibble memory
105  * allocation. Evemtually we'll put in something better.
106  */
107 #if HAVE_CONFIG_H
108 #include <config.h>
109 #endif
110
111 #include <assert.h>
112 #include <string.h>
113 #include <yaz/xmalloc.h>
114 #include <yaz/nmem.h>
115 #include <yaz/log.h>
116 #include <yaz/oid.h>
117
118 #ifdef WIN32
119 #include <windows.h>
120 #endif
121
122 #ifdef _REENTRANT
123 #if HAVE_PTHREAD_H
124 #include <pthread.h>
125 #elif HAVE_PTH_H
126 #include <pth.h>
127 #endif
128
129 #endif
130
131 #define NMEM_CHUNK (4*1024)
132
133 #ifdef WIN32
134 static CRITICAL_SECTION critical_section;
135 #define NMEM_ENTER EnterCriticalSection(&critical_section)
136 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
137 #endif
138
139 #ifdef _REENTRANT
140 #if HAVE_PTHREAD_H
141 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
142 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
143 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
144 #elif HAVE_PTH_H
145 static pth_mutex_t nmem_mutex;
146 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
147 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
148 #else
149 #error x
150 #endif
151 #else
152 #define NMEM_ENTER
153 #define NMEM_LEAVE
154 #endif
155
156 struct nmem_mutex {
157 #ifdef WIN32
158     CRITICAL_SECTION m_handle;
159 #elif _REENTRANT
160
161 #if HAVE_PTHREAD_H
162     pthread_mutex_t m_handle;
163 #elif HAVE_PTH_H
164     pth_mutex_t m_handle;
165 #endif
166
167 #else
168     int m_handle;
169 #endif
170 };
171
172 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
173 {
174     NMEM_ENTER;
175     if (!*p)
176     {
177         *p = (NMEM_MUTEX) malloc (sizeof(**p));
178 #ifdef WIN32
179         InitializeCriticalSection(&(*p)->m_handle);
180 #elif _REENTRANT
181         pthread_mutex_init (&(*p)->m_handle, 0);
182 #endif
183     }
184     NMEM_LEAVE;
185 }
186
187 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
188 {
189     if (p)
190     {
191 #ifdef WIN32
192         EnterCriticalSection(&p->m_handle);
193 #elif _REENTRANT
194         pthread_mutex_lock(&p->m_handle);
195 #endif
196     }
197 }
198
199 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
200 {
201     if (p)
202     {
203 #ifdef WIN32
204         LeaveCriticalSection(&p->m_handle);
205 #elif _REENTRANT
206         pthread_mutex_unlock(&p->m_handle);
207 #endif
208     }
209 }
210
211 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
212 {
213     NMEM_ENTER;
214     if (*p)
215     {
216 #ifdef WIN32
217         DeleteCriticalSection(&(*p)->m_handle);
218 #endif
219         free (*p);
220         *p = 0;
221     }
222     NMEM_LEAVE;
223 }
224
225 static nmem_block *freelist = NULL;        /* "global" freelists */
226 static nmem_control *cfreelist = NULL;
227 static int nmem_active_no = 0;
228 static int nmem_init_flag = 0;
229
230 #if NMEM_DEBUG
231 struct nmem_debug_info {
232     void *p;
233     char file[40];
234     int line;
235     struct nmem_debug_info *next;
236 };
237   
238 struct nmem_debug_info *nmem_debug_list = 0;  
239 #endif
240
241 static void free_block(nmem_block *p)
242 {  
243     p->next = freelist;
244     freelist = p;
245 #if NMEM_DEBUG
246     yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
247 #endif
248 }
249
250 #if NMEM_DEBUG
251 void nmem_print_list (void)
252 {
253     struct nmem_debug_info *p;
254
255     yaz_log (LOG_DEBUG, "nmem print list");
256     NMEM_ENTER;
257     for (p = nmem_debug_list; p; p = p->next)
258         yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
259                  nmem_total(p->p));
260     NMEM_LEAVE;
261 }
262 #endif
263 /*
264  * acquire a block with a minimum of size free bytes.
265  */
266 static nmem_block *get_block(int size)
267 {
268     nmem_block *r, *l;
269
270 #if NMEM_DEBUG
271     yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
272 #endif
273     for (r = freelist, l = 0; r; l = r, r = r->next)
274         if (r->size >= size)
275             break;
276     if (r)
277     {
278 #if NMEM_DEBUG
279         yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
280 #endif
281         if (l)
282             l->next = r->next;
283         else
284             freelist = r->next;
285     }
286     else
287     {
288         int get = NMEM_CHUNK;
289
290         if (get < size)
291             get = size;
292 #if NMEM_DEBUG
293         yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
294 #endif
295         r = (nmem_block *)xmalloc(sizeof(*r));
296         r->buf = (char *)xmalloc(r->size = get);
297     }
298     r->top = 0;
299     return r;
300 }
301
302 void nmem_reset(NMEM n)
303 {
304     nmem_block *t;
305
306 #if NMEM_DEBUG
307     yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
308 #endif
309     if (!n)
310         return;
311     NMEM_ENTER;
312     while (n->blocks)
313     {
314         t = n->blocks;
315         n->blocks = n->blocks->next;
316         free_block(t);
317     }
318     n->total = 0;
319     NMEM_LEAVE;
320 }
321
322 #if NMEM_DEBUG
323 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
324 #else
325 void *nmem_malloc(NMEM n, int size)
326 #endif
327 {
328     struct nmem_block *p;
329     char *r;
330
331 #if NMEM_DEBUG
332     yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
333                      n, size);
334 #endif
335     if (!n)
336     {
337         abort ();
338         return xmalloc(size);
339     }
340 #ifdef WIN32
341     assert (nmem_init_flag);
342 #endif
343     NMEM_ENTER;
344     p = n->blocks;
345     if (!p || p->size - p->top < size)
346     {
347         p = get_block(size);
348         p->next = n->blocks;
349         n->blocks = p;
350     }
351     r = p->buf + p->top;
352     /* align size */
353     p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
354     n->total += size;
355     NMEM_LEAVE;
356     return r;
357 }
358
359 int nmem_total(NMEM n)
360 {
361     return n->total;
362 }
363
364 #if NMEM_DEBUG
365 NMEM nmem_create_f(const char *file, int line)
366 #else
367 NMEM nmem_create(void)
368 #endif
369 {
370     NMEM r;
371 #if NMEM_DEBUG
372     struct nmem_debug_info *debug_p;
373 #endif
374     
375     NMEM_ENTER;
376     nmem_active_no++;
377     r = cfreelist;
378     if (r)
379         cfreelist = cfreelist->next;
380     else
381         r = (nmem_control *)xmalloc(sizeof(*r));
382     NMEM_LEAVE;
383
384 #if NMEM_DEBUG
385     yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
386                      nmem_active_no, r);
387 #endif
388     r->blocks = 0;
389     r->total = 0;
390     r->next = 0;
391
392 #if NMEM_DEBUG
393     for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
394         if (debug_p->p == r)
395         {
396             yaz_log (LOG_FATAL, "multi used block in nmem");
397             abort ();
398         }
399     debug_p = xmalloc (sizeof(*debug_p));
400     strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
401     debug_p->file[sizeof(debug_p->file)-1] = '\0';
402     debug_p->line = line;
403     debug_p->p = r;
404     debug_p->next = nmem_debug_list;
405     nmem_debug_list = debug_p;
406
407     nmem_print_list();
408 #endif
409     return r;
410 }
411
412 #if NMEM_DEBUG
413 void nmem_destroy_f(const char *file, int line, NMEM n)
414 #else
415 void nmem_destroy(NMEM n)
416 #endif
417 {
418 #if NMEM_DEBUG
419     struct nmem_debug_info **debug_p;
420     int ok = 0;
421 #endif
422     if (!n)
423         return;
424     
425 #if NMEM_DEBUG
426     yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
427                      nmem_active_no-1, n);
428     NMEM_ENTER;
429     for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
430         if ((*debug_p)->p == n)
431         {
432             struct nmem_debug_info *debug_save = *debug_p;
433             *debug_p = (*debug_p)->next;
434             xfree (debug_save);
435             ok = 1;
436             break;
437         }
438     NMEM_LEAVE;
439     nmem_print_list();
440     if (!ok)
441     {
442         yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
443                  file, line, n);
444         return;
445     }
446 #endif
447     nmem_reset(n);
448     NMEM_ENTER;
449     nmem_active_no--;
450     n->next = cfreelist;
451     cfreelist = n;
452     NMEM_LEAVE;
453 }
454
455 void nmem_transfer (NMEM dst, NMEM src)
456 {
457     nmem_block *t;
458     while ((t=src->blocks))
459     {
460         src->blocks = t->next;
461         t->next = dst->blocks;
462         dst->blocks = t;
463     }
464     dst->total += src->total;
465     src->total = 0;
466 }
467
468 void nmem_critical_enter (void)
469 {
470     NMEM_ENTER;
471 }
472
473 void nmem_critical_leave (void)
474 {
475     NMEM_LEAVE;
476 }
477
478 void nmem_init (void)
479 {
480     if (++nmem_init_flag == 1)
481     {
482 #ifdef WIN32
483         InitializeCriticalSection(&critical_section);
484 #endif
485
486 #ifdef _REENTRANT
487 #if HAVE_PTH_H
488         yaz_log (LOG_LOG, "pth_init");
489         pth_init ();
490         pth_mutex_init (&nmem_mutex);
491 #endif
492 #endif
493         nmem_active_no = 0;
494         freelist = NULL;
495         cfreelist = NULL;
496     }
497 }
498
499 void nmem_exit (void)
500 {
501     if (--nmem_init_flag == 0)
502     {
503         oid_exit();
504         while (freelist)
505         {
506             struct nmem_block *fl = freelist;
507             freelist = freelist->next;
508             xfree (fl->buf);
509             xfree (fl);
510         }
511         while (cfreelist)
512         {
513             struct nmem_control *cfl = cfreelist;
514             cfreelist = cfreelist->next;
515             xfree (cfl);
516         }
517 #ifdef WIN32
518         DeleteCriticalSection(&critical_section);
519 #endif
520     }
521 }
522