2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-03-31 11:18:25 adam
8 * Implemented odr_strdup. Added Reference ID to backend server API.
10 * Revision 1.15 1999/02/11 09:10:26 adam
11 * Function nmem_init only mandatory on Windows.
13 * Revision 1.14 1999/02/02 13:57:40 adam
14 * Uses preprocessor define WIN32 instead of WINDOWS to build code
15 * for Microsoft WIN32.
17 * Revision 1.13 1998/10/19 15:24:21 adam
18 * New nmem utility, nmem_transfer, that transfer blocks from one
21 * Revision 1.12 1998/10/13 16:00:18 adam
22 * Implemented nmem_critical_{enter,leave}.
24 * Revision 1.11 1998/08/21 14:13:36 adam
25 * Added GNU Configure script to build Makefiles.
27 * Revision 1.10 1998/07/20 12:35:57 adam
28 * Added more memory diagnostics (when NMEM_DEBUG is 1).
30 * Revision 1.9 1998/07/07 15:49:01 adam
33 * Revision 1.8 1998/07/03 14:21:27 adam
34 * Added critical sections for pthreads-library. Thanks to Ian Ibbotson,
35 * Fretwell Downing Informatics.
37 * Revision 1.7 1998/02/11 11:53:36 adam
38 * Changed code so that it compiles as C++.
40 * Revision 1.6 1997/10/31 12:20:09 adam
41 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
42 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
43 * Bug fix: missing fclose in data1_read_espec1.
45 * Revision 1.5 1997/10/06 09:09:52 adam
46 * Function mmem_exit releases memory used by the freelists.
48 * Revision 1.4 1997/09/29 07:12:50 adam
49 * NMEM thread safe. NMEM must be initialized before use (sigh) -
50 * routine nmem_init/nmem_exit implemented.
52 * Revision 1.3 1997/07/21 12:47:38 adam
53 * Moved definition of nmem_control and nmem_block.
55 * Revision 1.2 1995/12/13 13:44:37 quinn
56 * Modified Data1-system to use nmem
58 * Revision 1.1 1995/11/13 09:27:52 quinn
59 * Fiddling with the variant stuff.
65 * This is a simple and fairly wasteful little module for nibble memory
66 * allocation. Evemtually we'll put in something better.
85 #define NMEM_CHUNK (4*1024)
88 static CRITICAL_SECTION critical_section;
89 #define NMEM_ENTER EnterCriticalSection(&critical_section)
90 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
92 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
93 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
94 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
100 static nmem_block *freelist = NULL; /* "global" freelists */
101 static nmem_control *cfreelist = NULL;
102 static int nmem_active_no = 0;
103 static int nmem_init_flag = 0;
105 static void free_block(nmem_block *p)
110 logf (LOG_DEBUG, "nmem free_block p=%p", p);
115 * acquire a block with a minimum of size free bytes.
117 static nmem_block *get_block(int size)
122 logf (LOG_DEBUG, "nmem get_block size=%d", size);
124 for (r = freelist, l = 0; r; l = r, r = r->next)
130 logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
139 int get = NMEM_CHUNK;
144 logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
146 r = (nmem_block *)xmalloc(sizeof(*r));
147 r->buf = (char *)xmalloc(r->size = get);
153 void nmem_reset(NMEM n)
158 logf (LOG_DEBUG, "nmem_reset p=%p", n);
166 n->blocks = n->blocks->next;
174 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
176 void *nmem_malloc(NMEM n, int size)
179 struct nmem_block *p;
183 logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
189 return xmalloc(size);
192 assert (nmem_init_flag);
196 if (!p || p->size - p->top < size)
204 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
210 int nmem_total(NMEM n)
216 NMEM nmem_create_f(const char *file, int line)
218 NMEM nmem_create(void)
227 cfreelist = cfreelist->next;
229 r = (nmem_control *)xmalloc(sizeof(*r));
233 logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
234 nmem_active_no-1, r);
243 void nmem_destroy_f(const char *file, int line, NMEM n)
245 void nmem_destroy(NMEM n)
257 logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
262 void nmem_transfer (NMEM dst, NMEM src)
265 while ((t=src->blocks))
267 src->blocks = t->next;
268 t->next = dst->blocks;
271 dst->total += src->total;
275 void nmem_critical_enter (void)
280 void nmem_critical_leave (void)
285 void nmem_init (void)
289 InitializeCriticalSection(&critical_section);
296 void nmem_exit (void)
300 struct nmem_block *fl = freelist;
301 freelist = freelist->next;
307 struct nmem_control *cfl = cfreelist;
308 cfreelist = cfreelist->next;
312 DeleteCriticalSection(&critical_section);