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