1 /************************************************************************* 2 * TinyFugue - programmable mud client 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2005, 2006-2007 Ken Keys 4 * 5 * TinyFugue (aka "tf") is protected under the terms of the GNU 6 * General Public License. See the file "COPYING" for details. 7 ************************************************************************/ 8 /* $Id: malloc.h,v 35004.24 2007/01/13 23:12:39 kkeys Exp $ */ 9 10 /* Function hierarchy: 11 * xmalloc: will kill process if it fails. Calls dmalloc. 12 * dmalloc: if USE_DMALLOC, do debugging. Calls mmalloc. 13 * mmalloc: if USE_MMALLOC, use GNU mmalloc; otherwise call native functions. 14 * malloc: if !USE_MMALLOC, use native functions; otherwise, call mmalloc. 15 * 16 * XMALLOC is like xmalloc, but has file and line built in. 17 * MALLOC is like dmalloc, but has file and line built in. 18 * 19 * Most code will call XMALLOC() (for automatic error checking) or MALLOC() 20 * (if it can handle failure itself). Code may call xmalloc() or dmalloc() 21 * to use nonlocal file and line info. Code should almost never call 22 * mmalloc() or malloc() directly. 23 */ 24 25 #ifndef MALLOC_H 26 #define MALLOC_H 27 28 extern int low_memory_warning; 29 30 #if USE_MMALLOC 31 # include "mmalloc/mmalloc.h" 32 # define malloc(size) mmalloc(NULL, size) 33 # define calloc(size) mcalloc(NULL, size) 34 # define realloc(ptr, size) mrealloc(NULL, ptr, size) 35 # define free(ptr) mfree(NULL, ptr) 36 #else 37 # define mmalloc(md, size) malloc(size) 38 # define mcalloc(md, size) calloc(size) 39 # define mrealloc(md, ptr, size) realloc(ptr, size) 40 # define mfree(md, ptr) free(ptr) 41 #endif 42 43 #define XMALLOC(size) xmalloc(NULL, (size), __FILE__, __LINE__) 44 #define XCALLOC(size) xcalloc(NULL, (size), __FILE__, __LINE__) 45 #define XREALLOC(ptr, size) xrealloc(NULL, (ptr), (size), __FILE__, __LINE__) 46 #define MALLOC(size) dmalloc(NULL, (size), __FILE__, __LINE__) 47 #define CALLOC(size) dcalloc(NULL, (size), __FILE__, __LINE__) 48 #define REALLOC(ptr, size) drealloc(NULL, (ptr), (size), __FILE__, __LINE__) 49 #define FREE(ptr) xfree(NULL, (void*)(ptr), __FILE__, __LINE__) 50 51 #if USE_DMALLOC 52 extern void *dmalloc(void *md, long unsigned size, 53 const char *file, const int line); 54 extern void *dcalloc(void *md, long unsigned size, 55 const char *file, const int line); 56 extern void *drealloc(void *md, void *ptr, long unsigned size, 57 const char *file, const int line); 58 extern void dfree(void *md, void *ptr, 59 const char *file, const int line); 60 #else 61 # define dmalloc(md, size, file, line) mmalloc(md, size) 62 # define dcalloc(md, size, file, line) mcalloc(md, size) 63 # define drealloc(md, ptr, size, file, line) \ 64 mrealloc(md, (void*)(ptr), (size)) 65 # define dfree(md, ptr, file, line) mfree(md, (void*)(ptr)) 66 #endif 67 68 extern void *xmalloc(void *md, long unsigned size, 69 const char *file, const int line); 70 extern void *xcalloc(void *md, long unsigned size, 71 const char *file, const int line); 72 extern void *xrealloc(void *md, void *ptr, long unsigned size, 73 const char *file, const int line); 74 extern void xfree(void *md, void *ptr, 75 const char *file, const int line); 76 extern void init_malloc(void); 77 78 /* Fast allocation from pool. 79 * Should be used only on objects which are freed frequently, with md==NULL. 80 */ 81 #define palloc(item, type, pool, next, file, line) \ 82 ((pool) ? \ 83 ((item) = (pool), (pool) = (void*)(pool)->next) : \ 84 ((item) = (type *)xmalloc(NULL, sizeof(type), file, line))) 85 86 #if !USE_DMALLOC 87 # define pfree_fl(item, pool, next, file, line) \ 88 ((item)->next = (void*)(pool), (pool) = (item)) 89 # define pfreepool(type, pool, next) \ 90 do { \ 91 type *tmp; \ 92 while (pool) { \ 93 tmp = pool; \ 94 pool = (type*)pool->next; \ 95 FREE(tmp); \ 96 } \ 97 } while (0) 98 #else 99 # define pfree_fl(item, pool, next, file, line) \ 100 dfree(NULL, item, file, line) 101 # define pfreepool(type, pool, next) /* do nothing */ 102 #endif 103 104 #define pfree(item, pool, next) pfree_fl(item, pool, next, __FILE__, __LINE__) 105 106 #if USE_DMALLOC 107 extern void free_reserve(void); 108 extern void debug_mstats(const char *s); 109 #endif 110 111 #endif /* MALLOC_H */ 112