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.2 2003-12-04 11:47:50 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 struct nmem_debug_info *p;
154 yaz_log (LOG_DEBUG, "nmem print list");
156 for (p = nmem_debug_list; p; p = p->next)
157 yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
163 * acquire a block with a minimum of size free bytes.
165 static nmem_block *get_block(int size)
170 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
172 for (r = freelist, l = 0; r; l = r, r = r->next)
178 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
187 int get = NMEM_CHUNK;
192 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
194 r = (nmem_block *)xmalloc(sizeof(*r));
195 r->buf = (char *)xmalloc(r->size = get);
201 void nmem_reset(NMEM n)
206 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
214 n->blocks = n->blocks->next;
222 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
224 void *nmem_malloc(NMEM n, int size)
227 struct nmem_block *p;
231 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
236 yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
240 assert (nmem_init_flag);
244 if (!p || p->size - p->top < size)
252 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
258 int nmem_total(NMEM n)
264 NMEM nmem_create_f(const char *file, int line)
266 NMEM nmem_create(void)
271 struct nmem_debug_info *debug_p;
278 cfreelist = cfreelist->next;
280 r = (nmem_control *)xmalloc(sizeof(*r));
284 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
292 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
295 yaz_log (LOG_FATAL, "multi used block in nmem");
298 debug_p = xmalloc (sizeof(*debug_p));
299 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
300 debug_p->file[sizeof(debug_p->file)-1] = '\0';
301 debug_p->line = line;
303 debug_p->next = nmem_debug_list;
304 nmem_debug_list = debug_p;
312 void nmem_destroy_f(const char *file, int line, NMEM n)
314 void nmem_destroy(NMEM n)
318 struct nmem_debug_info **debug_p;
325 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
326 nmem_active_no-1, n);
328 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
329 if ((*debug_p)->p == n)
331 struct nmem_debug_info *debug_save = *debug_p;
332 *debug_p = (*debug_p)->next;
341 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
354 void nmem_transfer (NMEM dst, NMEM src)
357 while ((t=src->blocks))
359 src->blocks = t->next;
360 t->next = dst->blocks;
363 dst->total += src->total;
367 void nmem_critical_enter (void)
372 void nmem_critical_leave (void)
377 void nmem_init (void)
379 if (++nmem_init_flag == 1)
382 InitializeCriticalSection(&critical_section);
383 #elif YAZ_GNU_THREADS
384 yaz_log (LOG_LOG, "pth_init");
393 void nmem_exit (void)
395 if (--nmem_init_flag == 0)
400 struct nmem_block *fl = freelist;
401 freelist = freelist->next;
407 struct nmem_control *cfl = cfreelist;
408 cfreelist = cfreelist->next;
412 DeleteCriticalSection(&critical_section);
419 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
425 case DLL_PROCESS_ATTACH:
428 case DLL_PROCESS_DETACH:
440 void yaz_set_errno(int v)
445 void yaz_strerror(char *buf, int max)
449 DWORD err = GetLastError();
453 FORMAT_MESSAGE_FROM_SYSTEM,
456 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
466 #if YAZ_POSIX_THREADS
468 strerror_r(errno, buf, max);
469 /* if buffer is unset - use strerror anyway (GLIBC bug) */
471 strcpy(buf, strerror(yaz_errno()));
473 strcpy(buf, strerror(yaz_errno()));
476 strcpy(buf, strerror(yaz_errno()));
480 if ((cp=strrchr(buf, '\n')))
482 if ((cp=strrchr(buf, '\r')))