2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1999-02-02 13:57:40 adam
8 * Uses preprocessor define WIN32 instead of WINDOWS to build code
11 * Revision 1.13 1998/10/19 15:24:21 adam
12 * New nmem utility, nmem_transfer, that transfer blocks from one
15 * Revision 1.12 1998/10/13 16:00:18 adam
16 * Implemented nmem_critical_{enter,leave}.
18 * Revision 1.11 1998/08/21 14:13:36 adam
19 * Added GNU Configure script to build Makefiles.
21 * Revision 1.10 1998/07/20 12:35:57 adam
22 * Added more memory diagnostics (when NMEM_DEBUG is 1).
24 * Revision 1.9 1998/07/07 15:49:01 adam
27 * Revision 1.8 1998/07/03 14:21:27 adam
28 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
29 * Fretwell Downing Informatics.
31 * Revision 1.7 1998/02/11 11:53:36 adam
32 * Changed code so that it compiles as C++.
34 * Revision 1.6 1997/10/31 12:20:09 adam
35 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
36 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
37 * Bug fix: missing fclose in data1_read_espec1.
39 * Revision 1.5 1997/10/06 09:09:52 adam
40 * Function mmem_exit releases memory used by the freelists.
42 * Revision 1.4 1997/09/29 07:12:50 adam
43 * NMEM thread safe. NMEM must be initialized before use (sigh) -
44 * routine nmem_init/nmem_exit implemented.
46 * Revision 1.3 1997/07/21 12:47:38 adam
47 * Moved definition of nmem_control and nmem_block.
49 * Revision 1.2 1995/12/13 13:44:37 quinn
50 * Modified Data1-system to use nmem
52 * Revision 1.1 1995/11/13 09:27:52 quinn
53 * Fiddling with the variant stuff.
59 * This is a simple and fairly wasteful little module for nibble memory
60 * allocation. Evemtually we'll put in something better.
79 #define NMEM_CHUNK (4*1024)
82 static CRITICAL_SECTION critical_section;
83 #define NMEM_ENTER EnterCriticalSection(&critical_section)
84 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
86 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
87 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
88 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
94 static nmem_block *freelist = NULL; /* "global" freelists */
95 static nmem_control *cfreelist = NULL;
96 static int nmem_active_no = 0;
97 static int nmem_init_flag = 0;
99 static void free_block(nmem_block *p)
104 logf (LOG_DEBUG, "nmem free_block p=%p", p);
109 * acquire a block with a minimum of size free bytes.
111 static nmem_block *get_block(int size)
116 logf (LOG_DEBUG, "nmem get_block size=%d", size);
118 for (r = freelist, l = 0; r; l = r, r = r->next)
124 logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
133 int get = NMEM_CHUNK;
138 logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
140 r = (nmem_block *)xmalloc(sizeof(*r));
141 r->buf = (char *)xmalloc(r->size = get);
147 void nmem_reset(NMEM n)
152 logf (LOG_DEBUG, "nmem_reset p=%p", n);
160 n->blocks = n->blocks->next;
168 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
170 void *nmem_malloc(NMEM n, int size)
173 struct nmem_block *p;
177 logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
181 return xmalloc(size);
182 assert (nmem_init_flag);
185 if (!p || p->size - p->top < size)
193 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
199 int nmem_total(NMEM n)
205 NMEM nmem_create_f(const char *file, int line)
207 NMEM nmem_create(void)
216 cfreelist = cfreelist->next;
218 r = (nmem_control *)xmalloc(sizeof(*r));
222 logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
223 nmem_active_no-1, r);
232 void nmem_destroy_f(const char *file, int line, NMEM n)
234 void nmem_destroy(NMEM n)
246 logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
251 void nmem_transfer (NMEM dst, NMEM src)
254 while ((t=src->blocks))
256 src->blocks = t->next;
257 t->next = dst->blocks;
260 dst->total += src->total;
264 void nmem_critical_enter (void)
269 void nmem_critical_leave (void)
274 void nmem_init (void)
278 InitializeCriticalSection(&critical_section);
285 void nmem_exit (void)
289 struct nmem_block *fl = freelist;
290 freelist = freelist->next;
296 struct nmem_control *cfl = cfreelist;
297 cfreelist = cfreelist->next;
301 DeleteCriticalSection(&critical_section);