2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: nmem.c,v 1.36 2002-09-10 18:41:18 adam Exp $
10 * This is a simple and fairly wasteful little module for nibble memory
11 * allocation. Evemtually we'll put in something better.
20 #include <yaz/xmalloc.h>
37 #define NMEM_CHUNK (4*1024)
40 static CRITICAL_SECTION critical_section;
41 #define NMEM_ENTER EnterCriticalSection(&critical_section)
42 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
44 CRITICAL_SECTION m_handle;
46 #elif YAZ_POSIX_THREADS
47 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
48 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
49 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
51 pthread_mutex_t m_handle;
54 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
55 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
56 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
68 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
73 *p = (NMEM_MUTEX) malloc (sizeof(**p));
75 InitializeCriticalSection(&(*p)->m_handle);
76 #elif YAZ_POSIX_THREADS
77 pthread_mutex_init (&(*p)->m_handle, 0);
79 pth_mutex_init (&(*p)->m_handle);
85 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
90 EnterCriticalSection(&p->m_handle);
91 #elif YAZ_POSIX_THREADS
92 pthread_mutex_lock(&p->m_handle);
97 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
102 LeaveCriticalSection(&p->m_handle);
103 #elif YAZ_POSIX_THREADS
104 pthread_mutex_unlock(&p->m_handle);
109 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
115 DeleteCriticalSection(&(*p)->m_handle);
123 static nmem_block *freelist = NULL; /* "global" freelists */
124 static nmem_control *cfreelist = NULL;
125 static int nmem_active_no = 0;
126 static int nmem_init_flag = 0;
129 struct nmem_debug_info {
133 struct nmem_debug_info *next;
136 struct nmem_debug_info *nmem_debug_list = 0;
139 static void free_block(nmem_block *p)
144 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
149 void nmem_print_list (void)
151 struct nmem_debug_info *p;
153 yaz_log (LOG_DEBUG, "nmem print list");
155 for (p = nmem_debug_list; p; p = p->next)
156 yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
162 * acquire a block with a minimum of size free bytes.
164 static nmem_block *get_block(int size)
169 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
171 for (r = freelist, l = 0; r; l = r, r = r->next)
177 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
186 int get = NMEM_CHUNK;
191 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
193 r = (nmem_block *)xmalloc(sizeof(*r));
194 r->buf = (char *)xmalloc(r->size = get);
200 void nmem_reset(NMEM n)
205 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
213 n->blocks = n->blocks->next;
221 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
223 void *nmem_malloc(NMEM n, int size)
226 struct nmem_block *p;
230 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
235 yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
239 assert (nmem_init_flag);
243 if (!p || p->size - p->top < size)
251 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
257 int nmem_total(NMEM n)
263 NMEM nmem_create_f(const char *file, int line)
265 NMEM nmem_create(void)
270 struct nmem_debug_info *debug_p;
277 cfreelist = cfreelist->next;
279 r = (nmem_control *)xmalloc(sizeof(*r));
283 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
291 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
294 yaz_log (LOG_FATAL, "multi used block in nmem");
297 debug_p = xmalloc (sizeof(*debug_p));
298 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
299 debug_p->file[sizeof(debug_p->file)-1] = '\0';
300 debug_p->line = line;
302 debug_p->next = nmem_debug_list;
303 nmem_debug_list = debug_p;
311 void nmem_destroy_f(const char *file, int line, NMEM n)
313 void nmem_destroy(NMEM n)
317 struct nmem_debug_info **debug_p;
324 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
325 nmem_active_no-1, n);
327 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
328 if ((*debug_p)->p == n)
330 struct nmem_debug_info *debug_save = *debug_p;
331 *debug_p = (*debug_p)->next;
340 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
353 void nmem_transfer (NMEM dst, NMEM src)
356 while ((t=src->blocks))
358 src->blocks = t->next;
359 t->next = dst->blocks;
362 dst->total += src->total;
366 void nmem_critical_enter (void)
371 void nmem_critical_leave (void)
376 void nmem_init (void)
378 if (++nmem_init_flag == 1)
381 InitializeCriticalSection(&critical_section);
382 #elif YAZ_GNU_THREADS
383 yaz_log (LOG_LOG, "pth_init");
392 void nmem_exit (void)
394 if (--nmem_init_flag == 0)
399 struct nmem_block *fl = freelist;
400 freelist = freelist->next;
406 struct nmem_control *cfl = cfreelist;
407 cfreelist = cfreelist->next;
411 DeleteCriticalSection(&critical_section);
418 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
424 case DLL_PROCESS_ATTACH:
427 case DLL_PROCESS_DETACH: