blob: 37dcf3b4f1f0d86119a9ae1ca291bca08fd89c7e [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 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080061 * Macros, variables and associated types *
Daniel Veillard56a4cb82001-03-24 17:00:36 +000062 * *
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,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300208 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
209 (long unsigned)size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000210 xmlMallocBreakpoint();
211 }
212
Owen Taylor3473f882001-02-23 17:55:21 +0000213 TEST_POINT
214
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000215 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000216}
217
218/**
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000219 * xmlMallocAtomicLoc:
220 * @size: an int specifying the size in byte to allocate.
221 * @file: the file name or NULL
222 * @line: the line number
223 *
224 * a malloc() equivalent, with logging of the allocation info.
225 *
226 * Returns a pointer to the allocated area or NULL in case of lack of memory.
227 */
228
229void *
230xmlMallocAtomicLoc(size_t size, const char * file, int line)
231{
232 MEMHDR *p;
233 void *ret;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000234
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000235 if (!xmlMemInitialized) xmlInitMemory();
236#ifdef DEBUG_MEMORY
237 xmlGenericError(xmlGenericErrorContext,
238 "Malloc(%d)\n",size);
239#endif
240
241 TEST_POINT
Daniel Veillard09459bf2008-07-30 12:58:11 +0000242
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000243 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
244
245 if (!p) {
246 xmlGenericError(xmlGenericErrorContext,
247 "xmlMallocLoc : Out of free space\n");
248 xmlMemoryDump();
249 return(NULL);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000250 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000251 p->mh_tag = MEMTAG;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000252 p->mh_size = size;
253 p->mh_type = MALLOC_ATOMIC_TYPE;
254 p->mh_file = file;
255 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000256 xmlMutexLock(xmlMemMutex);
257 p->mh_number = ++block;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000258 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000259 debugMemBlocks++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000260 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
261#ifdef MEM_LIST
262 debugmem_list_add(p);
263#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000264 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000265
266#ifdef DEBUG_MEMORY
267 xmlGenericError(xmlGenericErrorContext,
268 "Malloc(%d) Ok\n",size);
269#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000270
William M. Brack0622fe82003-11-29 10:47:56 +0000271 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000272
273 ret = HDR_2_CLIENT(p);
274
275 if (xmlMemTraceBlockAt == ret) {
276 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300277 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
278 (long unsigned)size);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000279 xmlMallocBreakpoint();
280 }
281
282 TEST_POINT
283
284 return(ret);
285}
286/**
Owen Taylor3473f882001-02-23 17:55:21 +0000287 * xmlMemMalloc:
288 * @size: an int specifying the size in byte to allocate.
289 *
290 * a malloc() equivalent, with logging of the allocation info.
291 *
292 * Returns a pointer to the allocated area or NULL in case of lack of memory.
293 */
294
295void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000296xmlMemMalloc(size_t size)
Owen Taylor3473f882001-02-23 17:55:21 +0000297{
298 return(xmlMallocLoc(size, "none", 0));
299}
300
301/**
302 * xmlReallocLoc:
303 * @ptr: the initial memory block pointer
304 * @size: an int specifying the size in byte to allocate.
305 * @file: the file name or NULL
306 * @line: the line number
307 *
308 * a realloc() equivalent, with logging of the allocation info.
309 *
310 * Returns a pointer to the allocated area or NULL in case of lack of memory.
311 */
312
313void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000314xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
Owen Taylor3473f882001-02-23 17:55:21 +0000315{
316 MEMHDR *p;
317 unsigned long number;
Daniel Veillard529233c2004-07-02 12:23:44 +0000318#ifdef DEBUG_MEMORY
319 size_t oldsize;
320#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000321
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000322 if (ptr == NULL)
Aleksey Sanine9f08112004-01-22 22:20:31 +0000323 return(xmlMallocLoc(size, file, line));
324
325 if (!xmlMemInitialized) xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +0000326 TEST_POINT
327
328 p = CLIENT_2_HDR(ptr);
329 number = p->mh_number;
Daniel Veillard18ffe202005-04-14 17:50:59 +0000330 if (xmlMemStopAtBlock == number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000331 if (p->mh_tag != MEMTAG) {
332 Mem_Tag_Err(p);
333 goto error;
334 }
335 p->mh_tag = ~MEMTAG;
William M. Brack0622fe82003-11-29 10:47:56 +0000336 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000337 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000338 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000339#ifdef DEBUG_MEMORY
340 oldsize = p->mh_size;
341#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000342#ifdef MEM_LIST
343 debugmem_list_delete(p);
344#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000345 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000346
Owen Taylor3473f882001-02-23 17:55:21 +0000347 p = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
348 if (!p) {
349 goto error;
350 }
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000351 if (xmlMemTraceBlockAt == ptr) {
352 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300353 "%p : Realloced(%lu -> %lu) Ok\n",
354 xmlMemTraceBlockAt, (long unsigned)p->mh_size,
355 (long unsigned)size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000356 xmlMallocBreakpoint();
357 }
Owen Taylor3473f882001-02-23 17:55:21 +0000358 p->mh_tag = MEMTAG;
359 p->mh_number = number;
360 p->mh_type = REALLOC_TYPE;
361 p->mh_size = size;
362 p->mh_file = file;
363 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000364 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000365 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000366 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000367 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
368#ifdef MEM_LIST
369 debugmem_list_add(p);
370#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000371 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000372
373 TEST_POINT
374
Daniel Veillard529233c2004-07-02 12:23:44 +0000375#ifdef DEBUG_MEMORY
376 xmlGenericError(xmlGenericErrorContext,
377 "Realloced(%d to %d) Ok\n", oldsize, size);
378#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000379 return(HDR_2_CLIENT(p));
Daniel Veillard09459bf2008-07-30 12:58:11 +0000380
381error:
Owen Taylor3473f882001-02-23 17:55:21 +0000382 return(NULL);
383}
384
385/**
386 * xmlMemRealloc:
387 * @ptr: the initial memory block pointer
388 * @size: an int specifying the size in byte to allocate.
389 *
390 * a realloc() equivalent, with logging of the allocation info.
391 *
392 * Returns a pointer to the allocated area or NULL in case of lack of memory.
393 */
394
395void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000396xmlMemRealloc(void *ptr,size_t size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000397 return(xmlReallocLoc(ptr, size, "none", 0));
398}
399
400/**
401 * xmlMemFree:
402 * @ptr: the memory block pointer
403 *
404 * a free() equivalent, with error checking.
405 */
406void
407xmlMemFree(void *ptr)
408{
409 MEMHDR *p;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000410 char *target;
Daniel Veillard529233c2004-07-02 12:23:44 +0000411#ifdef DEBUG_MEMORY
412 size_t size;
413#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000414
Daniel Veillard2a512da2007-10-30 20:24:40 +0000415 if (ptr == NULL)
416 return;
417
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000418 if (ptr == (void *) -1) {
419 xmlGenericError(xmlGenericErrorContext,
420 "trying to free pointer from freed area\n");
421 goto error;
422 }
423
424 if (xmlMemTraceBlockAt == ptr) {
425 xmlGenericError(xmlGenericErrorContext,
426 "%p : Freed()\n", xmlMemTraceBlockAt);
427 xmlMallocBreakpoint();
428 }
429
Owen Taylor3473f882001-02-23 17:55:21 +0000430 TEST_POINT
431
Daniel Veillard92ad2102001-03-27 12:47:33 +0000432 target = (char *) ptr;
433
Owen Taylor3473f882001-02-23 17:55:21 +0000434 p = CLIENT_2_HDR(ptr);
435 if (p->mh_tag != MEMTAG) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000436 Mem_Tag_Err(p);
437 goto error;
Owen Taylor3473f882001-02-23 17:55:21 +0000438 }
Daniel Veillard18ffe202005-04-14 17:50:59 +0000439 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000440 p->mh_tag = ~MEMTAG;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000441 memset(target, -1, p->mh_size);
William M. Brack0622fe82003-11-29 10:47:56 +0000442 xmlMutexLock(xmlMemMutex);
443 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000444 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000445#ifdef DEBUG_MEMORY
446 size = p->mh_size;
447#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000448#ifdef MEM_LIST
449 debugmem_list_delete(p);
450#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000451 xmlMutexUnlock(xmlMemMutex);
452
Owen Taylor3473f882001-02-23 17:55:21 +0000453 free(p);
454
455 TEST_POINT
456
Daniel Veillard529233c2004-07-02 12:23:44 +0000457#ifdef DEBUG_MEMORY
458 xmlGenericError(xmlGenericErrorContext,
459 "Freed(%d) Ok\n", size);
460#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000461
Owen Taylor3473f882001-02-23 17:55:21 +0000462 return;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000463
464error:
Owen Taylor3473f882001-02-23 17:55:21 +0000465 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000466 "xmlMemFree(%lX) error\n", (unsigned long) ptr);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000467 xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000468 return;
469}
470
471/**
472 * xmlMemStrdupLoc:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000473 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000474 * @file: the file name or NULL
475 * @line: the line number
476 *
477 * a strdup() equivalent, with logging of the allocation info.
478 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000479 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000480 */
481
482char *
483xmlMemStrdupLoc(const char *str, const char *file, int line)
484{
485 char *s;
486 size_t size = strlen(str) + 1;
487 MEMHDR *p;
488
489 if (!xmlMemInitialized) xmlInitMemory();
490 TEST_POINT
491
492 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
493 if (!p) {
494 goto error;
495 }
496 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000497 p->mh_size = size;
498 p->mh_type = STRDUP_TYPE;
499 p->mh_file = file;
500 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000501 xmlMutexLock(xmlMemMutex);
502 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000503 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000504 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000505 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
506#ifdef MEM_LIST
507 debugmem_list_add(p);
508#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000509 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000510
Owen Taylor3473f882001-02-23 17:55:21 +0000511 s = (char *) HDR_2_CLIENT(p);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000512
William M. Brack0622fe82003-11-29 10:47:56 +0000513 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000514
515 if (s != NULL)
516 strcpy(s,str);
517 else
518 goto error;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000519
Owen Taylor3473f882001-02-23 17:55:21 +0000520 TEST_POINT
521
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000522 if (xmlMemTraceBlockAt == s) {
523 xmlGenericError(xmlGenericErrorContext,
524 "%p : Strdup() Ok\n", xmlMemTraceBlockAt);
525 xmlMallocBreakpoint();
526 }
527
Owen Taylor3473f882001-02-23 17:55:21 +0000528 return(s);
529
530error:
531 return(NULL);
532}
533
534/**
535 * xmlMemoryStrdup:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000536 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000537 *
538 * a strdup() equivalent, with logging of the allocation info.
539 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000540 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000541 */
542
543char *
544xmlMemoryStrdup(const char *str) {
545 return(xmlMemStrdupLoc(str, "none", 0));
546}
547
548/**
549 * xmlMemUsed:
550 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000551 * Provides the amount of memory currently allocated
Owen Taylor3473f882001-02-23 17:55:21 +0000552 *
553 * Returns an int representing the amount of memory allocated.
554 */
555
556int
557xmlMemUsed(void) {
558 return(debugMemSize);
559}
560
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000561/**
562 * xmlMemBlocks:
563 *
564 * Provides the number of memory areas currently allocated
565 *
566 * Returns an int representing the number of blocks
567 */
568
569int
570xmlMemBlocks(void) {
571 return(debugMemBlocks);
572}
573
Owen Taylor3473f882001-02-23 17:55:21 +0000574#ifdef MEM_LIST
575/**
576 * xmlMemContentShow:
577 * @fp: a FILE descriptor used as the output file
578 * @p: a memory block header
579 *
580 * tries to show some content from the memory block
581 */
582
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000583static void
Owen Taylor3473f882001-02-23 17:55:21 +0000584xmlMemContentShow(FILE *fp, MEMHDR *p)
585{
Gaurav7966a762014-05-09 17:00:08 +0800586 int i,j,k,len;
587 const char *buf;
Owen Taylor3473f882001-02-23 17:55:21 +0000588
589 if (p == NULL) {
590 fprintf(fp, " NULL");
591 return;
592 }
Gaurav7966a762014-05-09 17:00:08 +0800593 len = p->mh_size;
594 buf = (const char *) HDR_2_CLIENT(p);
Owen Taylor3473f882001-02-23 17:55:21 +0000595
596 for (i = 0;i < len;i++) {
597 if (buf[i] == 0) break;
Daniel Veillard9f28f302002-02-15 20:48:08 +0000598 if (!isprint((unsigned char) buf[i])) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000599 }
600 if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
601 if (len >= 4) {
602 MEMHDR *q;
603 void *cur;
604
Daniel Veillardf4721d62006-10-11 21:12:10 +0000605 for (j = 0;(j < len -3) && (j < 40);j += 4) {
Owen Taylor3473f882001-02-23 17:55:21 +0000606 cur = *((void **) &buf[j]);
607 q = CLIENT_2_HDR(cur);
608 p = memlist;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000609 k = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000610 while (p != NULL) {
611 if (p == q) break;
612 p = p->mh_next;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000613 if (k++ > 100) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000614 }
615 if ((p != NULL) && (p == q)) {
616 fprintf(fp, " pointer to #%lu at index %d",
617 p->mh_number, j);
618 return;
619 }
620 }
621 }
622 } else if ((i == 0) && (buf[i] == 0)) {
623 fprintf(fp," null");
624 } else {
Daniel Veillard09459bf2008-07-30 12:58:11 +0000625 if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000626 else {
627 fprintf(fp," [");
628 for (j = 0;j < i;j++)
629 fprintf(fp,"%c", buf[j]);
630 fprintf(fp,"]");
631 }
632 }
633}
634#endif
635
636/**
Daniel Veillard09459bf2008-07-30 12:58:11 +0000637 * xmlMemDisplayLast:
638 * @fp: a FILE descriptor used as the output file, if NULL, the result is
639 * written to the file .memorylist
640 * @nbBytes: the amount of memory to dump
641 *
642 * the last nbBytes of memory allocated and not freed, useful for dumping
643 * the memory left allocated between two places at runtime.
644 */
645
646void
647xmlMemDisplayLast(FILE *fp, long nbBytes)
648{
649#ifdef MEM_LIST
650 MEMHDR *p;
651 unsigned idx;
652 int nb = 0;
653#endif
654 FILE *old_fp = fp;
655
656 if (nbBytes <= 0)
657 return;
658
659 if (fp == NULL) {
660 fp = fopen(".memorylist", "w");
661 if (fp == NULL)
662 return;
663 }
664
665#ifdef MEM_LIST
666 fprintf(fp," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
667 nbBytes, debugMemSize, debugMaxMemSize);
668 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
669 idx = 0;
670 xmlMutexLock(xmlMemMutex);
671 p = memlist;
672 while ((p) && (nbBytes > 0)) {
673 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
674 (unsigned long)p->mh_size);
675 switch (p->mh_type) {
676 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
677 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
678 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
679 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
680 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
681 default:
682 fprintf(fp,"Unknown memory block, may be corrupted");
683 xmlMutexUnlock(xmlMemMutex);
684 if (old_fp == NULL)
685 fclose(fp);
686 return;
687 }
688 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
689 if (p->mh_tag != MEMTAG)
690 fprintf(fp," INVALID");
691 nb++;
692 if (nb < 100)
693 xmlMemContentShow(fp, p);
694 else
695 fprintf(fp," skip");
696
697 fprintf(fp,"\n");
698 nbBytes -= (unsigned long)p->mh_size;
699 p = p->mh_next;
700 }
701 xmlMutexUnlock(xmlMemMutex);
702#else
703 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
704#endif
705 if (old_fp == NULL)
706 fclose(fp);
707}
708
709/**
Owen Taylor3473f882001-02-23 17:55:21 +0000710 * xmlMemDisplay:
711 * @fp: a FILE descriptor used as the output file, if NULL, the result is
712 * written to the file .memorylist
713 *
714 * show in-extenso the memory blocks allocated
715 */
716
717void
718xmlMemDisplay(FILE *fp)
719{
720#ifdef MEM_LIST
721 MEMHDR *p;
Daniel Veillard144024e2002-02-13 21:14:46 +0000722 unsigned idx;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000723 int nb = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000724#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
725 time_t currentTime;
726 char buf[500];
727 struct tm * tstruct;
Daniel Veillard942d6c72005-05-08 11:39:56 +0000728#endif
729#endif
730 FILE *old_fp = fp;
Owen Taylor3473f882001-02-23 17:55:21 +0000731
Daniel Veillard942d6c72005-05-08 11:39:56 +0000732 if (fp == NULL) {
733 fp = fopen(".memorylist", "w");
734 if (fp == NULL)
735 return;
736 }
737
738#ifdef MEM_LIST
739#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Owen Taylor3473f882001-02-23 17:55:21 +0000740 currentTime = time(NULL);
741 tstruct = localtime(&currentTime);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000742 strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Owen Taylor3473f882001-02-23 17:55:21 +0000743 fprintf(fp," %s\n\n", buf);
744#endif
745
Daniel Veillard09459bf2008-07-30 12:58:11 +0000746
Owen Taylor3473f882001-02-23 17:55:21 +0000747 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
748 debugMemSize, debugMaxMemSize);
749 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
750 idx = 0;
William M. Brack0622fe82003-11-29 10:47:56 +0000751 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000752 p = memlist;
753 while (p) {
Daniel Veillard144024e2002-02-13 21:14:46 +0000754 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
755 (unsigned long)p->mh_size);
Owen Taylor3473f882001-02-23 17:55:21 +0000756 switch (p->mh_type) {
757 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
758 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000759 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000760 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000761 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
762 default:
William M. Brack13dfa872004-09-18 04:52:08 +0000763 fprintf(fp,"Unknown memory block, may be corrupted");
Daniel Veillard529233c2004-07-02 12:23:44 +0000764 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard942d6c72005-05-08 11:39:56 +0000765 if (old_fp == NULL)
766 fclose(fp);
Daniel Veillard529233c2004-07-02 12:23:44 +0000767 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000768 }
William M. Brack13dfa872004-09-18 04:52:08 +0000769 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Owen Taylor3473f882001-02-23 17:55:21 +0000770 if (p->mh_tag != MEMTAG)
771 fprintf(fp," INVALID");
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000772 nb++;
773 if (nb < 100)
774 xmlMemContentShow(fp, p);
775 else
776 fprintf(fp," skip");
777
Owen Taylor3473f882001-02-23 17:55:21 +0000778 fprintf(fp,"\n");
779 p = p->mh_next;
780 }
William M. Brack0622fe82003-11-29 10:47:56 +0000781 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000782#else
783 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
784#endif
Daniel Veillard942d6c72005-05-08 11:39:56 +0000785 if (old_fp == NULL)
786 fclose(fp);
Owen Taylor3473f882001-02-23 17:55:21 +0000787}
788
789#ifdef MEM_LIST
790
Daniel Veillard01c13b52002-12-10 15:19:08 +0000791static void debugmem_list_add(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000792{
793 p->mh_next = memlist;
794 p->mh_prev = NULL;
795 if (memlist) memlist->mh_prev = p;
796 memlist = p;
797#ifdef MEM_LIST_DEBUG
798 if (stderr)
799 Mem_Display(stderr);
800#endif
801}
802
Daniel Veillard01c13b52002-12-10 15:19:08 +0000803static void debugmem_list_delete(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000804{
805 if (p->mh_next)
806 p->mh_next->mh_prev = p->mh_prev;
807 if (p->mh_prev)
808 p->mh_prev->mh_next = p->mh_next;
809 else memlist = p->mh_next;
810#ifdef MEM_LIST_DEBUG
811 if (stderr)
812 Mem_Display(stderr);
813#endif
814}
815
816#endif
817
818/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000819 * debugmem_tag_error:
820 *
821 * internal error function.
Owen Taylor3473f882001-02-23 17:55:21 +0000822 */
Daniel Veillard09459bf2008-07-30 12:58:11 +0000823
Daniel Veillard01c13b52002-12-10 15:19:08 +0000824static void debugmem_tag_error(void *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000825{
826 xmlGenericError(xmlGenericErrorContext,
827 "Memory tag error occurs :%p \n\t bye\n", p);
828#ifdef MEM_LIST
829 if (stderr)
830 xmlMemDisplay(stderr);
831#endif
832}
833
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000834#ifdef MEM_LIST
Daniel Veillardb44025c2001-10-11 22:55:55 +0000835static FILE *xmlMemoryDumpFile = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000836#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000837
Owen Taylor3473f882001-02-23 17:55:21 +0000838/**
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000839 * xmlMemShow:
840 * @fp: a FILE descriptor used as the output file
841 * @nr: number of entries to dump
842 *
843 * show a show display of the memory allocated, and dump
844 * the @nr last allocated areas which were not freed
845 */
846
847void
Daniel Veillardc064b472003-09-29 10:55:05 +0000848xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000849{
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000850#ifdef MEM_LIST
851 MEMHDR *p;
852#endif
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000853
854 if (fp != NULL)
855 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
856 debugMemSize, debugMaxMemSize);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000857#ifdef MEM_LIST
William M. Brack0622fe82003-11-29 10:47:56 +0000858 xmlMutexLock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000859 if (nr > 0) {
860 fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
861 p = memlist;
862 while ((p) && nr > 0) {
863 fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
864 switch (p->mh_type) {
865 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
866 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
867 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
868 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
869 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
870 default:fprintf(fp," ??? in ");break;
871 }
872 if (p->mh_file != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +0000873 fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000874 if (p->mh_tag != MEMTAG)
875 fprintf(fp," INVALID");
876 xmlMemContentShow(fp, p);
877 fprintf(fp,"\n");
878 nr--;
879 p = p->mh_next;
880 }
881 }
William M. Brack0622fe82003-11-29 10:47:56 +0000882 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000883#endif /* MEM_LIST */
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000884}
885
886/**
Owen Taylor3473f882001-02-23 17:55:21 +0000887 * xmlMemoryDump:
888 *
889 * Dump in-extenso the memory blocks allocated to the file .memorylist
890 */
891
892void
893xmlMemoryDump(void)
894{
Daniel Veillardc064b472003-09-29 10:55:05 +0000895#ifdef MEM_LIST
Owen Taylor3473f882001-02-23 17:55:21 +0000896 FILE *dump;
897
Daniel Veillard5997aca2002-03-18 18:36:20 +0000898 if (debugMaxMemSize == 0)
899 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000900 dump = fopen(".memdump", "w");
Daniel Veillardcd337f02001-11-22 18:20:37 +0000901 if (dump == NULL)
902 xmlMemoryDumpFile = stderr;
Owen Taylor3473f882001-02-23 17:55:21 +0000903 else xmlMemoryDumpFile = dump;
904
905 xmlMemDisplay(xmlMemoryDumpFile);
906
907 if (dump != NULL) fclose(dump);
Daniel Veillardc064b472003-09-29 10:55:05 +0000908#endif /* MEM_LIST */
Owen Taylor3473f882001-02-23 17:55:21 +0000909}
910
911
912/****************************************************************
913 * *
914 * Initialization Routines *
915 * *
916 ****************************************************************/
917
Owen Taylor3473f882001-02-23 17:55:21 +0000918/**
919 * xmlInitMemory:
920 *
921 * Initialize the memory layer.
922 *
923 * Returns 0 on success
924 */
Owen Taylor3473f882001-02-23 17:55:21 +0000925int
926xmlInitMemory(void)
927{
Daniel Veillarde15df582004-07-13 15:25:08 +0000928#ifdef HAVE_STDLIB_H
929 char *breakpoint;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000930#endif
Daniel Veillardf93a8662004-07-01 12:56:30 +0000931#ifdef DEBUG_MEMORY
932 xmlGenericError(xmlGenericErrorContext,
933 "xmlInitMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000934#endif
William M. Brack92029422004-01-04 01:01:14 +0000935 /*
936 This is really not good code (see Bug 130419). Suggestions for
937 improvement will be welcome!
938 */
939 if (xmlMemInitialized) return(-1);
William M. Brack0622fe82003-11-29 10:47:56 +0000940 xmlMemInitialized = 1;
William M. Brack0622fe82003-11-29 10:47:56 +0000941 xmlMemMutex = xmlNewMutex();
Owen Taylor3473f882001-02-23 17:55:21 +0000942
943#ifdef HAVE_STDLIB_H
944 breakpoint = getenv("XML_MEM_BREAKPOINT");
945 if (breakpoint != NULL) {
William M. Brack0622fe82003-11-29 10:47:56 +0000946 sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Owen Taylor3473f882001-02-23 17:55:21 +0000947 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000948#endif
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000949#ifdef HAVE_STDLIB_H
950 breakpoint = getenv("XML_MEM_TRACE");
951 if (breakpoint != NULL) {
952 sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
953 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000954#endif
955
Owen Taylor3473f882001-02-23 17:55:21 +0000956#ifdef DEBUG_MEMORY
957 xmlGenericError(xmlGenericErrorContext,
958 "xmlInitMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000959#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000960 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000961}
962
963/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000964 * xmlCleanupMemory:
965 *
Daniel Veillard91b955c2004-12-10 10:26:42 +0000966 * Free up all the memory allocated by the library for its own
967 * use. This should not be called by user level code.
William M. Brack72ee48d2003-12-30 08:30:19 +0000968 */
969void
970xmlCleanupMemory(void) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000971#ifdef DEBUG_MEMORY
972 xmlGenericError(xmlGenericErrorContext,
973 "xmlCleanupMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000974#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000975 if (xmlMemInitialized == 0)
976 return;
977
978 xmlFreeMutex(xmlMemMutex);
Daniel Veillard1a9b7082004-01-02 10:42:01 +0000979 xmlMemMutex = NULL;
William M. Brack72ee48d2003-12-30 08:30:19 +0000980 xmlMemInitialized = 0;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000981#ifdef DEBUG_MEMORY
982 xmlGenericError(xmlGenericErrorContext,
983 "xmlCleanupMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000984#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000985}
986
987/**
Owen Taylor3473f882001-02-23 17:55:21 +0000988 * xmlMemSetup:
989 * @freeFunc: the free() function to use
990 * @mallocFunc: the malloc() function to use
991 * @reallocFunc: the realloc() function to use
992 * @strdupFunc: the strdup() function to use
993 *
994 * Override the default memory access functions with a new set
995 * This has to be called before any other libxml routines !
996 *
997 * Should this be blocked if there was already some allocations
998 * done ?
999 *
1000 * Returns 0 on success
1001 */
1002int
1003xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1004 xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001005#ifdef DEBUG_MEMORY
1006 xmlGenericError(xmlGenericErrorContext,
1007 "xmlMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001008#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001009 if (freeFunc == NULL)
1010 return(-1);
1011 if (mallocFunc == NULL)
1012 return(-1);
1013 if (reallocFunc == NULL)
1014 return(-1);
1015 if (strdupFunc == NULL)
1016 return(-1);
1017 xmlFree = freeFunc;
1018 xmlMalloc = mallocFunc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001019 xmlMallocAtomic = mallocFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001020 xmlRealloc = reallocFunc;
1021 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001022#ifdef DEBUG_MEMORY
1023 xmlGenericError(xmlGenericErrorContext,
1024 "xmlMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001025#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001026 return(0);
1027}
1028
1029/**
1030 * xmlMemGet:
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001031 * @freeFunc: place to save the free() function in use
1032 * @mallocFunc: place to save the malloc() function in use
1033 * @reallocFunc: place to save the realloc() function in use
1034 * @strdupFunc: place to save the strdup() function in use
Owen Taylor3473f882001-02-23 17:55:21 +00001035 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001036 * Provides the memory access functions set currently in use
Owen Taylor3473f882001-02-23 17:55:21 +00001037 *
1038 * Returns 0 on success
1039 */
1040int
1041xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1042 xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
1043 if (freeFunc != NULL) *freeFunc = xmlFree;
1044 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1045 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1046 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1047 return(0);
1048}
1049
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001050/**
1051 * xmlGcMemSetup:
1052 * @freeFunc: the free() function to use
1053 * @mallocFunc: the malloc() function to use
1054 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1055 * @reallocFunc: the realloc() function to use
1056 * @strdupFunc: the strdup() function to use
1057 *
1058 * Override the default memory access functions with a new set
1059 * This has to be called before any other libxml routines !
1060 * The mallocAtomicFunc is specialized for atomic block
1061 * allocations (i.e. of areas useful for garbage collected memory allocators
1062 *
1063 * Should this be blocked if there was already some allocations
1064 * done ?
1065 *
1066 * Returns 0 on success
1067 */
1068int
1069xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1070 xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
1071 xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001072#ifdef DEBUG_MEMORY
1073 xmlGenericError(xmlGenericErrorContext,
1074 "xmlGcMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001075#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001076 if (freeFunc == NULL)
1077 return(-1);
1078 if (mallocFunc == NULL)
1079 return(-1);
1080 if (mallocAtomicFunc == NULL)
1081 return(-1);
1082 if (reallocFunc == NULL)
1083 return(-1);
1084 if (strdupFunc == NULL)
1085 return(-1);
1086 xmlFree = freeFunc;
1087 xmlMalloc = mallocFunc;
1088 xmlMallocAtomic = mallocAtomicFunc;
1089 xmlRealloc = reallocFunc;
1090 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001091#ifdef DEBUG_MEMORY
1092 xmlGenericError(xmlGenericErrorContext,
1093 "xmlGcMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001094#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001095 return(0);
1096}
1097
1098/**
1099 * xmlGcMemGet:
1100 * @freeFunc: place to save the free() function in use
1101 * @mallocFunc: place to save the malloc() function in use
1102 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1103 * @reallocFunc: place to save the realloc() function in use
1104 * @strdupFunc: place to save the strdup() function in use
1105 *
1106 * Provides the memory access functions set currently in use
1107 * The mallocAtomicFunc is specialized for atomic block
1108 * allocations (i.e. of areas useful for garbage collected memory allocators
1109 *
1110 * Returns 0 on success
1111 */
1112int
1113xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1114 xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
1115 xmlStrdupFunc *strdupFunc) {
1116 if (freeFunc != NULL) *freeFunc = xmlFree;
1117 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1118 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
1119 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1120 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1121 return(0);
1122}
1123
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001124#define bottom_xmlmemory
1125#include "elfgcchack.h"