2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: nmem.c,v 1.34 2001-11-18 21:05:13 adam Exp $
10 * This is a simple and fairly wasteful little module for nibble memory
11 * allocation. Evemtually we'll put in something better.
19 #include <yaz/xmalloc.h>
36 #define NMEM_CHUNK (4*1024)
39 static CRITICAL_SECTION critical_section;
40 #define NMEM_ENTER EnterCriticalSection(&critical_section)
41 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
43 CRITICAL_SECTION m_handle;
45 #elif YAZ_POSIX_THREADS
46 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
47 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
48 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
50 pthread_mutex_t m_handle;
53 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
54 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
55 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
67 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
72 *p = (NMEM_MUTEX) malloc (sizeof(**p));
74 InitializeCriticalSection(&(*p)->m_handle);
75 #elif YAZ_POSIX_THREADS
76 pthread_mutex_init (&(*p)->m_handle, 0);
78 pth_mutex_init (&(*p)->m_handle);
84 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
89 EnterCriticalSection(&p->m_handle);
90 #elif YAZ_POSIX_THREADS
91 pthread_mutex_lock(&p->m_handle);
96 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
101 LeaveCriticalSection(&p->m_handle);
102 #elif YAZ_POSIX_THREADS
103 pthread_mutex_unlock(&p->m_handle);
108 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
114 DeleteCriticalSection(&(*p)->m_handle);
122 static nmem_block *freelist = NULL; /* "global" freelists */
123 static nmem_control *cfreelist = NULL;
124 static int nmem_active_no = 0;
125 static int nmem_init_flag = 0;
128 struct nmem_debug_info {
132 struct nmem_debug_info *next;
135 struct nmem_debug_info *nmem_debug_list = 0;
138 static void free_block(nmem_block *p)
143 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
148 void nmem_print_list (void)
150 struct nmem_debug_info *p;
152 yaz_log (LOG_DEBUG, "nmem print list");
154 for (p = nmem_debug_list; p; p = p->next)
155 yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
161 * acquire a block with a minimum of size free bytes.
163 static nmem_block *get_block(int size)
168 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
170 for (r = freelist, l = 0; r; l = r, r = r->next)
176 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
185 int get = NMEM_CHUNK;
190 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
192 r = (nmem_block *)xmalloc(sizeof(*r));
193 r->buf = (char *)xmalloc(r->size = get);
199 void nmem_reset(NMEM n)
204 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
212 n->blocks = n->blocks->next;
220 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
222 void *nmem_malloc(NMEM n, int size)
225 struct nmem_block *p;
229 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
235 return xmalloc(size);
238 assert (nmem_init_flag);
242 if (!p || p->size - p->top < size)
250 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
256 int nmem_total(NMEM n)
262 NMEM nmem_create_f(const char *file, int line)
264 NMEM nmem_create(void)
269 struct nmem_debug_info *debug_p;
276 cfreelist = cfreelist->next;
278 r = (nmem_control *)xmalloc(sizeof(*r));
282 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
290 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
293 yaz_log (LOG_FATAL, "multi used block in nmem");
296 debug_p = xmalloc (sizeof(*debug_p));
297 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
298 debug_p->file[sizeof(debug_p->file)-1] = '\0';
299 debug_p->line = line;
301 debug_p->next = nmem_debug_list;
302 nmem_debug_list = debug_p;
310 void nmem_destroy_f(const char *file, int line, NMEM n)
312 void nmem_destroy(NMEM n)
316 struct nmem_debug_info **debug_p;
323 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
324 nmem_active_no-1, n);
326 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
327 if ((*debug_p)->p == n)
329 struct nmem_debug_info *debug_save = *debug_p;
330 *debug_p = (*debug_p)->next;
339 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
352 void nmem_transfer (NMEM dst, NMEM src)
355 while ((t=src->blocks))
357 src->blocks = t->next;
358 t->next = dst->blocks;
361 dst->total += src->total;
365 void nmem_critical_enter (void)
370 void nmem_critical_leave (void)
375 void nmem_init (void)
377 if (++nmem_init_flag == 1)
380 InitializeCriticalSection(&critical_section);
381 #elif YAZ_GNU_THREADS
382 yaz_log (LOG_LOG, "pth_init");
391 void nmem_exit (void)
393 if (--nmem_init_flag == 0)
398 struct nmem_block *fl = freelist;
399 freelist = freelist->next;
405 struct nmem_control *cfl = cfreelist;
406 cfreelist = cfreelist->next;
410 DeleteCriticalSection(&critical_section);
417 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
423 case DLL_PROCESS_ATTACH:
426 case DLL_PROCESS_DETACH: