blob: a3dc737fe098c82f55c48742bcd19c3c4c81f7af [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{
Yegor Yefremov74464452014-10-10 12:23:09 +0200316 MEMHDR *p, *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +0000317 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
Yegor Yefremov74464452014-10-10 12:23:09 +0200347 tmp = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
348 if (!tmp) {
349 free(p);
Owen Taylor3473f882001-02-23 17:55:21 +0000350 goto error;
351 }
Yegor Yefremov74464452014-10-10 12:23:09 +0200352 p = tmp;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000353 if (xmlMemTraceBlockAt == ptr) {
354 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300355 "%p : Realloced(%lu -> %lu) Ok\n",
356 xmlMemTraceBlockAt, (long unsigned)p->mh_size,
357 (long unsigned)size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000358 xmlMallocBreakpoint();
359 }
Owen Taylor3473f882001-02-23 17:55:21 +0000360 p->mh_tag = MEMTAG;
361 p->mh_number = number;
362 p->mh_type = REALLOC_TYPE;
363 p->mh_size = size;
364 p->mh_file = file;
365 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000366 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000367 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000368 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000369 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
370#ifdef MEM_LIST
371 debugmem_list_add(p);
372#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000373 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000374
375 TEST_POINT
376
Daniel Veillard529233c2004-07-02 12:23:44 +0000377#ifdef DEBUG_MEMORY
378 xmlGenericError(xmlGenericErrorContext,
379 "Realloced(%d to %d) Ok\n", oldsize, size);
380#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000381 return(HDR_2_CLIENT(p));
Daniel Veillard09459bf2008-07-30 12:58:11 +0000382
383error:
Owen Taylor3473f882001-02-23 17:55:21 +0000384 return(NULL);
385}
386
387/**
388 * xmlMemRealloc:
389 * @ptr: the initial memory block pointer
390 * @size: an int specifying the size in byte to allocate.
391 *
392 * a realloc() equivalent, with logging of the allocation info.
393 *
394 * Returns a pointer to the allocated area or NULL in case of lack of memory.
395 */
396
397void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000398xmlMemRealloc(void *ptr,size_t size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000399 return(xmlReallocLoc(ptr, size, "none", 0));
400}
401
402/**
403 * xmlMemFree:
404 * @ptr: the memory block pointer
405 *
406 * a free() equivalent, with error checking.
407 */
408void
409xmlMemFree(void *ptr)
410{
411 MEMHDR *p;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000412 char *target;
Daniel Veillard529233c2004-07-02 12:23:44 +0000413#ifdef DEBUG_MEMORY
414 size_t size;
415#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000416
Daniel Veillard2a512da2007-10-30 20:24:40 +0000417 if (ptr == NULL)
418 return;
419
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000420 if (ptr == (void *) -1) {
421 xmlGenericError(xmlGenericErrorContext,
422 "trying to free pointer from freed area\n");
423 goto error;
424 }
425
426 if (xmlMemTraceBlockAt == ptr) {
427 xmlGenericError(xmlGenericErrorContext,
428 "%p : Freed()\n", xmlMemTraceBlockAt);
429 xmlMallocBreakpoint();
430 }
431
Owen Taylor3473f882001-02-23 17:55:21 +0000432 TEST_POINT
433
Daniel Veillard92ad2102001-03-27 12:47:33 +0000434 target = (char *) ptr;
435
Owen Taylor3473f882001-02-23 17:55:21 +0000436 p = CLIENT_2_HDR(ptr);
437 if (p->mh_tag != MEMTAG) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000438 Mem_Tag_Err(p);
439 goto error;
Owen Taylor3473f882001-02-23 17:55:21 +0000440 }
Daniel Veillard18ffe202005-04-14 17:50:59 +0000441 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000442 p->mh_tag = ~MEMTAG;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000443 memset(target, -1, p->mh_size);
William M. Brack0622fe82003-11-29 10:47:56 +0000444 xmlMutexLock(xmlMemMutex);
445 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000446 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000447#ifdef DEBUG_MEMORY
448 size = p->mh_size;
449#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000450#ifdef MEM_LIST
451 debugmem_list_delete(p);
452#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000453 xmlMutexUnlock(xmlMemMutex);
454
Owen Taylor3473f882001-02-23 17:55:21 +0000455 free(p);
456
457 TEST_POINT
458
Daniel Veillard529233c2004-07-02 12:23:44 +0000459#ifdef DEBUG_MEMORY
460 xmlGenericError(xmlGenericErrorContext,
461 "Freed(%d) Ok\n", size);
462#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000463
Owen Taylor3473f882001-02-23 17:55:21 +0000464 return;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000465
466error:
Owen Taylor3473f882001-02-23 17:55:21 +0000467 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000468 "xmlMemFree(%lX) error\n", (unsigned long) ptr);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000469 xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000470 return;
471}
472
473/**
474 * xmlMemStrdupLoc:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000475 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000476 * @file: the file name or NULL
477 * @line: the line number
478 *
479 * a strdup() equivalent, with logging of the allocation info.
480 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000481 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000482 */
483
484char *
485xmlMemStrdupLoc(const char *str, const char *file, int line)
486{
487 char *s;
488 size_t size = strlen(str) + 1;
489 MEMHDR *p;
490
491 if (!xmlMemInitialized) xmlInitMemory();
492 TEST_POINT
493
494 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
495 if (!p) {
496 goto error;
497 }
498 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000499 p->mh_size = size;
500 p->mh_type = STRDUP_TYPE;
501 p->mh_file = file;
502 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000503 xmlMutexLock(xmlMemMutex);
504 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000505 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000506 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000507 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
508#ifdef MEM_LIST
509 debugmem_list_add(p);
510#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000511 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000512
Owen Taylor3473f882001-02-23 17:55:21 +0000513 s = (char *) HDR_2_CLIENT(p);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000514
William M. Brack0622fe82003-11-29 10:47:56 +0000515 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000516
Gaurav Guptab8480ae2014-07-26 21:14:53 +0800517 strcpy(s,str);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000518
Owen Taylor3473f882001-02-23 17:55:21 +0000519 TEST_POINT
520
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000521 if (xmlMemTraceBlockAt == s) {
522 xmlGenericError(xmlGenericErrorContext,
523 "%p : Strdup() Ok\n", xmlMemTraceBlockAt);
524 xmlMallocBreakpoint();
525 }
526
Owen Taylor3473f882001-02-23 17:55:21 +0000527 return(s);
528
529error:
530 return(NULL);
531}
532
533/**
534 * xmlMemoryStrdup:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000535 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000536 *
537 * a strdup() equivalent, with logging of the allocation info.
538 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000539 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000540 */
541
542char *
543xmlMemoryStrdup(const char *str) {
544 return(xmlMemStrdupLoc(str, "none", 0));
545}
546
547/**
548 * xmlMemUsed:
549 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000550 * Provides the amount of memory currently allocated
Owen Taylor3473f882001-02-23 17:55:21 +0000551 *
552 * Returns an int representing the amount of memory allocated.
553 */
554
555int
556xmlMemUsed(void) {
557 return(debugMemSize);
558}
559
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000560/**
561 * xmlMemBlocks:
562 *
563 * Provides the number of memory areas currently allocated
564 *
565 * Returns an int representing the number of blocks
566 */
567
568int
569xmlMemBlocks(void) {
570 return(debugMemBlocks);
571}
572
Owen Taylor3473f882001-02-23 17:55:21 +0000573#ifdef MEM_LIST
574/**
575 * xmlMemContentShow:
576 * @fp: a FILE descriptor used as the output file
577 * @p: a memory block header
578 *
579 * tries to show some content from the memory block
580 */
581
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000582static void
Owen Taylor3473f882001-02-23 17:55:21 +0000583xmlMemContentShow(FILE *fp, MEMHDR *p)
584{
Gaurav7966a762014-05-09 17:00:08 +0800585 int i,j,k,len;
586 const char *buf;
Owen Taylor3473f882001-02-23 17:55:21 +0000587
588 if (p == NULL) {
589 fprintf(fp, " NULL");
590 return;
591 }
Gaurav7966a762014-05-09 17:00:08 +0800592 len = p->mh_size;
593 buf = (const char *) HDR_2_CLIENT(p);
Owen Taylor3473f882001-02-23 17:55:21 +0000594
595 for (i = 0;i < len;i++) {
596 if (buf[i] == 0) break;
Daniel Veillard9f28f302002-02-15 20:48:08 +0000597 if (!isprint((unsigned char) buf[i])) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000598 }
599 if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
600 if (len >= 4) {
601 MEMHDR *q;
602 void *cur;
603
Daniel Veillardf4721d62006-10-11 21:12:10 +0000604 for (j = 0;(j < len -3) && (j < 40);j += 4) {
Owen Taylor3473f882001-02-23 17:55:21 +0000605 cur = *((void **) &buf[j]);
606 q = CLIENT_2_HDR(cur);
607 p = memlist;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000608 k = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000609 while (p != NULL) {
610 if (p == q) break;
611 p = p->mh_next;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000612 if (k++ > 100) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000613 }
614 if ((p != NULL) && (p == q)) {
615 fprintf(fp, " pointer to #%lu at index %d",
616 p->mh_number, j);
617 return;
618 }
619 }
620 }
621 } else if ((i == 0) && (buf[i] == 0)) {
622 fprintf(fp," null");
623 } else {
Daniel Veillard09459bf2008-07-30 12:58:11 +0000624 if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000625 else {
626 fprintf(fp," [");
627 for (j = 0;j < i;j++)
628 fprintf(fp,"%c", buf[j]);
629 fprintf(fp,"]");
630 }
631 }
632}
633#endif
634
635/**
Daniel Veillard09459bf2008-07-30 12:58:11 +0000636 * xmlMemDisplayLast:
637 * @fp: a FILE descriptor used as the output file, if NULL, the result is
638 * written to the file .memorylist
639 * @nbBytes: the amount of memory to dump
640 *
641 * the last nbBytes of memory allocated and not freed, useful for dumping
642 * the memory left allocated between two places at runtime.
643 */
644
645void
646xmlMemDisplayLast(FILE *fp, long nbBytes)
647{
648#ifdef MEM_LIST
649 MEMHDR *p;
650 unsigned idx;
651 int nb = 0;
652#endif
653 FILE *old_fp = fp;
654
655 if (nbBytes <= 0)
656 return;
657
658 if (fp == NULL) {
659 fp = fopen(".memorylist", "w");
660 if (fp == NULL)
661 return;
662 }
663
664#ifdef MEM_LIST
665 fprintf(fp," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
666 nbBytes, debugMemSize, debugMaxMemSize);
667 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
668 idx = 0;
669 xmlMutexLock(xmlMemMutex);
670 p = memlist;
671 while ((p) && (nbBytes > 0)) {
672 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
673 (unsigned long)p->mh_size);
674 switch (p->mh_type) {
675 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
676 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
677 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
678 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
679 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
680 default:
681 fprintf(fp,"Unknown memory block, may be corrupted");
682 xmlMutexUnlock(xmlMemMutex);
683 if (old_fp == NULL)
684 fclose(fp);
685 return;
686 }
687 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
688 if (p->mh_tag != MEMTAG)
689 fprintf(fp," INVALID");
690 nb++;
691 if (nb < 100)
692 xmlMemContentShow(fp, p);
693 else
694 fprintf(fp," skip");
695
696 fprintf(fp,"\n");
697 nbBytes -= (unsigned long)p->mh_size;
698 p = p->mh_next;
699 }
700 xmlMutexUnlock(xmlMemMutex);
701#else
702 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
703#endif
704 if (old_fp == NULL)
705 fclose(fp);
706}
707
708/**
Owen Taylor3473f882001-02-23 17:55:21 +0000709 * xmlMemDisplay:
710 * @fp: a FILE descriptor used as the output file, if NULL, the result is
711 * written to the file .memorylist
712 *
713 * show in-extenso the memory blocks allocated
714 */
715
716void
717xmlMemDisplay(FILE *fp)
718{
719#ifdef MEM_LIST
720 MEMHDR *p;
Daniel Veillard144024e2002-02-13 21:14:46 +0000721 unsigned idx;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000722 int nb = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000723#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
724 time_t currentTime;
725 char buf[500];
726 struct tm * tstruct;
Daniel Veillard942d6c72005-05-08 11:39:56 +0000727#endif
728#endif
729 FILE *old_fp = fp;
Owen Taylor3473f882001-02-23 17:55:21 +0000730
Daniel Veillard942d6c72005-05-08 11:39:56 +0000731 if (fp == NULL) {
732 fp = fopen(".memorylist", "w");
733 if (fp == NULL)
734 return;
735 }
736
737#ifdef MEM_LIST
738#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Owen Taylor3473f882001-02-23 17:55:21 +0000739 currentTime = time(NULL);
740 tstruct = localtime(&currentTime);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000741 strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Owen Taylor3473f882001-02-23 17:55:21 +0000742 fprintf(fp," %s\n\n", buf);
743#endif
744
Daniel Veillard09459bf2008-07-30 12:58:11 +0000745
Owen Taylor3473f882001-02-23 17:55:21 +0000746 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
747 debugMemSize, debugMaxMemSize);
748 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
749 idx = 0;
William M. Brack0622fe82003-11-29 10:47:56 +0000750 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000751 p = memlist;
752 while (p) {
Daniel Veillard144024e2002-02-13 21:14:46 +0000753 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
754 (unsigned long)p->mh_size);
Owen Taylor3473f882001-02-23 17:55:21 +0000755 switch (p->mh_type) {
756 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
757 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000758 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000759 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000760 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
761 default:
William M. Brack13dfa872004-09-18 04:52:08 +0000762 fprintf(fp,"Unknown memory block, may be corrupted");
Daniel Veillard529233c2004-07-02 12:23:44 +0000763 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard942d6c72005-05-08 11:39:56 +0000764 if (old_fp == NULL)
765 fclose(fp);
Daniel Veillard529233c2004-07-02 12:23:44 +0000766 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000767 }
William M. Brack13dfa872004-09-18 04:52:08 +0000768 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Owen Taylor3473f882001-02-23 17:55:21 +0000769 if (p->mh_tag != MEMTAG)
770 fprintf(fp," INVALID");
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000771 nb++;
772 if (nb < 100)
773 xmlMemContentShow(fp, p);
774 else
775 fprintf(fp," skip");
776
Owen Taylor3473f882001-02-23 17:55:21 +0000777 fprintf(fp,"\n");
778 p = p->mh_next;
779 }
William M. Brack0622fe82003-11-29 10:47:56 +0000780 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000781#else
782 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
783#endif
Daniel Veillard942d6c72005-05-08 11:39:56 +0000784 if (old_fp == NULL)
785 fclose(fp);
Owen Taylor3473f882001-02-23 17:55:21 +0000786}
787
788#ifdef MEM_LIST
789
Daniel Veillard01c13b52002-12-10 15:19:08 +0000790static void debugmem_list_add(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000791{
792 p->mh_next = memlist;
793 p->mh_prev = NULL;
794 if (memlist) memlist->mh_prev = p;
795 memlist = p;
796#ifdef MEM_LIST_DEBUG
797 if (stderr)
798 Mem_Display(stderr);
799#endif
800}
801
Daniel Veillard01c13b52002-12-10 15:19:08 +0000802static void debugmem_list_delete(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000803{
804 if (p->mh_next)
805 p->mh_next->mh_prev = p->mh_prev;
806 if (p->mh_prev)
807 p->mh_prev->mh_next = p->mh_next;
808 else memlist = p->mh_next;
809#ifdef MEM_LIST_DEBUG
810 if (stderr)
811 Mem_Display(stderr);
812#endif
813}
814
815#endif
816
817/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000818 * debugmem_tag_error:
819 *
820 * internal error function.
Owen Taylor3473f882001-02-23 17:55:21 +0000821 */
Daniel Veillard09459bf2008-07-30 12:58:11 +0000822
Daniel Veillard01c13b52002-12-10 15:19:08 +0000823static void debugmem_tag_error(void *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000824{
825 xmlGenericError(xmlGenericErrorContext,
826 "Memory tag error occurs :%p \n\t bye\n", p);
827#ifdef MEM_LIST
828 if (stderr)
829 xmlMemDisplay(stderr);
830#endif
831}
832
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000833#ifdef MEM_LIST
Daniel Veillardb44025c2001-10-11 22:55:55 +0000834static FILE *xmlMemoryDumpFile = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000835#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000836
Owen Taylor3473f882001-02-23 17:55:21 +0000837/**
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000838 * xmlMemShow:
839 * @fp: a FILE descriptor used as the output file
840 * @nr: number of entries to dump
841 *
842 * show a show display of the memory allocated, and dump
843 * the @nr last allocated areas which were not freed
844 */
845
846void
Daniel Veillardc064b472003-09-29 10:55:05 +0000847xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000848{
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000849#ifdef MEM_LIST
850 MEMHDR *p;
851#endif
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000852
853 if (fp != NULL)
854 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
855 debugMemSize, debugMaxMemSize);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000856#ifdef MEM_LIST
William M. Brack0622fe82003-11-29 10:47:56 +0000857 xmlMutexLock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000858 if (nr > 0) {
859 fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
860 p = memlist;
861 while ((p) && nr > 0) {
862 fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
863 switch (p->mh_type) {
864 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
865 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
866 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
867 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
868 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
869 default:fprintf(fp," ??? in ");break;
870 }
871 if (p->mh_file != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +0000872 fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000873 if (p->mh_tag != MEMTAG)
874 fprintf(fp," INVALID");
875 xmlMemContentShow(fp, p);
876 fprintf(fp,"\n");
877 nr--;
878 p = p->mh_next;
879 }
880 }
William M. Brack0622fe82003-11-29 10:47:56 +0000881 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000882#endif /* MEM_LIST */
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000883}
884
885/**
Owen Taylor3473f882001-02-23 17:55:21 +0000886 * xmlMemoryDump:
887 *
888 * Dump in-extenso the memory blocks allocated to the file .memorylist
889 */
890
891void
892xmlMemoryDump(void)
893{
Daniel Veillardc064b472003-09-29 10:55:05 +0000894#ifdef MEM_LIST
Owen Taylor3473f882001-02-23 17:55:21 +0000895 FILE *dump;
896
Daniel Veillard5997aca2002-03-18 18:36:20 +0000897 if (debugMaxMemSize == 0)
898 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000899 dump = fopen(".memdump", "w");
Daniel Veillardcd337f02001-11-22 18:20:37 +0000900 if (dump == NULL)
901 xmlMemoryDumpFile = stderr;
Owen Taylor3473f882001-02-23 17:55:21 +0000902 else xmlMemoryDumpFile = dump;
903
904 xmlMemDisplay(xmlMemoryDumpFile);
905
906 if (dump != NULL) fclose(dump);
Daniel Veillardc064b472003-09-29 10:55:05 +0000907#endif /* MEM_LIST */
Owen Taylor3473f882001-02-23 17:55:21 +0000908}
909
910
911/****************************************************************
912 * *
913 * Initialization Routines *
914 * *
915 ****************************************************************/
916
Owen Taylor3473f882001-02-23 17:55:21 +0000917/**
918 * xmlInitMemory:
919 *
920 * Initialize the memory layer.
921 *
922 * Returns 0 on success
923 */
Owen Taylor3473f882001-02-23 17:55:21 +0000924int
925xmlInitMemory(void)
926{
Daniel Veillarde15df582004-07-13 15:25:08 +0000927#ifdef HAVE_STDLIB_H
928 char *breakpoint;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000929#endif
Daniel Veillardf93a8662004-07-01 12:56:30 +0000930#ifdef DEBUG_MEMORY
931 xmlGenericError(xmlGenericErrorContext,
932 "xmlInitMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000933#endif
William M. Brack92029422004-01-04 01:01:14 +0000934 /*
935 This is really not good code (see Bug 130419). Suggestions for
936 improvement will be welcome!
937 */
938 if (xmlMemInitialized) return(-1);
William M. Brack0622fe82003-11-29 10:47:56 +0000939 xmlMemInitialized = 1;
William M. Brack0622fe82003-11-29 10:47:56 +0000940 xmlMemMutex = xmlNewMutex();
Owen Taylor3473f882001-02-23 17:55:21 +0000941
942#ifdef HAVE_STDLIB_H
943 breakpoint = getenv("XML_MEM_BREAKPOINT");
944 if (breakpoint != NULL) {
William M. Brack0622fe82003-11-29 10:47:56 +0000945 sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Owen Taylor3473f882001-02-23 17:55:21 +0000946 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000947#endif
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000948#ifdef HAVE_STDLIB_H
949 breakpoint = getenv("XML_MEM_TRACE");
950 if (breakpoint != NULL) {
951 sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
952 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000953#endif
954
Owen Taylor3473f882001-02-23 17:55:21 +0000955#ifdef DEBUG_MEMORY
956 xmlGenericError(xmlGenericErrorContext,
957 "xmlInitMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000958#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000959 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000960}
961
962/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000963 * xmlCleanupMemory:
964 *
Daniel Veillard91b955c2004-12-10 10:26:42 +0000965 * Free up all the memory allocated by the library for its own
966 * use. This should not be called by user level code.
William M. Brack72ee48d2003-12-30 08:30:19 +0000967 */
968void
969xmlCleanupMemory(void) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000970#ifdef DEBUG_MEMORY
971 xmlGenericError(xmlGenericErrorContext,
972 "xmlCleanupMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000973#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000974 if (xmlMemInitialized == 0)
975 return;
976
977 xmlFreeMutex(xmlMemMutex);
Daniel Veillard1a9b7082004-01-02 10:42:01 +0000978 xmlMemMutex = NULL;
William M. Brack72ee48d2003-12-30 08:30:19 +0000979 xmlMemInitialized = 0;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000980#ifdef DEBUG_MEMORY
981 xmlGenericError(xmlGenericErrorContext,
982 "xmlCleanupMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000983#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000984}
985
986/**
Owen Taylor3473f882001-02-23 17:55:21 +0000987 * xmlMemSetup:
988 * @freeFunc: the free() function to use
989 * @mallocFunc: the malloc() function to use
990 * @reallocFunc: the realloc() function to use
991 * @strdupFunc: the strdup() function to use
992 *
993 * Override the default memory access functions with a new set
994 * This has to be called before any other libxml routines !
995 *
996 * Should this be blocked if there was already some allocations
997 * done ?
998 *
999 * Returns 0 on success
1000 */
1001int
1002xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1003 xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001004#ifdef DEBUG_MEMORY
1005 xmlGenericError(xmlGenericErrorContext,
1006 "xmlMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001007#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001008 if (freeFunc == NULL)
1009 return(-1);
1010 if (mallocFunc == NULL)
1011 return(-1);
1012 if (reallocFunc == NULL)
1013 return(-1);
1014 if (strdupFunc == NULL)
1015 return(-1);
1016 xmlFree = freeFunc;
1017 xmlMalloc = mallocFunc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001018 xmlMallocAtomic = mallocFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001019 xmlRealloc = reallocFunc;
1020 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001021#ifdef DEBUG_MEMORY
1022 xmlGenericError(xmlGenericErrorContext,
1023 "xmlMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001024#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001025 return(0);
1026}
1027
1028/**
1029 * xmlMemGet:
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001030 * @freeFunc: place to save the free() function in use
1031 * @mallocFunc: place to save the malloc() function in use
1032 * @reallocFunc: place to save the realloc() function in use
1033 * @strdupFunc: place to save the strdup() function in use
Owen Taylor3473f882001-02-23 17:55:21 +00001034 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001035 * Provides the memory access functions set currently in use
Owen Taylor3473f882001-02-23 17:55:21 +00001036 *
1037 * Returns 0 on success
1038 */
1039int
1040xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1041 xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
1042 if (freeFunc != NULL) *freeFunc = xmlFree;
1043 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1044 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1045 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1046 return(0);
1047}
1048
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001049/**
1050 * xmlGcMemSetup:
1051 * @freeFunc: the free() function to use
1052 * @mallocFunc: the malloc() function to use
1053 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1054 * @reallocFunc: the realloc() function to use
1055 * @strdupFunc: the strdup() function to use
1056 *
1057 * Override the default memory access functions with a new set
1058 * This has to be called before any other libxml routines !
1059 * The mallocAtomicFunc is specialized for atomic block
1060 * allocations (i.e. of areas useful for garbage collected memory allocators
1061 *
1062 * Should this be blocked if there was already some allocations
1063 * done ?
1064 *
1065 * Returns 0 on success
1066 */
1067int
1068xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1069 xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
1070 xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001071#ifdef DEBUG_MEMORY
1072 xmlGenericError(xmlGenericErrorContext,
1073 "xmlGcMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001074#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001075 if (freeFunc == NULL)
1076 return(-1);
1077 if (mallocFunc == NULL)
1078 return(-1);
1079 if (mallocAtomicFunc == NULL)
1080 return(-1);
1081 if (reallocFunc == NULL)
1082 return(-1);
1083 if (strdupFunc == NULL)
1084 return(-1);
1085 xmlFree = freeFunc;
1086 xmlMalloc = mallocFunc;
1087 xmlMallocAtomic = mallocAtomicFunc;
1088 xmlRealloc = reallocFunc;
1089 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001090#ifdef DEBUG_MEMORY
1091 xmlGenericError(xmlGenericErrorContext,
1092 "xmlGcMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001093#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001094 return(0);
1095}
1096
1097/**
1098 * xmlGcMemGet:
1099 * @freeFunc: place to save the free() function in use
1100 * @mallocFunc: place to save the malloc() function in use
1101 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1102 * @reallocFunc: place to save the realloc() function in use
1103 * @strdupFunc: place to save the strdup() function in use
1104 *
1105 * Provides the memory access functions set currently in use
1106 * The mallocAtomicFunc is specialized for atomic block
1107 * allocations (i.e. of areas useful for garbage collected memory allocators
1108 *
1109 * Returns 0 on success
1110 */
1111int
1112xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1113 xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
1114 xmlStrdupFunc *strdupFunc) {
1115 if (freeFunc != NULL) *freeFunc = xmlFree;
1116 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1117 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
1118 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1119 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1120 return(0);
1121}
1122
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001123#define bottom_xmlmemory
1124#include "elfgcchack.h"