2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 2000-02-29 13:44:55 adam
8 * Check for config.h (currently not generated).
10 * Revision 1.20 2000/01/06 14:59:13 adam
11 * Added oid_init/oid_exit. Changed oid_exit.
13 * Revision 1.19 1999/11/30 13:47:12 adam
14 * Improved installation. Moved header files to include/yaz.
16 * Revision 1.18 1999/08/27 09:40:32 adam
17 * Renamed logf function to yaz_log. Removed VC++ project files.
19 * Revision 1.17 1999/07/13 13:28:25 adam
20 * Better debugging for NMEM routines.
22 * Revision 1.16 1999/03/31 11:18:25 adam
23 * Implemented odr_strdup. Added Reference ID to backend server API.
25 * Revision 1.15 1999/02/11 09:10:26 adam
26 * Function nmem_init only mandatory on Windows.
28 * Revision 1.14 1999/02/02 13:57:40 adam
29 * Uses preprocessor define WIN32 instead of WINDOWS to build code
30 * for Microsoft WIN32.
32 * Revision 1.13 1998/10/19 15:24:21 adam
33 * New nmem utility, nmem_transfer, that transfer blocks from one
36 * Revision 1.12 1998/10/13 16:00:18 adam
37 * Implemented nmem_critical_{enter,leave}.
39 * Revision 1.11 1998/08/21 14:13:36 adam
40 * Added GNU Configure script to build Makefiles.
42 * Revision 1.10 1998/07/20 12:35:57 adam
43 * Added more memory diagnostics (when NMEM_DEBUG is 1).
45 * Revision 1.9 1998/07/07 15:49:01 adam
48 * Revision 1.8 1998/07/03 14:21:27 adam
49 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
50 * Fretwell Downing Informatics.
52 * Revision 1.7 1998/02/11 11:53:36 adam
53 * Changed code so that it compiles as C++.
55 * Revision 1.6 1997/10/31 12:20:09 adam
56 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
57 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
58 * Bug fix: missing fclose in data1_read_espec1.
60 * Revision 1.5 1997/10/06 09:09:52 adam
61 * Function mmem_exit releases memory used by the freelists.
63 * Revision 1.4 1997/09/29 07:12:50 adam
64 * NMEM thread safe. NMEM must be initialized before use (sigh) -
65 * routine nmem_init/nmem_exit implemented.
67 * Revision 1.3 1997/07/21 12:47:38 adam
68 * Moved definition of nmem_control and nmem_block.
70 * Revision 1.2 1995/12/13 13:44:37 quinn
71 * Modified Data1-system to use nmem
73 * Revision 1.1 1995/11/13 09:27:52 quinn
74 * Fiddling with the variant stuff.
80 * This is a simple and fairly wasteful little module for nibble memory
81 * allocation. Evemtually we'll put in something better.
89 #include <yaz/xmalloc.h>
104 #define NMEM_CHUNK (4*1024)
107 static CRITICAL_SECTION critical_section;
108 #define NMEM_ENTER EnterCriticalSection(&critical_section)
109 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
111 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
112 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
113 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
119 static nmem_block *freelist = NULL; /* "global" freelists */
120 static nmem_control *cfreelist = NULL;
121 static int nmem_active_no = 0;
122 static int nmem_init_flag = 0;
129 struct nmem_debug *next;
132 struct nmem_debug *nmem_debug_list = 0;
135 static void free_block(nmem_block *p)
140 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
145 void nmem_print_list (void)
147 struct nmem_debug *p;
149 yaz_log (LOG_DEBUG, "nmem print list");
151 for (p = nmem_debug_list; p; p = p->next)
152 yaz_log (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p);
157 * acquire a block with a minimum of size free bytes.
159 static nmem_block *get_block(int size)
164 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
166 for (r = freelist, l = 0; r; l = r, r = r->next)
172 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
181 int get = NMEM_CHUNK;
186 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
188 r = (nmem_block *)xmalloc(sizeof(*r));
189 r->buf = (char *)xmalloc(r->size = get);
195 void nmem_reset(NMEM n)
200 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
208 n->blocks = n->blocks->next;
216 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
218 void *nmem_malloc(NMEM n, int size)
221 struct nmem_block *p;
225 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
231 return xmalloc(size);
234 assert (nmem_init_flag);
238 if (!p || p->size - p->top < size)
246 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
252 int nmem_total(NMEM n)
258 NMEM nmem_create_f(const char *file, int line)
260 NMEM nmem_create(void)
265 struct nmem_debug *debug_p;
272 cfreelist = cfreelist->next;
274 r = (nmem_control *)xmalloc(sizeof(*r));
278 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
286 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
289 yaz_log (LOG_FATAL, "multi used block in nmem");
292 debug_p = xmalloc (sizeof(*debug_p));
293 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
294 debug_p->file[sizeof(debug_p->file)-1] = '\0';
295 debug_p->line = line;
297 debug_p->next = nmem_debug_list;
298 nmem_debug_list = debug_p;
306 void nmem_destroy_f(const char *file, int line, NMEM n)
308 void nmem_destroy(NMEM n)
312 struct nmem_debug **debug_p;
319 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
320 nmem_active_no-1, n);
322 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
323 if ((*debug_p)->p == n)
325 struct nmem_debug *debug_save = *debug_p;
326 *debug_p = (*debug_p)->next;
335 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
348 void nmem_transfer (NMEM dst, NMEM src)
351 while ((t=src->blocks))
353 src->blocks = t->next;
354 t->next = dst->blocks;
357 dst->total += src->total;
361 void nmem_critical_enter (void)
366 void nmem_critical_leave (void)
371 void nmem_init (void)
375 InitializeCriticalSection(&critical_section);
382 void nmem_exit (void)
386 struct nmem_block *fl = freelist;
387 freelist = freelist->next;
393 struct nmem_control *cfl = cfreelist;
394 cfreelist = cfreelist->next;
399 DeleteCriticalSection(&critical_section);