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 | * |
| 37 | * keep track of all allocated blocks for error reporting |
| 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 | * * |
| 61 | * Macros, variables and associated types * |
| 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; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 165 | |
| 166 | if (!xmlMemInitialized) xmlInitMemory(); |
| 167 | #ifdef DEBUG_MEMORY |
| 168 | xmlGenericError(xmlGenericErrorContext, |
| 169 | "Malloc(%d)\n",size); |
| 170 | #endif |
| 171 | |
| 172 | TEST_POINT |
| 173 | |
| 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); |
| 181 | } |
| 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); |
| 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 |
| 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, |
| 208 | "%p : Malloc(%d) Ok\n", xmlMemTraceBlockAt, size); |
| 209 | xmlMallocBreakpoint(); |
| 210 | } |
| 211 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 212 | TEST_POINT |
| 213 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 214 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 218 | * xmlMallocAtomicLoc: |
| 219 | * @size: an int specifying the size in byte to allocate. |
| 220 | * @file: the file name or NULL |
| 221 | * @line: the line number |
| 222 | * |
| 223 | * a malloc() equivalent, with logging of the allocation info. |
| 224 | * |
| 225 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 226 | */ |
| 227 | |
| 228 | void * |
| 229 | xmlMallocAtomicLoc(size_t size, const char * file, int line) |
| 230 | { |
| 231 | MEMHDR *p; |
| 232 | void *ret; |
| 233 | |
| 234 | if (!xmlMemInitialized) xmlInitMemory(); |
| 235 | #ifdef DEBUG_MEMORY |
| 236 | xmlGenericError(xmlGenericErrorContext, |
| 237 | "Malloc(%d)\n",size); |
| 238 | #endif |
| 239 | |
| 240 | TEST_POINT |
| 241 | |
| 242 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 243 | |
| 244 | if (!p) { |
| 245 | xmlGenericError(xmlGenericErrorContext, |
| 246 | "xmlMallocLoc : Out of free space\n"); |
| 247 | xmlMemoryDump(); |
| 248 | return(NULL); |
| 249 | } |
| 250 | p->mh_tag = MEMTAG; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 251 | p->mh_size = size; |
| 252 | p->mh_type = MALLOC_ATOMIC_TYPE; |
| 253 | p->mh_file = file; |
| 254 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 255 | xmlMutexLock(xmlMemMutex); |
| 256 | p->mh_number = ++block; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 257 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 258 | debugMemBlocks++; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 259 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 260 | #ifdef MEM_LIST |
| 261 | debugmem_list_add(p); |
| 262 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 263 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 264 | |
| 265 | #ifdef DEBUG_MEMORY |
| 266 | xmlGenericError(xmlGenericErrorContext, |
| 267 | "Malloc(%d) Ok\n",size); |
| 268 | #endif |
| 269 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 270 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 271 | |
| 272 | ret = HDR_2_CLIENT(p); |
| 273 | |
| 274 | if (xmlMemTraceBlockAt == ret) { |
| 275 | xmlGenericError(xmlGenericErrorContext, |
| 276 | "%p : Malloc(%d) Ok\n", xmlMemTraceBlockAt, size); |
| 277 | xmlMallocBreakpoint(); |
| 278 | } |
| 279 | |
| 280 | TEST_POINT |
| 281 | |
| 282 | return(ret); |
| 283 | } |
| 284 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 285 | * xmlMemMalloc: |
| 286 | * @size: an int specifying the size in byte to allocate. |
| 287 | * |
| 288 | * a malloc() equivalent, with logging of the allocation info. |
| 289 | * |
| 290 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 291 | */ |
| 292 | |
| 293 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 294 | xmlMemMalloc(size_t size) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 295 | { |
| 296 | return(xmlMallocLoc(size, "none", 0)); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * xmlReallocLoc: |
| 301 | * @ptr: the initial memory block pointer |
| 302 | * @size: an int specifying the size in byte to allocate. |
| 303 | * @file: the file name or NULL |
| 304 | * @line: the line number |
| 305 | * |
| 306 | * a realloc() equivalent, with logging of the allocation info. |
| 307 | * |
| 308 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 309 | */ |
| 310 | |
| 311 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 312 | xmlReallocLoc(void *ptr,size_t size, const char * file, int line) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 313 | { |
| 314 | MEMHDR *p; |
| 315 | unsigned long number; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 316 | #ifdef DEBUG_MEMORY |
| 317 | size_t oldsize; |
| 318 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 319 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 320 | if (ptr == NULL) |
Aleksey Sanin | e9f0811 | 2004-01-22 22:20:31 +0000 | [diff] [blame] | 321 | return(xmlMallocLoc(size, file, line)); |
| 322 | |
| 323 | if (!xmlMemInitialized) xmlInitMemory(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 324 | TEST_POINT |
| 325 | |
| 326 | p = CLIENT_2_HDR(ptr); |
| 327 | number = p->mh_number; |
Daniel Veillard | 18ffe20 | 2005-04-14 17:50:59 +0000 | [diff] [blame] | 328 | if (xmlMemStopAtBlock == number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 329 | if (p->mh_tag != MEMTAG) { |
| 330 | Mem_Tag_Err(p); |
| 331 | goto error; |
| 332 | } |
| 333 | p->mh_tag = ~MEMTAG; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 334 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 335 | debugMemSize -= p->mh_size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 336 | debugMemBlocks--; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 337 | #ifdef DEBUG_MEMORY |
| 338 | oldsize = p->mh_size; |
| 339 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 340 | #ifdef MEM_LIST |
| 341 | debugmem_list_delete(p); |
| 342 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 343 | xmlMutexUnlock(xmlMemMutex); |
| 344 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 345 | p = (MEMHDR *) realloc(p,RESERVE_SIZE+size); |
| 346 | if (!p) { |
| 347 | goto error; |
| 348 | } |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 349 | if (xmlMemTraceBlockAt == ptr) { |
| 350 | xmlGenericError(xmlGenericErrorContext, |
| 351 | "%p : Realloced(%d -> %d) Ok\n", |
| 352 | xmlMemTraceBlockAt, p->mh_size, size); |
| 353 | xmlMallocBreakpoint(); |
| 354 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 355 | p->mh_tag = MEMTAG; |
| 356 | p->mh_number = number; |
| 357 | p->mh_type = REALLOC_TYPE; |
| 358 | p->mh_size = size; |
| 359 | p->mh_file = file; |
| 360 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 361 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 362 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 363 | debugMemBlocks++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 364 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 365 | #ifdef MEM_LIST |
| 366 | debugmem_list_add(p); |
| 367 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 368 | xmlMutexUnlock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 369 | |
| 370 | TEST_POINT |
| 371 | |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 372 | #ifdef DEBUG_MEMORY |
| 373 | xmlGenericError(xmlGenericErrorContext, |
| 374 | "Realloced(%d to %d) Ok\n", oldsize, size); |
| 375 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 376 | return(HDR_2_CLIENT(p)); |
| 377 | |
| 378 | error: |
| 379 | return(NULL); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * xmlMemRealloc: |
| 384 | * @ptr: the initial memory block pointer |
| 385 | * @size: an int specifying the size in byte to allocate. |
| 386 | * |
| 387 | * a realloc() equivalent, with logging of the allocation info. |
| 388 | * |
| 389 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 390 | */ |
| 391 | |
| 392 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 393 | xmlMemRealloc(void *ptr,size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 394 | return(xmlReallocLoc(ptr, size, "none", 0)); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * xmlMemFree: |
| 399 | * @ptr: the memory block pointer |
| 400 | * |
| 401 | * a free() equivalent, with error checking. |
| 402 | */ |
| 403 | void |
| 404 | xmlMemFree(void *ptr) |
| 405 | { |
| 406 | MEMHDR *p; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 407 | char *target; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 408 | #ifdef DEBUG_MEMORY |
| 409 | size_t size; |
| 410 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 411 | |
Daniel Veillard | 2a512da | 2007-10-30 20:24:40 +0000 | [diff] [blame^] | 412 | if (ptr == NULL) |
| 413 | return; |
| 414 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 415 | if (ptr == (void *) -1) { |
| 416 | xmlGenericError(xmlGenericErrorContext, |
| 417 | "trying to free pointer from freed area\n"); |
| 418 | goto error; |
| 419 | } |
| 420 | |
| 421 | if (xmlMemTraceBlockAt == ptr) { |
| 422 | xmlGenericError(xmlGenericErrorContext, |
| 423 | "%p : Freed()\n", xmlMemTraceBlockAt); |
| 424 | xmlMallocBreakpoint(); |
| 425 | } |
| 426 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | TEST_POINT |
| 428 | |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 429 | target = (char *) ptr; |
| 430 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 431 | p = CLIENT_2_HDR(ptr); |
| 432 | if (p->mh_tag != MEMTAG) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 433 | Mem_Tag_Err(p); |
| 434 | goto error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 435 | } |
Daniel Veillard | 18ffe20 | 2005-04-14 17:50:59 +0000 | [diff] [blame] | 436 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 437 | p->mh_tag = ~MEMTAG; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 438 | memset(target, -1, p->mh_size); |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 439 | xmlMutexLock(xmlMemMutex); |
| 440 | debugMemSize -= p->mh_size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 441 | debugMemBlocks--; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 442 | #ifdef DEBUG_MEMORY |
| 443 | size = p->mh_size; |
| 444 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 445 | #ifdef MEM_LIST |
| 446 | debugmem_list_delete(p); |
| 447 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 448 | xmlMutexUnlock(xmlMemMutex); |
| 449 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 450 | free(p); |
| 451 | |
| 452 | TEST_POINT |
| 453 | |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 454 | #ifdef DEBUG_MEMORY |
| 455 | xmlGenericError(xmlGenericErrorContext, |
| 456 | "Freed(%d) Ok\n", size); |
| 457 | #endif |
| 458 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 459 | return; |
| 460 | |
| 461 | error: |
| 462 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 463 | "xmlMemFree(%lX) error\n", (unsigned long) ptr); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 464 | xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * xmlMemStrdupLoc: |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 470 | * @str: the initial string pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 471 | * @file: the file name or NULL |
| 472 | * @line: the line number |
| 473 | * |
| 474 | * a strdup() equivalent, with logging of the allocation info. |
| 475 | * |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 476 | * 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] | 477 | */ |
| 478 | |
| 479 | char * |
| 480 | xmlMemStrdupLoc(const char *str, const char *file, int line) |
| 481 | { |
| 482 | char *s; |
| 483 | size_t size = strlen(str) + 1; |
| 484 | MEMHDR *p; |
| 485 | |
| 486 | if (!xmlMemInitialized) xmlInitMemory(); |
| 487 | TEST_POINT |
| 488 | |
| 489 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 490 | if (!p) { |
| 491 | goto error; |
| 492 | } |
| 493 | p->mh_tag = MEMTAG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 494 | p->mh_size = size; |
| 495 | p->mh_type = STRDUP_TYPE; |
| 496 | p->mh_file = file; |
| 497 | p->mh_line = line; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 498 | xmlMutexLock(xmlMemMutex); |
| 499 | p->mh_number = ++block; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 500 | debugMemSize += size; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 501 | debugMemBlocks++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 502 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 503 | #ifdef MEM_LIST |
| 504 | debugmem_list_add(p); |
| 505 | #endif |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 506 | xmlMutexUnlock(xmlMemMutex); |
| 507 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 508 | s = (char *) HDR_2_CLIENT(p); |
| 509 | |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 510 | if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 511 | |
| 512 | if (s != NULL) |
| 513 | strcpy(s,str); |
| 514 | else |
| 515 | goto error; |
| 516 | |
| 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 | { |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 583 | int i,j,k,len = p->mh_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 584 | const char *buf = (const char *) HDR_2_CLIENT(p); |
| 585 | |
| 586 | if (p == NULL) { |
| 587 | fprintf(fp, " NULL"); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | for (i = 0;i < len;i++) { |
| 592 | if (buf[i] == 0) break; |
Daniel Veillard | 9f28f30 | 2002-02-15 20:48:08 +0000 | [diff] [blame] | 593 | if (!isprint((unsigned char) buf[i])) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 594 | } |
| 595 | if ((i < 4) && ((buf[i] != 0) || (i == 0))) { |
| 596 | if (len >= 4) { |
| 597 | MEMHDR *q; |
| 598 | void *cur; |
| 599 | |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 600 | for (j = 0;(j < len -3) && (j < 40);j += 4) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 601 | cur = *((void **) &buf[j]); |
| 602 | q = CLIENT_2_HDR(cur); |
| 603 | p = memlist; |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 604 | k = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 605 | while (p != NULL) { |
| 606 | if (p == q) break; |
| 607 | p = p->mh_next; |
Daniel Veillard | f4721d6 | 2006-10-11 21:12:10 +0000 | [diff] [blame] | 608 | if (k++ > 100) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 609 | } |
| 610 | if ((p != NULL) && (p == q)) { |
| 611 | fprintf(fp, " pointer to #%lu at index %d", |
| 612 | p->mh_number, j); |
| 613 | return; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } else if ((i == 0) && (buf[i] == 0)) { |
| 618 | fprintf(fp," null"); |
| 619 | } else { |
| 620 | if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf); |
| 621 | else { |
| 622 | fprintf(fp," ["); |
| 623 | for (j = 0;j < i;j++) |
| 624 | fprintf(fp,"%c", buf[j]); |
| 625 | fprintf(fp,"]"); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | #endif |
| 630 | |
| 631 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 632 | * xmlMemDisplay: |
| 633 | * @fp: a FILE descriptor used as the output file, if NULL, the result is |
| 634 | * written to the file .memorylist |
| 635 | * |
| 636 | * show in-extenso the memory blocks allocated |
| 637 | */ |
| 638 | |
| 639 | void |
| 640 | xmlMemDisplay(FILE *fp) |
| 641 | { |
| 642 | #ifdef MEM_LIST |
| 643 | MEMHDR *p; |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 644 | unsigned idx; |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 645 | int nb = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 646 | #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME) |
| 647 | time_t currentTime; |
| 648 | char buf[500]; |
| 649 | struct tm * tstruct; |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 650 | #endif |
| 651 | #endif |
| 652 | FILE *old_fp = fp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 653 | |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 654 | if (fp == NULL) { |
| 655 | fp = fopen(".memorylist", "w"); |
| 656 | if (fp == NULL) |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | #ifdef MEM_LIST |
| 661 | #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 662 | currentTime = time(NULL); |
| 663 | tstruct = localtime(¤tTime); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 664 | strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 665 | fprintf(fp," %s\n\n", buf); |
| 666 | #endif |
| 667 | |
| 668 | |
| 669 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 670 | debugMemSize, debugMaxMemSize); |
| 671 | fprintf(fp,"BLOCK NUMBER SIZE TYPE\n"); |
| 672 | idx = 0; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 673 | xmlMutexLock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 674 | p = memlist; |
| 675 | while (p) { |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 676 | fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number, |
| 677 | (unsigned long)p->mh_size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 678 | switch (p->mh_type) { |
| 679 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 680 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 681 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 682 | case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break; |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 683 | case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break; |
| 684 | default: |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 685 | fprintf(fp,"Unknown memory block, may be corrupted"); |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 686 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 687 | if (old_fp == NULL) |
| 688 | fclose(fp); |
Daniel Veillard | 529233c | 2004-07-02 12:23:44 +0000 | [diff] [blame] | 689 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 690 | } |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 691 | 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] | 692 | if (p->mh_tag != MEMTAG) |
| 693 | fprintf(fp," INVALID"); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 694 | nb++; |
| 695 | if (nb < 100) |
| 696 | xmlMemContentShow(fp, p); |
| 697 | else |
| 698 | fprintf(fp," skip"); |
| 699 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 700 | fprintf(fp,"\n"); |
| 701 | p = p->mh_next; |
| 702 | } |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 703 | xmlMutexUnlock(xmlMemMutex); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 704 | #else |
| 705 | fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n"); |
| 706 | #endif |
Daniel Veillard | 942d6c7 | 2005-05-08 11:39:56 +0000 | [diff] [blame] | 707 | if (old_fp == NULL) |
| 708 | fclose(fp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | #ifdef MEM_LIST |
| 712 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 713 | static void debugmem_list_add(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 714 | { |
| 715 | p->mh_next = memlist; |
| 716 | p->mh_prev = NULL; |
| 717 | if (memlist) memlist->mh_prev = p; |
| 718 | memlist = p; |
| 719 | #ifdef MEM_LIST_DEBUG |
| 720 | if (stderr) |
| 721 | Mem_Display(stderr); |
| 722 | #endif |
| 723 | } |
| 724 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 725 | static void debugmem_list_delete(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 726 | { |
| 727 | if (p->mh_next) |
| 728 | p->mh_next->mh_prev = p->mh_prev; |
| 729 | if (p->mh_prev) |
| 730 | p->mh_prev->mh_next = p->mh_next; |
| 731 | else memlist = p->mh_next; |
| 732 | #ifdef MEM_LIST_DEBUG |
| 733 | if (stderr) |
| 734 | Mem_Display(stderr); |
| 735 | #endif |
| 736 | } |
| 737 | |
| 738 | #endif |
| 739 | |
| 740 | /* |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 741 | * debugmem_tag_error: |
| 742 | * |
| 743 | * internal error function. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 744 | */ |
| 745 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 746 | static void debugmem_tag_error(void *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 747 | { |
| 748 | xmlGenericError(xmlGenericErrorContext, |
| 749 | "Memory tag error occurs :%p \n\t bye\n", p); |
| 750 | #ifdef MEM_LIST |
| 751 | if (stderr) |
| 752 | xmlMemDisplay(stderr); |
| 753 | #endif |
| 754 | } |
| 755 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 756 | #ifdef MEM_LIST |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 757 | static FILE *xmlMemoryDumpFile = NULL; |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 758 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 759 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 760 | /** |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 761 | * xmlMemShow: |
| 762 | * @fp: a FILE descriptor used as the output file |
| 763 | * @nr: number of entries to dump |
| 764 | * |
| 765 | * show a show display of the memory allocated, and dump |
| 766 | * the @nr last allocated areas which were not freed |
| 767 | */ |
| 768 | |
| 769 | void |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 770 | xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED) |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 771 | { |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 772 | #ifdef MEM_LIST |
| 773 | MEMHDR *p; |
| 774 | #endif |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 775 | |
| 776 | if (fp != NULL) |
| 777 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 778 | debugMemSize, debugMaxMemSize); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 779 | #ifdef MEM_LIST |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 780 | xmlMutexLock(xmlMemMutex); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 781 | if (nr > 0) { |
| 782 | fprintf(fp,"NUMBER SIZE TYPE WHERE\n"); |
| 783 | p = memlist; |
| 784 | while ((p) && nr > 0) { |
| 785 | fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size); |
| 786 | switch (p->mh_type) { |
| 787 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 788 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
| 789 | case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break; |
| 790 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
| 791 | case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break; |
| 792 | default:fprintf(fp," ??? in ");break; |
| 793 | } |
| 794 | if (p->mh_file != NULL) |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 795 | fprintf(fp,"%s(%u)", p->mh_file, p->mh_line); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 796 | if (p->mh_tag != MEMTAG) |
| 797 | fprintf(fp," INVALID"); |
| 798 | xmlMemContentShow(fp, p); |
| 799 | fprintf(fp,"\n"); |
| 800 | nr--; |
| 801 | p = p->mh_next; |
| 802 | } |
| 803 | } |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 804 | xmlMutexUnlock(xmlMemMutex); |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 805 | #endif /* MEM_LIST */ |
Daniel Veillard | fb43bd6 | 2003-09-29 09:22:39 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 809 | * xmlMemoryDump: |
| 810 | * |
| 811 | * Dump in-extenso the memory blocks allocated to the file .memorylist |
| 812 | */ |
| 813 | |
| 814 | void |
| 815 | xmlMemoryDump(void) |
| 816 | { |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 817 | #ifdef MEM_LIST |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 818 | FILE *dump; |
| 819 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 820 | if (debugMaxMemSize == 0) |
| 821 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 822 | dump = fopen(".memdump", "w"); |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 823 | if (dump == NULL) |
| 824 | xmlMemoryDumpFile = stderr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 825 | else xmlMemoryDumpFile = dump; |
| 826 | |
| 827 | xmlMemDisplay(xmlMemoryDumpFile); |
| 828 | |
| 829 | if (dump != NULL) fclose(dump); |
Daniel Veillard | c064b47 | 2003-09-29 10:55:05 +0000 | [diff] [blame] | 830 | #endif /* MEM_LIST */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
| 834 | /**************************************************************** |
| 835 | * * |
| 836 | * Initialization Routines * |
| 837 | * * |
| 838 | ****************************************************************/ |
| 839 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 840 | /** |
| 841 | * xmlInitMemory: |
| 842 | * |
| 843 | * Initialize the memory layer. |
| 844 | * |
| 845 | * Returns 0 on success |
| 846 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 847 | int |
| 848 | xmlInitMemory(void) |
| 849 | { |
Daniel Veillard | e15df58 | 2004-07-13 15:25:08 +0000 | [diff] [blame] | 850 | #ifdef HAVE_STDLIB_H |
| 851 | char *breakpoint; |
| 852 | #endif |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 853 | #ifdef DEBUG_MEMORY |
| 854 | xmlGenericError(xmlGenericErrorContext, |
| 855 | "xmlInitMemory()\n"); |
| 856 | #endif |
William M. Brack | 9202942 | 2004-01-04 01:01:14 +0000 | [diff] [blame] | 857 | /* |
| 858 | This is really not good code (see Bug 130419). Suggestions for |
| 859 | improvement will be welcome! |
| 860 | */ |
| 861 | if (xmlMemInitialized) return(-1); |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 862 | xmlMemInitialized = 1; |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 863 | xmlMemMutex = xmlNewMutex(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 864 | |
| 865 | #ifdef HAVE_STDLIB_H |
| 866 | breakpoint = getenv("XML_MEM_BREAKPOINT"); |
| 867 | if (breakpoint != NULL) { |
William M. Brack | 0622fe8 | 2003-11-29 10:47:56 +0000 | [diff] [blame] | 868 | sscanf(breakpoint, "%ud", &xmlMemStopAtBlock); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 869 | } |
| 870 | #endif |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 871 | #ifdef HAVE_STDLIB_H |
| 872 | breakpoint = getenv("XML_MEM_TRACE"); |
| 873 | if (breakpoint != NULL) { |
| 874 | sscanf(breakpoint, "%p", &xmlMemTraceBlockAt); |
| 875 | } |
| 876 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 877 | |
| 878 | #ifdef DEBUG_MEMORY |
| 879 | xmlGenericError(xmlGenericErrorContext, |
| 880 | "xmlInitMemory() Ok\n"); |
| 881 | #endif |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 882 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 886 | * xmlCleanupMemory: |
| 887 | * |
Daniel Veillard | 91b955c | 2004-12-10 10:26:42 +0000 | [diff] [blame] | 888 | * Free up all the memory allocated by the library for its own |
| 889 | * use. This should not be called by user level code. |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 890 | */ |
| 891 | void |
| 892 | xmlCleanupMemory(void) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 893 | #ifdef DEBUG_MEMORY |
| 894 | xmlGenericError(xmlGenericErrorContext, |
| 895 | "xmlCleanupMemory()\n"); |
| 896 | #endif |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 897 | if (xmlMemInitialized == 0) |
| 898 | return; |
| 899 | |
| 900 | xmlFreeMutex(xmlMemMutex); |
Daniel Veillard | 1a9b708 | 2004-01-02 10:42:01 +0000 | [diff] [blame] | 901 | xmlMemMutex = NULL; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 902 | xmlMemInitialized = 0; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 903 | #ifdef DEBUG_MEMORY |
| 904 | xmlGenericError(xmlGenericErrorContext, |
| 905 | "xmlCleanupMemory() Ok\n"); |
| 906 | #endif |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 910 | * xmlMemSetup: |
| 911 | * @freeFunc: the free() function to use |
| 912 | * @mallocFunc: the malloc() function to use |
| 913 | * @reallocFunc: the realloc() function to use |
| 914 | * @strdupFunc: the strdup() function to use |
| 915 | * |
| 916 | * Override the default memory access functions with a new set |
| 917 | * This has to be called before any other libxml routines ! |
| 918 | * |
| 919 | * Should this be blocked if there was already some allocations |
| 920 | * done ? |
| 921 | * |
| 922 | * Returns 0 on success |
| 923 | */ |
| 924 | int |
| 925 | xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, |
| 926 | xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 927 | #ifdef DEBUG_MEMORY |
| 928 | xmlGenericError(xmlGenericErrorContext, |
| 929 | "xmlMemSetup()\n"); |
| 930 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 931 | if (freeFunc == NULL) |
| 932 | return(-1); |
| 933 | if (mallocFunc == NULL) |
| 934 | return(-1); |
| 935 | if (reallocFunc == NULL) |
| 936 | return(-1); |
| 937 | if (strdupFunc == NULL) |
| 938 | return(-1); |
| 939 | xmlFree = freeFunc; |
| 940 | xmlMalloc = mallocFunc; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 941 | xmlMallocAtomic = mallocFunc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 942 | xmlRealloc = reallocFunc; |
| 943 | xmlMemStrdup = strdupFunc; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 944 | #ifdef DEBUG_MEMORY |
| 945 | xmlGenericError(xmlGenericErrorContext, |
| 946 | "xmlMemSetup() Ok\n"); |
| 947 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 948 | return(0); |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * xmlMemGet: |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 953 | * @freeFunc: place to save the free() function in use |
| 954 | * @mallocFunc: place to save the malloc() function in use |
| 955 | * @reallocFunc: place to save the realloc() function in use |
| 956 | * @strdupFunc: place to save the strdup() function in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 957 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 958 | * Provides the memory access functions set currently in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 959 | * |
| 960 | * Returns 0 on success |
| 961 | */ |
| 962 | int |
| 963 | xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc, |
| 964 | xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) { |
| 965 | if (freeFunc != NULL) *freeFunc = xmlFree; |
| 966 | if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 967 | if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 968 | if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 969 | return(0); |
| 970 | } |
| 971 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 972 | /** |
| 973 | * xmlGcMemSetup: |
| 974 | * @freeFunc: the free() function to use |
| 975 | * @mallocFunc: the malloc() function to use |
| 976 | * @mallocAtomicFunc: the malloc() function to use for atomic allocations |
| 977 | * @reallocFunc: the realloc() function to use |
| 978 | * @strdupFunc: the strdup() function to use |
| 979 | * |
| 980 | * Override the default memory access functions with a new set |
| 981 | * This has to be called before any other libxml routines ! |
| 982 | * The mallocAtomicFunc is specialized for atomic block |
| 983 | * allocations (i.e. of areas useful for garbage collected memory allocators |
| 984 | * |
| 985 | * Should this be blocked if there was already some allocations |
| 986 | * done ? |
| 987 | * |
| 988 | * Returns 0 on success |
| 989 | */ |
| 990 | int |
| 991 | xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, |
| 992 | xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc, |
| 993 | xmlStrdupFunc strdupFunc) { |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 994 | #ifdef DEBUG_MEMORY |
| 995 | xmlGenericError(xmlGenericErrorContext, |
| 996 | "xmlGcMemSetup()\n"); |
| 997 | #endif |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 998 | if (freeFunc == NULL) |
| 999 | return(-1); |
| 1000 | if (mallocFunc == NULL) |
| 1001 | return(-1); |
| 1002 | if (mallocAtomicFunc == NULL) |
| 1003 | return(-1); |
| 1004 | if (reallocFunc == NULL) |
| 1005 | return(-1); |
| 1006 | if (strdupFunc == NULL) |
| 1007 | return(-1); |
| 1008 | xmlFree = freeFunc; |
| 1009 | xmlMalloc = mallocFunc; |
| 1010 | xmlMallocAtomic = mallocAtomicFunc; |
| 1011 | xmlRealloc = reallocFunc; |
| 1012 | xmlMemStrdup = strdupFunc; |
Daniel Veillard | f93a866 | 2004-07-01 12:56:30 +0000 | [diff] [blame] | 1013 | #ifdef DEBUG_MEMORY |
| 1014 | xmlGenericError(xmlGenericErrorContext, |
| 1015 | "xmlGcMemSetup() Ok\n"); |
| 1016 | #endif |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1017 | return(0); |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * xmlGcMemGet: |
| 1022 | * @freeFunc: place to save the free() function in use |
| 1023 | * @mallocFunc: place to save the malloc() function in use |
| 1024 | * @mallocAtomicFunc: place to save the atomic malloc() function in use |
| 1025 | * @reallocFunc: place to save the realloc() function in use |
| 1026 | * @strdupFunc: place to save the strdup() function in use |
| 1027 | * |
| 1028 | * Provides the memory access functions set currently in use |
| 1029 | * The mallocAtomicFunc is specialized for atomic block |
| 1030 | * allocations (i.e. of areas useful for garbage collected memory allocators |
| 1031 | * |
| 1032 | * Returns 0 on success |
| 1033 | */ |
| 1034 | int |
| 1035 | xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc, |
| 1036 | xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc, |
| 1037 | xmlStrdupFunc *strdupFunc) { |
| 1038 | if (freeFunc != NULL) *freeFunc = xmlFree; |
| 1039 | if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 1040 | if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; |
| 1041 | if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 1042 | if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 1043 | return(0); |
| 1044 | } |
| 1045 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 1046 | #define bottom_xmlmemory |
| 1047 | #include "elfgcchack.h" |