2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.10 2005-01-04 21:27:54 mike 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)
58 #define NMEM_ALIGN (offsetof(struct align, u))
60 static int log_level=0;
61 static int log_level_initialized=0;
64 static CRITICAL_SECTION critical_section;
65 #define NMEM_ENTER EnterCriticalSection(&critical_section)
66 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
68 CRITICAL_SECTION m_handle;
70 #elif YAZ_POSIX_THREADS
71 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
72 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
73 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
75 pthread_mutex_t m_handle;
78 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
79 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
80 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
92 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
94 if (!log_level_initialized)
96 log_level=yaz_log_module_level("nmem");
97 log_level_initialized=1;
103 *p = (NMEM_MUTEX) malloc (sizeof(**p));
105 InitializeCriticalSection(&(*p)->m_handle);
106 #elif YAZ_POSIX_THREADS
107 pthread_mutex_init (&(*p)->m_handle, 0);
108 #elif YAZ_GNU_THREADS
109 pth_mutex_init (&(*p)->m_handle);
115 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
120 EnterCriticalSection(&p->m_handle);
121 #elif YAZ_POSIX_THREADS
122 pthread_mutex_lock(&p->m_handle);
127 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
132 LeaveCriticalSection(&p->m_handle);
133 #elif YAZ_POSIX_THREADS
134 pthread_mutex_unlock(&p->m_handle);
139 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
145 DeleteCriticalSection(&(*p)->m_handle);
153 static nmem_block *freelist = NULL; /* "global" freelists */
154 static nmem_control *cfreelist = NULL;
155 static int nmem_active_no = 0;
156 static int nmem_init_flag = 0;
159 struct nmem_debug_info {
163 struct nmem_debug_info *next;
166 struct nmem_debug_info *nmem_debug_list = 0;
169 static void free_block(nmem_block *p)
171 memset(p->buf, 'Y', p->size);
175 yaz_log (log_level, "nmem free_block p=%p", p);
179 void nmem_print_list (void)
182 nmem_print_list_l(log_level);
185 void nmem_print_list_l (int level)
187 struct nmem_debug_info *p;
189 yaz_log (level, "nmem print list");
191 for (p = nmem_debug_list; p; p = p->next)
192 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
198 * acquire a block with a minimum of size free bytes.
200 static nmem_block *get_block(int size)
205 yaz_log (log_level, "nmem get_block size=%d", size);
207 for (r = freelist, l = 0; r; l = r, r = r->next)
213 yaz_log (log_level, "nmem get_block found free block p=%p", r);
221 int get = NMEM_CHUNK;
226 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
228 r = (nmem_block *)xmalloc(sizeof(*r));
229 r->buf = (char *)xmalloc(r->size = get);
235 void nmem_reset(NMEM n)
239 yaz_log (log_level, "nmem_reset p=%p", n);
246 n->blocks = n->blocks->next;
254 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
256 void *nmem_malloc(NMEM n, int size)
259 struct nmem_block *p;
264 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
265 file, line, n, size);
269 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
273 assert (nmem_init_flag);
277 if (!p || p->size - p->top < size)
285 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
291 int nmem_total(NMEM n)
297 NMEM nmem_create_f(const char *file, int line)
299 NMEM nmem_create(void)
304 struct nmem_debug_info *debug_p;
306 if (!log_level_initialized)
308 log_level=yaz_log_module_level("nmem");
309 log_level_initialized=1;
316 cfreelist = cfreelist->next;
318 r = (nmem_control *)xmalloc(sizeof(*r));
322 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
330 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
333 yaz_log (YLOG_FATAL, "multi used block in nmem");
336 debug_p = xmalloc (sizeof(*debug_p));
337 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
338 debug_p->file[sizeof(debug_p->file)-1] = '\0';
339 debug_p->line = line;
341 debug_p->next = nmem_debug_list;
342 nmem_debug_list = debug_p;
350 void nmem_destroy_f(const char *file, int line, NMEM n)
352 void nmem_destroy(NMEM n)
356 struct nmem_debug_info **debug_p;
363 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
364 nmem_active_no-1, n);
366 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
367 if ((*debug_p)->p == n)
369 struct nmem_debug_info *debug_save = *debug_p;
370 *debug_p = (*debug_p)->next;
379 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
392 void nmem_transfer (NMEM dst, NMEM src)
395 while ((t = src->blocks))
397 src->blocks = t->next;
398 t->next = dst->blocks;
401 dst->total += src->total;
405 void nmem_critical_enter (void)
410 void nmem_critical_leave (void)
415 void nmem_init (void)
417 if (!log_level_initialized)
419 log_level=yaz_log_module_level("nmem");
420 log_level_initialized=1;
423 if (++nmem_init_flag == 1)
426 InitializeCriticalSection(&critical_section);
427 #elif YAZ_GNU_THREADS
428 yaz_log (log_level, "pth_init"); /* ??? */
437 void nmem_exit (void)
439 if (--nmem_init_flag == 0)
444 struct nmem_block *fl = freelist;
445 freelist = freelist->next;
451 struct nmem_control *cfl = cfreelist;
452 cfreelist = cfreelist->next;
456 DeleteCriticalSection(&critical_section);
463 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
469 case DLL_PROCESS_ATTACH:
472 case DLL_PROCESS_DETACH:
484 void yaz_set_errno(int v)
489 void yaz_strerror(char *buf, int max)
495 if (!log_level_initialized)
497 log_level = yaz_log_module_level("nmem");
498 log_level_initialized = 1;
502 err = GetLastError();
506 FORMAT_MESSAGE_FROM_SYSTEM,
509 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
519 #if YAZ_POSIX_THREADS
521 strerror_r(errno, buf, max);
522 /* if buffer is unset - use strerror anyway (GLIBC bug) */
524 strcpy(buf, strerror(yaz_errno()));
526 strcpy(buf, strerror(yaz_errno()));
529 strcpy(buf, strerror(yaz_errno()));
533 if ((cp = strrchr(buf, '\n')))
535 if ((cp = strrchr(buf, '\r')))