2 * Copyright (c) 1995-2005, Index Data.
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.11 2005-01-05 10:23:42 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!
26 #include <yaz/xmalloc.h>
43 #define NMEM_CHUNK (4*1024)
60 #define NMEM_ALIGN (offsetof(struct align, u))
62 static int log_level=0;
63 static int log_level_initialized=0;
66 static CRITICAL_SECTION critical_section;
67 #define NMEM_ENTER EnterCriticalSection(&critical_section)
68 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
70 CRITICAL_SECTION m_handle;
72 #elif YAZ_POSIX_THREADS
73 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
74 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
75 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
77 pthread_mutex_t m_handle;
80 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
81 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
82 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
94 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
96 if (!log_level_initialized)
98 log_level=yaz_log_module_level("nmem");
99 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);
117 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
122 EnterCriticalSection(&p->m_handle);
123 #elif YAZ_POSIX_THREADS
124 pthread_mutex_lock(&p->m_handle);
129 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
134 LeaveCriticalSection(&p->m_handle);
135 #elif YAZ_POSIX_THREADS
136 pthread_mutex_unlock(&p->m_handle);
141 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
147 DeleteCriticalSection(&(*p)->m_handle);
155 static nmem_block *freelist = NULL; /* "global" freelists */
156 static nmem_control *cfreelist = NULL;
157 static int nmem_active_no = 0;
158 static int nmem_init_flag = 0;
161 struct nmem_debug_info {
165 struct nmem_debug_info *next;
168 struct nmem_debug_info *nmem_debug_list = 0;
171 static void free_block(nmem_block *p)
173 memset(p->buf, 'Y', p->size);
177 yaz_log (log_level, "nmem free_block p=%p", p);
181 void nmem_print_list (void)
184 nmem_print_list_l(log_level);
187 void nmem_print_list_l (int level)
189 struct nmem_debug_info *p;
191 yaz_log (level, "nmem print list");
193 for (p = nmem_debug_list; p; p = p->next)
194 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
200 * acquire a block with a minimum of size free bytes.
202 static nmem_block *get_block(int size)
207 yaz_log (log_level, "nmem get_block size=%d", size);
209 for (r = freelist, l = 0; r; l = r, r = r->next)
215 yaz_log (log_level, "nmem get_block found free block p=%p", r);
223 int get = NMEM_CHUNK;
228 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
230 r = (nmem_block *)xmalloc(sizeof(*r));
231 r->buf = (char *)xmalloc(r->size = get);
237 void nmem_reset(NMEM n)
241 yaz_log (log_level, "nmem_reset p=%p", n);
248 n->blocks = n->blocks->next;
256 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
258 void *nmem_malloc(NMEM n, int size)
261 struct nmem_block *p;
266 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
267 file, line, n, size);
271 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
275 assert (nmem_init_flag);
279 if (!p || p->size - p->top < size)
287 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
293 int nmem_total(NMEM n)
299 NMEM nmem_create_f(const char *file, int line)
301 NMEM nmem_create(void)
306 struct nmem_debug_info *debug_p;
308 if (!log_level_initialized)
310 log_level=yaz_log_module_level("nmem");
311 log_level_initialized=1;
318 cfreelist = cfreelist->next;
320 r = (nmem_control *)xmalloc(sizeof(*r));
324 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
332 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
335 yaz_log (YLOG_FATAL, "multi used block in nmem");
338 debug_p = xmalloc (sizeof(*debug_p));
339 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
340 debug_p->file[sizeof(debug_p->file)-1] = '\0';
341 debug_p->line = line;
343 debug_p->next = nmem_debug_list;
344 nmem_debug_list = debug_p;
352 void nmem_destroy_f(const char *file, int line, NMEM n)
354 void nmem_destroy(NMEM n)
358 struct nmem_debug_info **debug_p;
365 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
366 nmem_active_no-1, n);
368 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
369 if ((*debug_p)->p == n)
371 struct nmem_debug_info *debug_save = *debug_p;
372 *debug_p = (*debug_p)->next;
381 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
394 void nmem_transfer (NMEM dst, NMEM src)
397 while ((t = src->blocks))
399 src->blocks = t->next;
400 t->next = dst->blocks;
403 dst->total += src->total;
407 void nmem_critical_enter (void)
412 void nmem_critical_leave (void)
417 void nmem_init (void)
419 if (!log_level_initialized)
421 log_level=yaz_log_module_level("nmem");
422 log_level_initialized=1;
425 if (++nmem_init_flag == 1)
428 InitializeCriticalSection(&critical_section);
429 #elif YAZ_GNU_THREADS
430 yaz_log (log_level, "pth_init"); /* ??? */
439 void nmem_exit (void)
441 if (--nmem_init_flag == 0)
446 struct nmem_block *fl = freelist;
447 freelist = freelist->next;
453 struct nmem_control *cfl = cfreelist;
454 cfreelist = cfreelist->next;
458 DeleteCriticalSection(&critical_section);
465 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
471 case DLL_PROCESS_ATTACH:
474 case DLL_PROCESS_DETACH:
486 void yaz_set_errno(int v)
491 void yaz_strerror(char *buf, int max)
497 if (!log_level_initialized)
499 log_level = yaz_log_module_level("nmem");
500 log_level_initialized = 1;
504 err = GetLastError();
508 FORMAT_MESSAGE_FROM_SYSTEM,
511 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
521 #if YAZ_POSIX_THREADS
523 strerror_r(errno, buf, max);
524 /* if buffer is unset - use strerror anyway (GLIBC bug) */
526 strcpy(buf, strerror(yaz_errno()));
528 strcpy(buf, strerror(yaz_errno()));
531 strcpy(buf, strerror(yaz_errno()));
535 if ((cp = strrchr(buf, '\n')))
537 if ((cp = strrchr(buf, '\r')))