2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.14 2005-01-21 09:23:27 adam Exp $
10 * \brief Implements Nibble Memory
12 * This is a simple and fairly wasteful little module for nibble memory
13 * allocation. Evemtually we'll put in something better.
15 * FIXME - it also has some semaphore stuff, and stuff to handle errno.
16 * These should be moved to some other place!
27 #include <yaz/xmalloc.h>
44 #define NMEM_CHUNK (4*1024)
61 #define NMEM_ALIGN (offsetof(struct align, u))
63 static int log_level = 0;
64 static int log_level_initialized = 0;
67 static CRITICAL_SECTION critical_section;
68 #define NMEM_ENTER EnterCriticalSection(&critical_section)
69 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
71 CRITICAL_SECTION m_handle;
73 #elif YAZ_POSIX_THREADS
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);
78 pthread_mutex_t m_handle;
81 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
82 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
83 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
95 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
97 if (!log_level_initialized)
99 log_level = yaz_log_module_level("nmem");
100 log_level_initialized = 1;
105 *p = (NMEM_MUTEX) malloc (sizeof(**p));
107 InitializeCriticalSection(&(*p)->m_handle);
108 #elif YAZ_POSIX_THREADS
109 pthread_mutex_init (&(*p)->m_handle, 0);
110 #elif YAZ_GNU_THREADS
111 pth_mutex_init (&(*p)->m_handle);
116 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
121 EnterCriticalSection(&p->m_handle);
122 #elif YAZ_POSIX_THREADS
123 pthread_mutex_lock(&p->m_handle);
128 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
133 LeaveCriticalSection(&p->m_handle);
134 #elif YAZ_POSIX_THREADS
135 pthread_mutex_unlock(&p->m_handle);
140 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
145 DeleteCriticalSection(&(*p)->m_handle);
152 static nmem_block *freelist = NULL; /* "global" freelists */
153 static nmem_control *cfreelist = NULL;
154 static int nmem_active_no = 0;
155 static int nmem_init_flag = 0;
158 struct nmem_debug_info {
162 struct nmem_debug_info *next;
165 struct nmem_debug_info *nmem_debug_list = 0;
168 static void free_block(nmem_block *p)
170 memset(p->buf, 'Y', p->size);
174 yaz_log (log_level, "nmem free_block p=%p", p);
178 void nmem_print_list (void)
181 nmem_print_list_l(log_level);
184 void nmem_print_list_l (int level)
186 struct nmem_debug_info *p;
188 yaz_log (level, "nmem print list");
190 for (p = nmem_debug_list; p; p = p->next)
191 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
197 * acquire a block with a minimum of size free bytes.
199 static nmem_block *get_block(int size)
204 yaz_log (log_level, "nmem get_block size=%d", size);
206 for (r = freelist, l = 0; r; l = r, r = r->next)
212 yaz_log (log_level, "nmem get_block found free block p=%p", r);
220 int get = NMEM_CHUNK;
225 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
227 r = (nmem_block *)xmalloc(sizeof(*r));
228 r->buf = (char *)xmalloc(r->size = get);
234 void nmem_reset(NMEM n)
238 yaz_log (log_level, "nmem_reset p=%p", n);
245 n->blocks = n->blocks->next;
253 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
255 void *nmem_malloc(NMEM n, int size)
258 struct nmem_block *p;
263 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
264 file, line, n, size);
268 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
272 assert (nmem_init_flag);
276 if (!p || p->size - p->top < size)
284 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
290 int nmem_total(NMEM n)
296 NMEM nmem_create_f(const char *file, int line)
298 NMEM nmem_create(void)
303 struct nmem_debug_info *debug_p;
305 if (!log_level_initialized)
307 log_level = yaz_log_module_level("nmem");
308 log_level_initialized = 1;
315 cfreelist = cfreelist->next;
317 r = (nmem_control *)xmalloc(sizeof(*r));
321 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
329 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
332 yaz_log (YLOG_FATAL, "multi used block in nmem");
335 debug_p = xmalloc (sizeof(*debug_p));
336 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
337 debug_p->file[sizeof(debug_p->file)-1] = '\0';
338 debug_p->line = line;
340 debug_p->next = nmem_debug_list;
341 nmem_debug_list = debug_p;
349 void nmem_destroy_f(const char *file, int line, NMEM n)
351 void nmem_destroy(NMEM n)
355 struct nmem_debug_info **debug_p;
362 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
363 nmem_active_no-1, n);
365 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
366 if ((*debug_p)->p == n)
368 struct nmem_debug_info *debug_save = *debug_p;
369 *debug_p = (*debug_p)->next;
378 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
391 void nmem_transfer (NMEM dst, NMEM src)
394 while ((t = src->blocks))
396 src->blocks = t->next;
397 t->next = dst->blocks;
400 dst->total += src->total;
404 void nmem_critical_enter (void)
409 void nmem_critical_leave (void)
414 void nmem_init (void)
417 if (++nmem_init_flag == 1)
420 InitializeCriticalSection(&critical_section);
421 #elif YAZ_GNU_THREADS
428 if (!log_level_initialized)
430 log_level = yaz_log_module_level("nmem");
431 log_level_initialized = 1;
435 void nmem_exit (void)
437 if (--nmem_init_flag == 0)
442 struct nmem_block *fl = freelist;
443 freelist = freelist->next;
449 struct nmem_control *cfl = cfreelist;
450 cfreelist = cfreelist->next;
454 DeleteCriticalSection(&critical_section);
461 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
467 case DLL_PROCESS_ATTACH:
470 case DLL_PROCESS_DETACH:
482 void yaz_set_errno(int v)
487 void yaz_strerror(char *buf, int max)
493 if (!log_level_initialized)
495 log_level = yaz_log_module_level("nmem");
496 log_level_initialized = 1;
500 err = GetLastError();
504 FORMAT_MESSAGE_FROM_SYSTEM,
507 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
517 #if YAZ_POSIX_THREADS
519 strerror_r(errno, buf, max);
520 /* if buffer is unset - use strerror anyway (GLIBC bug) */
522 strcpy(buf, strerror(yaz_errno()));
524 strcpy(buf, strerror(yaz_errno()));
527 strcpy(buf, strerror(yaz_errno()));
531 if ((cp = strrchr(buf, '\n')))
533 if ((cp = strrchr(buf, '\r')))