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