2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.9 2005-01-04 01:25:20 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)
43 #define NMEM_ALIGN (sizeof(long)*2)
45 static int log_level=0;
46 static int log_level_initialized=0;
49 static CRITICAL_SECTION critical_section;
50 #define NMEM_ENTER EnterCriticalSection(&critical_section)
51 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
53 CRITICAL_SECTION m_handle;
55 #elif YAZ_POSIX_THREADS
56 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
57 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
58 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
60 pthread_mutex_t m_handle;
63 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
64 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
65 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
77 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
79 if (!log_level_initialized)
81 log_level=yaz_log_module_level("nmem");
82 log_level_initialized=1;
88 *p = (NMEM_MUTEX) malloc (sizeof(**p));
90 InitializeCriticalSection(&(*p)->m_handle);
91 #elif YAZ_POSIX_THREADS
92 pthread_mutex_init (&(*p)->m_handle, 0);
94 pth_mutex_init (&(*p)->m_handle);
100 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
105 EnterCriticalSection(&p->m_handle);
106 #elif YAZ_POSIX_THREADS
107 pthread_mutex_lock(&p->m_handle);
112 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
117 LeaveCriticalSection(&p->m_handle);
118 #elif YAZ_POSIX_THREADS
119 pthread_mutex_unlock(&p->m_handle);
124 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
130 DeleteCriticalSection(&(*p)->m_handle);
138 static nmem_block *freelist = NULL; /* "global" freelists */
139 static nmem_control *cfreelist = NULL;
140 static int nmem_active_no = 0;
141 static int nmem_init_flag = 0;
144 struct nmem_debug_info {
148 struct nmem_debug_info *next;
151 struct nmem_debug_info *nmem_debug_list = 0;
154 static void free_block(nmem_block *p)
156 memset(p->buf, 'Y', p->size);
160 yaz_log (log_level, "nmem free_block p=%p", p);
164 void nmem_print_list (void)
167 nmem_print_list_l(log_level);
170 void nmem_print_list_l (int level)
172 struct nmem_debug_info *p;
174 yaz_log (level, "nmem print list");
176 for (p = nmem_debug_list; p; p = p->next)
177 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
183 * acquire a block with a minimum of size free bytes.
185 static nmem_block *get_block(int size)
190 yaz_log (log_level, "nmem get_block size=%d", size);
192 for (r = freelist, l = 0; r; l = r, r = r->next)
198 yaz_log (log_level, "nmem get_block found free block p=%p", r);
206 int get = NMEM_CHUNK;
211 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
213 r = (nmem_block *)xmalloc(sizeof(*r));
214 r->buf = (char *)xmalloc(r->size = get);
220 void nmem_reset(NMEM n)
224 yaz_log (log_level, "nmem_reset p=%p", n);
231 n->blocks = n->blocks->next;
239 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
241 void *nmem_malloc(NMEM n, int size)
244 struct nmem_block *p;
249 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
250 file, line, n, size);
254 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
258 assert (nmem_init_flag);
262 if (!p || p->size - p->top < size)
270 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
276 int nmem_total(NMEM n)
282 NMEM nmem_create_f(const char *file, int line)
284 NMEM nmem_create(void)
289 struct nmem_debug_info *debug_p;
291 if (!log_level_initialized)
293 log_level=yaz_log_module_level("nmem");
294 log_level_initialized=1;
301 cfreelist = cfreelist->next;
303 r = (nmem_control *)xmalloc(sizeof(*r));
307 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
315 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
318 yaz_log (YLOG_FATAL, "multi used block in nmem");
321 debug_p = xmalloc (sizeof(*debug_p));
322 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
323 debug_p->file[sizeof(debug_p->file)-1] = '\0';
324 debug_p->line = line;
326 debug_p->next = nmem_debug_list;
327 nmem_debug_list = debug_p;
335 void nmem_destroy_f(const char *file, int line, NMEM n)
337 void nmem_destroy(NMEM n)
341 struct nmem_debug_info **debug_p;
348 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
349 nmem_active_no-1, n);
351 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
352 if ((*debug_p)->p == n)
354 struct nmem_debug_info *debug_save = *debug_p;
355 *debug_p = (*debug_p)->next;
364 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
377 void nmem_transfer (NMEM dst, NMEM src)
380 while ((t = src->blocks))
382 src->blocks = t->next;
383 t->next = dst->blocks;
386 dst->total += src->total;
390 void nmem_critical_enter (void)
395 void nmem_critical_leave (void)
400 void nmem_init (void)
402 if (!log_level_initialized)
404 log_level=yaz_log_module_level("nmem");
405 log_level_initialized=1;
408 if (++nmem_init_flag == 1)
411 InitializeCriticalSection(&critical_section);
412 #elif YAZ_GNU_THREADS
413 yaz_log (log_level, "pth_init"); /* ??? */
422 void nmem_exit (void)
424 if (--nmem_init_flag == 0)
429 struct nmem_block *fl = freelist;
430 freelist = freelist->next;
436 struct nmem_control *cfl = cfreelist;
437 cfreelist = cfreelist->next;
441 DeleteCriticalSection(&critical_section);
448 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
454 case DLL_PROCESS_ATTACH:
457 case DLL_PROCESS_DETACH:
469 void yaz_set_errno(int v)
474 void yaz_strerror(char *buf, int max)
480 if (!log_level_initialized)
482 log_level = yaz_log_module_level("nmem");
483 log_level_initialized = 1;
487 err = GetLastError();
491 FORMAT_MESSAGE_FROM_SYSTEM,
494 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
504 #if YAZ_POSIX_THREADS
506 strerror_r(errno, buf, max);
507 /* if buffer is unset - use strerror anyway (GLIBC bug) */
509 strcpy(buf, strerror(yaz_errno()));
511 strcpy(buf, strerror(yaz_errno()));
514 strcpy(buf, strerror(yaz_errno()));
518 if ((cp = strrchr(buf, '\n')))
520 if ((cp = strrchr(buf, '\r')))