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