2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1999-11-30 13:47:12 adam
8 * Improved installation. Moved header files to include/yaz.
10 * Revision 1.18 1999/08/27 09:40:32 adam
11 * Renamed logf function to yaz_log. Removed VC++ project files.
13 * Revision 1.17 1999/07/13 13:28:25 adam
14 * Better debugging for NMEM routines.
16 * Revision 1.16 1999/03/31 11:18:25 adam
17 * Implemented odr_strdup. Added Reference ID to backend server API.
19 * Revision 1.15 1999/02/11 09:10:26 adam
20 * Function nmem_init only mandatory on Windows.
22 * Revision 1.14 1999/02/02 13:57:40 adam
23 * Uses preprocessor define WIN32 instead of WINDOWS to build code
24 * for Microsoft WIN32.
26 * Revision 1.13 1998/10/19 15:24:21 adam
27 * New nmem utility, nmem_transfer, that transfer blocks from one
30 * Revision 1.12 1998/10/13 16:00:18 adam
31 * Implemented nmem_critical_{enter,leave}.
33 * Revision 1.11 1998/08/21 14:13:36 adam
34 * Added GNU Configure script to build Makefiles.
36 * Revision 1.10 1998/07/20 12:35:57 adam
37 * Added more memory diagnostics (when NMEM_DEBUG is 1).
39 * Revision 1.9 1998/07/07 15:49:01 adam
42 * Revision 1.8 1998/07/03 14:21:27 adam
43 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
44 * Fretwell Downing Informatics.
46 * Revision 1.7 1998/02/11 11:53:36 adam
47 * Changed code so that it compiles as C++.
49 * Revision 1.6 1997/10/31 12:20:09 adam
50 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
51 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
52 * Bug fix: missing fclose in data1_read_espec1.
54 * Revision 1.5 1997/10/06 09:09:52 adam
55 * Function mmem_exit releases memory used by the freelists.
57 * Revision 1.4 1997/09/29 07:12:50 adam
58 * NMEM thread safe. NMEM must be initialized before use (sigh) -
59 * routine nmem_init/nmem_exit implemented.
61 * Revision 1.3 1997/07/21 12:47:38 adam
62 * Moved definition of nmem_control and nmem_block.
64 * Revision 1.2 1995/12/13 13:44:37 quinn
65 * Modified Data1-system to use nmem
67 * Revision 1.1 1995/11/13 09:27:52 quinn
68 * Fiddling with the variant stuff.
74 * This is a simple and fairly wasteful little module for nibble memory
75 * allocation. Evemtually we'll put in something better.
80 #include <yaz/xmalloc.h>
95 #define NMEM_CHUNK (4*1024)
98 static CRITICAL_SECTION critical_section;
99 #define NMEM_ENTER EnterCriticalSection(&critical_section)
100 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
102 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
103 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
104 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
110 static nmem_block *freelist = NULL; /* "global" freelists */
111 static nmem_control *cfreelist = NULL;
112 static int nmem_active_no = 0;
113 static int nmem_init_flag = 0;
120 struct nmem_debug *next;
123 struct nmem_debug *nmem_debug_list = 0;
126 static void free_block(nmem_block *p)
131 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
136 void nmem_print_list (void)
138 struct nmem_debug *p;
140 yaz_log (LOG_DEBUG, "nmem print list");
142 for (p = nmem_debug_list; p; p = p->next)
143 yaz_log (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p);
148 * acquire a block with a minimum of size free bytes.
150 static nmem_block *get_block(int size)
155 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
157 for (r = freelist, l = 0; r; l = r, r = r->next)
163 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
172 int get = NMEM_CHUNK;
177 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
179 r = (nmem_block *)xmalloc(sizeof(*r));
180 r->buf = (char *)xmalloc(r->size = get);
186 void nmem_reset(NMEM n)
191 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
199 n->blocks = n->blocks->next;
207 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
209 void *nmem_malloc(NMEM n, int size)
212 struct nmem_block *p;
216 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
222 return xmalloc(size);
225 assert (nmem_init_flag);
229 if (!p || p->size - p->top < size)
237 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
243 int nmem_total(NMEM n)
249 NMEM nmem_create_f(const char *file, int line)
251 NMEM nmem_create(void)
256 struct nmem_debug *debug_p;
263 cfreelist = cfreelist->next;
265 r = (nmem_control *)xmalloc(sizeof(*r));
269 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
277 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
280 yaz_log (LOG_FATAL, "multi used block in nmem");
283 debug_p = xmalloc (sizeof(*debug_p));
284 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
285 debug_p->file[sizeof(debug_p->file)-1] = '\0';
286 debug_p->line = line;
288 debug_p->next = nmem_debug_list;
289 nmem_debug_list = debug_p;
297 void nmem_destroy_f(const char *file, int line, NMEM n)
299 void nmem_destroy(NMEM n)
303 struct nmem_debug **debug_p;
310 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
311 nmem_active_no-1, n);
313 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
314 if ((*debug_p)->p == n)
316 struct nmem_debug *debug_save = *debug_p;
317 *debug_p = (*debug_p)->next;
326 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
339 void nmem_transfer (NMEM dst, NMEM src)
342 while ((t=src->blocks))
344 src->blocks = t->next;
345 t->next = dst->blocks;
348 dst->total += src->total;
352 void nmem_critical_enter (void)
357 void nmem_critical_leave (void)
362 void nmem_init (void)
366 InitializeCriticalSection(&critical_section);
373 void nmem_exit (void)
377 struct nmem_block *fl = freelist;
378 freelist = freelist->next;
384 struct nmem_control *cfl = cfreelist;
385 cfreelist = cfreelist->next;
389 DeleteCriticalSection(&critical_section);