blob: 433abb8efedf5dac482ed744857ccfa896d43309 [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 *
Daniel Veillard09459bf2008-07-30 12:58:11 +000037 * keep track of all allocated blocks for error reporting
Daniel Veillard70cab352002-02-06 16:06:58 +000038 * 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;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000165
Owen Taylor3473f882001-02-23 17:55:21 +0000166 if (!xmlMemInitialized) xmlInitMemory();
167#ifdef DEBUG_MEMORY
168 xmlGenericError(xmlGenericErrorContext,
169 "Malloc(%d)\n",size);
170#endif
171
172 TEST_POINT
Daniel Veillard09459bf2008-07-30 12:58:11 +0000173
Owen Taylor3473f882001-02-23 17:55:21 +0000174 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);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000181 }
Owen Taylor3473f882001-02-23 17:55:21 +0000182 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);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000196
Owen Taylor3473f882001-02-23 17:55:21 +0000197#ifdef DEBUG_MEMORY
198 xmlGenericError(xmlGenericErrorContext,
199 "Malloc(%d) Ok\n",size);
200#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000201
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,
Daniel Veillardf63085d2009-01-18 20:53:59 +0000208 "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000209 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;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000233
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000234 if (!xmlMemInitialized) xmlInitMemory();
235#ifdef DEBUG_MEMORY
236 xmlGenericError(xmlGenericErrorContext,
237 "Malloc(%d)\n",size);
238#endif
239
240 TEST_POINT
Daniel Veillard09459bf2008-07-30 12:58:11 +0000241
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000242 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);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000249 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000250 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
Daniel Veillard09459bf2008-07-30 12:58:11 +0000269
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,
Daniel Veillardf63085d2009-01-18 20:53:59 +0000276 "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000277 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);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000344
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,
Daniel Veillardf63085d2009-01-18 20:53:59 +0000351 "%p : Realloced(%ld -> %ld) Ok\n",
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000352 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));
Daniel Veillard09459bf2008-07-30 12:58:11 +0000377
378error:
Owen Taylor3473f882001-02-23 17:55:21 +0000379 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 Veillard2a512da2007-10-30 20:24:40 +0000412 if (ptr == NULL)
413 return;
414
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000415 if (ptr == (void *) -1) {
416 xmlGenericError(xmlGenericErrorContext,
417 "trying to free pointer from freed area\n");
418 goto error;
419 }
420
421 if (xmlMemTraceBlockAt == ptr) {
422 xmlGenericError(xmlGenericErrorContext,
423 "%p : Freed()\n", xmlMemTraceBlockAt);
424 xmlMallocBreakpoint();
425 }
426
Owen Taylor3473f882001-02-23 17:55:21 +0000427 TEST_POINT
428
Daniel Veillard92ad2102001-03-27 12:47:33 +0000429 target = (char *) ptr;
430
Owen Taylor3473f882001-02-23 17:55:21 +0000431 p = CLIENT_2_HDR(ptr);
432 if (p->mh_tag != MEMTAG) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000433 Mem_Tag_Err(p);
434 goto error;
Owen Taylor3473f882001-02-23 17:55:21 +0000435 }
Daniel Veillard18ffe202005-04-14 17:50:59 +0000436 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000437 p->mh_tag = ~MEMTAG;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000438 memset(target, -1, p->mh_size);
William M. Brack0622fe82003-11-29 10:47:56 +0000439 xmlMutexLock(xmlMemMutex);
440 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000441 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000442#ifdef DEBUG_MEMORY
443 size = p->mh_size;
444#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000445#ifdef MEM_LIST
446 debugmem_list_delete(p);
447#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000448 xmlMutexUnlock(xmlMemMutex);
449
Owen Taylor3473f882001-02-23 17:55:21 +0000450 free(p);
451
452 TEST_POINT
453
Daniel Veillard529233c2004-07-02 12:23:44 +0000454#ifdef DEBUG_MEMORY
455 xmlGenericError(xmlGenericErrorContext,
456 "Freed(%d) Ok\n", size);
457#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000458
Owen Taylor3473f882001-02-23 17:55:21 +0000459 return;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000460
461error:
Owen Taylor3473f882001-02-23 17:55:21 +0000462 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000463 "xmlMemFree(%lX) error\n", (unsigned long) ptr);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000464 xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000465 return;
466}
467
468/**
469 * xmlMemStrdupLoc:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000470 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000471 * @file: the file name or NULL
472 * @line: the line number
473 *
474 * a strdup() equivalent, with logging of the allocation info.
475 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000476 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000477 */
478
479char *
480xmlMemStrdupLoc(const char *str, const char *file, int line)
481{
482 char *s;
483 size_t size = strlen(str) + 1;
484 MEMHDR *p;
485
486 if (!xmlMemInitialized) xmlInitMemory();
487 TEST_POINT
488
489 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
490 if (!p) {
491 goto error;
492 }
493 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000494 p->mh_size = size;
495 p->mh_type = STRDUP_TYPE;
496 p->mh_file = file;
497 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000498 xmlMutexLock(xmlMemMutex);
499 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000500 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000501 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000502 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
503#ifdef MEM_LIST
504 debugmem_list_add(p);
505#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000506 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000507
Owen Taylor3473f882001-02-23 17:55:21 +0000508 s = (char *) HDR_2_CLIENT(p);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000509
William M. Brack0622fe82003-11-29 10:47:56 +0000510 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000511
512 if (s != NULL)
513 strcpy(s,str);
514 else
515 goto error;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000516
Owen Taylor3473f882001-02-23 17:55:21 +0000517 TEST_POINT
518
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000519 if (xmlMemTraceBlockAt == s) {
520 xmlGenericError(xmlGenericErrorContext,
521 "%p : Strdup() Ok\n", xmlMemTraceBlockAt);
522 xmlMallocBreakpoint();
523 }
524
Owen Taylor3473f882001-02-23 17:55:21 +0000525 return(s);
526
527error:
528 return(NULL);
529}
530
531/**
532 * xmlMemoryStrdup:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000533 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000534 *
535 * a strdup() equivalent, with logging of the allocation info.
536 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000537 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000538 */
539
540char *
541xmlMemoryStrdup(const char *str) {
542 return(xmlMemStrdupLoc(str, "none", 0));
543}
544
545/**
546 * xmlMemUsed:
547 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000548 * Provides the amount of memory currently allocated
Owen Taylor3473f882001-02-23 17:55:21 +0000549 *
550 * Returns an int representing the amount of memory allocated.
551 */
552
553int
554xmlMemUsed(void) {
555 return(debugMemSize);
556}
557
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000558/**
559 * xmlMemBlocks:
560 *
561 * Provides the number of memory areas currently allocated
562 *
563 * Returns an int representing the number of blocks
564 */
565
566int
567xmlMemBlocks(void) {
568 return(debugMemBlocks);
569}
570
Owen Taylor3473f882001-02-23 17:55:21 +0000571#ifdef MEM_LIST
572/**
573 * xmlMemContentShow:
574 * @fp: a FILE descriptor used as the output file
575 * @p: a memory block header
576 *
577 * tries to show some content from the memory block
578 */
579
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000580static void
Owen Taylor3473f882001-02-23 17:55:21 +0000581xmlMemContentShow(FILE *fp, MEMHDR *p)
582{
Daniel Veillardf4721d62006-10-11 21:12:10 +0000583 int i,j,k,len = p->mh_size;
Owen Taylor3473f882001-02-23 17:55:21 +0000584 const char *buf = (const char *) HDR_2_CLIENT(p);
585
586 if (p == NULL) {
587 fprintf(fp, " NULL");
588 return;
589 }
590
591 for (i = 0;i < len;i++) {
592 if (buf[i] == 0) break;
Daniel Veillard9f28f302002-02-15 20:48:08 +0000593 if (!isprint((unsigned char) buf[i])) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000594 }
595 if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
596 if (len >= 4) {
597 MEMHDR *q;
598 void *cur;
599
Daniel Veillardf4721d62006-10-11 21:12:10 +0000600 for (j = 0;(j < len -3) && (j < 40);j += 4) {
Owen Taylor3473f882001-02-23 17:55:21 +0000601 cur = *((void **) &buf[j]);
602 q = CLIENT_2_HDR(cur);
603 p = memlist;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000604 k = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000605 while (p != NULL) {
606 if (p == q) break;
607 p = p->mh_next;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000608 if (k++ > 100) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000609 }
610 if ((p != NULL) && (p == q)) {
611 fprintf(fp, " pointer to #%lu at index %d",
612 p->mh_number, j);
613 return;
614 }
615 }
616 }
617 } else if ((i == 0) && (buf[i] == 0)) {
618 fprintf(fp," null");
619 } else {
Daniel Veillard09459bf2008-07-30 12:58:11 +0000620 if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000621 else {
622 fprintf(fp," [");
623 for (j = 0;j < i;j++)
624 fprintf(fp,"%c", buf[j]);
625 fprintf(fp,"]");
626 }
627 }
628}
629#endif
630
631/**
Daniel Veillard09459bf2008-07-30 12:58:11 +0000632 * xmlMemDisplayLast:
633 * @fp: a FILE descriptor used as the output file, if NULL, the result is
634 * written to the file .memorylist
635 * @nbBytes: the amount of memory to dump
636 *
637 * the last nbBytes of memory allocated and not freed, useful for dumping
638 * the memory left allocated between two places at runtime.
639 */
640
641void
642xmlMemDisplayLast(FILE *fp, long nbBytes)
643{
644#ifdef MEM_LIST
645 MEMHDR *p;
646 unsigned idx;
647 int nb = 0;
648#endif
649 FILE *old_fp = fp;
650
651 if (nbBytes <= 0)
652 return;
653
654 if (fp == NULL) {
655 fp = fopen(".memorylist", "w");
656 if (fp == NULL)
657 return;
658 }
659
660#ifdef MEM_LIST
661 fprintf(fp," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
662 nbBytes, debugMemSize, debugMaxMemSize);
663 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
664 idx = 0;
665 xmlMutexLock(xmlMemMutex);
666 p = memlist;
667 while ((p) && (nbBytes > 0)) {
668 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
669 (unsigned long)p->mh_size);
670 switch (p->mh_type) {
671 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
672 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
673 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
674 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
675 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
676 default:
677 fprintf(fp,"Unknown memory block, may be corrupted");
678 xmlMutexUnlock(xmlMemMutex);
679 if (old_fp == NULL)
680 fclose(fp);
681 return;
682 }
683 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
684 if (p->mh_tag != MEMTAG)
685 fprintf(fp," INVALID");
686 nb++;
687 if (nb < 100)
688 xmlMemContentShow(fp, p);
689 else
690 fprintf(fp," skip");
691
692 fprintf(fp,"\n");
693 nbBytes -= (unsigned long)p->mh_size;
694 p = p->mh_next;
695 }
696 xmlMutexUnlock(xmlMemMutex);
697#else
698 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
699#endif
700 if (old_fp == NULL)
701 fclose(fp);
702}
703
704/**
Owen Taylor3473f882001-02-23 17:55:21 +0000705 * xmlMemDisplay:
706 * @fp: a FILE descriptor used as the output file, if NULL, the result is
707 * written to the file .memorylist
708 *
709 * show in-extenso the memory blocks allocated
710 */
711
712void
713xmlMemDisplay(FILE *fp)
714{
715#ifdef MEM_LIST
716 MEMHDR *p;
Daniel Veillard144024e2002-02-13 21:14:46 +0000717 unsigned idx;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000718 int nb = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000719#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
720 time_t currentTime;
721 char buf[500];
722 struct tm * tstruct;
Daniel Veillard942d6c72005-05-08 11:39:56 +0000723#endif
724#endif
725 FILE *old_fp = fp;
Owen Taylor3473f882001-02-23 17:55:21 +0000726
Daniel Veillard942d6c72005-05-08 11:39:56 +0000727 if (fp == NULL) {
728 fp = fopen(".memorylist", "w");
729 if (fp == NULL)
730 return;
731 }
732
733#ifdef MEM_LIST
734#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Owen Taylor3473f882001-02-23 17:55:21 +0000735 currentTime = time(NULL);
736 tstruct = localtime(&currentTime);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000737 strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Owen Taylor3473f882001-02-23 17:55:21 +0000738 fprintf(fp," %s\n\n", buf);
739#endif
740
Daniel Veillard09459bf2008-07-30 12:58:11 +0000741
Owen Taylor3473f882001-02-23 17:55:21 +0000742 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
743 debugMemSize, debugMaxMemSize);
744 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
745 idx = 0;
William M. Brack0622fe82003-11-29 10:47:56 +0000746 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000747 p = memlist;
748 while (p) {
Daniel Veillard144024e2002-02-13 21:14:46 +0000749 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
750 (unsigned long)p->mh_size);
Owen Taylor3473f882001-02-23 17:55:21 +0000751 switch (p->mh_type) {
752 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
753 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000754 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000755 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000756 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
757 default:
William M. Brack13dfa872004-09-18 04:52:08 +0000758 fprintf(fp,"Unknown memory block, may be corrupted");
Daniel Veillard529233c2004-07-02 12:23:44 +0000759 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard942d6c72005-05-08 11:39:56 +0000760 if (old_fp == NULL)
761 fclose(fp);
Daniel Veillard529233c2004-07-02 12:23:44 +0000762 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000763 }
William M. Brack13dfa872004-09-18 04:52:08 +0000764 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Owen Taylor3473f882001-02-23 17:55:21 +0000765 if (p->mh_tag != MEMTAG)
766 fprintf(fp," INVALID");
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000767 nb++;
768 if (nb < 100)
769 xmlMemContentShow(fp, p);
770 else
771 fprintf(fp," skip");
772
Owen Taylor3473f882001-02-23 17:55:21 +0000773 fprintf(fp,"\n");
774 p = p->mh_next;
775 }
William M. Brack0622fe82003-11-29 10:47:56 +0000776 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000777#else
778 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
779#endif
Daniel Veillard942d6c72005-05-08 11:39:56 +0000780 if (old_fp == NULL)
781 fclose(fp);
Owen Taylor3473f882001-02-23 17:55:21 +0000782}
783
784#ifdef MEM_LIST
785
Daniel Veillard01c13b52002-12-10 15:19:08 +0000786static void debugmem_list_add(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000787{
788 p->mh_next = memlist;
789 p->mh_prev = NULL;
790 if (memlist) memlist->mh_prev = p;
791 memlist = p;
792#ifdef MEM_LIST_DEBUG
793 if (stderr)
794 Mem_Display(stderr);
795#endif
796}
797
Daniel Veillard01c13b52002-12-10 15:19:08 +0000798static void debugmem_list_delete(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000799{
800 if (p->mh_next)
801 p->mh_next->mh_prev = p->mh_prev;
802 if (p->mh_prev)
803 p->mh_prev->mh_next = p->mh_next;
804 else memlist = p->mh_next;
805#ifdef MEM_LIST_DEBUG
806 if (stderr)
807 Mem_Display(stderr);
808#endif
809}
810
811#endif
812
813/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000814 * debugmem_tag_error:
815 *
816 * internal error function.
Owen Taylor3473f882001-02-23 17:55:21 +0000817 */
Daniel Veillard09459bf2008-07-30 12:58:11 +0000818
Daniel Veillard01c13b52002-12-10 15:19:08 +0000819static void debugmem_tag_error(void *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000820{
821 xmlGenericError(xmlGenericErrorContext,
822 "Memory tag error occurs :%p \n\t bye\n", p);
823#ifdef MEM_LIST
824 if (stderr)
825 xmlMemDisplay(stderr);
826#endif
827}
828
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000829#ifdef MEM_LIST
Daniel Veillardb44025c2001-10-11 22:55:55 +0000830static FILE *xmlMemoryDumpFile = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000831#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000832
Owen Taylor3473f882001-02-23 17:55:21 +0000833/**
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000834 * xmlMemShow:
835 * @fp: a FILE descriptor used as the output file
836 * @nr: number of entries to dump
837 *
838 * show a show display of the memory allocated, and dump
839 * the @nr last allocated areas which were not freed
840 */
841
842void
Daniel Veillardc064b472003-09-29 10:55:05 +0000843xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000844{
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000845#ifdef MEM_LIST
846 MEMHDR *p;
847#endif
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000848
849 if (fp != NULL)
850 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
851 debugMemSize, debugMaxMemSize);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000852#ifdef MEM_LIST
William M. Brack0622fe82003-11-29 10:47:56 +0000853 xmlMutexLock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000854 if (nr > 0) {
855 fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
856 p = memlist;
857 while ((p) && nr > 0) {
858 fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
859 switch (p->mh_type) {
860 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
861 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
862 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
863 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
864 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
865 default:fprintf(fp," ??? in ");break;
866 }
867 if (p->mh_file != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +0000868 fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000869 if (p->mh_tag != MEMTAG)
870 fprintf(fp," INVALID");
871 xmlMemContentShow(fp, p);
872 fprintf(fp,"\n");
873 nr--;
874 p = p->mh_next;
875 }
876 }
William M. Brack0622fe82003-11-29 10:47:56 +0000877 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000878#endif /* MEM_LIST */
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000879}
880
881/**
Owen Taylor3473f882001-02-23 17:55:21 +0000882 * xmlMemoryDump:
883 *
884 * Dump in-extenso the memory blocks allocated to the file .memorylist
885 */
886
887void
888xmlMemoryDump(void)
889{
Daniel Veillardc064b472003-09-29 10:55:05 +0000890#ifdef MEM_LIST
Owen Taylor3473f882001-02-23 17:55:21 +0000891 FILE *dump;
892
Daniel Veillard5997aca2002-03-18 18:36:20 +0000893 if (debugMaxMemSize == 0)
894 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000895 dump = fopen(".memdump", "w");
Daniel Veillardcd337f02001-11-22 18:20:37 +0000896 if (dump == NULL)
897 xmlMemoryDumpFile = stderr;
Owen Taylor3473f882001-02-23 17:55:21 +0000898 else xmlMemoryDumpFile = dump;
899
900 xmlMemDisplay(xmlMemoryDumpFile);
901
902 if (dump != NULL) fclose(dump);
Daniel Veillardc064b472003-09-29 10:55:05 +0000903#endif /* MEM_LIST */
Owen Taylor3473f882001-02-23 17:55:21 +0000904}
905
906
907/****************************************************************
908 * *
909 * Initialization Routines *
910 * *
911 ****************************************************************/
912
Owen Taylor3473f882001-02-23 17:55:21 +0000913/**
914 * xmlInitMemory:
915 *
916 * Initialize the memory layer.
917 *
918 * Returns 0 on success
919 */
Owen Taylor3473f882001-02-23 17:55:21 +0000920int
921xmlInitMemory(void)
922{
Daniel Veillarde15df582004-07-13 15:25:08 +0000923#ifdef HAVE_STDLIB_H
924 char *breakpoint;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000925#endif
Daniel Veillardf93a8662004-07-01 12:56:30 +0000926#ifdef DEBUG_MEMORY
927 xmlGenericError(xmlGenericErrorContext,
928 "xmlInitMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000929#endif
William M. Brack92029422004-01-04 01:01:14 +0000930 /*
931 This is really not good code (see Bug 130419). Suggestions for
932 improvement will be welcome!
933 */
934 if (xmlMemInitialized) return(-1);
William M. Brack0622fe82003-11-29 10:47:56 +0000935 xmlMemInitialized = 1;
William M. Brack0622fe82003-11-29 10:47:56 +0000936 xmlMemMutex = xmlNewMutex();
Owen Taylor3473f882001-02-23 17:55:21 +0000937
938#ifdef HAVE_STDLIB_H
939 breakpoint = getenv("XML_MEM_BREAKPOINT");
940 if (breakpoint != NULL) {
William M. Brack0622fe82003-11-29 10:47:56 +0000941 sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Owen Taylor3473f882001-02-23 17:55:21 +0000942 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000943#endif
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000944#ifdef HAVE_STDLIB_H
945 breakpoint = getenv("XML_MEM_TRACE");
946 if (breakpoint != NULL) {
947 sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
948 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000949#endif
950
Owen Taylor3473f882001-02-23 17:55:21 +0000951#ifdef DEBUG_MEMORY
952 xmlGenericError(xmlGenericErrorContext,
953 "xmlInitMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000954#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000955 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000956}
957
958/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000959 * xmlCleanupMemory:
960 *
Daniel Veillard91b955c2004-12-10 10:26:42 +0000961 * Free up all the memory allocated by the library for its own
962 * use. This should not be called by user level code.
William M. Brack72ee48d2003-12-30 08:30:19 +0000963 */
964void
965xmlCleanupMemory(void) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000966#ifdef DEBUG_MEMORY
967 xmlGenericError(xmlGenericErrorContext,
968 "xmlCleanupMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000969#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000970 if (xmlMemInitialized == 0)
971 return;
972
973 xmlFreeMutex(xmlMemMutex);
Daniel Veillard1a9b7082004-01-02 10:42:01 +0000974 xmlMemMutex = NULL;
William M. Brack72ee48d2003-12-30 08:30:19 +0000975 xmlMemInitialized = 0;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000976#ifdef DEBUG_MEMORY
977 xmlGenericError(xmlGenericErrorContext,
978 "xmlCleanupMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000979#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000980}
981
982/**
Owen Taylor3473f882001-02-23 17:55:21 +0000983 * xmlMemSetup:
984 * @freeFunc: the free() function to use
985 * @mallocFunc: the malloc() function to use
986 * @reallocFunc: the realloc() function to use
987 * @strdupFunc: the strdup() function to use
988 *
989 * Override the default memory access functions with a new set
990 * This has to be called before any other libxml routines !
991 *
992 * Should this be blocked if there was already some allocations
993 * done ?
994 *
995 * Returns 0 on success
996 */
997int
998xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
999 xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001000#ifdef DEBUG_MEMORY
1001 xmlGenericError(xmlGenericErrorContext,
1002 "xmlMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001003#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001004 if (freeFunc == NULL)
1005 return(-1);
1006 if (mallocFunc == NULL)
1007 return(-1);
1008 if (reallocFunc == NULL)
1009 return(-1);
1010 if (strdupFunc == NULL)
1011 return(-1);
1012 xmlFree = freeFunc;
1013 xmlMalloc = mallocFunc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001014 xmlMallocAtomic = mallocFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001015 xmlRealloc = reallocFunc;
1016 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001017#ifdef DEBUG_MEMORY
1018 xmlGenericError(xmlGenericErrorContext,
1019 "xmlMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001020#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001021 return(0);
1022}
1023
1024/**
1025 * xmlMemGet:
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001026 * @freeFunc: place to save the free() function in use
1027 * @mallocFunc: place to save the malloc() function in use
1028 * @reallocFunc: place to save the realloc() function in use
1029 * @strdupFunc: place to save the strdup() function in use
Owen Taylor3473f882001-02-23 17:55:21 +00001030 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001031 * Provides the memory access functions set currently in use
Owen Taylor3473f882001-02-23 17:55:21 +00001032 *
1033 * Returns 0 on success
1034 */
1035int
1036xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1037 xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
1038 if (freeFunc != NULL) *freeFunc = xmlFree;
1039 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1040 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1041 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1042 return(0);
1043}
1044
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001045/**
1046 * xmlGcMemSetup:
1047 * @freeFunc: the free() function to use
1048 * @mallocFunc: the malloc() function to use
1049 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1050 * @reallocFunc: the realloc() function to use
1051 * @strdupFunc: the strdup() function to use
1052 *
1053 * Override the default memory access functions with a new set
1054 * This has to be called before any other libxml routines !
1055 * The mallocAtomicFunc is specialized for atomic block
1056 * allocations (i.e. of areas useful for garbage collected memory allocators
1057 *
1058 * Should this be blocked if there was already some allocations
1059 * done ?
1060 *
1061 * Returns 0 on success
1062 */
1063int
1064xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1065 xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
1066 xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001067#ifdef DEBUG_MEMORY
1068 xmlGenericError(xmlGenericErrorContext,
1069 "xmlGcMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001070#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001071 if (freeFunc == NULL)
1072 return(-1);
1073 if (mallocFunc == NULL)
1074 return(-1);
1075 if (mallocAtomicFunc == NULL)
1076 return(-1);
1077 if (reallocFunc == NULL)
1078 return(-1);
1079 if (strdupFunc == NULL)
1080 return(-1);
1081 xmlFree = freeFunc;
1082 xmlMalloc = mallocFunc;
1083 xmlMallocAtomic = mallocAtomicFunc;
1084 xmlRealloc = reallocFunc;
1085 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001086#ifdef DEBUG_MEMORY
1087 xmlGenericError(xmlGenericErrorContext,
1088 "xmlGcMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001089#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001090 return(0);
1091}
1092
1093/**
1094 * xmlGcMemGet:
1095 * @freeFunc: place to save the free() function in use
1096 * @mallocFunc: place to save the malloc() function in use
1097 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1098 * @reallocFunc: place to save the realloc() function in use
1099 * @strdupFunc: place to save the strdup() function in use
1100 *
1101 * Provides the memory access functions set currently in use
1102 * The mallocAtomicFunc is specialized for atomic block
1103 * allocations (i.e. of areas useful for garbage collected memory allocators
1104 *
1105 * Returns 0 on success
1106 */
1107int
1108xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1109 xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
1110 xmlStrdupFunc *strdupFunc) {
1111 if (freeFunc != NULL) *freeFunc = xmlFree;
1112 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1113 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
1114 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1115 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1116 return(0);
1117}
1118
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001119#define bottom_xmlmemory
1120#include "elfgcchack.h"