2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 2000-05-03 22:00:00 adam
8 * Reference counter (if multiple modules are init/freeing nmem).
10 * Revision 1.21 2000/02/29 13:44:55 adam
11 * Check for config.h (currently not generated).
13 * Revision 1.20 2000/01/06 14:59:13 adam
14 * Added oid_init/oid_exit. Changed oid_exit.
16 * Revision 1.19 1999/11/30 13:47:12 adam
17 * Improved installation. Moved header files to include/yaz.
19 * Revision 1.18 1999/08/27 09:40:32 adam
20 * Renamed logf function to yaz_log. Removed VC++ project files.
22 * Revision 1.17 1999/07/13 13:28:25 adam
23 * Better debugging for NMEM routines.
25 * Revision 1.16 1999/03/31 11:18:25 adam
26 * Implemented odr_strdup. Added Reference ID to backend server API.
28 * Revision 1.15 1999/02/11 09:10:26 adam
29 * Function nmem_init only mandatory on Windows.
31 * Revision 1.14 1999/02/02 13:57:40 adam
32 * Uses preprocessor define WIN32 instead of WINDOWS to build code
33 * for Microsoft WIN32.
35 * Revision 1.13 1998/10/19 15:24:21 adam
36 * New nmem utility, nmem_transfer, that transfer blocks from one
39 * Revision 1.12 1998/10/13 16:00:18 adam
40 * Implemented nmem_critical_{enter,leave}.
42 * Revision 1.11 1998/08/21 14:13:36 adam
43 * Added GNU Configure script to build Makefiles.
45 * Revision 1.10 1998/07/20 12:35:57 adam
46 * Added more memory diagnostics (when NMEM_DEBUG is 1).
48 * Revision 1.9 1998/07/07 15:49:01 adam
51 * Revision 1.8 1998/07/03 14:21:27 adam
52 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
53 * Fretwell Downing Informatics.
55 * Revision 1.7 1998/02/11 11:53:36 adam
56 * Changed code so that it compiles as C++.
58 * Revision 1.6 1997/10/31 12:20:09 adam
59 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
60 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
61 * Bug fix: missing fclose in data1_read_espec1.
63 * Revision 1.5 1997/10/06 09:09:52 adam
64 * Function mmem_exit releases memory used by the freelists.
66 * Revision 1.4 1997/09/29 07:12:50 adam
67 * NMEM thread safe. NMEM must be initialized before use (sigh) -
68 * routine nmem_init/nmem_exit implemented.
70 * Revision 1.3 1997/07/21 12:47:38 adam
71 * Moved definition of nmem_control and nmem_block.
73 * Revision 1.2 1995/12/13 13:44:37 quinn
74 * Modified Data1-system to use nmem
76 * Revision 1.1 1995/11/13 09:27:52 quinn
77 * Fiddling with the variant stuff.
83 * This is a simple and fairly wasteful little module for nibble memory
84 * allocation. Evemtually we'll put in something better.
92 #include <yaz/xmalloc.h>
107 #define NMEM_CHUNK (4*1024)
110 static CRITICAL_SECTION critical_section;
111 #define NMEM_ENTER EnterCriticalSection(&critical_section)
112 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
114 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
115 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
116 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
122 static nmem_block *freelist = NULL; /* "global" freelists */
123 static nmem_control *cfreelist = NULL;
124 static int nmem_active_no = 0;
125 static int nmem_init_flag = 0;
132 struct nmem_debug *next;
135 struct nmem_debug *nmem_debug_list = 0;
138 static void free_block(nmem_block *p)
143 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
148 void nmem_print_list (void)
150 struct nmem_debug *p;
152 yaz_log (LOG_DEBUG, "nmem print list");
154 for (p = nmem_debug_list; p; p = p->next)
155 yaz_log (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p);
160 * acquire a block with a minimum of size free bytes.
162 static nmem_block *get_block(int size)
167 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
169 for (r = freelist, l = 0; r; l = r, r = r->next)
175 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
184 int get = NMEM_CHUNK;
189 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
191 r = (nmem_block *)xmalloc(sizeof(*r));
192 r->buf = (char *)xmalloc(r->size = get);
198 void nmem_reset(NMEM n)
203 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
211 n->blocks = n->blocks->next;
219 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
221 void *nmem_malloc(NMEM n, int size)
224 struct nmem_block *p;
228 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
234 return xmalloc(size);
237 assert (nmem_init_flag);
241 if (!p || p->size - p->top < size)
249 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
255 int nmem_total(NMEM n)
261 NMEM nmem_create_f(const char *file, int line)
263 NMEM nmem_create(void)
268 struct nmem_debug *debug_p;
275 cfreelist = cfreelist->next;
277 r = (nmem_control *)xmalloc(sizeof(*r));
281 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
289 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
292 yaz_log (LOG_FATAL, "multi used block in nmem");
295 debug_p = xmalloc (sizeof(*debug_p));
296 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
297 debug_p->file[sizeof(debug_p->file)-1] = '\0';
298 debug_p->line = line;
300 debug_p->next = nmem_debug_list;
301 nmem_debug_list = debug_p;
309 void nmem_destroy_f(const char *file, int line, NMEM n)
311 void nmem_destroy(NMEM n)
315 struct nmem_debug **debug_p;
322 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
323 nmem_active_no-1, n);
325 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
326 if ((*debug_p)->p == n)
328 struct nmem_debug *debug_save = *debug_p;
329 *debug_p = (*debug_p)->next;
338 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
351 void nmem_transfer (NMEM dst, NMEM src)
354 while ((t=src->blocks))
356 src->blocks = t->next;
357 t->next = dst->blocks;
360 dst->total += src->total;
364 void nmem_critical_enter (void)
369 void nmem_critical_leave (void)
374 void nmem_init (void)
376 if (++nmem_init_flag == 1)
379 InitializeCriticalSection(&critical_section);
387 void nmem_exit (void)
389 if (--nmem_init_flag == 0)
393 struct nmem_block *fl = freelist;
394 freelist = freelist->next;
400 struct nmem_control *cfl = cfreelist;
401 cfreelist = cfreelist->next;
405 DeleteCriticalSection(&critical_section);