| OLD | NEW |
| 1 /* | 1 /* |
| 2 * xmlmemory.c: libxml memory allocator wrapper. | 2 * xmlmemory.c: libxml memory allocator wrapper. |
| 3 * | 3 * |
| 4 * daniel@veillard.com | 4 * daniel@veillard.com |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #define IN_LIBXML | 7 #define IN_LIBXML |
| 8 #include "libxml.h" | 8 #include "libxml.h" |
| 9 | 9 |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 static int xmlMemInitialized = 0; | 51 static int xmlMemInitialized = 0; |
| 52 static unsigned long debugMemSize = 0; | 52 static unsigned long debugMemSize = 0; |
| 53 static unsigned long debugMemBlocks = 0; | 53 static unsigned long debugMemBlocks = 0; |
| 54 static unsigned long debugMaxMemSize = 0; | 54 static unsigned long debugMaxMemSize = 0; |
| 55 static xmlMutexPtr xmlMemMutex = NULL; | 55 static xmlMutexPtr xmlMemMutex = NULL; |
| 56 | 56 |
| 57 void xmlMallocBreakpoint(void); | 57 void xmlMallocBreakpoint(void); |
| 58 | 58 |
| 59 /************************************************************************ | 59 /************************************************************************ |
| 60 * * | 60 * * |
| 61 * » » Macros, variables and associated types» » » * | 61 *» » Macros, variables and associated types» » » * |
| 62 * * | 62 * * |
| 63 ************************************************************************/ | 63 ************************************************************************/ |
| 64 | 64 |
| 65 #if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED) | 65 #if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED) |
| 66 #ifdef xmlMalloc | 66 #ifdef xmlMalloc |
| 67 #undef xmlMalloc | 67 #undef xmlMalloc |
| 68 #endif | 68 #endif |
| 69 #ifdef xmlRealloc | 69 #ifdef xmlRealloc |
| 70 #undef xmlRealloc | 70 #undef xmlRealloc |
| 71 #endif | 71 #endif |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 xmlGenericError(xmlGenericErrorContext, | 198 xmlGenericError(xmlGenericErrorContext, |
| 199 "Malloc(%d) Ok\n",size); | 199 "Malloc(%d) Ok\n",size); |
| 200 #endif | 200 #endif |
| 201 | 201 |
| 202 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); | 202 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
| 203 | 203 |
| 204 ret = HDR_2_CLIENT(p); | 204 ret = HDR_2_CLIENT(p); |
| 205 | 205 |
| 206 if (xmlMemTraceBlockAt == ret) { | 206 if (xmlMemTraceBlockAt == ret) { |
| 207 xmlGenericError(xmlGenericErrorContext, | 207 xmlGenericError(xmlGenericErrorContext, |
| 208 » » » "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size); | 208 » » » "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, |
| 209 » » » (long unsigned)size); |
| 209 xmlMallocBreakpoint(); | 210 xmlMallocBreakpoint(); |
| 210 } | 211 } |
| 211 | 212 |
| 212 TEST_POINT | 213 TEST_POINT |
| 213 | 214 |
| 214 return(ret); | 215 return(ret); |
| 215 } | 216 } |
| 216 | 217 |
| 217 /** | 218 /** |
| 218 * xmlMallocAtomicLoc: | 219 * xmlMallocAtomicLoc: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 xmlGenericError(xmlGenericErrorContext, | 267 xmlGenericError(xmlGenericErrorContext, |
| 267 "Malloc(%d) Ok\n",size); | 268 "Malloc(%d) Ok\n",size); |
| 268 #endif | 269 #endif |
| 269 | 270 |
| 270 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); | 271 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
| 271 | 272 |
| 272 ret = HDR_2_CLIENT(p); | 273 ret = HDR_2_CLIENT(p); |
| 273 | 274 |
| 274 if (xmlMemTraceBlockAt == ret) { | 275 if (xmlMemTraceBlockAt == ret) { |
| 275 xmlGenericError(xmlGenericErrorContext, | 276 xmlGenericError(xmlGenericErrorContext, |
| 276 » » » "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size); | 277 » » » "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, |
| 278 » » » (long unsigned)size); |
| 277 xmlMallocBreakpoint(); | 279 xmlMallocBreakpoint(); |
| 278 } | 280 } |
| 279 | 281 |
| 280 TEST_POINT | 282 TEST_POINT |
| 281 | 283 |
| 282 return(ret); | 284 return(ret); |
| 283 } | 285 } |
| 284 /** | 286 /** |
| 285 * xmlMemMalloc: | 287 * xmlMemMalloc: |
| 286 * @size: an int specifying the size in byte to allocate. | 288 * @size: an int specifying the size in byte to allocate. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 304 * @line: the line number | 306 * @line: the line number |
| 305 * | 307 * |
| 306 * a realloc() equivalent, with logging of the allocation info. | 308 * a realloc() equivalent, with logging of the allocation info. |
| 307 * | 309 * |
| 308 * Returns a pointer to the allocated area or NULL in case of lack of memory. | 310 * Returns a pointer to the allocated area or NULL in case of lack of memory. |
| 309 */ | 311 */ |
| 310 | 312 |
| 311 void * | 313 void * |
| 312 xmlReallocLoc(void *ptr,size_t size, const char * file, int line) | 314 xmlReallocLoc(void *ptr,size_t size, const char * file, int line) |
| 313 { | 315 { |
| 314 MEMHDR *p; | 316 MEMHDR *p, *tmp; |
| 315 unsigned long number; | 317 unsigned long number; |
| 316 #ifdef DEBUG_MEMORY | 318 #ifdef DEBUG_MEMORY |
| 317 size_t oldsize; | 319 size_t oldsize; |
| 318 #endif | 320 #endif |
| 319 | 321 |
| 320 if (ptr == NULL) | 322 if (ptr == NULL) |
| 321 return(xmlMallocLoc(size, file, line)); | 323 return(xmlMallocLoc(size, file, line)); |
| 322 | 324 |
| 323 if (!xmlMemInitialized) xmlInitMemory(); | 325 if (!xmlMemInitialized) xmlInitMemory(); |
| 324 TEST_POINT | 326 TEST_POINT |
| (...skipping 10 matching lines...) Expand all Loading... |
| 335 debugMemSize -= p->mh_size; | 337 debugMemSize -= p->mh_size; |
| 336 debugMemBlocks--; | 338 debugMemBlocks--; |
| 337 #ifdef DEBUG_MEMORY | 339 #ifdef DEBUG_MEMORY |
| 338 oldsize = p->mh_size; | 340 oldsize = p->mh_size; |
| 339 #endif | 341 #endif |
| 340 #ifdef MEM_LIST | 342 #ifdef MEM_LIST |
| 341 debugmem_list_delete(p); | 343 debugmem_list_delete(p); |
| 342 #endif | 344 #endif |
| 343 xmlMutexUnlock(xmlMemMutex); | 345 xmlMutexUnlock(xmlMemMutex); |
| 344 | 346 |
| 345 p = (MEMHDR *) realloc(p,RESERVE_SIZE+size); | 347 tmp = (MEMHDR *) realloc(p,RESERVE_SIZE+size); |
| 346 if (!p) { | 348 if (!tmp) { |
| 349 » free(p); |
| 347 goto error; | 350 goto error; |
| 348 } | 351 } |
| 352 p = tmp; |
| 349 if (xmlMemTraceBlockAt == ptr) { | 353 if (xmlMemTraceBlockAt == ptr) { |
| 350 xmlGenericError(xmlGenericErrorContext, | 354 xmlGenericError(xmlGenericErrorContext, |
| 351 » » » "%p : Realloced(%ld -> %ld) Ok\n", | 355 » » » "%p : Realloced(%lu -> %lu) Ok\n", |
| 352 » » » xmlMemTraceBlockAt, p->mh_size, size); | 356 » » » xmlMemTraceBlockAt, (long unsigned)p->mh_size, |
| 357 » » » (long unsigned)size); |
| 353 xmlMallocBreakpoint(); | 358 xmlMallocBreakpoint(); |
| 354 } | 359 } |
| 355 p->mh_tag = MEMTAG; | 360 p->mh_tag = MEMTAG; |
| 356 p->mh_number = number; | 361 p->mh_number = number; |
| 357 p->mh_type = REALLOC_TYPE; | 362 p->mh_type = REALLOC_TYPE; |
| 358 p->mh_size = size; | 363 p->mh_size = size; |
| 359 p->mh_file = file; | 364 p->mh_file = file; |
| 360 p->mh_line = line; | 365 p->mh_line = line; |
| 361 xmlMutexLock(xmlMemMutex); | 366 xmlMutexLock(xmlMemMutex); |
| 362 debugMemSize += size; | 367 debugMemSize += size; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; | 507 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize; |
| 503 #ifdef MEM_LIST | 508 #ifdef MEM_LIST |
| 504 debugmem_list_add(p); | 509 debugmem_list_add(p); |
| 505 #endif | 510 #endif |
| 506 xmlMutexUnlock(xmlMemMutex); | 511 xmlMutexUnlock(xmlMemMutex); |
| 507 | 512 |
| 508 s = (char *) HDR_2_CLIENT(p); | 513 s = (char *) HDR_2_CLIENT(p); |
| 509 | 514 |
| 510 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); | 515 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint(); |
| 511 | 516 |
| 512 if (s != NULL) | 517 strcpy(s,str); |
| 513 strcpy(s,str); | |
| 514 else | |
| 515 goto error; | |
| 516 | 518 |
| 517 TEST_POINT | 519 TEST_POINT |
| 518 | 520 |
| 519 if (xmlMemTraceBlockAt == s) { | 521 if (xmlMemTraceBlockAt == s) { |
| 520 xmlGenericError(xmlGenericErrorContext, | 522 xmlGenericError(xmlGenericErrorContext, |
| 521 "%p : Strdup() Ok\n", xmlMemTraceBlockAt); | 523 "%p : Strdup() Ok\n", xmlMemTraceBlockAt); |
| 522 xmlMallocBreakpoint(); | 524 xmlMallocBreakpoint(); |
| 523 } | 525 } |
| 524 | 526 |
| 525 return(s); | 527 return(s); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 545 /** | 547 /** |
| 546 * xmlMemUsed: | 548 * xmlMemUsed: |
| 547 * | 549 * |
| 548 * Provides the amount of memory currently allocated | 550 * Provides the amount of memory currently allocated |
| 549 * | 551 * |
| 550 * Returns an int representing the amount of memory allocated. | 552 * Returns an int representing the amount of memory allocated. |
| 551 */ | 553 */ |
| 552 | 554 |
| 553 int | 555 int |
| 554 xmlMemUsed(void) { | 556 xmlMemUsed(void) { |
| 555 return(debugMemSize); | 557 int res; |
| 558 |
| 559 xmlMutexLock(xmlMemMutex); |
| 560 res = debugMemSize; |
| 561 xmlMutexUnlock(xmlMemMutex); |
| 562 return(res); |
| 556 } | 563 } |
| 557 | 564 |
| 558 /** | 565 /** |
| 559 * xmlMemBlocks: | 566 * xmlMemBlocks: |
| 560 * | 567 * |
| 561 * Provides the number of memory areas currently allocated | 568 * Provides the number of memory areas currently allocated |
| 562 * | 569 * |
| 563 * Returns an int representing the number of blocks | 570 * Returns an int representing the number of blocks |
| 564 */ | 571 */ |
| 565 | 572 |
| 566 int | 573 int |
| 567 xmlMemBlocks(void) { | 574 xmlMemBlocks(void) { |
| 568 return(debugMemBlocks); | 575 int res; |
| 576 |
| 577 xmlMutexLock(xmlMemMutex); |
| 578 res = debugMemBlocks; |
| 579 xmlMutexUnlock(xmlMemMutex); |
| 580 return(res); |
| 569 } | 581 } |
| 570 | 582 |
| 571 #ifdef MEM_LIST | 583 #ifdef MEM_LIST |
| 572 /** | 584 /** |
| 573 * xmlMemContentShow: | 585 * xmlMemContentShow: |
| 574 * @fp: a FILE descriptor used as the output file | 586 * @fp: a FILE descriptor used as the output file |
| 575 * @p: a memory block header | 587 * @p: a memory block header |
| 576 * | 588 * |
| 577 * tries to show some content from the memory block | 589 * tries to show some content from the memory block |
| 578 */ | 590 */ |
| 579 | 591 |
| 580 static void | 592 static void |
| 581 xmlMemContentShow(FILE *fp, MEMHDR *p) | 593 xmlMemContentShow(FILE *fp, MEMHDR *p) |
| 582 { | 594 { |
| 583 int i,j,k,len = p->mh_size; | 595 int i,j,k,len; |
| 584 const char *buf = (const char *) HDR_2_CLIENT(p); | 596 const char *buf; |
| 585 | 597 |
| 586 if (p == NULL) { | 598 if (p == NULL) { |
| 587 fprintf(fp, " NULL"); | 599 fprintf(fp, " NULL"); |
| 588 return; | 600 return; |
| 589 } | 601 } |
| 602 len = p->mh_size; |
| 603 buf = (const char *) HDR_2_CLIENT(p); |
| 590 | 604 |
| 591 for (i = 0;i < len;i++) { | 605 for (i = 0;i < len;i++) { |
| 592 if (buf[i] == 0) break; | 606 if (buf[i] == 0) break; |
| 593 if (!isprint((unsigned char) buf[i])) break; | 607 if (!isprint((unsigned char) buf[i])) break; |
| 594 } | 608 } |
| 595 if ((i < 4) && ((buf[i] != 0) || (i == 0))) { | 609 if ((i < 4) && ((buf[i] != 0) || (i == 0))) { |
| 596 if (len >= 4) { | 610 if (len >= 4) { |
| 597 MEMHDR *q; | 611 MEMHDR *q; |
| 598 void *cur; | 612 void *cur; |
| 599 | 613 |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1111 if (freeFunc != NULL) *freeFunc = xmlFree; | 1125 if (freeFunc != NULL) *freeFunc = xmlFree; |
| 1112 if (mallocFunc != NULL) *mallocFunc = xmlMalloc; | 1126 if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
| 1113 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; | 1127 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; |
| 1114 if (reallocFunc != NULL) *reallocFunc = xmlRealloc; | 1128 if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
| 1115 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; | 1129 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
| 1116 return(0); | 1130 return(0); |
| 1117 } | 1131 } |
| 1118 | 1132 |
| 1119 #define bottom_xmlmemory | 1133 #define bottom_xmlmemory |
| 1120 #include "elfgcchack.h" | 1134 #include "elfgcchack.h" |
| OLD | NEW |