Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 2 | * xmlmemory.c: libxml memory allocator wrapper. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 7 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 8 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
| 12 | #ifdef HAVE_SYS_TYPES_H |
| 13 | #include <sys/types.h> |
| 14 | #endif |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 15 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | #ifdef HAVE_TIME_H |
| 17 | #include <time.h> |
| 18 | #endif |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 19 | |
| 20 | #ifdef HAVE_STDLIB_H |
| 21 | #include <stdlib.h> |
| 22 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 23 | #ifdef HAVE_MALLOC_H |
| 24 | #include <malloc.h> |
| 25 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 26 | #endif |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 27 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 28 | #ifdef HAVE_CTYPE_H |
| 29 | #include <ctype.h> |
| 30 | #endif |
| 31 | |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 32 | /* #define DEBUG_MEMORY */ |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 34 | /** |
| 35 | * MEM_LIST: |
| 36 | * |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 37 | * keep track of all allocated blocks for error reporting |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 38 | * Always build the memory list ! |
| 39 | */ |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 40 | #ifdef DEBUG_MEMORY_LOCATION |
Daniel Veillard | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 41 | #ifndef MEM_LIST |
| 42 | #define MEM_LIST /* keep a list of all the allocated memory blocks */ |
| 43 | #endif |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 44 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 45 | |
William M. Brack | 5ab479b | 2004-06-10 13:00:15 +0000 | [diff] [blame] | 46 | #include <libxml/globals.h> /* must come before xmlmemory.h */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 47 | #include <libxml/xmlmemory.h> |
| 48 | #include <libxml/xmlerror.h> |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 49 | #include <libxml/threads.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 50 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 51 | static int xmlMemInitialized = 0; |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 52 | static unsigned long debugMemSize = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 53 | static unsigned long debugMemBlocks = 0; |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 54 | static unsigned long debugMaxMemSize = 0; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 55 | static xmlMutexPtr xmlMemMutex = NULL; |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 56 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 57 | void xmlMallocBreakpoint(void); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 58 | |
| 59 | /************************************************************************ |
| 60 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 61 | * Macros, variables and associated types * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 62 | * * |
| 63 | ************************************************************************/ |
| 64 | |
William M. Brack | 5ab479b | 2004-06-10 13:00:15 +0000 | [diff] [blame] | 65 | #if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 66 | #ifdef xmlMalloc |
| 67 | #undef xmlMalloc |
| 68 | #endif |
| 69 | #ifdef xmlRealloc |
| 70 | #undef xmlRealloc |
| 71 | #endif |
| 72 | #ifdef xmlMemStrdup |
| 73 | #undef xmlMemStrdup |
| 74 | #endif |
William M. Brack | 5ab479b | 2004-06-10 13:00:15 +0000 | [diff] [blame] | 75 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Each of the blocks allocated begin with a header containing informations |
| 79 | */ |
| 80 | |
| 81 | #define MEMTAG 0x5aa5 |
| 82 | |
| 83 | #define MALLOC_TYPE 1 |
| 84 | #define REALLOC_TYPE 2 |
| 85 | #define STRDUP_TYPE 3 |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 86 | #define MALLOC_ATOMIC_TYPE 4 |
| 87 | #define REALLOC_ATOMIC_TYPE 5 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | |
| 89 | typedef struct memnod { |
| 90 | unsigned int mh_tag; |
| 91 | unsigned int mh_type; |
| 92 | unsigned long mh_number; |
| 93 | size_t mh_size; |
| 94 | #ifdef MEM_LIST |
| 95 | struct memnod *mh_next; |
| 96 | struct memnod *mh_prev; |
| 97 | #endif |
| 98 | const char *mh_file; |
| 99 | unsigned int mh_line; |
| 100 | } MEMHDR; |
| 101 | |
| 102 | |
| 103 | #ifdef SUN4 |
| 104 | #define ALIGN_SIZE 16 |
| 105 | #else |
| 106 | #define ALIGN_SIZE sizeof(double) |
| 107 | #endif |
| 108 | #define HDR_SIZE sizeof(MEMHDR) |
| 109 | #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \ |
| 110 | / ALIGN_SIZE ) * ALIGN_SIZE) |
| 111 | |
| 112 | |
| 113 | #define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE)) |
| 114 | #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE)) |
| 115 | |
| 116 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 117 | static unsigned int block=0; |
| 118 | static unsigned int xmlMemStopAtBlock = 0; |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 119 | static void *xmlMemTraceBlockAt = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 120 | #ifdef MEM_LIST |
| 121 | static MEMHDR *memlist = NULL; |
| 122 | #endif |
| 123 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 124 | static void debugmem_tag_error(void *addr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 125 | #ifdef MEM_LIST |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 126 | static void debugmem_list_add(MEMHDR *); |
| 127 | static void debugmem_list_delete(MEMHDR *); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 128 | #endif |
| 129 | #define Mem_Tag_Err(a) debugmem_tag_error(a); |
| 130 | |
| 131 | #ifndef TEST_POINT |
| 132 | #define TEST_POINT |
| 133 | #endif |
| 134 | |
| 135 | /** |
| 136 | * xmlMallocBreakpoint: |
| 137 | * |
| 138 | * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block |
| 139 | * number reaches the specified value this function is called. One need to add a breakpoint |
| 140 | * to it to get the context in which the given block is allocated. |
| 141 | */ |
| 142 | |
| 143 | void |
| 144 | xmlMallocBreakpoint(void) { |
| 145 | xmlGenericError(xmlGenericErrorContext, |
| 146 | "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * xmlMallocLoc: |
| 151 | * @size: an int specifying the size in byte to allocate. |
| 152 | * @file: the file name or NULL |
| 153 | * @line: the line number |
| 154 | * |
| 155 | * a malloc() equivalent, with logging of the allocation info. |
| 156 | * |
| 157 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 158 | */ |
| 159 | |
| 160 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 161 | xmlMallocLoc(size_t size, const char * file, int line) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 162 | { |
| 163 | MEMHDR *p; |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 164 | void *ret; |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 165 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 166 | if (!xmlMemInitialized) xmlInitMemory(); |
| 167 | #ifdef DEBUG_MEMORY |
| 168 | xmlGenericError(xmlGenericErrorContext, |
| 169 | "Malloc(%d)\n",size); |
| 170 | #endif |
| 171 | |
| 172 | TEST_POINT |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 173 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 174 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 175 | |
| 176 | if (!p) { |
| 177 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 178 | "xmlMallocLoc : Out of free space\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | xmlMemoryDump(); |
| 180 | return(NULL); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 181 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 182 | p->mh_tag = MEMTAG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 183 | p->mh_size = size; |
| 184 | p->mh_type = MALLOC_TYPE; |
| 185 | p->mh_file = file; |
| 186 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 187 | xmlMutexLock(xmlMemMutex); |
| 188 | p->mh_number = ++block; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 189 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 190 | debugMemBlocks++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 191 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 192 | #ifdef MEM_LIST |
| 193 | debugmem_list_add(p); |
| 194 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 195 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 196 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 197 | #ifdef DEBUG_MEMORY |
| 198 | xmlGenericError(xmlGenericErrorContext, |
| 199 | "Malloc(%d) Ok\n",size); |
| 200 | #endif |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 201 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 202 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 203 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 204 | ret = HDR_2_CLIENT(p); |
| 205 | |
| 206 | if (xmlMemTraceBlockAt == ret) { |
| 207 | xmlGenericError(xmlGenericErrorContext, |
Stefan Kost | ecb5d5a | 2011-05-06 17:40:10 +0300 | [diff] [blame] | 208 | "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, |
| 209 | (long unsigned)size); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 210 | xmlMallocBreakpoint(); |
| 211 | } |
| 212 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 213 | TEST_POINT |
| 214 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 215 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 219 | * xmlMallocAtomicLoc: |
| 220 | * @size: an int specifying the size in byte to allocate. |
| 221 | * @file: the file name or NULL |
| 222 | * @line: the line number |
| 223 | * |
| 224 | * a malloc() equivalent, with logging of the allocation info. |
| 225 | * |
| 226 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 227 | */ |
| 228 | |
| 229 | void * |
| 230 | xmlMallocAtomicLoc(size_t size, const char * file, int line) |
| 231 | { |
| 232 | MEMHDR *p; |
| 233 | void *ret; |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 234 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 235 | if (!xmlMemInitialized) xmlInitMemory(); |
| 236 | #ifdef DEBUG_MEMORY |
| 237 | xmlGenericError(xmlGenericErrorContext, |
| 238 | "Malloc(%d)\n",size); |
| 239 | #endif |
| 240 | |
| 241 | TEST_POINT |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 242 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 243 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 244 | |
| 245 | if (!p) { |
| 246 | xmlGenericError(xmlGenericErrorContext, |
| 247 | "xmlMallocLoc : Out of free space\n"); |
| 248 | xmlMemoryDump(); |
| 249 | return(NULL); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 250 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 251 | p->mh_tag = MEMTAG; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 252 | p->mh_size = size; |
| 253 | p->mh_type = MALLOC_ATOMIC_TYPE; |
| 254 | p->mh_file = file; |
| 255 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 256 | xmlMutexLock(xmlMemMutex); |
| 257 | p->mh_number = ++block; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 258 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 259 | debugMemBlocks++; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 260 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 261 | #ifdef MEM_LIST |
| 262 | debugmem_list_add(p); |
| 263 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 264 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 265 | |
| 266 | #ifdef DEBUG_MEMORY |
| 267 | xmlGenericError(xmlGenericErrorContext, |
| 268 | "Malloc(%d) Ok\n",size); |
| 269 | #endif |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 270 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 271 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 272 | |
| 273 | ret = HDR_2_CLIENT(p); |
| 274 | |
| 275 | if (xmlMemTraceBlockAt == ret) { |
| 276 | xmlGenericError(xmlGenericErrorContext, |
Stefan Kost | ecb5d5a | 2011-05-06 17:40:10 +0300 | [diff] [blame] | 277 | "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, |
| 278 | (long unsigned)size); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 279 | xmlMallocBreakpoint(); |
| 280 | } |
| 281 | |
| 282 | TEST_POINT |
| 283 | |
| 284 | return(ret); |
| 285 | } |
| 286 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 287 | * xmlMemMalloc: |
| 288 | * @size: an int specifying the size in byte to allocate. |
| 289 | * |
| 290 | * a malloc() equivalent, with logging of the allocation info. |
| 291 | * |
| 292 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 293 | */ |
| 294 | |
| 295 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 296 | xmlMemMalloc(size_t size) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 297 | { |
| 298 | return(xmlMallocLoc(size, "none", 0)); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * xmlReallocLoc: |
| 303 | * @ptr: the initial memory block pointer |
| 304 | * @size: an int specifying the size in byte to allocate. |
| 305 | * @file: the file name or NULL |
| 306 | * @line: the line number |
| 307 | * |
| 308 | * a realloc() equivalent, with logging of the allocation info. |
| 309 | * |
| 310 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 311 | */ |
| 312 | |
| 313 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 314 | xmlReallocLoc(void *ptr,size_t size, const char * file, int line) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 315 | { |
| 316 | MEMHDR *p; |
| 317 | unsigned long number; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 318 | #ifdef DEBUG_MEMORY |
| 319 | size_t oldsize; |
| 320 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 321 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 322 | if (ptr == NULL) |
Aleksey Sanin | e9f0811 | 2004-01-22 22:20:31 +0000 | [diff] [blame] | 323 | return(xmlMallocLoc(size, file, line)); |
| 324 | |
| 325 | if (!xmlMemInitialized) xmlInitMemory(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 326 | TEST_POINT |
| 327 | |
| 328 | p = CLIENT_2_HDR(ptr); |
| 329 | number = p->mh_number; |
Daniel Veillard | 18ffe20 | 2005-04-14 17:50:59 +0000 | [diff] [blame] | 330 | if (xmlMemStopAtBlock == number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 331 | if (p->mh_tag != MEMTAG) { |
| 332 | Mem_Tag_Err(p); |
| 333 | goto error; |
| 334 | } |
| 335 | p->mh_tag = ~MEMTAG; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 336 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 337 | debugMemSize -= p->mh_size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 338 | debugMemBlocks--; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 339 | #ifdef DEBUG_MEMORY |
| 340 | oldsize = p->mh_size; |
| 341 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 342 | #ifdef MEM_LIST |
| 343 | debugmem_list_delete(p); |
| 344 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 345 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 346 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 347 | p = (MEMHDR *) realloc(p,RESERVE_SIZE+size); |
| 348 | if (!p) { |
| 349 | goto error; |
| 350 | } |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 351 | if (xmlMemTraceBlockAt == ptr) { |
| 352 | xmlGenericError(xmlGenericErrorContext, |
Stefan Kost | ecb5d5a | 2011-05-06 17:40:10 +0300 | [diff] [blame] | 353 | "%p : Realloced(%lu -> %lu) Ok\n", |
| 354 | xmlMemTraceBlockAt, (long unsigned)p->mh_size, |
| 355 | (long unsigned)size); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 356 | xmlMallocBreakpoint(); |
| 357 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 358 | p->mh_tag = MEMTAG; |
| 359 | p->mh_number = number; |
| 360 | p->mh_type = REALLOC_TYPE; |
| 361 | p->mh_size = size; |
| 362 | p->mh_file = file; |
| 363 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 364 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 365 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 366 | debugMemBlocks++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 367 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 368 | #ifdef MEM_LIST |
| 369 | debugmem_list_add(p); |
| 370 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 371 | xmlMutexUnlock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 372 | |
| 373 | TEST_POINT |
| 374 | |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 375 | #ifdef DEBUG_MEMORY |
| 376 | xmlGenericError(xmlGenericErrorContext, |
| 377 | "Realloced(%d to %d) Ok\n", oldsize, size); |
| 378 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 379 | return(HDR_2_CLIENT(p)); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 380 | |
| 381 | error: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 382 | return(NULL); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * xmlMemRealloc: |
| 387 | * @ptr: the initial memory block pointer |
| 388 | * @size: an int specifying the size in byte to allocate. |
| 389 | * |
| 390 | * a realloc() equivalent, with logging of the allocation info. |
| 391 | * |
| 392 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 393 | */ |
| 394 | |
| 395 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 396 | xmlMemRealloc(void *ptr,size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 397 | return(xmlReallocLoc(ptr, size, "none", 0)); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * xmlMemFree: |
| 402 | * @ptr: the memory block pointer |
| 403 | * |
| 404 | * a free() equivalent, with error checking. |
| 405 | */ |
| 406 | void |
| 407 | xmlMemFree(void *ptr) |
| 408 | { |
| 409 | MEMHDR *p; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 410 | char *target; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 411 | #ifdef DEBUG_MEMORY |
| 412 | size_t size; |
| 413 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 414 | |
Daniel Veillard | 2a512da | 2007-10-30 20:24:40 +0000 | [diff] [blame] | 415 | if (ptr == NULL) |
| 416 | return; |
| 417 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 418 | if (ptr == (void *) -1) { |
| 419 | xmlGenericError(xmlGenericErrorContext, |
| 420 | "trying to free pointer from freed area\n"); |
| 421 | goto error; |
| 422 | } |
| 423 | |
| 424 | if (xmlMemTraceBlockAt == ptr) { |
| 425 | xmlGenericError(xmlGenericErrorContext, |
| 426 | "%p : Freed()\n", xmlMemTraceBlockAt); |
| 427 | xmlMallocBreakpoint(); |
| 428 | } |
| 429 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 430 | TEST_POINT |
| 431 | |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 432 | target = (char *) ptr; |
| 433 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 434 | p = CLIENT_2_HDR(ptr); |
| 435 | if (p->mh_tag != MEMTAG) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 436 | Mem_Tag_Err(p); |
| 437 | goto error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 438 | } |
Daniel Veillard | 18ffe20 | 2005-04-14 17:50:59 +0000 | [diff] [blame] | 439 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 440 | p->mh_tag = ~MEMTAG; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 441 | memset(target, -1, p->mh_size); |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 442 | xmlMutexLock(xmlMemMutex); |
| 443 | debugMemSize -= p->mh_size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 444 | debugMemBlocks--; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 445 | #ifdef DEBUG_MEMORY |
| 446 | size = p->mh_size; |
| 447 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 448 | #ifdef MEM_LIST |
| 449 | debugmem_list_delete(p); |
| 450 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 451 | xmlMutexUnlock(xmlMemMutex); |
| 452 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 453 | free(p); |
| 454 | |
| 455 | TEST_POINT |
| 456 | |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 457 | #ifdef DEBUG_MEMORY |
| 458 | xmlGenericError(xmlGenericErrorContext, |
| 459 | "Freed(%d) Ok\n", size); |
| 460 | #endif |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 461 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 462 | return; |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 463 | |
| 464 | error: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 465 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 466 | "xmlMemFree(%lX) error\n", (unsigned long) ptr); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 467 | xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * xmlMemStrdupLoc: |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 473 | * @str: the initial string pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 474 | * @file: the file name or NULL |
| 475 | * @line: the line number |
| 476 | * |
| 477 | * a strdup() equivalent, with logging of the allocation info. |
| 478 | * |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 479 | * Returns a pointer to the new string or NULL if allocation error occurred. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 480 | */ |
| 481 | |
| 482 | char * |
| 483 | xmlMemStrdupLoc(const char *str, const char *file, int line) |
| 484 | { |
| 485 | char *s; |
| 486 | size_t size = strlen(str) + 1; |
| 487 | MEMHDR *p; |
| 488 | |
| 489 | if (!xmlMemInitialized) xmlInitMemory(); |
| 490 | TEST_POINT |
| 491 | |
| 492 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 493 | if (!p) { |
| 494 | goto error; |
| 495 | } |
| 496 | p->mh_tag = MEMTAG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 497 | p->mh_size = size; |
| 498 | p->mh_type = STRDUP_TYPE; |
| 499 | p->mh_file = file; |
| 500 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 501 | xmlMutexLock(xmlMemMutex); |
| 502 | p->mh_number = ++block; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 503 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 504 | debugMemBlocks++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 505 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 506 | #ifdef MEM_LIST |
| 507 | debugmem_list_add(p); |
| 508 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 509 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 510 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 511 | s = (char *) HDR_2_CLIENT(p); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 512 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 513 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 514 | |
Gaurav Gupta | b8480ae | 2014-07-26 21:14:53 +0800 | [diff] [blame] | 515 | strcpy(s,str); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 516 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 517 | TEST_POINT |
| 518 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 519 | if (xmlMemTraceBlockAt == s) { |
| 520 | xmlGenericError(xmlGenericErrorContext, |
| 521 | "%p : Strdup() Ok\n", xmlMemTraceBlockAt); |
| 522 | xmlMallocBreakpoint(); |
| 523 | } |
| 524 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 525 | return(s); |
| 526 | |
| 527 | error: |
| 528 | return(NULL); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * xmlMemoryStrdup: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 533 | * @str: the initial string pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 534 | * |
| 535 | * a strdup() equivalent, with logging of the allocation info. |
| 536 | * |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 537 | * Returns a pointer to the new string or NULL if allocation error occurred. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 538 | */ |
| 539 | |
| 540 | char * |
| 541 | xmlMemoryStrdup(const char *str) { |
| 542 | return(xmlMemStrdupLoc(str, "none", 0)); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * xmlMemUsed: |
| 547 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 548 | * Provides the amount of memory currently allocated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 549 | * |
| 550 | * Returns an int representing the amount of memory allocated. |
| 551 | */ |
| 552 | |
| 553 | int |
| 554 | xmlMemUsed(void) { |
| 555 | return(debugMemSize); |
| 556 | } |
| 557 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 558 | /** |
| 559 | * xmlMemBlocks: |
| 560 | * |
| 561 | * Provides the number of memory areas currently allocated |
| 562 | * |
| 563 | * Returns an int representing the number of blocks |
| 564 | */ |
| 565 | |
| 566 | int |
| 567 | xmlMemBlocks(void) { |
| 568 | return(debugMemBlocks); |
| 569 | } |
| 570 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 571 | #ifdef MEM_LIST |
| 572 | /** |
| 573 | * xmlMemContentShow: |
| 574 | * @fp: a FILE descriptor used as the output file |
| 575 | * @p: a memory block header |
| 576 | * |
| 577 | * tries to show some content from the memory block |
| 578 | */ |
| 579 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 580 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 581 | xmlMemContentShow(FILE *fp, MEMHDR *p) |
| 582 | { |
Gaurav | 7966a76 | 2014-05-09 17:00:08 +0800 | [diff] [blame] | 583 | int i,j,k,len; |
| 584 | const char *buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 585 | |
| 586 | if (p == NULL) { |
| 587 | fprintf(fp, " NULL"); |
| 588 | return; |
| 589 | } |
Gaurav | 7966a76 | 2014-05-09 17:00:08 +0800 | [diff] [blame] | 590 | len = p->mh_size; |
| 591 | buf = (const char *) HDR_2_CLIENT(p); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 592 | |
| 593 | for (i = 0;i < len;i++) { |
| 594 | if (buf[i] == 0) break; |
Daniel Veillard | 9f28f30 | 2002-02-15 20:48:08 +0000 | [diff] [blame] | 595 | if (!isprint((unsigned char) buf[i])) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 596 | } |
| 597 | if ((i < 4) && ((buf[i] != 0) || (i == 0))) { |
| 598 | if (len >= 4) { |
| 599 | MEMHDR *q; |
| 600 | void *cur; |
| 601 | |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 602 | for (j = 0;(j < len -3) && (j < 40);j += 4) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 603 | cur = *((void **) &buf[j]); |
| 604 | q = CLIENT_2_HDR(cur); |
| 605 | p = memlist; |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 606 | k = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 607 | while (p != NULL) { |
| 608 | if (p == q) break; |
| 609 | p = p->mh_next; |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 610 | if (k++ > 100) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 611 | } |
| 612 | if ((p != NULL) && (p == q)) { |
| 613 | fprintf(fp, " pointer to #%lu at index %d", |
| 614 | p->mh_number, j); |
| 615 | return; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } else if ((i == 0) && (buf[i] == 0)) { |
| 620 | fprintf(fp," null"); |
| 621 | } else { |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 622 | if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 623 | else { |
| 624 | fprintf(fp," ["); |
| 625 | for (j = 0;j < i;j++) |
| 626 | fprintf(fp,"%c", buf[j]); |
| 627 | fprintf(fp,"]"); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | #endif |
| 632 | |
| 633 | /** |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 634 | * xmlMemDisplayLast: |
| 635 | * @fp: a FILE descriptor used as the output file, if NULL, the result is |
| 636 | * written to the file .memorylist |
| 637 | * @nbBytes: the amount of memory to dump |
| 638 | * |
| 639 | * the last nbBytes of memory allocated and not freed, useful for dumping |
| 640 | * the memory left allocated between two places at runtime. |
| 641 | */ |
| 642 | |
| 643 | void |
| 644 | xmlMemDisplayLast(FILE *fp, long nbBytes) |
| 645 | { |
| 646 | #ifdef MEM_LIST |
| 647 | MEMHDR *p; |
| 648 | unsigned idx; |
| 649 | int nb = 0; |
| 650 | #endif |
| 651 | FILE *old_fp = fp; |
| 652 | |
| 653 | if (nbBytes <= 0) |
| 654 | return; |
| 655 | |
| 656 | if (fp == NULL) { |
| 657 | fp = fopen(".memorylist", "w"); |
| 658 | if (fp == NULL) |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | #ifdef MEM_LIST |
| 663 | fprintf(fp," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 664 | nbBytes, debugMemSize, debugMaxMemSize); |
| 665 | fprintf(fp,"BLOCK NUMBER SIZE TYPE\n"); |
| 666 | idx = 0; |
| 667 | xmlMutexLock(xmlMemMutex); |
| 668 | p = memlist; |
| 669 | while ((p) && (nbBytes > 0)) { |
| 670 | fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number, |
| 671 | (unsigned long)p->mh_size); |
| 672 | switch (p->mh_type) { |
| 673 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 674 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
| 675 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
| 676 | case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break; |
| 677 | case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break; |
| 678 | default: |
| 679 | fprintf(fp,"Unknown memory block, may be corrupted"); |
| 680 | xmlMutexUnlock(xmlMemMutex); |
| 681 | if (old_fp == NULL) |
| 682 | fclose(fp); |
| 683 | return; |
| 684 | } |
| 685 | if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line); |
| 686 | if (p->mh_tag != MEMTAG) |
| 687 | fprintf(fp," INVALID"); |
| 688 | nb++; |
| 689 | if (nb < 100) |
| 690 | xmlMemContentShow(fp, p); |
| 691 | else |
| 692 | fprintf(fp," skip"); |
| 693 | |
| 694 | fprintf(fp,"\n"); |
| 695 | nbBytes -= (unsigned long)p->mh_size; |
| 696 | p = p->mh_next; |
| 697 | } |
| 698 | xmlMutexUnlock(xmlMemMutex); |
| 699 | #else |
| 700 | fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n"); |
| 701 | #endif |
| 702 | if (old_fp == NULL) |
| 703 | fclose(fp); |
| 704 | } |
| 705 | |
| 706 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 707 | * xmlMemDisplay: |
| 708 | * @fp: a FILE descriptor used as the output file, if NULL, the result is |
| 709 | * written to the file .memorylist |
| 710 | * |
| 711 | * show in-extenso the memory blocks allocated |
| 712 | */ |
| 713 | |
| 714 | void |
| 715 | xmlMemDisplay(FILE *fp) |
| 716 | { |
| 717 | #ifdef MEM_LIST |
| 718 | MEMHDR *p; |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 719 | unsigned idx; |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 720 | int nb = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 721 | #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME) |
| 722 | time_t currentTime; |
| 723 | char buf[500]; |
| 724 | struct tm * tstruct; |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 725 | #endif |
| 726 | #endif |
| 727 | FILE *old_fp = fp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 728 | |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 729 | if (fp == NULL) { |
| 730 | fp = fopen(".memorylist", "w"); |
| 731 | if (fp == NULL) |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | #ifdef MEM_LIST |
| 736 | #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 737 | currentTime = time(NULL); |
| 738 | tstruct = localtime(¤tTime); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 739 | strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 740 | fprintf(fp," %s\n\n", buf); |
| 741 | #endif |
| 742 | |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 743 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 744 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 745 | debugMemSize, debugMaxMemSize); |
| 746 | fprintf(fp,"BLOCK NUMBER SIZE TYPE\n"); |
| 747 | idx = 0; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 748 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 749 | p = memlist; |
| 750 | while (p) { |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 751 | fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number, |
| 752 | (unsigned long)p->mh_size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 753 | switch (p->mh_type) { |
| 754 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 755 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 756 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 757 | case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 758 | case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break; |
| 759 | default: |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 760 | fprintf(fp,"Unknown memory block, may be corrupted"); |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 761 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 762 | if (old_fp == NULL) |
| 763 | fclose(fp); |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 764 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 765 | } |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 766 | if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 767 | if (p->mh_tag != MEMTAG) |
| 768 | fprintf(fp," INVALID"); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 769 | nb++; |
| 770 | if (nb < 100) |
| 771 | xmlMemContentShow(fp, p); |
| 772 | else |
| 773 | fprintf(fp," skip"); |
| 774 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 775 | fprintf(fp,"\n"); |
| 776 | p = p->mh_next; |
| 777 | } |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 778 | xmlMutexUnlock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 779 | #else |
| 780 | fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n"); |
| 781 | #endif |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 782 | if (old_fp == NULL) |
| 783 | fclose(fp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | #ifdef MEM_LIST |
| 787 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 788 | static void debugmem_list_add(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 789 | { |
| 790 | p->mh_next = memlist; |
| 791 | p->mh_prev = NULL; |
| 792 | if (memlist) memlist->mh_prev = p; |
| 793 | memlist = p; |
| 794 | #ifdef MEM_LIST_DEBUG |
| 795 | if (stderr) |
| 796 | Mem_Display(stderr); |
| 797 | #endif |
| 798 | } |
| 799 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 800 | static void debugmem_list_delete(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 801 | { |
| 802 | if (p->mh_next) |
| 803 | p->mh_next->mh_prev = p->mh_prev; |
| 804 | if (p->mh_prev) |
| 805 | p->mh_prev->mh_next = p->mh_next; |
| 806 | else memlist = p->mh_next; |
| 807 | #ifdef MEM_LIST_DEBUG |
| 808 | if (stderr) |
| 809 | Mem_Display(stderr); |
| 810 | #endif |
| 811 | } |
| 812 | |
| 813 | #endif |
| 814 | |
| 815 | /* |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 816 | * debugmem_tag_error: |
| 817 | * |
| 818 | * internal error function. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 819 | */ |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 820 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 821 | static void debugmem_tag_error(void *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 822 | { |
| 823 | xmlGenericError(xmlGenericErrorContext, |
| 824 | "Memory tag error occurs :%p \n\t bye\n", p); |
| 825 | #ifdef MEM_LIST |
| 826 | if (stderr) |
| 827 | xmlMemDisplay(stderr); |
| 828 | #endif |
| 829 | } |
| 830 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 831 | #ifdef MEM_LIST |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 832 | static FILE *xmlMemoryDumpFile = NULL; |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 833 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 834 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 835 | /** |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 836 | * xmlMemShow: |
| 837 | * @fp: a FILE descriptor used as the output file |
| 838 | * @nr: number of entries to dump |
| 839 | * |
| 840 | * show a show display of the memory allocated, and dump |
| 841 | * the @nr last allocated areas which were not freed |
| 842 | */ |
| 843 | |
| 844 | void |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 845 | xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED) |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 846 | { |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 847 | #ifdef MEM_LIST |
| 848 | MEMHDR *p; |
| 849 | #endif |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 850 | |
| 851 | if (fp != NULL) |
| 852 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 853 | debugMemSize, debugMaxMemSize); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 854 | #ifdef MEM_LIST |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 855 | xmlMutexLock(xmlMemMutex); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 856 | if (nr > 0) { |
| 857 | fprintf(fp,"NUMBER SIZE TYPE WHERE\n"); |
| 858 | p = memlist; |
| 859 | while ((p) && nr > 0) { |
| 860 | fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size); |
| 861 | switch (p->mh_type) { |
| 862 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 863 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
| 864 | case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break; |
| 865 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
| 866 | case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break; |
| 867 | default:fprintf(fp," ??? in ");break; |
| 868 | } |
| 869 | if (p->mh_file != NULL) |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 870 | fprintf(fp,"%s(%u)", p->mh_file, p->mh_line); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 871 | if (p->mh_tag != MEMTAG) |
| 872 | fprintf(fp," INVALID"); |
| 873 | xmlMemContentShow(fp, p); |
| 874 | fprintf(fp,"\n"); |
| 875 | nr--; |
| 876 | p = p->mh_next; |
| 877 | } |
| 878 | } |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 879 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 880 | #endif /* MEM_LIST */ |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 884 | * xmlMemoryDump: |
| 885 | * |
| 886 | * Dump in-extenso the memory blocks allocated to the file .memorylist |
| 887 | */ |
| 888 | |
| 889 | void |
| 890 | xmlMemoryDump(void) |
| 891 | { |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 892 | #ifdef MEM_LIST |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 893 | FILE *dump; |
| 894 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 895 | if (debugMaxMemSize == 0) |
| 896 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 897 | dump = fopen(".memdump", "w"); |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 898 | if (dump == NULL) |
| 899 | xmlMemoryDumpFile = stderr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | else xmlMemoryDumpFile = dump; |
| 901 | |
| 902 | xmlMemDisplay(xmlMemoryDumpFile); |
| 903 | |
| 904 | if (dump != NULL) fclose(dump); |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 905 | #endif /* MEM_LIST */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| 909 | /**************************************************************** |
| 910 | * * |
| 911 | * Initialization Routines * |
| 912 | * * |
| 913 | ****************************************************************/ |
| 914 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 915 | /** |
| 916 | * xmlInitMemory: |
| 917 | * |
| 918 | * Initialize the memory layer. |
| 919 | * |
| 920 | * Returns 0 on success |
| 921 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 922 | int |
| 923 | xmlInitMemory(void) |
| 924 | { |
Daniel Veillard | e15df58 | 2004-07-13 15:25:08 +0000 | [diff] [blame] | 925 | #ifdef HAVE_STDLIB_H |
| 926 | char *breakpoint; |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 927 | #endif |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 928 | #ifdef DEBUG_MEMORY |
| 929 | xmlGenericError(xmlGenericErrorContext, |
| 930 | "xmlInitMemory()\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 931 | #endif |
William M. Brack | 9202942 | 2004-01-04 01:01:14 +0000 | [diff] [blame] | 932 | /* |
| 933 | This is really not good code (see Bug 130419). Suggestions for |
| 934 | improvement will be welcome! |
| 935 | */ |
| 936 | if (xmlMemInitialized) return(-1); |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 937 | xmlMemInitialized = 1; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 938 | xmlMemMutex = xmlNewMutex(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 939 | |
| 940 | #ifdef HAVE_STDLIB_H |
| 941 | breakpoint = getenv("XML_MEM_BREAKPOINT"); |
| 942 | if (breakpoint != NULL) { |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 943 | sscanf(breakpoint, "%ud", &xmlMemStopAtBlock); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 944 | } |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 945 | #endif |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 946 | #ifdef HAVE_STDLIB_H |
| 947 | breakpoint = getenv("XML_MEM_TRACE"); |
| 948 | if (breakpoint != NULL) { |
| 949 | sscanf(breakpoint, "%p", &xmlMemTraceBlockAt); |
| 950 | } |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 951 | #endif |
| 952 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 953 | #ifdef DEBUG_MEMORY |
| 954 | xmlGenericError(xmlGenericErrorContext, |
| 955 | "xmlInitMemory() Ok\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 956 | #endif |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 957 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /** |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 961 | * xmlCleanupMemory: |
| 962 | * |
Daniel Veillard | 91b955c | 2004-12-10 10:26:42 +0000 | [diff] [blame] | 963 | * Free up all the memory allocated by the library for its own |
| 964 | * use. This should not be called by user level code. |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 965 | */ |
| 966 | void |
| 967 | xmlCleanupMemory(void) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 968 | #ifdef DEBUG_MEMORY |
| 969 | xmlGenericError(xmlGenericErrorContext, |
| 970 | "xmlCleanupMemory()\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 971 | #endif |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 972 | if (xmlMemInitialized == 0) |
| 973 | return; |
| 974 | |
| 975 | xmlFreeMutex(xmlMemMutex); |
Daniel Veillard | 1a9b708 | 2004-01-02 10:42:01 +0000 | [diff] [blame] | 976 | xmlMemMutex = NULL; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 977 | xmlMemInitialized = 0; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 978 | #ifdef DEBUG_MEMORY |
| 979 | xmlGenericError(xmlGenericErrorContext, |
| 980 | "xmlCleanupMemory() Ok\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 981 | #endif |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 985 | * xmlMemSetup: |
| 986 | * @freeFunc: the free() function to use |
| 987 | * @mallocFunc: the malloc() function to use |
| 988 | * @reallocFunc: the realloc() function to use |
| 989 | * @strdupFunc: the strdup() function to use |
| 990 | * |
| 991 | * Override the default memory access functions with a new set |
| 992 | * This has to be called before any other libxml routines ! |
| 993 | * |
| 994 | * Should this be blocked if there was already some allocations |
| 995 | * done ? |
| 996 | * |
| 997 | * Returns 0 on success |
| 998 | */ |
| 999 | int |
| 1000 | xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, |
| 1001 | xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 1002 | #ifdef DEBUG_MEMORY |
| 1003 | xmlGenericError(xmlGenericErrorContext, |
| 1004 | "xmlMemSetup()\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 1005 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1006 | if (freeFunc == NULL) |
| 1007 | return(-1); |
| 1008 | if (mallocFunc == NULL) |
| 1009 | return(-1); |
| 1010 | if (reallocFunc == NULL) |
| 1011 | return(-1); |
| 1012 | if (strdupFunc == NULL) |
| 1013 | return(-1); |
| 1014 | xmlFree = freeFunc; |
| 1015 | xmlMalloc = mallocFunc; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1016 | xmlMallocAtomic = mallocFunc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1017 | xmlRealloc = reallocFunc; |
| 1018 | xmlMemStrdup = strdupFunc; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 1019 | #ifdef DEBUG_MEMORY |
| 1020 | xmlGenericError(xmlGenericErrorContext, |
| 1021 | "xmlMemSetup() Ok\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 1022 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1023 | return(0); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * xmlMemGet: |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1028 | * @freeFunc: place to save the free() function in use |
| 1029 | * @mallocFunc: place to save the malloc() function in use |
| 1030 | * @reallocFunc: place to save the realloc() function in use |
| 1031 | * @strdupFunc: place to save the strdup() function in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1032 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1033 | * Provides the memory access functions set currently in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1034 | * |
| 1035 | * Returns 0 on success |
| 1036 | */ |
| 1037 | int |
| 1038 | xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc, |
| 1039 | xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) { |
| 1040 | if (freeFunc != NULL) *freeFunc = xmlFree; |
| 1041 | if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 1042 | if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 1043 | if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 1044 | return(0); |
| 1045 | } |
| 1046 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1047 | /** |
| 1048 | * xmlGcMemSetup: |
| 1049 | * @freeFunc: the free() function to use |
| 1050 | * @mallocFunc: the malloc() function to use |
| 1051 | * @mallocAtomicFunc: the malloc() function to use for atomic allocations |
| 1052 | * @reallocFunc: the realloc() function to use |
| 1053 | * @strdupFunc: the strdup() function to use |
| 1054 | * |
| 1055 | * Override the default memory access functions with a new set |
| 1056 | * This has to be called before any other libxml routines ! |
| 1057 | * The mallocAtomicFunc is specialized for atomic block |
| 1058 | * allocations (i.e. of areas useful for garbage collected memory allocators |
| 1059 | * |
| 1060 | * Should this be blocked if there was already some allocations |
| 1061 | * done ? |
| 1062 | * |
| 1063 | * Returns 0 on success |
| 1064 | */ |
| 1065 | int |
| 1066 | xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, |
| 1067 | xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc, |
| 1068 | xmlStrdupFunc strdupFunc) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 1069 | #ifdef DEBUG_MEMORY |
| 1070 | xmlGenericError(xmlGenericErrorContext, |
| 1071 | "xmlGcMemSetup()\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 1072 | #endif |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1073 | if (freeFunc == NULL) |
| 1074 | return(-1); |
| 1075 | if (mallocFunc == NULL) |
| 1076 | return(-1); |
| 1077 | if (mallocAtomicFunc == NULL) |
| 1078 | return(-1); |
| 1079 | if (reallocFunc == NULL) |
| 1080 | return(-1); |
| 1081 | if (strdupFunc == NULL) |
| 1082 | return(-1); |
| 1083 | xmlFree = freeFunc; |
| 1084 | xmlMalloc = mallocFunc; |
| 1085 | xmlMallocAtomic = mallocAtomicFunc; |
| 1086 | xmlRealloc = reallocFunc; |
| 1087 | xmlMemStrdup = strdupFunc; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 1088 | #ifdef DEBUG_MEMORY |
| 1089 | xmlGenericError(xmlGenericErrorContext, |
| 1090 | "xmlGcMemSetup() Ok\n"); |
Daniel Veillard | 09459bf | 2008-07-30 12:58:11 +0000 | [diff] [blame] | 1091 | #endif |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1092 | return(0); |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * xmlGcMemGet: |
| 1097 | * @freeFunc: place to save the free() function in use |
| 1098 | * @mallocFunc: place to save the malloc() function in use |
| 1099 | * @mallocAtomicFunc: place to save the atomic malloc() function in use |
| 1100 | * @reallocFunc: place to save the realloc() function in use |
| 1101 | * @strdupFunc: place to save the strdup() function in use |
| 1102 | * |
| 1103 | * Provides the memory access functions set currently in use |
| 1104 | * The mallocAtomicFunc is specialized for atomic block |
| 1105 | * allocations (i.e. of areas useful for garbage collected memory allocators |
| 1106 | * |
| 1107 | * Returns 0 on success |
| 1108 | */ |
| 1109 | int |
| 1110 | xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc, |
| 1111 | xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc, |
| 1112 | xmlStrdupFunc *strdupFunc) { |
| 1113 | if (freeFunc != NULL) *freeFunc = xmlFree; |
| 1114 | if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 1115 | if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; |
| 1116 | if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 1117 | if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 1118 | return(0); |
| 1119 | } |
| 1120 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1121 | #define bottom_xmlmemory |
| 1122 | #include "elfgcchack.h" |