blob: 1bbc315a7e9d40134ec2b6e3780573734c526d5f [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillard26908ab2002-01-01 16:50:03 +00002 * xmlmemory.c: libxml memory allocator wrapper.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00004 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00005 */
6
Daniel Veillard34ce8be2002-03-18 19:37:11 +00007#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +00008#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +00009
Owen Taylor3473f882001-02-23 17:55:21 +000010#include <string.h>
11
12#ifdef HAVE_SYS_TYPES_H
13#include <sys/types.h>
14#endif
Daniel Veillard0ba59232002-02-10 13:20:39 +000015
Owen Taylor3473f882001-02-23 17:55:21 +000016#ifdef HAVE_TIME_H
17#include <time.h>
18#endif
Daniel Veillard0ba59232002-02-10 13:20:39 +000019
20#ifdef HAVE_STDLIB_H
21#include <stdlib.h>
22#else
Owen Taylor3473f882001-02-23 17:55:21 +000023#ifdef HAVE_MALLOC_H
24#include <malloc.h>
25#endif
Owen Taylor3473f882001-02-23 17:55:21 +000026#endif
Daniel Veillard0ba59232002-02-10 13:20:39 +000027
Owen Taylor3473f882001-02-23 17:55:21 +000028#ifdef HAVE_CTYPE_H
29#include <ctype.h>
30#endif
31
Daniel Veillardf93a8662004-07-01 12:56:30 +000032/* #define DEBUG_MEMORY */
Daniel Veillard4432df22003-09-28 18:58:27 +000033
Daniel Veillard70cab352002-02-06 16:06:58 +000034/**
35 * MEM_LIST:
36 *
37 * keep track of all allocated blocks for error reporting
38 * Always build the memory list !
39 */
Daniel Veillardc064b472003-09-29 10:55:05 +000040#ifdef DEBUG_MEMORY_LOCATION
Daniel Veillard70cab352002-02-06 16:06:58 +000041#ifndef MEM_LIST
42#define MEM_LIST /* keep a list of all the allocated memory blocks */
43#endif
Daniel Veillardc064b472003-09-29 10:55:05 +000044#endif
Owen Taylor3473f882001-02-23 17:55:21 +000045
William M. Brack5ab479b2004-06-10 13:00:15 +000046#include <libxml/globals.h> /* must come before xmlmemory.h */
Owen Taylor3473f882001-02-23 17:55:21 +000047#include <libxml/xmlmemory.h>
48#include <libxml/xmlerror.h>
William M. Brack0622fe82003-11-29 10:47:56 +000049#include <libxml/threads.h>
Owen Taylor3473f882001-02-23 17:55:21 +000050
Daniel Veillard4432df22003-09-28 18:58:27 +000051static int xmlMemInitialized = 0;
Daniel Veillardfb43bd62003-09-29 09:22:39 +000052static unsigned long debugMemSize = 0;
Daniel Veillard36e5cd52004-11-02 14:52:23 +000053static unsigned long debugMemBlocks = 0;
Daniel Veillardfb43bd62003-09-29 09:22:39 +000054static unsigned long debugMaxMemSize = 0;
William M. Brack0622fe82003-11-29 10:47:56 +000055static xmlMutexPtr xmlMemMutex = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +000056
Daniel Veillard56a4cb82001-03-24 17:00:36 +000057void xmlMallocBreakpoint(void);
Daniel Veillard56a4cb82001-03-24 17:00:36 +000058
59/************************************************************************
60 * *
61 * Macros, variables and associated types *
62 * *
63 ************************************************************************/
64
William M. Brack5ab479b2004-06-10 13:00:15 +000065#if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +000066#ifdef xmlMalloc
67#undef xmlMalloc
68#endif
69#ifdef xmlRealloc
70#undef xmlRealloc
71#endif
72#ifdef xmlMemStrdup
73#undef xmlMemStrdup
74#endif
William M. Brack5ab479b2004-06-10 13:00:15 +000075#endif
Owen Taylor3473f882001-02-23 17:55:21 +000076
77/*
78 * Each of the blocks allocated begin with a header containing informations
79 */
80
81#define MEMTAG 0x5aa5
82
83#define MALLOC_TYPE 1
84#define REALLOC_TYPE 2
85#define STRDUP_TYPE 3
Daniel Veillard3c908dc2003-04-19 00:07:51 +000086#define MALLOC_ATOMIC_TYPE 4
87#define REALLOC_ATOMIC_TYPE 5
Owen Taylor3473f882001-02-23 17:55:21 +000088
89typedef struct memnod {
90 unsigned int mh_tag;
91 unsigned int mh_type;
92 unsigned long mh_number;
93 size_t mh_size;
94#ifdef MEM_LIST
95 struct memnod *mh_next;
96 struct memnod *mh_prev;
97#endif
98 const char *mh_file;
99 unsigned int mh_line;
100} MEMHDR;
101
102
103#ifdef SUN4
104#define ALIGN_SIZE 16
105#else
106#define ALIGN_SIZE sizeof(double)
107#endif
108#define HDR_SIZE sizeof(MEMHDR)
109#define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \
110 / ALIGN_SIZE ) * ALIGN_SIZE)
111
112
113#define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE))
114#define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE))
115
116
William M. Brack0622fe82003-11-29 10:47:56 +0000117static unsigned int block=0;
118static unsigned int xmlMemStopAtBlock = 0;
Daniel Veillardb44025c2001-10-11 22:55:55 +0000119static void *xmlMemTraceBlockAt = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000120#ifdef MEM_LIST
121static MEMHDR *memlist = NULL;
122#endif
123
Daniel Veillard01c13b52002-12-10 15:19:08 +0000124static void debugmem_tag_error(void *addr);
Owen Taylor3473f882001-02-23 17:55:21 +0000125#ifdef MEM_LIST
Daniel Veillard01c13b52002-12-10 15:19:08 +0000126static void debugmem_list_add(MEMHDR *);
127static void debugmem_list_delete(MEMHDR *);
Owen Taylor3473f882001-02-23 17:55:21 +0000128#endif
129#define Mem_Tag_Err(a) debugmem_tag_error(a);
130
131#ifndef TEST_POINT
132#define TEST_POINT
133#endif
134
135/**
136 * xmlMallocBreakpoint:
137 *
138 * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block
139 * number reaches the specified value this function is called. One need to add a breakpoint
140 * to it to get the context in which the given block is allocated.
141 */
142
143void
144xmlMallocBreakpoint(void) {
145 xmlGenericError(xmlGenericErrorContext,
146 "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock);
147}
148
149/**
150 * xmlMallocLoc:
151 * @size: an int specifying the size in byte to allocate.
152 * @file: the file name or NULL
153 * @line: the line number
154 *
155 * a malloc() equivalent, with logging of the allocation info.
156 *
157 * Returns a pointer to the allocated area or NULL in case of lack of memory.
158 */
159
160void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000161xmlMallocLoc(size_t size, const char * file, int line)
Owen Taylor3473f882001-02-23 17:55:21 +0000162{
163 MEMHDR *p;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000164 void *ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000165
166 if (!xmlMemInitialized) xmlInitMemory();
167#ifdef DEBUG_MEMORY
168 xmlGenericError(xmlGenericErrorContext,
169 "Malloc(%d)\n",size);
170#endif
171
172 TEST_POINT
173
174 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
175
176 if (!p) {
177 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000178 "xmlMallocLoc : Out of free space\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000179 xmlMemoryDump();
180 return(NULL);
181 }
182 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000183 p->mh_size = size;
184 p->mh_type = MALLOC_TYPE;
185 p->mh_file = file;
186 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000187 xmlMutexLock(xmlMemMutex);
188 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000189 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000190 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000191 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
192#ifdef MEM_LIST
193 debugmem_list_add(p);
194#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000195 xmlMutexUnlock(xmlMemMutex);
196
Owen Taylor3473f882001-02-23 17:55:21 +0000197#ifdef DEBUG_MEMORY
198 xmlGenericError(xmlGenericErrorContext,
199 "Malloc(%d) Ok\n",size);
200#endif
201
William M. Brack0622fe82003-11-29 10:47:56 +0000202 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000203
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000204 ret = HDR_2_CLIENT(p);
205
206 if (xmlMemTraceBlockAt == ret) {
207 xmlGenericError(xmlGenericErrorContext,
208 "%p : Malloc(%d) Ok\n", xmlMemTraceBlockAt, size);
209 xmlMallocBreakpoint();
210 }
211
Owen Taylor3473f882001-02-23 17:55:21 +0000212 TEST_POINT
213
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000214 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000215}
216
217/**
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000218 * xmlMallocAtomicLoc:
219 * @size: an int specifying the size in byte to allocate.
220 * @file: the file name or NULL
221 * @line: the line number
222 *
223 * a malloc() equivalent, with logging of the allocation info.
224 *
225 * Returns a pointer to the allocated area or NULL in case of lack of memory.
226 */
227
228void *
229xmlMallocAtomicLoc(size_t size, const char * file, int line)
230{
231 MEMHDR *p;
232 void *ret;
233
234 if (!xmlMemInitialized) xmlInitMemory();
235#ifdef DEBUG_MEMORY
236 xmlGenericError(xmlGenericErrorContext,
237 "Malloc(%d)\n",size);
238#endif
239
240 TEST_POINT
241
242 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
243
244 if (!p) {
245 xmlGenericError(xmlGenericErrorContext,
246 "xmlMallocLoc : Out of free space\n");
247 xmlMemoryDump();
248 return(NULL);
249 }
250 p->mh_tag = MEMTAG;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000251 p->mh_size = size;
252 p->mh_type = MALLOC_ATOMIC_TYPE;
253 p->mh_file = file;
254 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000255 xmlMutexLock(xmlMemMutex);
256 p->mh_number = ++block;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000257 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000258 debugMemBlocks++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000259 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
260#ifdef MEM_LIST
261 debugmem_list_add(p);
262#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000263 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000264
265#ifdef DEBUG_MEMORY
266 xmlGenericError(xmlGenericErrorContext,
267 "Malloc(%d) Ok\n",size);
268#endif
269
William M. Brack0622fe82003-11-29 10:47:56 +0000270 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000271
272 ret = HDR_2_CLIENT(p);
273
274 if (xmlMemTraceBlockAt == ret) {
275 xmlGenericError(xmlGenericErrorContext,
276 "%p : Malloc(%d) Ok\n", xmlMemTraceBlockAt, size);
277 xmlMallocBreakpoint();
278 }
279
280 TEST_POINT
281
282 return(ret);
283}
284/**
Owen Taylor3473f882001-02-23 17:55:21 +0000285 * xmlMemMalloc:
286 * @size: an int specifying the size in byte to allocate.
287 *
288 * a malloc() equivalent, with logging of the allocation info.
289 *
290 * Returns a pointer to the allocated area or NULL in case of lack of memory.
291 */
292
293void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000294xmlMemMalloc(size_t size)
Owen Taylor3473f882001-02-23 17:55:21 +0000295{
296 return(xmlMallocLoc(size, "none", 0));
297}
298
299/**
300 * xmlReallocLoc:
301 * @ptr: the initial memory block pointer
302 * @size: an int specifying the size in byte to allocate.
303 * @file: the file name or NULL
304 * @line: the line number
305 *
306 * a realloc() equivalent, with logging of the allocation info.
307 *
308 * Returns a pointer to the allocated area or NULL in case of lack of memory.
309 */
310
311void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000312xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
Owen Taylor3473f882001-02-23 17:55:21 +0000313{
314 MEMHDR *p;
315 unsigned long number;
Daniel Veillard529233c2004-07-02 12:23:44 +0000316#ifdef DEBUG_MEMORY
317 size_t oldsize;
318#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000319
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000320 if (ptr == NULL)
Aleksey Sanine9f08112004-01-22 22:20:31 +0000321 return(xmlMallocLoc(size, file, line));
322
323 if (!xmlMemInitialized) xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +0000324 TEST_POINT
325
326 p = CLIENT_2_HDR(ptr);
327 number = p->mh_number;
Daniel Veillard18ffe202005-04-14 17:50:59 +0000328 if (xmlMemStopAtBlock == number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000329 if (p->mh_tag != MEMTAG) {
330 Mem_Tag_Err(p);
331 goto error;
332 }
333 p->mh_tag = ~MEMTAG;
William M. Brack0622fe82003-11-29 10:47:56 +0000334 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000335 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000336 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000337#ifdef DEBUG_MEMORY
338 oldsize = p->mh_size;
339#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000340#ifdef MEM_LIST
341 debugmem_list_delete(p);
342#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000343 xmlMutexUnlock(xmlMemMutex);
344
Owen Taylor3473f882001-02-23 17:55:21 +0000345 p = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
346 if (!p) {
347 goto error;
348 }
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000349 if (xmlMemTraceBlockAt == ptr) {
350 xmlGenericError(xmlGenericErrorContext,
351 "%p : Realloced(%d -> %d) Ok\n",
352 xmlMemTraceBlockAt, p->mh_size, size);
353 xmlMallocBreakpoint();
354 }
Owen Taylor3473f882001-02-23 17:55:21 +0000355 p->mh_tag = MEMTAG;
356 p->mh_number = number;
357 p->mh_type = REALLOC_TYPE;
358 p->mh_size = size;
359 p->mh_file = file;
360 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000361 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000362 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000363 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000364 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
365#ifdef MEM_LIST
366 debugmem_list_add(p);
367#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000368 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000369
370 TEST_POINT
371
Daniel Veillard529233c2004-07-02 12:23:44 +0000372#ifdef DEBUG_MEMORY
373 xmlGenericError(xmlGenericErrorContext,
374 "Realloced(%d to %d) Ok\n", oldsize, size);
375#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000376 return(HDR_2_CLIENT(p));
377
378error:
379 return(NULL);
380}
381
382/**
383 * xmlMemRealloc:
384 * @ptr: the initial memory block pointer
385 * @size: an int specifying the size in byte to allocate.
386 *
387 * a realloc() equivalent, with logging of the allocation info.
388 *
389 * Returns a pointer to the allocated area or NULL in case of lack of memory.
390 */
391
392void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000393xmlMemRealloc(void *ptr,size_t size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000394 return(xmlReallocLoc(ptr, size, "none", 0));
395}
396
397/**
398 * xmlMemFree:
399 * @ptr: the memory block pointer
400 *
401 * a free() equivalent, with error checking.
402 */
403void
404xmlMemFree(void *ptr)
405{
406 MEMHDR *p;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000407 char *target;
Daniel Veillard529233c2004-07-02 12:23:44 +0000408#ifdef DEBUG_MEMORY
409 size_t size;
410#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000411
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000412 if (ptr == (void *) -1) {
413 xmlGenericError(xmlGenericErrorContext,
414 "trying to free pointer from freed area\n");
415 goto error;
416 }
417
418 if (xmlMemTraceBlockAt == ptr) {
419 xmlGenericError(xmlGenericErrorContext,
420 "%p : Freed()\n", xmlMemTraceBlockAt);
421 xmlMallocBreakpoint();
422 }
423
Owen Taylor3473f882001-02-23 17:55:21 +0000424 TEST_POINT
425
Daniel Veillard92ad2102001-03-27 12:47:33 +0000426 target = (char *) ptr;
427
Owen Taylor3473f882001-02-23 17:55:21 +0000428 p = CLIENT_2_HDR(ptr);
429 if (p->mh_tag != MEMTAG) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000430 Mem_Tag_Err(p);
431 goto error;
Owen Taylor3473f882001-02-23 17:55:21 +0000432 }
Daniel Veillard18ffe202005-04-14 17:50:59 +0000433 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000434 p->mh_tag = ~MEMTAG;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000435 memset(target, -1, p->mh_size);
William M. Brack0622fe82003-11-29 10:47:56 +0000436 xmlMutexLock(xmlMemMutex);
437 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000438 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000439#ifdef DEBUG_MEMORY
440 size = p->mh_size;
441#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000442#ifdef MEM_LIST
443 debugmem_list_delete(p);
444#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000445 xmlMutexUnlock(xmlMemMutex);
446
Owen Taylor3473f882001-02-23 17:55:21 +0000447 free(p);
448
449 TEST_POINT
450
Daniel Veillard529233c2004-07-02 12:23:44 +0000451#ifdef DEBUG_MEMORY
452 xmlGenericError(xmlGenericErrorContext,
453 "Freed(%d) Ok\n", size);
454#endif
455
Owen Taylor3473f882001-02-23 17:55:21 +0000456 return;
457
458error:
459 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000460 "xmlMemFree(%lX) error\n", (unsigned long) ptr);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000461 xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000462 return;
463}
464
465/**
466 * xmlMemStrdupLoc:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000467 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000468 * @file: the file name or NULL
469 * @line: the line number
470 *
471 * a strdup() equivalent, with logging of the allocation info.
472 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000473 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000474 */
475
476char *
477xmlMemStrdupLoc(const char *str, const char *file, int line)
478{
479 char *s;
480 size_t size = strlen(str) + 1;
481 MEMHDR *p;
482
483 if (!xmlMemInitialized) xmlInitMemory();
484 TEST_POINT
485
486 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
487 if (!p) {
488 goto error;
489 }
490 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000491 p->mh_size = size;
492 p->mh_type = STRDUP_TYPE;
493 p->mh_file = file;
494 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000495 xmlMutexLock(xmlMemMutex);
496 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000497 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000498 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000499 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
500#ifdef MEM_LIST
501 debugmem_list_add(p);
502#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000503 xmlMutexUnlock(xmlMemMutex);
504
Owen Taylor3473f882001-02-23 17:55:21 +0000505 s = (char *) HDR_2_CLIENT(p);
506
William M. Brack0622fe82003-11-29 10:47:56 +0000507 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000508
509 if (s != NULL)
510 strcpy(s,str);
511 else
512 goto error;
513
514 TEST_POINT
515
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000516 if (xmlMemTraceBlockAt == s) {
517 xmlGenericError(xmlGenericErrorContext,
518 "%p : Strdup() Ok\n", xmlMemTraceBlockAt);
519 xmlMallocBreakpoint();
520 }
521
Owen Taylor3473f882001-02-23 17:55:21 +0000522 return(s);
523
524error:
525 return(NULL);
526}
527
528/**
529 * xmlMemoryStrdup:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000530 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000531 *
532 * a strdup() equivalent, with logging of the allocation info.
533 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000534 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000535 */
536
537char *
538xmlMemoryStrdup(const char *str) {
539 return(xmlMemStrdupLoc(str, "none", 0));
540}
541
542/**
543 * xmlMemUsed:
544 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000545 * Provides the amount of memory currently allocated
Owen Taylor3473f882001-02-23 17:55:21 +0000546 *
547 * Returns an int representing the amount of memory allocated.
548 */
549
550int
551xmlMemUsed(void) {
552 return(debugMemSize);
553}
554
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000555/**
556 * xmlMemBlocks:
557 *
558 * Provides the number of memory areas currently allocated
559 *
560 * Returns an int representing the number of blocks
561 */
562
563int
564xmlMemBlocks(void) {
565 return(debugMemBlocks);
566}
567
Owen Taylor3473f882001-02-23 17:55:21 +0000568#ifdef MEM_LIST
569/**
570 * xmlMemContentShow:
571 * @fp: a FILE descriptor used as the output file
572 * @p: a memory block header
573 *
574 * tries to show some content from the memory block
575 */
576
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000577static void
Owen Taylor3473f882001-02-23 17:55:21 +0000578xmlMemContentShow(FILE *fp, MEMHDR *p)
579{
580 int i,j,len = p->mh_size;
581 const char *buf = (const char *) HDR_2_CLIENT(p);
582
583 if (p == NULL) {
584 fprintf(fp, " NULL");
585 return;
586 }
587
588 for (i = 0;i < len;i++) {
589 if (buf[i] == 0) break;
Daniel Veillard9f28f302002-02-15 20:48:08 +0000590 if (!isprint((unsigned char) buf[i])) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000591 }
592 if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
593 if (len >= 4) {
594 MEMHDR *q;
595 void *cur;
596
597 for (j = 0;j < len -3;j += 4) {
598 cur = *((void **) &buf[j]);
599 q = CLIENT_2_HDR(cur);
600 p = memlist;
601 while (p != NULL) {
602 if (p == q) break;
603 p = p->mh_next;
604 }
605 if ((p != NULL) && (p == q)) {
606 fprintf(fp, " pointer to #%lu at index %d",
607 p->mh_number, j);
608 return;
609 }
610 }
611 }
612 } else if ((i == 0) && (buf[i] == 0)) {
613 fprintf(fp," null");
614 } else {
615 if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
616 else {
617 fprintf(fp," [");
618 for (j = 0;j < i;j++)
619 fprintf(fp,"%c", buf[j]);
620 fprintf(fp,"]");
621 }
622 }
623}
624#endif
625
626/**
Owen Taylor3473f882001-02-23 17:55:21 +0000627 * xmlMemDisplay:
628 * @fp: a FILE descriptor used as the output file, if NULL, the result is
629 * written to the file .memorylist
630 *
631 * show in-extenso the memory blocks allocated
632 */
633
634void
635xmlMemDisplay(FILE *fp)
636{
637#ifdef MEM_LIST
638 MEMHDR *p;
Daniel Veillard144024e2002-02-13 21:14:46 +0000639 unsigned idx;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000640 int nb = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000641#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
642 time_t currentTime;
643 char buf[500];
644 struct tm * tstruct;
645
646 currentTime = time(NULL);
647 tstruct = localtime(&currentTime);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000648 strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Owen Taylor3473f882001-02-23 17:55:21 +0000649 fprintf(fp," %s\n\n", buf);
650#endif
651
652
653 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
654 debugMemSize, debugMaxMemSize);
655 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
656 idx = 0;
William M. Brack0622fe82003-11-29 10:47:56 +0000657 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000658 p = memlist;
659 while (p) {
Daniel Veillard144024e2002-02-13 21:14:46 +0000660 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
661 (unsigned long)p->mh_size);
Owen Taylor3473f882001-02-23 17:55:21 +0000662 switch (p->mh_type) {
663 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
664 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000665 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000666 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000667 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
668 default:
William M. Brack13dfa872004-09-18 04:52:08 +0000669 fprintf(fp,"Unknown memory block, may be corrupted");
Daniel Veillard529233c2004-07-02 12:23:44 +0000670 xmlMutexUnlock(xmlMemMutex);
671 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000672 }
William M. Brack13dfa872004-09-18 04:52:08 +0000673 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Owen Taylor3473f882001-02-23 17:55:21 +0000674 if (p->mh_tag != MEMTAG)
675 fprintf(fp," INVALID");
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000676 nb++;
677 if (nb < 100)
678 xmlMemContentShow(fp, p);
679 else
680 fprintf(fp," skip");
681
Owen Taylor3473f882001-02-23 17:55:21 +0000682 fprintf(fp,"\n");
683 p = p->mh_next;
684 }
William M. Brack0622fe82003-11-29 10:47:56 +0000685 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000686#else
687 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
688#endif
689}
690
691#ifdef MEM_LIST
692
Daniel Veillard01c13b52002-12-10 15:19:08 +0000693static void debugmem_list_add(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000694{
695 p->mh_next = memlist;
696 p->mh_prev = NULL;
697 if (memlist) memlist->mh_prev = p;
698 memlist = p;
699#ifdef MEM_LIST_DEBUG
700 if (stderr)
701 Mem_Display(stderr);
702#endif
703}
704
Daniel Veillard01c13b52002-12-10 15:19:08 +0000705static void debugmem_list_delete(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000706{
707 if (p->mh_next)
708 p->mh_next->mh_prev = p->mh_prev;
709 if (p->mh_prev)
710 p->mh_prev->mh_next = p->mh_next;
711 else memlist = p->mh_next;
712#ifdef MEM_LIST_DEBUG
713 if (stderr)
714 Mem_Display(stderr);
715#endif
716}
717
718#endif
719
720/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000721 * debugmem_tag_error:
722 *
723 * internal error function.
Owen Taylor3473f882001-02-23 17:55:21 +0000724 */
725
Daniel Veillard01c13b52002-12-10 15:19:08 +0000726static void debugmem_tag_error(void *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000727{
728 xmlGenericError(xmlGenericErrorContext,
729 "Memory tag error occurs :%p \n\t bye\n", p);
730#ifdef MEM_LIST
731 if (stderr)
732 xmlMemDisplay(stderr);
733#endif
734}
735
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000736#ifdef MEM_LIST
Daniel Veillardb44025c2001-10-11 22:55:55 +0000737static FILE *xmlMemoryDumpFile = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000738#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000739
Owen Taylor3473f882001-02-23 17:55:21 +0000740/**
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000741 * xmlMemShow:
742 * @fp: a FILE descriptor used as the output file
743 * @nr: number of entries to dump
744 *
745 * show a show display of the memory allocated, and dump
746 * the @nr last allocated areas which were not freed
747 */
748
749void
Daniel Veillardc064b472003-09-29 10:55:05 +0000750xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000751{
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000752#ifdef MEM_LIST
753 MEMHDR *p;
754#endif
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000755
756 if (fp != NULL)
757 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
758 debugMemSize, debugMaxMemSize);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000759#ifdef MEM_LIST
William M. Brack0622fe82003-11-29 10:47:56 +0000760 xmlMutexLock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000761 if (nr > 0) {
762 fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
763 p = memlist;
764 while ((p) && nr > 0) {
765 fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
766 switch (p->mh_type) {
767 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
768 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
769 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
770 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
771 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
772 default:fprintf(fp," ??? in ");break;
773 }
774 if (p->mh_file != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +0000775 fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000776 if (p->mh_tag != MEMTAG)
777 fprintf(fp," INVALID");
778 xmlMemContentShow(fp, p);
779 fprintf(fp,"\n");
780 nr--;
781 p = p->mh_next;
782 }
783 }
William M. Brack0622fe82003-11-29 10:47:56 +0000784 xmlMutexUnlock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000785#endif /* MEM_LIST */
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000786}
787
788/**
Owen Taylor3473f882001-02-23 17:55:21 +0000789 * xmlMemoryDump:
790 *
791 * Dump in-extenso the memory blocks allocated to the file .memorylist
792 */
793
794void
795xmlMemoryDump(void)
796{
Daniel Veillardc064b472003-09-29 10:55:05 +0000797#ifdef MEM_LIST
Owen Taylor3473f882001-02-23 17:55:21 +0000798 FILE *dump;
799
Daniel Veillard5997aca2002-03-18 18:36:20 +0000800 if (debugMaxMemSize == 0)
801 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000802 dump = fopen(".memdump", "w");
Daniel Veillardcd337f02001-11-22 18:20:37 +0000803 if (dump == NULL)
804 xmlMemoryDumpFile = stderr;
Owen Taylor3473f882001-02-23 17:55:21 +0000805 else xmlMemoryDumpFile = dump;
806
807 xmlMemDisplay(xmlMemoryDumpFile);
808
809 if (dump != NULL) fclose(dump);
Daniel Veillardc064b472003-09-29 10:55:05 +0000810#endif /* MEM_LIST */
Owen Taylor3473f882001-02-23 17:55:21 +0000811}
812
813
814/****************************************************************
815 * *
816 * Initialization Routines *
817 * *
818 ****************************************************************/
819
Owen Taylor3473f882001-02-23 17:55:21 +0000820/**
821 * xmlInitMemory:
822 *
823 * Initialize the memory layer.
824 *
825 * Returns 0 on success
826 */
Owen Taylor3473f882001-02-23 17:55:21 +0000827int
828xmlInitMemory(void)
829{
Daniel Veillarde15df582004-07-13 15:25:08 +0000830#ifdef HAVE_STDLIB_H
831 char *breakpoint;
832#endif
Daniel Veillardf93a8662004-07-01 12:56:30 +0000833#ifdef DEBUG_MEMORY
834 xmlGenericError(xmlGenericErrorContext,
835 "xmlInitMemory()\n");
836#endif
William M. Brack92029422004-01-04 01:01:14 +0000837 /*
838 This is really not good code (see Bug 130419). Suggestions for
839 improvement will be welcome!
840 */
841 if (xmlMemInitialized) return(-1);
William M. Brack0622fe82003-11-29 10:47:56 +0000842 xmlMemInitialized = 1;
William M. Brack0622fe82003-11-29 10:47:56 +0000843 xmlMemMutex = xmlNewMutex();
Owen Taylor3473f882001-02-23 17:55:21 +0000844
845#ifdef HAVE_STDLIB_H
846 breakpoint = getenv("XML_MEM_BREAKPOINT");
847 if (breakpoint != NULL) {
William M. Brack0622fe82003-11-29 10:47:56 +0000848 sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Owen Taylor3473f882001-02-23 17:55:21 +0000849 }
850#endif
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000851#ifdef HAVE_STDLIB_H
852 breakpoint = getenv("XML_MEM_TRACE");
853 if (breakpoint != NULL) {
854 sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
855 }
856#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000857
858#ifdef DEBUG_MEMORY
859 xmlGenericError(xmlGenericErrorContext,
860 "xmlInitMemory() Ok\n");
861#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000862 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000863}
864
865/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000866 * xmlCleanupMemory:
867 *
Daniel Veillard91b955c2004-12-10 10:26:42 +0000868 * Free up all the memory allocated by the library for its own
869 * use. This should not be called by user level code.
William M. Brack72ee48d2003-12-30 08:30:19 +0000870 */
871void
872xmlCleanupMemory(void) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000873#ifdef DEBUG_MEMORY
874 xmlGenericError(xmlGenericErrorContext,
875 "xmlCleanupMemory()\n");
876#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000877 if (xmlMemInitialized == 0)
878 return;
879
880 xmlFreeMutex(xmlMemMutex);
Daniel Veillard1a9b7082004-01-02 10:42:01 +0000881 xmlMemMutex = NULL;
William M. Brack72ee48d2003-12-30 08:30:19 +0000882 xmlMemInitialized = 0;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000883#ifdef DEBUG_MEMORY
884 xmlGenericError(xmlGenericErrorContext,
885 "xmlCleanupMemory() Ok\n");
886#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000887}
888
889/**
Owen Taylor3473f882001-02-23 17:55:21 +0000890 * xmlMemSetup:
891 * @freeFunc: the free() function to use
892 * @mallocFunc: the malloc() function to use
893 * @reallocFunc: the realloc() function to use
894 * @strdupFunc: the strdup() function to use
895 *
896 * Override the default memory access functions with a new set
897 * This has to be called before any other libxml routines !
898 *
899 * Should this be blocked if there was already some allocations
900 * done ?
901 *
902 * Returns 0 on success
903 */
904int
905xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
906 xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000907#ifdef DEBUG_MEMORY
908 xmlGenericError(xmlGenericErrorContext,
909 "xmlMemSetup()\n");
910#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000911 if (freeFunc == NULL)
912 return(-1);
913 if (mallocFunc == NULL)
914 return(-1);
915 if (reallocFunc == NULL)
916 return(-1);
917 if (strdupFunc == NULL)
918 return(-1);
919 xmlFree = freeFunc;
920 xmlMalloc = mallocFunc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000921 xmlMallocAtomic = mallocFunc;
Owen Taylor3473f882001-02-23 17:55:21 +0000922 xmlRealloc = reallocFunc;
923 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000924#ifdef DEBUG_MEMORY
925 xmlGenericError(xmlGenericErrorContext,
926 "xmlMemSetup() Ok\n");
927#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000928 return(0);
929}
930
931/**
932 * xmlMemGet:
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000933 * @freeFunc: place to save the free() function in use
934 * @mallocFunc: place to save the malloc() function in use
935 * @reallocFunc: place to save the realloc() function in use
936 * @strdupFunc: place to save the strdup() function in use
Owen Taylor3473f882001-02-23 17:55:21 +0000937 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000938 * Provides the memory access functions set currently in use
Owen Taylor3473f882001-02-23 17:55:21 +0000939 *
940 * Returns 0 on success
941 */
942int
943xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
944 xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
945 if (freeFunc != NULL) *freeFunc = xmlFree;
946 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
947 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
948 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
949 return(0);
950}
951
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000952/**
953 * xmlGcMemSetup:
954 * @freeFunc: the free() function to use
955 * @mallocFunc: the malloc() function to use
956 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
957 * @reallocFunc: the realloc() function to use
958 * @strdupFunc: the strdup() function to use
959 *
960 * Override the default memory access functions with a new set
961 * This has to be called before any other libxml routines !
962 * The mallocAtomicFunc is specialized for atomic block
963 * allocations (i.e. of areas useful for garbage collected memory allocators
964 *
965 * Should this be blocked if there was already some allocations
966 * done ?
967 *
968 * Returns 0 on success
969 */
970int
971xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
972 xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
973 xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000974#ifdef DEBUG_MEMORY
975 xmlGenericError(xmlGenericErrorContext,
976 "xmlGcMemSetup()\n");
977#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000978 if (freeFunc == NULL)
979 return(-1);
980 if (mallocFunc == NULL)
981 return(-1);
982 if (mallocAtomicFunc == NULL)
983 return(-1);
984 if (reallocFunc == NULL)
985 return(-1);
986 if (strdupFunc == NULL)
987 return(-1);
988 xmlFree = freeFunc;
989 xmlMalloc = mallocFunc;
990 xmlMallocAtomic = mallocAtomicFunc;
991 xmlRealloc = reallocFunc;
992 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000993#ifdef DEBUG_MEMORY
994 xmlGenericError(xmlGenericErrorContext,
995 "xmlGcMemSetup() Ok\n");
996#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000997 return(0);
998}
999
1000/**
1001 * xmlGcMemGet:
1002 * @freeFunc: place to save the free() function in use
1003 * @mallocFunc: place to save the malloc() function in use
1004 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1005 * @reallocFunc: place to save the realloc() function in use
1006 * @strdupFunc: place to save the strdup() function in use
1007 *
1008 * Provides the memory access functions set currently in use
1009 * The mallocAtomicFunc is specialized for atomic block
1010 * allocations (i.e. of areas useful for garbage collected memory allocators
1011 *
1012 * Returns 0 on success
1013 */
1014int
1015xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1016 xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
1017 xmlStrdupFunc *strdupFunc) {
1018 if (freeFunc != NULL) *freeFunc = xmlFree;
1019 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1020 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
1021 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1022 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1023 return(0);
1024}
1025
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001026#define bottom_xmlmemory
1027#include "elfgcchack.h"