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