2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 2000-05-11 14:37:55 adam
10 * Revision 1.23 2000/05/09 10:55:05 adam
11 * Public nmem_print_list (for debugging).
13 * Revision 1.22 2000/05/03 22:00:00 adam
14 * Reference counter (if multiple modules are init/freeing nmem).
16 * Revision 1.21 2000/02/29 13:44:55 adam
17 * Check for config.h (currently not generated).
19 * Revision 1.20 2000/01/06 14:59:13 adam
20 * Added oid_init/oid_exit. Changed oid_exit.
22 * Revision 1.19 1999/11/30 13:47:12 adam
23 * Improved installation. Moved header files to include/yaz.
25 * Revision 1.18 1999/08/27 09:40:32 adam
26 * Renamed logf function to yaz_log. Removed VC++ project files.
28 * Revision 1.17 1999/07/13 13:28:25 adam
29 * Better debugging for NMEM routines.
31 * Revision 1.16 1999/03/31 11:18:25 adam
32 * Implemented odr_strdup. Added Reference ID to backend server API.
34 * Revision 1.15 1999/02/11 09:10:26 adam
35 * Function nmem_init only mandatory on Windows.
37 * Revision 1.14 1999/02/02 13:57:40 adam
38 * Uses preprocessor define WIN32 instead of WINDOWS to build code
39 * for Microsoft WIN32.
41 * Revision 1.13 1998/10/19 15:24:21 adam
42 * New nmem utility, nmem_transfer, that transfer blocks from one
45 * Revision 1.12 1998/10/13 16:00:18 adam
46 * Implemented nmem_critical_{enter,leave}.
48 * Revision 1.11 1998/08/21 14:13:36 adam
49 * Added GNU Configure script to build Makefiles.
51 * Revision 1.10 1998/07/20 12:35:57 adam
52 * Added more memory diagnostics (when NMEM_DEBUG is 1).
54 * Revision 1.9 1998/07/07 15:49:01 adam
57 * Revision 1.8 1998/07/03 14:21:27 adam
58 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
59 * Fretwell Downing Informatics.
61 * Revision 1.7 1998/02/11 11:53:36 adam
62 * Changed code so that it compiles as C++.
64 * Revision 1.6 1997/10/31 12:20:09 adam
65 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
66 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
67 * Bug fix: missing fclose in data1_read_espec1.
69 * Revision 1.5 1997/10/06 09:09:52 adam
70 * Function mmem_exit releases memory used by the freelists.
72 * Revision 1.4 1997/09/29 07:12:50 adam
73 * NMEM thread safe. NMEM must be initialized before use (sigh) -
74 * routine nmem_init/nmem_exit implemented.
76 * Revision 1.3 1997/07/21 12:47:38 adam
77 * Moved definition of nmem_control and nmem_block.
79 * Revision 1.2 1995/12/13 13:44:37 quinn
80 * Modified Data1-system to use nmem
82 * Revision 1.1 1995/11/13 09:27:52 quinn
83 * Fiddling with the variant stuff.
89 * This is a simple and fairly wasteful little module for nibble memory
90 * allocation. Evemtually we'll put in something better.
98 #include <yaz/xmalloc.h>
113 #define NMEM_CHUNK (4*1024)
116 static CRITICAL_SECTION critical_section;
117 #define NMEM_ENTER EnterCriticalSection(&critical_section)
118 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
120 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
121 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
122 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
128 static nmem_block *freelist = NULL; /* "global" freelists */
129 static nmem_control *cfreelist = NULL;
130 static int nmem_active_no = 0;
131 static int nmem_init_flag = 0;
134 struct nmem_debug_info {
138 struct nmem_debug_info *next;
141 struct nmem_debug_info *nmem_debug_list = 0;
144 static void free_block(nmem_block *p)
149 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
154 void nmem_print_list (void)
156 struct nmem_debug_info *p;
158 yaz_log (LOG_DEBUG, "nmem print list");
160 for (p = nmem_debug_list; p; p = p->next)
161 yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
167 * acquire a block with a minimum of size free bytes.
169 static nmem_block *get_block(int size)
174 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
176 for (r = freelist, l = 0; r; l = r, r = r->next)
182 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
191 int get = NMEM_CHUNK;
196 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
198 r = (nmem_block *)xmalloc(sizeof(*r));
199 r->buf = (char *)xmalloc(r->size = get);
205 void nmem_reset(NMEM n)
210 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
218 n->blocks = n->blocks->next;
226 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
228 void *nmem_malloc(NMEM n, int size)
231 struct nmem_block *p;
235 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
241 return xmalloc(size);
244 assert (nmem_init_flag);
248 if (!p || p->size - p->top < size)
256 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
262 int nmem_total(NMEM n)
268 NMEM nmem_create_f(const char *file, int line)
270 NMEM nmem_create(void)
275 struct nmem_debug_info *debug_p;
282 cfreelist = cfreelist->next;
284 r = (nmem_control *)xmalloc(sizeof(*r));
288 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
296 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
299 yaz_log (LOG_FATAL, "multi used block in nmem");
302 debug_p = xmalloc (sizeof(*debug_p));
303 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
304 debug_p->file[sizeof(debug_p->file)-1] = '\0';
305 debug_p->line = line;
307 debug_p->next = nmem_debug_list;
308 nmem_debug_list = debug_p;
316 void nmem_destroy_f(const char *file, int line, NMEM n)
318 void nmem_destroy(NMEM n)
322 struct nmem_debug_info **debug_p;
329 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
330 nmem_active_no-1, n);
332 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
333 if ((*debug_p)->p == n)
335 struct nmem_debug_info *debug_save = *debug_p;
336 *debug_p = (*debug_p)->next;
345 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
358 void nmem_transfer (NMEM dst, NMEM src)
361 while ((t=src->blocks))
363 src->blocks = t->next;
364 t->next = dst->blocks;
367 dst->total += src->total;
371 void nmem_critical_enter (void)
376 void nmem_critical_leave (void)
381 void nmem_init (void)
383 if (++nmem_init_flag == 1)
386 InitializeCriticalSection(&critical_section);
394 void nmem_exit (void)
396 if (--nmem_init_flag == 0)
400 struct nmem_block *fl = freelist;
401 freelist = freelist->next;
407 struct nmem_control *cfl = cfreelist;
408 cfreelist = cfreelist->next;
412 DeleteCriticalSection(&critical_section);