2 * Copyright (c) 1995-2003, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: nmem.c,v 1.3 2004-01-15 10:16:27 adam Exp $
10 * This is a simple and fairly wasteful little module for nibble memory
11 * allocation. Evemtually we'll put in something better.
20 #include <yaz/xmalloc.h>
37 #define NMEM_CHUNK (4*1024)
40 static CRITICAL_SECTION critical_section;
41 #define NMEM_ENTER EnterCriticalSection(&critical_section)
42 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
44 CRITICAL_SECTION m_handle;
46 #elif YAZ_POSIX_THREADS
47 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
48 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
49 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
51 pthread_mutex_t m_handle;
54 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
55 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
56 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
68 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
73 *p = (NMEM_MUTEX) malloc (sizeof(**p));
75 InitializeCriticalSection(&(*p)->m_handle);
76 #elif YAZ_POSIX_THREADS
77 pthread_mutex_init (&(*p)->m_handle, 0);
79 pth_mutex_init (&(*p)->m_handle);
85 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
90 EnterCriticalSection(&p->m_handle);
91 #elif YAZ_POSIX_THREADS
92 pthread_mutex_lock(&p->m_handle);
97 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
102 LeaveCriticalSection(&p->m_handle);
103 #elif YAZ_POSIX_THREADS
104 pthread_mutex_unlock(&p->m_handle);
109 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
115 DeleteCriticalSection(&(*p)->m_handle);
123 static nmem_block *freelist = NULL; /* "global" freelists */
124 static nmem_control *cfreelist = NULL;
125 static int nmem_active_no = 0;
126 static int nmem_init_flag = 0;
129 struct nmem_debug_info {
133 struct nmem_debug_info *next;
136 struct nmem_debug_info *nmem_debug_list = 0;
139 static void free_block(nmem_block *p)
141 memset(p->buf, 'Y', p->size);
145 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
150 void nmem_print_list (void)
152 nmem_print_list_l(LOG_DEBUG);
155 void nmem_print_list_l (int level)
157 struct nmem_debug_info *p;
159 yaz_log (level, "nmem print list");
161 for (p = nmem_debug_list; p; p = p->next)
162 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
168 * acquire a block with a minimum of size free bytes.
170 static nmem_block *get_block(int size)
175 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
177 for (r = freelist, l = 0; r; l = r, r = r->next)
183 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
192 int get = NMEM_CHUNK;
197 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
199 r = (nmem_block *)xmalloc(sizeof(*r));
200 r->buf = (char *)xmalloc(r->size = get);
206 void nmem_reset(NMEM n)
211 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
219 n->blocks = n->blocks->next;
227 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
229 void *nmem_malloc(NMEM n, int size)
232 struct nmem_block *p;
236 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
241 yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
245 assert (nmem_init_flag);
249 if (!p || p->size - p->top < size)
257 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
263 int nmem_total(NMEM n)
269 NMEM nmem_create_f(const char *file, int line)
271 NMEM nmem_create(void)
276 struct nmem_debug_info *debug_p;
283 cfreelist = cfreelist->next;
285 r = (nmem_control *)xmalloc(sizeof(*r));
289 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
297 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
300 yaz_log (LOG_FATAL, "multi used block in nmem");
303 debug_p = xmalloc (sizeof(*debug_p));
304 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
305 debug_p->file[sizeof(debug_p->file)-1] = '\0';
306 debug_p->line = line;
308 debug_p->next = nmem_debug_list;
309 nmem_debug_list = debug_p;
317 void nmem_destroy_f(const char *file, int line, NMEM n)
319 void nmem_destroy(NMEM n)
323 struct nmem_debug_info **debug_p;
330 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
331 nmem_active_no-1, n);
333 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
334 if ((*debug_p)->p == n)
336 struct nmem_debug_info *debug_save = *debug_p;
337 *debug_p = (*debug_p)->next;
346 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
359 void nmem_transfer (NMEM dst, NMEM src)
362 while ((t=src->blocks))
364 src->blocks = t->next;
365 t->next = dst->blocks;
368 dst->total += src->total;
372 void nmem_critical_enter (void)
377 void nmem_critical_leave (void)
382 void nmem_init (void)
384 if (++nmem_init_flag == 1)
387 InitializeCriticalSection(&critical_section);
388 #elif YAZ_GNU_THREADS
389 yaz_log (LOG_LOG, "pth_init");
398 void nmem_exit (void)
400 if (--nmem_init_flag == 0)
405 struct nmem_block *fl = freelist;
406 freelist = freelist->next;
412 struct nmem_control *cfl = cfreelist;
413 cfreelist = cfreelist->next;
417 DeleteCriticalSection(&critical_section);
424 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
430 case DLL_PROCESS_ATTACH:
433 case DLL_PROCESS_DETACH:
445 void yaz_set_errno(int v)
450 void yaz_strerror(char *buf, int max)
454 DWORD err = GetLastError();
458 FORMAT_MESSAGE_FROM_SYSTEM,
461 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
471 #if YAZ_POSIX_THREADS
473 strerror_r(errno, buf, max);
474 /* if buffer is unset - use strerror anyway (GLIBC bug) */
476 strcpy(buf, strerror(yaz_errno()));
478 strcpy(buf, strerror(yaz_errno()));
481 strcpy(buf, strerror(yaz_errno()));
485 if ((cp=strrchr(buf, '\n')))
487 if ((cp=strrchr(buf, '\r')))