2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.20 2005-09-09 10:32:09 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 int get = NMEM_CHUNK;
242 yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
244 r = (nmem_block *)xmalloc(sizeof(*r));
245 r->buf = (char *)xmalloc(r->size = get);
251 void nmem_reset(NMEM n)
255 yaz_log (log_level, "nmem_reset p=%p", n);
262 n->blocks = n->blocks->next;
270 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
272 void *nmem_malloc(NMEM n, int size)
275 struct nmem_block *p;
280 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
281 file, line, n, size);
285 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
289 assert (nmem_init_flag);
293 if (!p || p->size < size + p->top)
301 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
307 int nmem_total(NMEM n)
313 NMEM nmem_create_f(const char *file, int line)
315 NMEM nmem_create(void)
320 struct nmem_debug_info *debug_p;
322 if (!log_level_initialized)
324 log_level = yaz_log_module_level("nmem");
325 log_level_initialized = 1;
332 cfreelist = cfreelist->next;
334 r = (nmem_control *)xmalloc(sizeof(*r));
338 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
346 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
349 yaz_log (YLOG_FATAL, "multi used block in nmem");
352 debug_p = xmalloc (sizeof(*debug_p));
353 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
354 debug_p->file[sizeof(debug_p->file)-1] = '\0';
355 debug_p->line = line;
357 debug_p->next = nmem_debug_list;
358 nmem_debug_list = debug_p;
366 void nmem_destroy_f(const char *file, int line, NMEM n)
368 void nmem_destroy(NMEM n)
372 struct nmem_debug_info **debug_p;
379 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
380 nmem_active_no-1, n);
382 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
383 if ((*debug_p)->p == n)
385 struct nmem_debug_info *debug_save = *debug_p;
386 *debug_p = (*debug_p)->next;
395 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
408 void nmem_transfer (NMEM dst, NMEM src)
411 while ((t = src->blocks))
413 src->blocks = t->next;
414 t->next = dst->blocks;
417 dst->total += src->total;
421 void nmem_critical_enter (void)
426 void nmem_critical_leave (void)
431 void nmem_init (void)
434 if (++nmem_init_flag == 1)
437 InitializeCriticalSection(&critical_section);
438 #elif YAZ_GNU_THREADS
445 if (!log_level_initialized)
447 log_level = yaz_log_module_level("nmem");
448 log_level_initialized = 1;
452 void nmem_exit (void)
454 if (--nmem_init_flag == 0)
459 struct nmem_block *fl = freelist;
460 freelist = freelist->next;
466 struct nmem_control *cfl = cfreelist;
467 cfreelist = cfreelist->next;
471 DeleteCriticalSection(&critical_section);
478 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
484 case DLL_PROCESS_ATTACH:
487 case DLL_PROCESS_DETACH:
499 void yaz_set_errno(int v)
504 void yaz_strerror(char *buf, int max)
510 if (!log_level_initialized)
512 log_level = yaz_log_module_level("nmem");
513 log_level_initialized = 1;
517 err = GetLastError();
521 FORMAT_MESSAGE_FROM_SYSTEM,
524 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
534 #if YAZ_POSIX_THREADS
536 strerror_r(errno, buf, max);
537 /* if buffer is unset - use strerror anyway (GLIBC bug) */
539 strcpy(buf, strerror(yaz_errno()));
541 strcpy(buf, strerror(yaz_errno()));
544 strcpy(buf, strerror(yaz_errno()));
548 if ((cp = strrchr(buf, '\n')))
550 if ((cp = strrchr(buf, '\r')))
556 * indent-tabs-mode: nil
558 * vim: shiftwidth=4 tabstop=8 expandtab