2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.8 2004-12-16 08:59:36 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!
25 #include <yaz/xmalloc.h>
42 #define NMEM_CHUNK (4*1024)
44 static int log_level=0;
45 static int log_level_initialized=0;
48 static CRITICAL_SECTION critical_section;
49 #define NMEM_ENTER EnterCriticalSection(&critical_section)
50 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
52 CRITICAL_SECTION m_handle;
54 #elif YAZ_POSIX_THREADS
55 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
56 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
57 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
59 pthread_mutex_t m_handle;
62 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
63 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
64 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
76 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
78 if (!log_level_initialized)
80 log_level=yaz_log_module_level("nmem");
81 log_level_initialized=1;
87 *p = (NMEM_MUTEX) malloc (sizeof(**p));
89 InitializeCriticalSection(&(*p)->m_handle);
90 #elif YAZ_POSIX_THREADS
91 pthread_mutex_init (&(*p)->m_handle, 0);
93 pth_mutex_init (&(*p)->m_handle);
99 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
104 EnterCriticalSection(&p->m_handle);
105 #elif YAZ_POSIX_THREADS
106 pthread_mutex_lock(&p->m_handle);
111 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
116 LeaveCriticalSection(&p->m_handle);
117 #elif YAZ_POSIX_THREADS
118 pthread_mutex_unlock(&p->m_handle);
123 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
129 DeleteCriticalSection(&(*p)->m_handle);
137 static nmem_block *freelist = NULL; /* "global" freelists */
138 static nmem_control *cfreelist = NULL;
139 static int nmem_active_no = 0;
140 static int nmem_init_flag = 0;
143 struct nmem_debug_info {
147 struct nmem_debug_info *next;
150 struct nmem_debug_info *nmem_debug_list = 0;
153 static void free_block(nmem_block *p)
155 memset(p->buf, 'Y', p->size);
159 yaz_log (log_level, "nmem free_block p=%p", p);
163 void nmem_print_list (void)
166 nmem_print_list_l(log_level);
169 void nmem_print_list_l (int level)
171 struct nmem_debug_info *p;
173 yaz_log (level, "nmem print list");
175 for (p = nmem_debug_list; p; p = p->next)
176 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
182 * acquire a block with a minimum of size free bytes.
184 static nmem_block *get_block(int size)
189 yaz_log (log_level, "nmem get_block size=%d", size);
191 for (r = freelist, l = 0; r; l = r, r = r->next)
197 yaz_log (log_level, "nmem get_block found free block p=%p", r);
205 int get = NMEM_CHUNK;
210 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
212 r = (nmem_block *)xmalloc(sizeof(*r));
213 r->buf = (char *)xmalloc(r->size = get);
219 void nmem_reset(NMEM n)
223 yaz_log (log_level, "nmem_reset p=%p", n);
230 n->blocks = n->blocks->next;
238 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
240 void *nmem_malloc(NMEM n, int size)
243 struct nmem_block *p;
248 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
249 file, line, n, size);
253 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
257 assert (nmem_init_flag);
261 if (!p || p->size - p->top < size)
269 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
275 int nmem_total(NMEM n)
281 NMEM nmem_create_f(const char *file, int line)
283 NMEM nmem_create(void)
288 struct nmem_debug_info *debug_p;
290 if (!log_level_initialized)
292 log_level=yaz_log_module_level("nmem");
293 log_level_initialized=1;
300 cfreelist = cfreelist->next;
302 r = (nmem_control *)xmalloc(sizeof(*r));
306 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
314 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
317 yaz_log (YLOG_FATAL, "multi used block in nmem");
320 debug_p = xmalloc (sizeof(*debug_p));
321 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
322 debug_p->file[sizeof(debug_p->file)-1] = '\0';
323 debug_p->line = line;
325 debug_p->next = nmem_debug_list;
326 nmem_debug_list = debug_p;
334 void nmem_destroy_f(const char *file, int line, NMEM n)
336 void nmem_destroy(NMEM n)
340 struct nmem_debug_info **debug_p;
347 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
348 nmem_active_no-1, n);
350 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
351 if ((*debug_p)->p == n)
353 struct nmem_debug_info *debug_save = *debug_p;
354 *debug_p = (*debug_p)->next;
363 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
376 void nmem_transfer (NMEM dst, NMEM src)
379 while ((t = src->blocks))
381 src->blocks = t->next;
382 t->next = dst->blocks;
385 dst->total += src->total;
389 void nmem_critical_enter (void)
394 void nmem_critical_leave (void)
399 void nmem_init (void)
401 if (!log_level_initialized)
403 log_level=yaz_log_module_level("nmem");
404 log_level_initialized=1;
407 if (++nmem_init_flag == 1)
410 InitializeCriticalSection(&critical_section);
411 #elif YAZ_GNU_THREADS
412 yaz_log (log_level, "pth_init"); /* ??? */
421 void nmem_exit (void)
423 if (--nmem_init_flag == 0)
428 struct nmem_block *fl = freelist;
429 freelist = freelist->next;
435 struct nmem_control *cfl = cfreelist;
436 cfreelist = cfreelist->next;
440 DeleteCriticalSection(&critical_section);
447 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
453 case DLL_PROCESS_ATTACH:
456 case DLL_PROCESS_DETACH:
468 void yaz_set_errno(int v)
473 void yaz_strerror(char *buf, int max)
479 if (!log_level_initialized)
481 log_level = yaz_log_module_level("nmem");
482 log_level_initialized = 1;
486 err = GetLastError();
490 FORMAT_MESSAGE_FROM_SYSTEM,
493 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
503 #if YAZ_POSIX_THREADS
505 strerror_r(errno, buf, max);
506 /* if buffer is unset - use strerror anyway (GLIBC bug) */
508 strcpy(buf, strerror(yaz_errno()));
510 strcpy(buf, strerror(yaz_errno()));
513 strcpy(buf, strerror(yaz_errno()));
517 if ((cp = strrchr(buf, '\n')))
519 if ((cp = strrchr(buf, '\r')))