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