2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.29 2007-04-12 13:52:57 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>
43 #define NMEM_CHUNK (4*1024)
47 char *buf; /* memory allocated in this block */
48 size_t size; /* size of buf */
49 size_t top; /* top of buffer */
50 struct nmem_block *next;
56 struct nmem_block *blocks;
57 struct nmem_control *next;
75 #define NMEM_ALIGN (offsetof(struct align, u))
77 static int log_level = 0;
78 static int log_level_initialized = 0;
81 static CRITICAL_SECTION critical_section;
82 #define NMEM_ENTER EnterCriticalSection(&critical_section)
83 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
85 CRITICAL_SECTION m_handle;
87 #elif YAZ_POSIX_THREADS
88 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
89 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
90 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
92 pthread_mutex_t m_handle;
95 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
96 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
97 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
109 size_t nmem_memory_in_use = 0;
110 size_t nmem_memory_free = 0;
112 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
117 *p = (NMEM_MUTEX) malloc(sizeof(**p));
119 InitializeCriticalSection(&(*p)->m_handle);
120 #elif YAZ_POSIX_THREADS
121 pthread_mutex_init(&(*p)->m_handle, 0);
122 #elif YAZ_GNU_THREADS
123 pth_mutex_init(&(*p)->m_handle);
127 if (!log_level_initialized)
129 log_level_initialized = 1;
130 log_level = yaz_log_module_level("nmem");
135 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
140 EnterCriticalSection(&p->m_handle);
141 #elif YAZ_POSIX_THREADS
142 pthread_mutex_lock(&p->m_handle);
147 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
152 LeaveCriticalSection(&p->m_handle);
153 #elif YAZ_POSIX_THREADS
154 pthread_mutex_unlock(&p->m_handle);
159 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
164 DeleteCriticalSection(&(*p)->m_handle);
171 /** \brief free NMEM memory blocks . Reused in get_block */
172 static struct nmem_block *freelist = NULL;
174 /** \brief free NMEM control blocks. Reused in nmem_create */
175 static struct nmem_control *cfreelist = NULL;
177 /** \brief number NMEM's in use (number of nmem_controls not in free list) */
178 static int nmem_active_no = 0;
180 /** \brief NMEM usage counter */
181 static int nmem_init_flag = 0;
183 /** \brief whether nmem blocks should be reassigned to heap */
184 static int nmem_release_in_heap = 0;
187 struct nmem_debug_info {
191 struct nmem_debug_info *next;
194 struct nmem_debug_info *nmem_debug_list = 0;
197 static void free_block(struct nmem_block *p)
199 nmem_memory_in_use -= p->size;
200 if (nmem_release_in_heap)
207 memset(p->buf, 'Y', p->size);
210 nmem_memory_free += p->size;
213 yaz_log (log_level, "nmem free_block p=%p", p);
217 void nmem_print_list (void)
220 nmem_print_list_l(log_level);
223 void nmem_print_list_l (int level)
225 struct nmem_debug_info *p;
227 yaz_log (level, "nmem print list");
229 for (p = nmem_debug_list; p; p = p->next)
230 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
236 * acquire a block with a minimum of size free bytes.
238 static struct nmem_block *get_block(size_t size)
240 struct nmem_block *r, *l;
243 yaz_log (log_level, "nmem get_block size=%ld", (long) size);
245 for (r = freelist, l = 0; r; l = r, r = r->next)
251 yaz_log (log_level, "nmem get_block found free block p=%p", r);
256 nmem_memory_free -= r->size;
260 size_t get = NMEM_CHUNK;
265 yaz_log (log_level, "nmem get_block alloc new block size=%ld",
268 r = (struct nmem_block *) xmalloc(sizeof(*r));
269 r->buf = (char *)xmalloc(r->size = get);
271 nmem_memory_in_use += r->size;
276 void nmem_reset(NMEM n)
278 struct nmem_block *t;
280 yaz_log (log_level, "nmem_reset p=%p", n);
287 n->blocks = n->blocks->next;
295 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
297 void *nmem_malloc(NMEM n, int size)
300 struct nmem_block *p;
305 yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
306 file, line, n, size);
310 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
314 assert (nmem_init_flag);
318 if (!p || p->size < size + p->top)
326 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
332 int nmem_total(NMEM n)
338 NMEM nmem_create_f(const char *file, int line)
340 NMEM nmem_create(void)
345 struct nmem_debug_info *debug_p;
347 if (!log_level_initialized)
349 log_level = yaz_log_module_level("nmem");
350 log_level_initialized = 1;
357 cfreelist = cfreelist->next;
359 r = (struct nmem_control *)xmalloc(sizeof(*r));
363 yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
371 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
374 yaz_log (YLOG_FATAL, "multi used block in nmem");
377 debug_p = xmalloc (sizeof(*debug_p));
378 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
379 debug_p->file[sizeof(debug_p->file)-1] = '\0';
380 debug_p->line = line;
382 debug_p->next = nmem_debug_list;
383 nmem_debug_list = debug_p;
391 void nmem_destroy_f(const char *file, int line, NMEM n)
393 void nmem_destroy(NMEM n)
397 struct nmem_debug_info **debug_p;
404 yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
405 nmem_active_no-1, n);
407 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
408 if ((*debug_p)->p == n)
410 struct nmem_debug_info *debug_save = *debug_p;
411 *debug_p = (*debug_p)->next;
420 yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
428 if (nmem_release_in_heap)
440 void nmem_transfer (NMEM dst, NMEM src)
442 struct nmem_block *t;
443 while ((t = src->blocks))
445 src->blocks = t->next;
446 t->next = dst->blocks;
449 dst->total += src->total;
453 void nmem_get_memory_in_use(size_t *p)
456 *p = nmem_memory_in_use;
460 void nmem_get_memory_free(size_t *p)
463 *p = nmem_memory_free;
467 void nmem_critical_enter (void)
472 void nmem_critical_leave (void)
477 void nmem_init (void)
479 if (++nmem_init_flag == 1)
482 InitializeCriticalSection(&critical_section);
483 #elif YAZ_GNU_THREADS
490 if (!log_level_initialized)
492 log_level = yaz_log_module_level("nmem");
493 log_level_initialized = 1;
497 void nmem_exit (void)
499 if (--nmem_init_flag == 0)
503 struct nmem_block *fl = freelist;
504 nmem_memory_free -= fl->size;
505 freelist = freelist->next;
511 struct nmem_control *cfl = cfreelist;
512 cfreelist = cfreelist->next;
516 DeleteCriticalSection(&critical_section);
523 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
529 case DLL_PROCESS_ATTACH:
532 case DLL_PROCESS_DETACH:
544 void yaz_set_errno(int v)
549 void yaz_strerror(char *buf, int max)
555 if (!log_level_initialized)
557 log_level = yaz_log_module_level("nmem");
558 log_level_initialized = 1;
562 err = GetLastError();
566 FORMAT_MESSAGE_FROM_SYSTEM,
569 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
579 #if YAZ_POSIX_THREADS
581 strerror_r(errno, buf, max);
582 /* if buffer is unset - use strerror anyway (GLIBC bug) */
584 strcpy(buf, strerror(yaz_errno()));
586 strcpy(buf, strerror(yaz_errno()));
589 strcpy(buf, strerror(yaz_errno()));
593 if ((cp = strrchr(buf, '\n')))
595 if ((cp = strrchr(buf, '\r')))
601 * indent-tabs-mode: nil
603 * vim: shiftwidth=4 tabstop=8 expandtab