2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1999-07-13 13:28:25 adam
8 * Better debugging for NMEM routines.
10 * Revision 1.16 1999/03/31 11:18:25 adam
11 * Implemented odr_strdup. Added Reference ID to backend server API.
13 * Revision 1.15 1999/02/11 09:10:26 adam
14 * Function nmem_init only mandatory on Windows.
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.
20 * Revision 1.13 1998/10/19 15:24:21 adam
21 * New nmem utility, nmem_transfer, that transfer blocks from one
24 * Revision 1.12 1998/10/13 16:00:18 adam
25 * Implemented nmem_critical_{enter,leave}.
27 * Revision 1.11 1998/08/21 14:13:36 adam
28 * Added GNU Configure script to build Makefiles.
30 * Revision 1.10 1998/07/20 12:35:57 adam
31 * Added more memory diagnostics (when NMEM_DEBUG is 1).
33 * Revision 1.9 1998/07/07 15:49:01 adam
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.
40 * Revision 1.7 1998/02/11 11:53:36 adam
41 * Changed code so that it compiles as C++.
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.
48 * Revision 1.5 1997/10/06 09:09:52 adam
49 * Function mmem_exit releases memory used by the freelists.
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.
55 * Revision 1.3 1997/07/21 12:47:38 adam
56 * Moved definition of nmem_control and nmem_block.
58 * Revision 1.2 1995/12/13 13:44:37 quinn
59 * Modified Data1-system to use nmem
61 * Revision 1.1 1995/11/13 09:27:52 quinn
62 * Fiddling with the variant stuff.
68 * This is a simple and fairly wasteful little module for nibble memory
69 * allocation. Evemtually we'll put in something better.
89 #define NMEM_CHUNK (4*1024)
92 static CRITICAL_SECTION critical_section;
93 #define NMEM_ENTER EnterCriticalSection(&critical_section)
94 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
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);
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;
114 struct nmem_debug *next;
117 struct nmem_debug *nmem_debug_list = 0;
120 static void free_block(nmem_block *p)
125 logf (LOG_DEBUG, "nmem free_block p=%p", p);
130 void nmem_print_list (void)
132 struct nmem_debug *p;
134 logf (LOG_DEBUG, "nmem print list");
136 for (p = nmem_debug_list; p; p = p->next)
137 logf (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p);
142 * acquire a block with a minimum of size free bytes.
144 static nmem_block *get_block(int size)
149 logf (LOG_DEBUG, "nmem get_block size=%d", size);
151 for (r = freelist, l = 0; r; l = r, r = r->next)
157 logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
166 int get = NMEM_CHUNK;
171 logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
173 r = (nmem_block *)xmalloc(sizeof(*r));
174 r->buf = (char *)xmalloc(r->size = get);
180 void nmem_reset(NMEM n)
185 logf (LOG_DEBUG, "nmem_reset p=%p", n);
193 n->blocks = n->blocks->next;
201 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
203 void *nmem_malloc(NMEM n, int size)
206 struct nmem_block *p;
210 logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
216 return xmalloc(size);
219 assert (nmem_init_flag);
223 if (!p || p->size - p->top < size)
231 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
237 int nmem_total(NMEM n)
243 NMEM nmem_create_f(const char *file, int line)
245 NMEM nmem_create(void)
250 struct nmem_debug *debug_p;
257 cfreelist = cfreelist->next;
259 r = (nmem_control *)xmalloc(sizeof(*r));
263 logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
271 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
274 logf (LOG_FATAL, "multi used block in nmem");
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;
282 debug_p->next = nmem_debug_list;
283 nmem_debug_list = debug_p;
291 void nmem_destroy_f(const char *file, int line, NMEM n)
293 void nmem_destroy(NMEM n)
297 struct nmem_debug **debug_p;
304 logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
305 nmem_active_no-1, n);
307 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
308 if ((*debug_p)->p == n)
310 struct nmem_debug *debug_save = *debug_p;
311 *debug_p = (*debug_p)->next;
320 logf (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
333 void nmem_transfer (NMEM dst, NMEM src)
336 while ((t=src->blocks))
338 src->blocks = t->next;
339 t->next = dst->blocks;
342 dst->total += src->total;
346 void nmem_critical_enter (void)
351 void nmem_critical_leave (void)
356 void nmem_init (void)
360 InitializeCriticalSection(&critical_section);
367 void nmem_exit (void)
371 struct nmem_block *fl = freelist;
372 freelist = freelist->next;
378 struct nmem_control *cfl = cfreelist;
379 cfreelist = cfreelist->next;
383 DeleteCriticalSection(&critical_section);