Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xmlmemory.h: interface for the memory allocation debug. |
| 3 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 4 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
| 8 | #ifndef _DEBUG_MEMORY_ALLOC_ |
| 9 | #define _DEBUG_MEMORY_ALLOC_ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <libxml/xmlversion.h> |
| 13 | |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 14 | /** |
| 15 | * DEBUG_MEMORY: |
| 16 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 17 | * DEBUG_MEMORY replaces the allocator with a collect and debug |
| 18 | * shell to the libc allocator. |
| 19 | * DEBUG_MEMORY should only be activated when debugging |
| 20 | * libxml i.e. if libxml has been configured with --with-debug-mem too. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 21 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 22 | /* #define DEBUG_MEMORY_FREED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 23 | /* #define DEBUG_MEMORY_LOCATION */ |
| 24 | |
| 25 | #ifdef DEBUG |
| 26 | #ifndef DEBUG_MEMORY |
| 27 | #define DEBUG_MEMORY |
| 28 | #endif |
| 29 | #endif |
| 30 | |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 31 | /** |
| 32 | * DEBUG_MEMORY_LOCATION: |
| 33 | * |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 34 | * DEBUG_MEMORY_LOCATION should be activated only when debugging |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 35 | * libxml i.e. if libxml has been configured with --with-debug-mem too. |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 36 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | #ifdef DEBUG_MEMORY_LOCATION |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /* |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 45 | * The XML memory wrapper support 4 basic overloadable functions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | */ |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 47 | /** |
| 48 | * xmlFreeFunc: |
| 49 | * @mem: an already allocated block of memory |
| 50 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 51 | * Signature for a free() implementation. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 52 | */ |
| 53 | typedef void (*xmlFreeFunc)(void *mem); |
| 54 | /** |
| 55 | * xmlMallocFunc: |
| 56 | * @size: the size requested in bytes |
| 57 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 58 | * Signature for a malloc() implementation. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 59 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 60 | * Returns a pointer to the newly allocated block or NULL in case of error. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 61 | */ |
| 62 | typedef void *(*xmlMallocFunc)(size_t size); |
| 63 | |
| 64 | /** |
| 65 | * xmlReallocFunc: |
| 66 | * @mem: an already allocated block of memory |
| 67 | * @size: the new size requested in bytes |
| 68 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 69 | * Signature for a realloc() implementation. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 70 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 71 | * Returns a pointer to the newly reallocated block or NULL in case of error. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 72 | */ |
| 73 | typedef void *(*xmlReallocFunc)(void *mem, size_t size); |
| 74 | |
| 75 | /** |
| 76 | * xmlStrdupFunc: |
| 77 | * @str: a zero terminated string |
| 78 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 79 | * Signature for an strdup() implementation. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 80 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 81 | * Returns the copy of the string or NULL in case of error. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 82 | */ |
| 83 | typedef char *(*xmlStrdupFunc)(const char *str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 86 | * The 4 interfaces used for all memory handling within libxml. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 87 | LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree; |
| 88 | LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 89 | LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 90 | LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc; |
| 91 | LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup; |
Daniel Veillard | 6c4ffaf | 2002-02-11 08:54:05 +0000 | [diff] [blame] | 92 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 95 | * The way to overload the existing functions. |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 96 | * The xmlGc function have an extra entry for atomic block |
| 97 | * allocations useful for garbage collected memory allocators |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 98 | */ |
| 99 | int xmlMemSetup (xmlFreeFunc freeFunc, |
| 100 | xmlMallocFunc mallocFunc, |
| 101 | xmlReallocFunc reallocFunc, |
| 102 | xmlStrdupFunc strdupFunc); |
| 103 | int xmlMemGet (xmlFreeFunc *freeFunc, |
| 104 | xmlMallocFunc *mallocFunc, |
| 105 | xmlReallocFunc *reallocFunc, |
| 106 | xmlStrdupFunc *strdupFunc); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 107 | int xmlGcMemSetup (xmlFreeFunc freeFunc, |
| 108 | xmlMallocFunc mallocFunc, |
| 109 | xmlMallocFunc mallocAtomicFunc, |
| 110 | xmlReallocFunc reallocFunc, |
| 111 | xmlStrdupFunc strdupFunc); |
| 112 | int xmlGcMemGet (xmlFreeFunc *freeFunc, |
| 113 | xmlMallocFunc *mallocFunc, |
| 114 | xmlMallocFunc *mallocAtomicFunc, |
| 115 | xmlReallocFunc *reallocFunc, |
| 116 | xmlStrdupFunc *strdupFunc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 119 | * Initialization of the memory layer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 120 | */ |
| 121 | int xmlInitMemory (void); |
| 122 | |
| 123 | /* |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame^] | 124 | * These are specific to the XML debug memory wrapper. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 125 | */ |
| 126 | int xmlMemUsed (void); |
| 127 | void xmlMemDisplay (FILE *fp); |
| 128 | void xmlMemShow (FILE *fp, int nr); |
| 129 | void xmlMemoryDump (void); |
Igor Zlatkovic | 7ae91bc | 2002-11-08 17:18:52 +0000 | [diff] [blame] | 130 | void * xmlMemMalloc (size_t size); |
| 131 | void * xmlMemRealloc (void *ptr,size_t size); |
| 132 | void xmlMemFree (void *ptr); |
| 133 | char * xmlMemoryStrdup (const char *str); |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame^] | 134 | void * xmlMallocLoc (size_t size, const char *file, int line); |
| 135 | void * xmlReallocLoc (void *ptr, size_t size, const char *file, int line); |
| 136 | void * xmlMallocAtomicLoc (size_t size, const char *file, int line); |
| 137 | char * xmlMemStrdupLoc (const char *str, const char *file, int line); |
| 138 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 139 | |
| 140 | #ifdef DEBUG_MEMORY_LOCATION |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 141 | /** |
| 142 | * xmlMalloc: |
| 143 | * @size: number of bytes to allocate |
| 144 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 145 | * Wrapper for the malloc() function used in the XML library. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 146 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 147 | * Returns the pointer to the allocated area or NULL in case of error. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 148 | */ |
| 149 | #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__) |
| 150 | /** |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 151 | * xmlMallocAtomic: |
| 152 | * @size: number of bytes to allocate |
| 153 | * |
| 154 | * Wrapper for the malloc() function used in the XML library for allocation |
| 155 | * of block not containing pointers to other areas. |
| 156 | * |
| 157 | * Returns the pointer to the allocated area or NULL in case of error. |
| 158 | */ |
| 159 | #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__) |
| 160 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 161 | * xmlRealloc: |
| 162 | * @ptr: pointer to the existing allocated area |
| 163 | * @size: number of bytes to allocate |
| 164 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 165 | * Wrapper for the realloc() function used in the XML library. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 166 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 167 | * Returns the pointer to the allocated area or NULL in case of error. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 168 | */ |
| 169 | #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__) |
| 170 | /** |
| 171 | * xmlMemStrdup: |
| 172 | * @str: pointer to the existing string |
| 173 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 174 | * Wrapper for the strdup() function, xmlStrdup() is usually preferred. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 175 | * |
Daniel Veillard | 61f2617 | 2002-03-12 18:46:39 +0000 | [diff] [blame] | 176 | * Returns the pointer to the allocated area or NULL in case of error. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 177 | */ |
| 178 | #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 180 | void * xmlMallocLoc(size_t size, const char *file, int line); |
| 181 | void * xmlReallocLoc(void *ptr,size_t size, const char *file, int line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 182 | char * xmlMemStrdupLoc(const char *str, const char *file, int line); |
| 183 | #endif /* DEBUG_MEMORY_LOCATION */ |
| 184 | |
| 185 | #ifdef __cplusplus |
| 186 | } |
| 187 | #endif /* __cplusplus */ |
| 188 | |
Daniel Veillard | 6c4ffaf | 2002-02-11 08:54:05 +0000 | [diff] [blame] | 189 | #ifndef __XML_GLOBALS_H |
| 190 | #ifndef __XML_THREADS_H__ |
| 191 | #include <libxml/threads.h> |
| 192 | #include <libxml/globals.h> |
| 193 | #endif |
| 194 | #endif |
| 195 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 196 | #endif /* _DEBUG_MEMORY_ALLOC_ */ |
| 197 | |