2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-08-21 14:13:36 adam
8 * Added GNU Configure script to build Makefiles.
10 * Revision 1.10 1998/07/20 12:35:57 adam
11 * Added more memory diagnostics (when NMEM_DEBUG is 1).
13 * Revision 1.9 1998/07/07 15:49:01 adam
16 * Revision 1.8 1998/07/03 14:21:27 adam
17 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
18 * Fretwell Downing Informatics.
20 * Revision 1.7 1998/02/11 11:53:36 adam
21 * Changed code so that it compiles as C++.
23 * Revision 1.6 1997/10/31 12:20:09 adam
24 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
25 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
26 * Bug fix: missing fclose in data1_read_espec1.
28 * Revision 1.5 1997/10/06 09:09:52 adam
29 * Function mmem_exit releases memory used by the freelists.
31 * Revision 1.4 1997/09/29 07:12:50 adam
32 * NMEM thread safe. NMEM must be initialized before use (sigh) -
33 * routine nmem_init/nmem_exit implemented.
35 * Revision 1.3 1997/07/21 12:47:38 adam
36 * Moved definition of nmem_control and nmem_block.
38 * Revision 1.2 1995/12/13 13:44:37 quinn
39 * Modified Data1-system to use nmem
41 * Revision 1.1 1995/11/13 09:27:52 quinn
42 * Fiddling with the variant stuff.
48 * This is a simple and fairly wasteful little module for nibble memory
49 * allocation. Evemtually we'll put in something better.
67 #define NMEM_CHUNK (4*1024)
70 static CRITICAL_SECTION critical_section;
71 #define NMEM_ENTER EnterCriticalSection(&critical_section)
72 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
74 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
75 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
76 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
82 static nmem_block *freelist = NULL; /* "global" freelists */
83 static nmem_control *cfreelist = NULL;
84 static int nmem_active_no = 0;
86 static void free_block(nmem_block *p)
91 logf (LOG_DEBUG, "nmem free_block p=%p", p);
96 * acquire a block with a minimum of size free bytes.
98 static nmem_block *get_block(int size)
103 logf (LOG_DEBUG, "nmem get_block size=%d", size);
105 for (r = freelist, l = 0; r; l = r, r = r->next)
111 logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
120 int get = NMEM_CHUNK;
125 logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
127 r = (nmem_block *)xmalloc(sizeof(*r));
128 r->buf = (char *)xmalloc(r->size = get);
134 void nmem_reset(NMEM n)
139 logf (LOG_DEBUG, "nmem_reset p=%p", n);
147 n->blocks = n->blocks->next;
155 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
157 void *nmem_malloc(NMEM n, int size)
160 struct nmem_block *p;
164 logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
168 return xmalloc(size);
171 if (!p || p->size - p->top < size)
179 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
185 int nmem_total(NMEM n)
191 NMEM nmem_create_f(const char *file, int line)
193 NMEM nmem_create(void)
202 cfreelist = cfreelist->next;
204 r = (nmem_control *)xmalloc(sizeof(*r));
208 logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
209 nmem_active_no-1, r);
218 void nmem_destroy_f(const char *file, int line, NMEM n)
220 void nmem_destroy(NMEM n)
232 logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
237 void nmem_init (void)
240 InitializeCriticalSection(&critical_section);
247 void nmem_exit (void)
251 struct nmem_block *fl = freelist;
252 freelist = freelist->next;
258 struct nmem_control *cfl = cfreelist;
259 cfreelist = cfreelist->next;
263 DeleteCriticalSection(&critical_section);