2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.22 2006-04-21 10:28:06 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)
115 *p = (NMEM_MUTEX) malloc(sizeof(**p));
117 InitializeCriticalSection(&(*p)->m_handle);
118 #elif YAZ_POSIX_THREADS
119 pthread_mutex_init(&(*p)->m_handle, 0);
120 #elif YAZ_GNU_THREADS
121 pth_mutex_init(&(*p)->m_handle);
125 if (!log_level_initialized)
127 log_level_initialized = 1;
128 log_level = yaz_log_module_level("nmem");
133 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
138 EnterCriticalSection(&p->m_handle);
139 #elif YAZ_POSIX_THREADS
140 pthread_mutex_lock(&p->m_handle);
145 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
150 LeaveCriticalSection(&p->m_handle);
151 #elif YAZ_POSIX_THREADS
152 pthread_mutex_unlock(&p->m_handle);
157 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
162 DeleteCriticalSection(&(*p)->m_handle);
169 static nmem_block *freelist = NULL; /* "global" freelists */
170 static nmem_control *cfreelist = NULL;
171 static int nmem_active_no = 0;
172 static int nmem_init_flag = 0;
175 struct nmem_debug_info {
179 struct nmem_debug_info *next;
182 struct nmem_debug_info *nmem_debug_list = 0;
185 static void free_block(nmem_block *p)
187 memset(p->buf, 'Y', p->size);
191 yaz_log (log_level, "nmem free_block p=%p", p);
195 void nmem_print_list (void)
198 nmem_print_list_l(log_level);
201 void nmem_print_list_l (int level)
203 struct nmem_debug_info *p;
205 yaz_log (level, "nmem print list");
207 for (p = nmem_debug_list; p; p = p->next)
208 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
214 * acquire a block with a minimum of size free bytes.
216 static nmem_block *get_block(size_t size)
221 yaz_log (log_level, "nmem get_block size=%ld", (long) size);
223 for (r = freelist, l = 0; r; l = r, r = r->next)
229 yaz_log (log_level, "nmem get_block found free block p=%p", r);
237 size_t get = NMEM_CHUNK;
242 yaz_log (log_level, "nmem get_block alloc new block size=%ld",
245 r = (nmem_block *)xmalloc(sizeof(*r));
246 r->buf = (char *)xmalloc(r->size = get);
252 void nmem_reset(NMEM n)
256 yaz_log (log_level, "nmem_reset p=%p", n);
263 n->blocks = n->blocks->next;
271 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
273 void *nmem_malloc(NMEM n, int size)
276 struct nmem_block *p;
281 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
282 file, line, n, size);
286 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
290 assert (nmem_init_flag);
294 if (!p || p->size < size + p->top)
302 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
308 int nmem_total(NMEM n)
314 NMEM nmem_create_f(const char *file, int line)
316 NMEM nmem_create(void)
321 struct nmem_debug_info *debug_p;
323 if (!log_level_initialized)
325 log_level = yaz_log_module_level("nmem");
326 log_level_initialized = 1;
333 cfreelist = cfreelist->next;
335 r = (nmem_control *)xmalloc(sizeof(*r));
339 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
347 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
350 yaz_log (YLOG_FATAL, "multi used block in nmem");
353 debug_p = xmalloc (sizeof(*debug_p));
354 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
355 debug_p->file[sizeof(debug_p->file)-1] = '\0';
356 debug_p->line = line;
358 debug_p->next = nmem_debug_list;
359 nmem_debug_list = debug_p;
367 void nmem_destroy_f(const char *file, int line, NMEM n)
369 void nmem_destroy(NMEM n)
373 struct nmem_debug_info **debug_p;
380 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
381 nmem_active_no-1, n);
383 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
384 if ((*debug_p)->p == n)
386 struct nmem_debug_info *debug_save = *debug_p;
387 *debug_p = (*debug_p)->next;
396 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
409 void nmem_transfer (NMEM dst, NMEM src)
412 while ((t = src->blocks))
414 src->blocks = t->next;
415 t->next = dst->blocks;
418 dst->total += src->total;
422 void nmem_critical_enter (void)
427 void nmem_critical_leave (void)
432 void nmem_init (void)
435 if (++nmem_init_flag == 1)
438 InitializeCriticalSection(&critical_section);
439 #elif YAZ_GNU_THREADS
446 if (!log_level_initialized)
448 log_level = yaz_log_module_level("nmem");
449 log_level_initialized = 1;
453 void nmem_exit (void)
455 if (--nmem_init_flag == 0)
460 struct nmem_block *fl = freelist;
461 freelist = freelist->next;
467 struct nmem_control *cfl = cfreelist;
468 cfreelist = cfreelist->next;
472 DeleteCriticalSection(&critical_section);
479 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
485 case DLL_PROCESS_ATTACH:
488 case DLL_PROCESS_DETACH:
500 void yaz_set_errno(int v)
505 void yaz_strerror(char *buf, int max)
511 if (!log_level_initialized)
513 log_level = yaz_log_module_level("nmem");
514 log_level_initialized = 1;
518 err = GetLastError();
522 FORMAT_MESSAGE_FROM_SYSTEM,
525 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
535 #if YAZ_POSIX_THREADS
537 strerror_r(errno, buf, max);
538 /* if buffer is unset - use strerror anyway (GLIBC bug) */
540 strcpy(buf, strerror(yaz_errno()));
542 strcpy(buf, strerror(yaz_errno()));
545 strcpy(buf, strerror(yaz_errno()));
549 if ((cp = strrchr(buf, '\n')))
551 if ((cp = strrchr(buf, '\r')))
557 * indent-tabs-mode: nil
559 * vim: shiftwidth=4 tabstop=8 expandtab