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 | 70cab35 | 2002-02-06 16:06:58 +0000 | [diff] [blame] | 32 | /** |
| 33 | * MEM_LIST: |
| 34 | * |
| 35 | * keep track of all allocated blocks for error reporting |
| 36 | * Always build the memory list ! |
| 37 | */ |
| 38 | #ifndef MEM_LIST |
| 39 | #define MEM_LIST /* keep a list of all the allocated memory blocks */ |
| 40 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 41 | |
| 42 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 43 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 44 | #include <libxml/xmlerror.h> |
| 45 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 46 | void xmlMallocBreakpoint(void); |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 47 | void * xmlMemMalloc(size_t size); |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 48 | void * xmlMemRealloc(void *ptr,size_t size); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 49 | void xmlMemFree(void *ptr); |
| 50 | char * xmlMemoryStrdup(const char *str); |
| 51 | |
| 52 | /************************************************************************ |
| 53 | * * |
| 54 | * Macros, variables and associated types * |
| 55 | * * |
| 56 | ************************************************************************/ |
| 57 | |
| 58 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 59 | #ifdef xmlMalloc |
| 60 | #undef xmlMalloc |
| 61 | #endif |
| 62 | #ifdef xmlRealloc |
| 63 | #undef xmlRealloc |
| 64 | #endif |
| 65 | #ifdef xmlMemStrdup |
| 66 | #undef xmlMemStrdup |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Each of the blocks allocated begin with a header containing informations |
| 72 | */ |
| 73 | |
| 74 | #define MEMTAG 0x5aa5 |
| 75 | |
| 76 | #define MALLOC_TYPE 1 |
| 77 | #define REALLOC_TYPE 2 |
| 78 | #define STRDUP_TYPE 3 |
| 79 | |
| 80 | typedef struct memnod { |
| 81 | unsigned int mh_tag; |
| 82 | unsigned int mh_type; |
| 83 | unsigned long mh_number; |
| 84 | size_t mh_size; |
| 85 | #ifdef MEM_LIST |
| 86 | struct memnod *mh_next; |
| 87 | struct memnod *mh_prev; |
| 88 | #endif |
| 89 | const char *mh_file; |
| 90 | unsigned int mh_line; |
| 91 | } MEMHDR; |
| 92 | |
| 93 | |
| 94 | #ifdef SUN4 |
| 95 | #define ALIGN_SIZE 16 |
| 96 | #else |
| 97 | #define ALIGN_SIZE sizeof(double) |
| 98 | #endif |
| 99 | #define HDR_SIZE sizeof(MEMHDR) |
| 100 | #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \ |
| 101 | / ALIGN_SIZE ) * ALIGN_SIZE) |
| 102 | |
| 103 | |
| 104 | #define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE)) |
| 105 | #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE)) |
| 106 | |
| 107 | |
| 108 | static unsigned long debugMemSize = 0; |
| 109 | static unsigned long debugMaxMemSize = 0; |
| 110 | static int block=0; |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 111 | static int xmlMemStopAtBlock = 0; |
| 112 | static void *xmlMemTraceBlockAt = NULL; |
| 113 | static int xmlMemInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 114 | #ifdef MEM_LIST |
| 115 | static MEMHDR *memlist = NULL; |
| 116 | #endif |
| 117 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 118 | static void debugmem_tag_error(void *addr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 119 | #ifdef MEM_LIST |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 120 | static void debugmem_list_add(MEMHDR *); |
| 121 | static void debugmem_list_delete(MEMHDR *); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 122 | #endif |
| 123 | #define Mem_Tag_Err(a) debugmem_tag_error(a); |
| 124 | |
| 125 | #ifndef TEST_POINT |
| 126 | #define TEST_POINT |
| 127 | #endif |
| 128 | |
| 129 | /** |
| 130 | * xmlMallocBreakpoint: |
| 131 | * |
| 132 | * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block |
| 133 | * number reaches the specified value this function is called. One need to add a breakpoint |
| 134 | * to it to get the context in which the given block is allocated. |
| 135 | */ |
| 136 | |
| 137 | void |
| 138 | xmlMallocBreakpoint(void) { |
| 139 | xmlGenericError(xmlGenericErrorContext, |
| 140 | "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * xmlMallocLoc: |
| 145 | * @size: an int specifying the size in byte to allocate. |
| 146 | * @file: the file name or NULL |
| 147 | * @line: the line number |
| 148 | * |
| 149 | * a malloc() equivalent, with logging of the allocation info. |
| 150 | * |
| 151 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 152 | */ |
| 153 | |
| 154 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 155 | xmlMallocLoc(size_t size, const char * file, int line) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 156 | { |
| 157 | MEMHDR *p; |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 158 | void *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | |
| 160 | if (!xmlMemInitialized) xmlInitMemory(); |
| 161 | #ifdef DEBUG_MEMORY |
| 162 | xmlGenericError(xmlGenericErrorContext, |
| 163 | "Malloc(%d)\n",size); |
| 164 | #endif |
| 165 | |
| 166 | TEST_POINT |
| 167 | |
| 168 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 169 | |
| 170 | if (!p) { |
| 171 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 172 | "xmlMallocLoc : Out of free space\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 173 | xmlMemoryDump(); |
| 174 | return(NULL); |
| 175 | } |
| 176 | p->mh_tag = MEMTAG; |
| 177 | p->mh_number = ++block; |
| 178 | p->mh_size = size; |
| 179 | p->mh_type = MALLOC_TYPE; |
| 180 | p->mh_file = file; |
| 181 | p->mh_line = line; |
| 182 | debugMemSize += size; |
| 183 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 184 | #ifdef MEM_LIST |
| 185 | debugmem_list_add(p); |
| 186 | #endif |
| 187 | |
| 188 | #ifdef DEBUG_MEMORY |
| 189 | xmlGenericError(xmlGenericErrorContext, |
| 190 | "Malloc(%d) Ok\n",size); |
| 191 | #endif |
| 192 | |
| 193 | if (xmlMemStopAtBlock == block) xmlMallocBreakpoint(); |
| 194 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 195 | ret = HDR_2_CLIENT(p); |
| 196 | |
| 197 | if (xmlMemTraceBlockAt == ret) { |
| 198 | xmlGenericError(xmlGenericErrorContext, |
| 199 | "%p : Malloc(%d) Ok\n", xmlMemTraceBlockAt, size); |
| 200 | xmlMallocBreakpoint(); |
| 201 | } |
| 202 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 203 | TEST_POINT |
| 204 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 205 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * xmlMemMalloc: |
| 210 | * @size: an int specifying the size in byte to allocate. |
| 211 | * |
| 212 | * a malloc() equivalent, with logging of the allocation info. |
| 213 | * |
| 214 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 215 | */ |
| 216 | |
| 217 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 218 | xmlMemMalloc(size_t size) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 219 | { |
| 220 | return(xmlMallocLoc(size, "none", 0)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * xmlReallocLoc: |
| 225 | * @ptr: the initial memory block pointer |
| 226 | * @size: an int specifying the size in byte to allocate. |
| 227 | * @file: the file name or NULL |
| 228 | * @line: the line number |
| 229 | * |
| 230 | * a realloc() equivalent, with logging of the allocation info. |
| 231 | * |
| 232 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 233 | */ |
| 234 | |
| 235 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 236 | xmlReallocLoc(void *ptr,size_t size, const char * file, int line) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 237 | { |
| 238 | MEMHDR *p; |
| 239 | unsigned long number; |
| 240 | |
| 241 | if (!xmlMemInitialized) xmlInitMemory(); |
| 242 | TEST_POINT |
| 243 | |
| 244 | p = CLIENT_2_HDR(ptr); |
| 245 | number = p->mh_number; |
| 246 | if (p->mh_tag != MEMTAG) { |
| 247 | Mem_Tag_Err(p); |
| 248 | goto error; |
| 249 | } |
| 250 | p->mh_tag = ~MEMTAG; |
| 251 | debugMemSize -= p->mh_size; |
| 252 | #ifdef MEM_LIST |
| 253 | debugmem_list_delete(p); |
| 254 | #endif |
| 255 | |
| 256 | p = (MEMHDR *) realloc(p,RESERVE_SIZE+size); |
| 257 | if (!p) { |
| 258 | goto error; |
| 259 | } |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 260 | if (xmlMemTraceBlockAt == ptr) { |
| 261 | xmlGenericError(xmlGenericErrorContext, |
| 262 | "%p : Realloced(%d -> %d) Ok\n", |
| 263 | xmlMemTraceBlockAt, p->mh_size, size); |
| 264 | xmlMallocBreakpoint(); |
| 265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 266 | p->mh_tag = MEMTAG; |
| 267 | p->mh_number = number; |
| 268 | p->mh_type = REALLOC_TYPE; |
| 269 | p->mh_size = size; |
| 270 | p->mh_file = file; |
| 271 | p->mh_line = line; |
| 272 | debugMemSize += size; |
| 273 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 274 | #ifdef MEM_LIST |
| 275 | debugmem_list_add(p); |
| 276 | #endif |
| 277 | |
| 278 | TEST_POINT |
| 279 | |
| 280 | return(HDR_2_CLIENT(p)); |
| 281 | |
| 282 | error: |
| 283 | return(NULL); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * xmlMemRealloc: |
| 288 | * @ptr: the initial memory block pointer |
| 289 | * @size: an int specifying the size in byte to allocate. |
| 290 | * |
| 291 | * a realloc() equivalent, with logging of the allocation info. |
| 292 | * |
| 293 | * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 294 | */ |
| 295 | |
| 296 | void * |
Daniel Veillard | 8599e70 | 2001-07-17 21:38:51 +0000 | [diff] [blame] | 297 | xmlMemRealloc(void *ptr,size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 298 | return(xmlReallocLoc(ptr, size, "none", 0)); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * xmlMemFree: |
| 303 | * @ptr: the memory block pointer |
| 304 | * |
| 305 | * a free() equivalent, with error checking. |
| 306 | */ |
| 307 | void |
| 308 | xmlMemFree(void *ptr) |
| 309 | { |
| 310 | MEMHDR *p; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 311 | char *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 312 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 313 | if (ptr == (void *) -1) { |
| 314 | xmlGenericError(xmlGenericErrorContext, |
| 315 | "trying to free pointer from freed area\n"); |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | if (xmlMemTraceBlockAt == ptr) { |
| 320 | xmlGenericError(xmlGenericErrorContext, |
| 321 | "%p : Freed()\n", xmlMemTraceBlockAt); |
| 322 | xmlMallocBreakpoint(); |
| 323 | } |
| 324 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 325 | TEST_POINT |
| 326 | |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 327 | target = (char *) ptr; |
| 328 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 329 | p = CLIENT_2_HDR(ptr); |
| 330 | if (p->mh_tag != MEMTAG) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 331 | Mem_Tag_Err(p); |
| 332 | goto error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 333 | } |
| 334 | p->mh_tag = ~MEMTAG; |
| 335 | debugMemSize -= p->mh_size; |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 336 | memset(target, -1, p->mh_size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 337 | |
| 338 | #ifdef MEM_LIST |
| 339 | debugmem_list_delete(p); |
| 340 | #endif |
| 341 | free(p); |
| 342 | |
| 343 | TEST_POINT |
| 344 | |
| 345 | return; |
| 346 | |
| 347 | error: |
| 348 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 349 | "xmlMemFree(%lX) error\n", (unsigned long) ptr); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 350 | xmlMallocBreakpoint(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * xmlMemStrdupLoc: |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 356 | * @str: the initial string pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 357 | * @file: the file name or NULL |
| 358 | * @line: the line number |
| 359 | * |
| 360 | * a strdup() equivalent, with logging of the allocation info. |
| 361 | * |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 362 | * 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] | 363 | */ |
| 364 | |
| 365 | char * |
| 366 | xmlMemStrdupLoc(const char *str, const char *file, int line) |
| 367 | { |
| 368 | char *s; |
| 369 | size_t size = strlen(str) + 1; |
| 370 | MEMHDR *p; |
| 371 | |
| 372 | if (!xmlMemInitialized) xmlInitMemory(); |
| 373 | TEST_POINT |
| 374 | |
| 375 | p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
| 376 | if (!p) { |
| 377 | goto error; |
| 378 | } |
| 379 | p->mh_tag = MEMTAG; |
| 380 | p->mh_number = ++block; |
| 381 | p->mh_size = size; |
| 382 | p->mh_type = STRDUP_TYPE; |
| 383 | p->mh_file = file; |
| 384 | p->mh_line = line; |
| 385 | debugMemSize += size; |
| 386 | if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 387 | #ifdef MEM_LIST |
| 388 | debugmem_list_add(p); |
| 389 | #endif |
| 390 | s = (char *) HDR_2_CLIENT(p); |
| 391 | |
| 392 | if (xmlMemStopAtBlock == block) xmlMallocBreakpoint(); |
| 393 | |
| 394 | if (s != NULL) |
| 395 | strcpy(s,str); |
| 396 | else |
| 397 | goto error; |
| 398 | |
| 399 | TEST_POINT |
| 400 | |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 401 | if (xmlMemTraceBlockAt == s) { |
| 402 | xmlGenericError(xmlGenericErrorContext, |
| 403 | "%p : Strdup() Ok\n", xmlMemTraceBlockAt); |
| 404 | xmlMallocBreakpoint(); |
| 405 | } |
| 406 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 407 | return(s); |
| 408 | |
| 409 | error: |
| 410 | return(NULL); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * xmlMemoryStrdup: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 415 | * @str: the initial string pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 416 | * |
| 417 | * a strdup() equivalent, with logging of the allocation info. |
| 418 | * |
Daniel Veillard | 26908ab | 2002-01-01 16:50:03 +0000 | [diff] [blame] | 419 | * 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] | 420 | */ |
| 421 | |
| 422 | char * |
| 423 | xmlMemoryStrdup(const char *str) { |
| 424 | return(xmlMemStrdupLoc(str, "none", 0)); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * xmlMemUsed: |
| 429 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 430 | * Provides the amount of memory currently allocated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 431 | * |
| 432 | * Returns an int representing the amount of memory allocated. |
| 433 | */ |
| 434 | |
| 435 | int |
| 436 | xmlMemUsed(void) { |
| 437 | return(debugMemSize); |
| 438 | } |
| 439 | |
| 440 | #ifdef MEM_LIST |
| 441 | /** |
| 442 | * xmlMemContentShow: |
| 443 | * @fp: a FILE descriptor used as the output file |
| 444 | * @p: a memory block header |
| 445 | * |
| 446 | * tries to show some content from the memory block |
| 447 | */ |
| 448 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 449 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 450 | xmlMemContentShow(FILE *fp, MEMHDR *p) |
| 451 | { |
| 452 | int i,j,len = p->mh_size; |
| 453 | const char *buf = (const char *) HDR_2_CLIENT(p); |
| 454 | |
| 455 | if (p == NULL) { |
| 456 | fprintf(fp, " NULL"); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | for (i = 0;i < len;i++) { |
| 461 | if (buf[i] == 0) break; |
Daniel Veillard | 9f28f30 | 2002-02-15 20:48:08 +0000 | [diff] [blame] | 462 | if (!isprint((unsigned char) buf[i])) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 463 | } |
| 464 | if ((i < 4) && ((buf[i] != 0) || (i == 0))) { |
| 465 | if (len >= 4) { |
| 466 | MEMHDR *q; |
| 467 | void *cur; |
| 468 | |
| 469 | for (j = 0;j < len -3;j += 4) { |
| 470 | cur = *((void **) &buf[j]); |
| 471 | q = CLIENT_2_HDR(cur); |
| 472 | p = memlist; |
| 473 | while (p != NULL) { |
| 474 | if (p == q) break; |
| 475 | p = p->mh_next; |
| 476 | } |
| 477 | if ((p != NULL) && (p == q)) { |
| 478 | fprintf(fp, " pointer to #%lu at index %d", |
| 479 | p->mh_number, j); |
| 480 | return; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } else if ((i == 0) && (buf[i] == 0)) { |
| 485 | fprintf(fp," null"); |
| 486 | } else { |
| 487 | if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf); |
| 488 | else { |
| 489 | fprintf(fp," ["); |
| 490 | for (j = 0;j < i;j++) |
| 491 | fprintf(fp,"%c", buf[j]); |
| 492 | fprintf(fp,"]"); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | /** |
| 499 | * xmlMemShow: |
| 500 | * @fp: a FILE descriptor used as the output file |
| 501 | * @nr: number of entries to dump |
| 502 | * |
| 503 | * show a show display of the memory allocated, and dump |
| 504 | * the @nr last allocated areas which were not freed |
| 505 | */ |
| 506 | |
| 507 | void |
| 508 | xmlMemShow(FILE *fp, int nr) |
| 509 | { |
| 510 | #ifdef MEM_LIST |
| 511 | MEMHDR *p; |
| 512 | #endif |
| 513 | |
| 514 | if (fp != NULL) |
| 515 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 516 | debugMemSize, debugMaxMemSize); |
| 517 | #ifdef MEM_LIST |
| 518 | if (nr > 0) { |
| 519 | fprintf(fp,"NUMBER SIZE TYPE WHERE\n"); |
| 520 | p = memlist; |
| 521 | while ((p) && nr > 0) { |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 522 | fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 523 | switch (p->mh_type) { |
| 524 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 525 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
| 526 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
| 527 | default:fprintf(fp," ??? in ");break; |
| 528 | } |
| 529 | if (p->mh_file != NULL) |
| 530 | fprintf(fp,"%s(%d)", p->mh_file, p->mh_line); |
| 531 | if (p->mh_tag != MEMTAG) |
| 532 | fprintf(fp," INVALID"); |
| 533 | xmlMemContentShow(fp, p); |
| 534 | fprintf(fp,"\n"); |
| 535 | nr--; |
| 536 | p = p->mh_next; |
| 537 | } |
| 538 | } |
| 539 | #endif /* MEM_LIST */ |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * xmlMemDisplay: |
| 544 | * @fp: a FILE descriptor used as the output file, if NULL, the result is |
| 545 | * written to the file .memorylist |
| 546 | * |
| 547 | * show in-extenso the memory blocks allocated |
| 548 | */ |
| 549 | |
| 550 | void |
| 551 | xmlMemDisplay(FILE *fp) |
| 552 | { |
| 553 | #ifdef MEM_LIST |
| 554 | MEMHDR *p; |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 555 | unsigned idx; |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 556 | int nb = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 557 | #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME) |
| 558 | time_t currentTime; |
| 559 | char buf[500]; |
| 560 | struct tm * tstruct; |
| 561 | |
| 562 | currentTime = time(NULL); |
| 563 | tstruct = localtime(¤tTime); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 564 | strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 565 | fprintf(fp," %s\n\n", buf); |
| 566 | #endif |
| 567 | |
| 568 | |
| 569 | fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n", |
| 570 | debugMemSize, debugMaxMemSize); |
| 571 | fprintf(fp,"BLOCK NUMBER SIZE TYPE\n"); |
| 572 | idx = 0; |
| 573 | p = memlist; |
| 574 | while (p) { |
Daniel Veillard | 144024e | 2002-02-13 21:14:46 +0000 | [diff] [blame] | 575 | fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number, |
| 576 | (unsigned long)p->mh_size); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 577 | switch (p->mh_type) { |
| 578 | case STRDUP_TYPE:fprintf(fp,"strdup() in ");break; |
| 579 | case MALLOC_TYPE:fprintf(fp,"malloc() in ");break; |
| 580 | case REALLOC_TYPE:fprintf(fp,"realloc() in ");break; |
| 581 | default:fprintf(fp," ??? in ");break; |
| 582 | } |
| 583 | if (p->mh_file != NULL) fprintf(fp,"%s(%d)", p->mh_file, p->mh_line); |
| 584 | if (p->mh_tag != MEMTAG) |
| 585 | fprintf(fp," INVALID"); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 586 | nb++; |
| 587 | if (nb < 100) |
| 588 | xmlMemContentShow(fp, p); |
| 589 | else |
| 590 | fprintf(fp," skip"); |
| 591 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 592 | fprintf(fp,"\n"); |
| 593 | p = p->mh_next; |
| 594 | } |
| 595 | #else |
| 596 | fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n"); |
| 597 | #endif |
| 598 | } |
| 599 | |
| 600 | #ifdef MEM_LIST |
| 601 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 602 | static void debugmem_list_add(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 603 | { |
| 604 | p->mh_next = memlist; |
| 605 | p->mh_prev = NULL; |
| 606 | if (memlist) memlist->mh_prev = p; |
| 607 | memlist = p; |
| 608 | #ifdef MEM_LIST_DEBUG |
| 609 | if (stderr) |
| 610 | Mem_Display(stderr); |
| 611 | #endif |
| 612 | } |
| 613 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 614 | static void debugmem_list_delete(MEMHDR *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 615 | { |
| 616 | if (p->mh_next) |
| 617 | p->mh_next->mh_prev = p->mh_prev; |
| 618 | if (p->mh_prev) |
| 619 | p->mh_prev->mh_next = p->mh_next; |
| 620 | else memlist = p->mh_next; |
| 621 | #ifdef MEM_LIST_DEBUG |
| 622 | if (stderr) |
| 623 | Mem_Display(stderr); |
| 624 | #endif |
| 625 | } |
| 626 | |
| 627 | #endif |
| 628 | |
| 629 | /* |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 630 | * debugmem_tag_error: |
| 631 | * |
| 632 | * internal error function. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | */ |
| 634 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 635 | static void debugmem_tag_error(void *p) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 636 | { |
| 637 | xmlGenericError(xmlGenericErrorContext, |
| 638 | "Memory tag error occurs :%p \n\t bye\n", p); |
| 639 | #ifdef MEM_LIST |
| 640 | if (stderr) |
| 641 | xmlMemDisplay(stderr); |
| 642 | #endif |
| 643 | } |
| 644 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 645 | static FILE *xmlMemoryDumpFile = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 646 | |
| 647 | |
| 648 | /** |
| 649 | * xmlMemoryDump: |
| 650 | * |
| 651 | * Dump in-extenso the memory blocks allocated to the file .memorylist |
| 652 | */ |
| 653 | |
| 654 | void |
| 655 | xmlMemoryDump(void) |
| 656 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 657 | FILE *dump; |
| 658 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 659 | if (debugMaxMemSize == 0) |
| 660 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 661 | dump = fopen(".memdump", "w"); |
Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 662 | if (dump == NULL) |
| 663 | xmlMemoryDumpFile = stderr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 664 | else xmlMemoryDumpFile = dump; |
| 665 | |
| 666 | xmlMemDisplay(xmlMemoryDumpFile); |
| 667 | |
| 668 | if (dump != NULL) fclose(dump); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
| 672 | /**************************************************************** |
| 673 | * * |
| 674 | * Initialization Routines * |
| 675 | * * |
| 676 | ****************************************************************/ |
| 677 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 678 | static int xmlInitMemoryDone = 0; |
| 679 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 680 | /** |
| 681 | * xmlInitMemory: |
| 682 | * |
| 683 | * Initialize the memory layer. |
| 684 | * |
| 685 | * Returns 0 on success |
| 686 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 687 | int |
| 688 | xmlInitMemory(void) |
| 689 | { |
| 690 | int ret; |
| 691 | |
| 692 | #ifdef HAVE_STDLIB_H |
| 693 | char *breakpoint; |
| 694 | #endif |
| 695 | |
| 696 | if (xmlInitMemoryDone) return(-1); |
| 697 | |
| 698 | #ifdef HAVE_STDLIB_H |
| 699 | breakpoint = getenv("XML_MEM_BREAKPOINT"); |
| 700 | if (breakpoint != NULL) { |
| 701 | sscanf(breakpoint, "%d", &xmlMemStopAtBlock); |
| 702 | } |
| 703 | #endif |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 704 | #ifdef HAVE_STDLIB_H |
| 705 | breakpoint = getenv("XML_MEM_TRACE"); |
| 706 | if (breakpoint != NULL) { |
| 707 | sscanf(breakpoint, "%p", &xmlMemTraceBlockAt); |
| 708 | } |
| 709 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | |
| 711 | #ifdef DEBUG_MEMORY |
| 712 | xmlGenericError(xmlGenericErrorContext, |
| 713 | "xmlInitMemory() Ok\n"); |
| 714 | #endif |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 715 | xmlMemInitialized = 1; |
| 716 | xmlInitMemoryDone = 1; |
| 717 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 718 | ret = 0; |
| 719 | return(ret); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * xmlMemSetup: |
| 724 | * @freeFunc: the free() function to use |
| 725 | * @mallocFunc: the malloc() function to use |
| 726 | * @reallocFunc: the realloc() function to use |
| 727 | * @strdupFunc: the strdup() function to use |
| 728 | * |
| 729 | * Override the default memory access functions with a new set |
| 730 | * This has to be called before any other libxml routines ! |
| 731 | * |
| 732 | * Should this be blocked if there was already some allocations |
| 733 | * done ? |
| 734 | * |
| 735 | * Returns 0 on success |
| 736 | */ |
| 737 | int |
| 738 | xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc, |
| 739 | xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) { |
| 740 | if (freeFunc == NULL) |
| 741 | return(-1); |
| 742 | if (mallocFunc == NULL) |
| 743 | return(-1); |
| 744 | if (reallocFunc == NULL) |
| 745 | return(-1); |
| 746 | if (strdupFunc == NULL) |
| 747 | return(-1); |
| 748 | xmlFree = freeFunc; |
| 749 | xmlMalloc = mallocFunc; |
| 750 | xmlRealloc = reallocFunc; |
| 751 | xmlMemStrdup = strdupFunc; |
| 752 | return(0); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * xmlMemGet: |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 757 | * @freeFunc: place to save the free() function in use |
| 758 | * @mallocFunc: place to save the malloc() function in use |
| 759 | * @reallocFunc: place to save the realloc() function in use |
| 760 | * @strdupFunc: place to save the strdup() function in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 762 | * Provides the memory access functions set currently in use |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 763 | * |
| 764 | * Returns 0 on success |
| 765 | */ |
| 766 | int |
| 767 | xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc, |
| 768 | xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) { |
| 769 | if (freeFunc != NULL) *freeFunc = xmlFree; |
| 770 | if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 771 | if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 772 | if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 773 | return(0); |
| 774 | } |
| 775 | |