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