2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.28 2007-01-03 08:42:15 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;
57 struct nmem_block *blocks;
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 size_t nmem_memory_in_use = 0;
111 size_t nmem_memory_free = 0;
113 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
118 *p = (NMEM_MUTEX) malloc(sizeof(**p));
120 InitializeCriticalSection(&(*p)->m_handle);
121 #elif YAZ_POSIX_THREADS
122 pthread_mutex_init(&(*p)->m_handle, 0);
123 #elif YAZ_GNU_THREADS
124 pth_mutex_init(&(*p)->m_handle);
128 if (!log_level_initialized)
130 log_level_initialized = 1;
131 log_level = yaz_log_module_level("nmem");
136 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
141 EnterCriticalSection(&p->m_handle);
142 #elif YAZ_POSIX_THREADS
143 pthread_mutex_lock(&p->m_handle);
148 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
153 LeaveCriticalSection(&p->m_handle);
154 #elif YAZ_POSIX_THREADS
155 pthread_mutex_unlock(&p->m_handle);
160 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
165 DeleteCriticalSection(&(*p)->m_handle);
172 /** \brief free NMEM memory blocks . Reused in get_block */
173 static struct nmem_block *freelist = NULL;
175 /** \brief free NMEM control blocks. Reused in nmem_create */
176 static struct nmem_control *cfreelist = NULL;
178 /** \brief number NMEM's in use (number of nmem_controls not in free list) */
179 static int nmem_active_no = 0;
181 /** \brief NMEM usage counter */
182 static int nmem_init_flag = 0;
184 /** \brief whether nmem blocks should be reassigned to heap */
185 static int nmem_release_in_heap = 0;
188 struct nmem_debug_info {
192 struct nmem_debug_info *next;
195 struct nmem_debug_info *nmem_debug_list = 0;
198 static void free_block(struct nmem_block *p)
200 nmem_memory_in_use -= p->size;
201 if (nmem_release_in_heap)
208 memset(p->buf, 'Y', p->size);
211 nmem_memory_free += p->size;
214 yaz_log (log_level, "nmem free_block p=%p", p);
218 void nmem_print_list (void)
221 nmem_print_list_l(log_level);
224 void nmem_print_list_l (int level)
226 struct nmem_debug_info *p;
228 yaz_log (level, "nmem print list");
230 for (p = nmem_debug_list; p; p = p->next)
231 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
237 * acquire a block with a minimum of size free bytes.
239 static struct nmem_block *get_block(size_t size)
241 struct nmem_block *r, *l;
244 yaz_log (log_level, "nmem get_block size=%ld", (long) size);
246 for (r = freelist, l = 0; r; l = r, r = r->next)
252 yaz_log (log_level, "nmem get_block found free block p=%p", r);
257 nmem_memory_free -= r->size;
261 size_t get = NMEM_CHUNK;
266 yaz_log (log_level, "nmem get_block alloc new block size=%ld",
269 r = (struct nmem_block *) xmalloc(sizeof(*r));
270 r->buf = (char *)xmalloc(r->size = get);
272 nmem_memory_in_use += r->size;
277 void nmem_reset(NMEM n)
279 struct nmem_block *t;
281 yaz_log (log_level, "nmem_reset p=%p", n);
288 n->blocks = n->blocks->next;
296 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
298 void *nmem_malloc(NMEM n, int size)
301 struct nmem_block *p;
306 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
307 file, line, n, size);
311 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
315 assert (nmem_init_flag);
319 if (!p || p->size < size + p->top)
327 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
333 int nmem_total(NMEM n)
339 NMEM nmem_create_f(const char *file, int line)
341 NMEM nmem_create(void)
346 struct nmem_debug_info *debug_p;
348 if (!log_level_initialized)
350 log_level = yaz_log_module_level("nmem");
351 log_level_initialized = 1;
358 cfreelist = cfreelist->next;
360 r = (struct nmem_control *)xmalloc(sizeof(*r));
364 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
372 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
375 yaz_log (YLOG_FATAL, "multi used block in nmem");
378 debug_p = xmalloc (sizeof(*debug_p));
379 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
380 debug_p->file[sizeof(debug_p->file)-1] = '\0';
381 debug_p->line = line;
383 debug_p->next = nmem_debug_list;
384 nmem_debug_list = debug_p;
392 void nmem_destroy_f(const char *file, int line, NMEM n)
394 void nmem_destroy(NMEM n)
398 struct nmem_debug_info **debug_p;
405 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
406 nmem_active_no-1, n);
408 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
409 if ((*debug_p)->p == n)
411 struct nmem_debug_info *debug_save = *debug_p;
412 *debug_p = (*debug_p)->next;
421 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
429 if (nmem_release_in_heap)
441 void nmem_transfer (NMEM dst, NMEM src)
443 struct nmem_block *t;
444 while ((t = src->blocks))
446 src->blocks = t->next;
447 t->next = dst->blocks;
450 dst->total += src->total;
454 void nmem_get_memory_in_use(size_t *p)
457 *p = nmem_memory_in_use;
461 void nmem_get_memory_free(size_t *p)
464 *p = nmem_memory_free;
468 void nmem_critical_enter (void)
473 void nmem_critical_leave (void)
478 void nmem_init (void)
480 if (++nmem_init_flag == 1)
483 InitializeCriticalSection(&critical_section);
484 #elif YAZ_GNU_THREADS
491 if (!log_level_initialized)
493 log_level = yaz_log_module_level("nmem");
494 log_level_initialized = 1;
498 void nmem_exit (void)
500 if (--nmem_init_flag == 0)
505 struct nmem_block *fl = freelist;
506 nmem_memory_free -= fl->size;
507 freelist = freelist->next;
513 struct nmem_control *cfl = cfreelist;
514 cfreelist = cfreelist->next;
518 DeleteCriticalSection(&critical_section);
525 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
531 case DLL_PROCESS_ATTACH:
534 case DLL_PROCESS_DETACH:
546 void yaz_set_errno(int v)
551 void yaz_strerror(char *buf, int max)
557 if (!log_level_initialized)
559 log_level = yaz_log_module_level("nmem");
560 log_level_initialized = 1;
564 err = GetLastError();
568 FORMAT_MESSAGE_FROM_SYSTEM,
571 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
581 #if YAZ_POSIX_THREADS
583 strerror_r(errno, buf, max);
584 /* if buffer is unset - use strerror anyway (GLIBC bug) */
586 strcpy(buf, strerror(yaz_errno()));
588 strcpy(buf, strerror(yaz_errno()));
591 strcpy(buf, strerror(yaz_errno()));
595 if ((cp = strrchr(buf, '\n')))
597 if ((cp = strrchr(buf, '\r')))
603 * indent-tabs-mode: nil
605 * vim: shiftwidth=4 tabstop=8 expandtab