2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1998-07-20 12:35:57 adam
8 * Added more memory diagnostics (when NMEM_DEBUG is 1).
10 * Revision 1.9 1998/07/07 15:49:01 adam
13 * Revision 1.8 1998/07/03 14:21:27 adam
14 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
15 * Fretwell Downing Informatics.
17 * Revision 1.7 1998/02/11 11:53:36 adam
18 * Changed code so that it compiles as C++.
20 * Revision 1.6 1997/10/31 12:20:09 adam
21 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
22 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
23 * Bug fix: missing fclose in data1_read_espec1.
25 * Revision 1.5 1997/10/06 09:09:52 adam
26 * Function mmem_exit releases memory used by the freelists.
28 * Revision 1.4 1997/09/29 07:12:50 adam
29 * NMEM thread safe. NMEM must be initialized before use (sigh) -
30 * routine nmem_init/nmem_exit implemented.
32 * Revision 1.3 1997/07/21 12:47:38 adam
33 * Moved definition of nmem_control and nmem_block.
35 * Revision 1.2 1995/12/13 13:44:37 quinn
36 * Modified Data1-system to use nmem
38 * Revision 1.1 1995/11/13 09:27:52 quinn
39 * Fiddling with the variant stuff.
45 * This is a simple and fairly wasteful little module for nibble memory
46 * allocation. Evemtually we'll put in something better.
58 #define NMEM_CHUNK (4*1024)
61 static CRITICAL_SECTION critical_section;
62 #define NMEM_ENTER EnterCriticalSection(&critical_section)
63 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
65 static pthread_mutex_t nmem_mutex;
66 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
67 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
73 static nmem_block *freelist = NULL; /* "global" freelists */
74 static nmem_control *cfreelist = NULL;
75 static int nmem_active_no = 0;
77 static void free_block(nmem_block *p)
82 logf (LOG_DEBUG, "nmem free_block p=%p", p);
87 * acquire a block with a minimum of size free bytes.
89 static nmem_block *get_block(int size)
94 logf (LOG_DEBUG, "nmem get_block size=%d", size);
96 for (r = freelist, l = 0; r; l = r, r = r->next)
102 logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
111 int get = NMEM_CHUNK;
116 logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
118 r = (nmem_block *)xmalloc(sizeof(*r));
119 r->buf = (char *)xmalloc(r->size = get);
125 void nmem_reset(NMEM n)
130 logf (LOG_DEBUG, "nmem_reset p=%p", n);
138 n->blocks = n->blocks->next;
146 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
148 void *nmem_malloc(NMEM n, int size)
151 struct nmem_block *p;
155 logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
159 return xmalloc(size);
162 if (!p || p->size - p->top < size)
170 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
176 int nmem_total(NMEM n)
182 NMEM nmem_create_f(const char *file, int line)
184 NMEM nmem_create(void)
193 cfreelist = cfreelist->next;
195 r = (nmem_control *)xmalloc(sizeof(*r));
199 logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
200 nmem_active_no-1, r);
209 void nmem_destroy_f(const char *file, int line, NMEM n)
211 void nmem_destroy(NMEM n)
223 logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
228 void nmem_init (void)
231 InitializeCriticalSection(&critical_section);
238 void nmem_exit (void)
242 struct nmem_block *fl = freelist;
243 freelist = freelist->next;
249 struct nmem_control *cfl = cfreelist;
250 cfreelist = cfreelist->next;
254 DeleteCriticalSection(&critical_section);