| 1 | /** |
| 2 | * @file |
| 3 | * |
| 4 | * @brief interface for the memory allocator |
| 5 | * |
| 6 | * provides interfaces for the memory allocator, |
| 7 | * including debugging capabilities. |
| 8 | * |
| 9 | * @copyright See Copyright for the status of this software. |
| 10 | * |
| 11 | * @author Daniel Veillard |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | #ifndef __DEBUG_MEMORY_ALLOC__ |
| 16 | #define __DEBUG_MEMORY_ALLOC__ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <libxml/xmlversion.h> |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | /* |
| 26 | * The XML memory wrapper support 4 basic overloadable functions. |
| 27 | */ |
| 28 | /** |
| 29 | * Signature for a free() implementation. |
| 30 | * |
| 31 | * @param mem an already allocated block of memory |
| 32 | */ |
| 33 | typedef void (*xmlFreeFunc)(void *mem); |
| 34 | /** |
| 35 | * Signature for a malloc() implementation. |
| 36 | * |
| 37 | * @param size the size requested in bytes |
| 38 | * @returns a pointer to the newly allocated block or NULL in case of error. |
| 39 | */ |
| 40 | typedef void *(*xmlMallocFunc)(size_t size) LIBXML_ATTR_ALLOC_SIZE(1); |
| 41 | |
| 42 | /** |
| 43 | * Signature for a realloc() implementation. |
| 44 | * |
| 45 | * @param mem an already allocated block of memory |
| 46 | * @param size the new size requested in bytes |
| 47 | * @returns a pointer to the newly reallocated block or NULL in case of error. |
| 48 | */ |
| 49 | typedef void *(*xmlReallocFunc)(void *mem, size_t size); |
| 50 | |
| 51 | /** |
| 52 | * Signature for an strdup() implementation. |
| 53 | * |
| 54 | * @param str a zero terminated string |
| 55 | * @returns the copy of the string or NULL in case of error. |
| 56 | */ |
| 57 | typedef char *(*xmlStrdupFunc)(const char *str); |
| 58 | |
| 59 | /* |
| 60 | * In general the memory allocation entry points are not kept |
| 61 | * thread specific but this can be overridden by LIBXML_THREAD_ALLOC_ENABLED |
| 62 | * - xmlMalloc |
| 63 | * - xmlMallocAtomic |
| 64 | * - xmlRealloc |
| 65 | * - xmlMemStrdup |
| 66 | * - xmlFree |
| 67 | */ |
| 68 | #ifdef LIBXML_THREAD_ALLOC_ENABLED |
| 69 | |
| 70 | XMLPUBFUN xmlMallocFunc *__xmlMalloc(void); |
| 71 | XMLPUBFUN xmlMallocFunc *__xmlMallocAtomic(void); |
| 72 | XMLPUBFUN xmlReallocFunc *__xmlRealloc(void); |
| 73 | XMLPUBFUN xmlFreeFunc *__xmlFree(void); |
| 74 | XMLPUBFUN xmlStrdupFunc *__xmlMemStrdup(void); |
| 75 | |
| 76 | #ifndef XML_GLOBALS_NO_REDEFINITION |
| 77 | #define xmlMalloc (*__xmlMalloc()) |
| 78 | #define xmlMallocAtomic (*__xmlMallocAtomic()) |
| 79 | #define xmlRealloc (*__xmlRealloc()) |
| 80 | #define xmlFree (*__xmlFree()) |
| 81 | #define xmlMemStrdup (*__xmlMemStrdup()) |
| 82 | #endif |
| 83 | |
| 84 | #else |
| 85 | |
| 86 | /** |
| 87 | * The variable holding the libxml malloc() implementation |
| 88 | */ |
| 89 | XMLPUBVAR xmlMallocFunc xmlMalloc; |
| 90 | /** |
| 91 | * The variable holding the libxml malloc() implementation for atomic |
| 92 | * data (i.e. blocks not containing pointers), useful when using a |
| 93 | * garbage collecting allocator. |
| 94 | * |
| 95 | * @deprecated Use #xmlMalloc |
| 96 | */ |
| 97 | XMLPUBVAR xmlMallocFunc xmlMallocAtomic; |
| 98 | /** |
| 99 | * The variable holding the libxml realloc() implementation |
| 100 | */ |
| 101 | XMLPUBVAR xmlReallocFunc xmlRealloc; |
| 102 | /** |
| 103 | * The variable holding the libxml free() implementation |
| 104 | */ |
| 105 | XMLPUBVAR xmlFreeFunc xmlFree; |
| 106 | /** |
| 107 | * The variable holding the libxml strdup() implementation |
| 108 | */ |
| 109 | XMLPUBVAR xmlStrdupFunc xmlMemStrdup; |
| 110 | |
| 111 | #endif |
| 112 | |
| 113 | /* |
| 114 | * The way to overload the existing functions. |
| 115 | * The xmlGc function have an extra entry for atomic block |
| 116 | * allocations useful for garbage collected memory allocators |
| 117 | */ |
| 118 | XMLPUBFUN int |
| 119 | xmlMemSetup (xmlFreeFunc freeFunc, |
| 120 | xmlMallocFunc mallocFunc, |
| 121 | xmlReallocFunc reallocFunc, |
| 122 | xmlStrdupFunc strdupFunc); |
| 123 | XMLPUBFUN int |
| 124 | xmlMemGet (xmlFreeFunc *freeFunc, |
| 125 | xmlMallocFunc *mallocFunc, |
| 126 | xmlReallocFunc *reallocFunc, |
| 127 | xmlStrdupFunc *strdupFunc); |
| 128 | XML_DEPRECATED |
| 129 | XMLPUBFUN int |
| 130 | xmlGcMemSetup (xmlFreeFunc freeFunc, |
| 131 | xmlMallocFunc mallocFunc, |
| 132 | xmlMallocFunc mallocAtomicFunc, |
| 133 | xmlReallocFunc reallocFunc, |
| 134 | xmlStrdupFunc strdupFunc); |
| 135 | XML_DEPRECATED |
| 136 | XMLPUBFUN int |
| 137 | xmlGcMemGet (xmlFreeFunc *freeFunc, |
| 138 | xmlMallocFunc *mallocFunc, |
| 139 | xmlMallocFunc *mallocAtomicFunc, |
| 140 | xmlReallocFunc *reallocFunc, |
| 141 | xmlStrdupFunc *strdupFunc); |
| 142 | |
| 143 | /* |
| 144 | * Initialization of the memory layer. |
| 145 | */ |
| 146 | XML_DEPRECATED |
| 147 | XMLPUBFUN int |
| 148 | xmlInitMemory (void); |
| 149 | |
| 150 | /* |
| 151 | * Cleanup of the memory layer. |
| 152 | */ |
| 153 | XML_DEPRECATED |
| 154 | XMLPUBFUN void |
| 155 | xmlCleanupMemory (void); |
| 156 | /* |
| 157 | * These are specific to the XML debug memory wrapper. |
| 158 | */ |
| 159 | XMLPUBFUN size_t |
| 160 | xmlMemSize (void *ptr); |
| 161 | XMLPUBFUN int |
| 162 | xmlMemUsed (void); |
| 163 | XMLPUBFUN int |
| 164 | xmlMemBlocks (void); |
| 165 | XML_DEPRECATED |
| 166 | XMLPUBFUN void |
| 167 | xmlMemDisplay (FILE *fp); |
| 168 | XML_DEPRECATED |
| 169 | XMLPUBFUN void |
| 170 | xmlMemDisplayLast(FILE *fp, long nbBytes); |
| 171 | XML_DEPRECATED |
| 172 | XMLPUBFUN void |
| 173 | xmlMemShow (FILE *fp, int nr); |
| 174 | XML_DEPRECATED |
| 175 | XMLPUBFUN void |
| 176 | xmlMemoryDump (void); |
| 177 | XMLPUBFUN void * |
| 178 | xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1); |
| 179 | XMLPUBFUN void * |
| 180 | xmlMemRealloc (void *ptr,size_t size); |
| 181 | XMLPUBFUN void |
| 182 | xmlMemFree (void *ptr); |
| 183 | XMLPUBFUN char * |
| 184 | xmlMemoryStrdup (const char *str); |
| 185 | XML_DEPRECATED |
| 186 | XMLPUBFUN void * |
| 187 | xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1); |
| 188 | XML_DEPRECATED |
| 189 | XMLPUBFUN void * |
| 190 | xmlReallocLoc (void *ptr, size_t size, const char *file, int line); |
| 191 | XML_DEPRECATED |
| 192 | XMLPUBFUN void * |
| 193 | xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1); |
| 194 | XML_DEPRECATED |
| 195 | XMLPUBFUN char * |
| 196 | xmlMemStrdupLoc (const char *str, const char *file, int line); |
| 197 | |
| 198 | #ifdef __cplusplus |
| 199 | } |
| 200 | #endif /* __cplusplus */ |
| 201 | |
| 202 | #endif /* __DEBUG_MEMORY_ALLOC__ */ |
| 203 | |
| 204 | |