2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.19 2005-06-25 15:46:04 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)
48 char *buf; /* memory allocated in this block */
49 size_t size; /* size of buf */
50 size_t top; /* top of buffer */
51 struct nmem_block *next;
58 struct nmem_control *next;
76 #define NMEM_ALIGN (offsetof(struct align, u))
78 static int log_level = 0;
79 static int log_level_initialized = 0;
82 static CRITICAL_SECTION critical_section;
83 #define NMEM_ENTER EnterCriticalSection(&critical_section)
84 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
86 CRITICAL_SECTION m_handle;
88 #elif YAZ_POSIX_THREADS
89 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
90 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
91 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
93 pthread_mutex_t m_handle;
96 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
97 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
98 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
100 pth_mutex_t m_handle;
110 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
114 *p = (NMEM_MUTEX) malloc (sizeof(**p));
116 InitializeCriticalSection(&(*p)->m_handle);
117 #elif YAZ_POSIX_THREADS
118 pthread_mutex_init (&(*p)->m_handle, 0);
119 #elif YAZ_GNU_THREADS
120 pth_mutex_init (&(*p)->m_handle);
123 if (!log_level_initialized)
125 log_level_initialized = 1;
126 log_level = yaz_log_module_level("nmem");
131 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
136 EnterCriticalSection(&p->m_handle);
137 #elif YAZ_POSIX_THREADS
138 pthread_mutex_lock(&p->m_handle);
143 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
148 LeaveCriticalSection(&p->m_handle);
149 #elif YAZ_POSIX_THREADS
150 pthread_mutex_unlock(&p->m_handle);
155 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
160 DeleteCriticalSection(&(*p)->m_handle);
167 static nmem_block *freelist = NULL; /* "global" freelists */
168 static nmem_control *cfreelist = NULL;
169 static int nmem_active_no = 0;
170 static int nmem_init_flag = 0;
173 struct nmem_debug_info {
177 struct nmem_debug_info *next;
180 struct nmem_debug_info *nmem_debug_list = 0;
183 static void free_block(nmem_block *p)
185 memset(p->buf, 'Y', p->size);
189 yaz_log (log_level, "nmem free_block p=%p", p);
193 void nmem_print_list (void)
196 nmem_print_list_l(log_level);
199 void nmem_print_list_l (int level)
201 struct nmem_debug_info *p;
203 yaz_log (level, "nmem print list");
205 for (p = nmem_debug_list; p; p = p->next)
206 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
212 * acquire a block with a minimum of size free bytes.
214 static nmem_block *get_block(size_t size)
219 yaz_log (log_level, "nmem get_block size=%ld", (long) size);
221 for (r = freelist, l = 0; r; l = r, r = r->next)
227 yaz_log (log_level, "nmem get_block found free block p=%p", r);
235 int get = NMEM_CHUNK;
240 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
242 r = (nmem_block *)xmalloc(sizeof(*r));
243 r->buf = (char *)xmalloc(r->size = get);
249 void nmem_reset(NMEM n)
253 yaz_log (log_level, "nmem_reset p=%p", n);
260 n->blocks = n->blocks->next;
268 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
270 void *nmem_malloc(NMEM n, int size)
273 struct nmem_block *p;
278 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
279 file, line, n, size);
283 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
287 assert (nmem_init_flag);
291 if (!p || p->size < size + p->top)
299 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
305 int nmem_total(NMEM n)
311 NMEM nmem_create_f(const char *file, int line)
313 NMEM nmem_create(void)
318 struct nmem_debug_info *debug_p;
320 if (!log_level_initialized)
322 log_level = yaz_log_module_level("nmem");
323 log_level_initialized = 1;
330 cfreelist = cfreelist->next;
332 r = (nmem_control *)xmalloc(sizeof(*r));
336 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
344 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
347 yaz_log (YLOG_FATAL, "multi used block in nmem");
350 debug_p = xmalloc (sizeof(*debug_p));
351 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
352 debug_p->file[sizeof(debug_p->file)-1] = '\0';
353 debug_p->line = line;
355 debug_p->next = nmem_debug_list;
356 nmem_debug_list = debug_p;
364 void nmem_destroy_f(const char *file, int line, NMEM n)
366 void nmem_destroy(NMEM n)
370 struct nmem_debug_info **debug_p;
377 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
378 nmem_active_no-1, n);
380 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
381 if ((*debug_p)->p == n)
383 struct nmem_debug_info *debug_save = *debug_p;
384 *debug_p = (*debug_p)->next;
393 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
406 void nmem_transfer (NMEM dst, NMEM src)
409 while ((t = src->blocks))
411 src->blocks = t->next;
412 t->next = dst->blocks;
415 dst->total += src->total;
419 void nmem_critical_enter (void)
424 void nmem_critical_leave (void)
429 void nmem_init (void)
432 if (++nmem_init_flag == 1)
435 InitializeCriticalSection(&critical_section);
436 #elif YAZ_GNU_THREADS
443 if (!log_level_initialized)
445 log_level = yaz_log_module_level("nmem");
446 log_level_initialized = 1;
450 void nmem_exit (void)
452 if (--nmem_init_flag == 0)
457 struct nmem_block *fl = freelist;
458 freelist = freelist->next;
464 struct nmem_control *cfl = cfreelist;
465 cfreelist = cfreelist->next;
469 DeleteCriticalSection(&critical_section);
476 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
482 case DLL_PROCESS_ATTACH:
485 case DLL_PROCESS_DETACH:
497 void yaz_set_errno(int v)
502 void yaz_strerror(char *buf, int max)
508 if (!log_level_initialized)
510 log_level = yaz_log_module_level("nmem");
511 log_level_initialized = 1;
515 err = GetLastError();
519 FORMAT_MESSAGE_FROM_SYSTEM,
522 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
532 #if YAZ_POSIX_THREADS
534 strerror_r(errno, buf, max);
535 /* if buffer is unset - use strerror anyway (GLIBC bug) */
537 strcpy(buf, strerror(yaz_errno()));
539 strcpy(buf, strerror(yaz_errno()));
542 strcpy(buf, strerror(yaz_errno()));
546 if ((cp = strrchr(buf, '\n')))
548 if ((cp = strrchr(buf, '\r')))
554 * indent-tabs-mode: nil
556 * vim: shiftwidth=4 tabstop=8 expandtab