blob: f08c8c3d3bb13f4c0688966d3f1854ef442ab802 [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
David Kilzer886529b2016-04-05 12:05:25 -0700112#define MAX_SIZE_T ((size_t)-1)
Owen Taylor3473f882001-02-23 17:55:21 +0000113
114#define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE))
115#define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE))
116
117
William M. Brack0622fe82003-11-29 10:47:56 +0000118static unsigned int block=0;
119static unsigned int xmlMemStopAtBlock = 0;
Daniel Veillardb44025c2001-10-11 22:55:55 +0000120static void *xmlMemTraceBlockAt = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000121#ifdef MEM_LIST
122static MEMHDR *memlist = NULL;
123#endif
124
Daniel Veillard01c13b52002-12-10 15:19:08 +0000125static void debugmem_tag_error(void *addr);
Owen Taylor3473f882001-02-23 17:55:21 +0000126#ifdef MEM_LIST
Daniel Veillard01c13b52002-12-10 15:19:08 +0000127static void debugmem_list_add(MEMHDR *);
128static void debugmem_list_delete(MEMHDR *);
Owen Taylor3473f882001-02-23 17:55:21 +0000129#endif
130#define Mem_Tag_Err(a) debugmem_tag_error(a);
131
132#ifndef TEST_POINT
133#define TEST_POINT
134#endif
135
136/**
137 * xmlMallocBreakpoint:
138 *
139 * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block
140 * number reaches the specified value this function is called. One need to add a breakpoint
141 * to it to get the context in which the given block is allocated.
142 */
143
144void
145xmlMallocBreakpoint(void) {
146 xmlGenericError(xmlGenericErrorContext,
147 "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock);
148}
149
150/**
151 * xmlMallocLoc:
152 * @size: an int specifying the size in byte to allocate.
153 * @file: the file name or NULL
154 * @line: the line number
155 *
156 * a malloc() equivalent, with logging of the allocation info.
157 *
158 * Returns a pointer to the allocated area or NULL in case of lack of memory.
159 */
160
161void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000162xmlMallocLoc(size_t size, const char * file, int line)
Owen Taylor3473f882001-02-23 17:55:21 +0000163{
164 MEMHDR *p;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000165 void *ret;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000166
Owen Taylor3473f882001-02-23 17:55:21 +0000167 if (!xmlMemInitialized) xmlInitMemory();
168#ifdef DEBUG_MEMORY
169 xmlGenericError(xmlGenericErrorContext,
170 "Malloc(%d)\n",size);
171#endif
172
173 TEST_POINT
Daniel Veillard09459bf2008-07-30 12:58:11 +0000174
Owen Taylor3473f882001-02-23 17:55:21 +0000175 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
176
177 if (!p) {
178 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000179 "xmlMallocLoc : Out of free space\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000180 xmlMemoryDump();
181 return(NULL);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000182 }
Owen Taylor3473f882001-02-23 17:55:21 +0000183 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000184 p->mh_size = size;
185 p->mh_type = MALLOC_TYPE;
186 p->mh_file = file;
187 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000188 xmlMutexLock(xmlMemMutex);
189 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000190 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000191 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000192 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
193#ifdef MEM_LIST
194 debugmem_list_add(p);
195#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000196 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000197
Owen Taylor3473f882001-02-23 17:55:21 +0000198#ifdef DEBUG_MEMORY
199 xmlGenericError(xmlGenericErrorContext,
200 "Malloc(%d) Ok\n",size);
201#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000202
William M. Brack0622fe82003-11-29 10:47:56 +0000203 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000204
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000205 ret = HDR_2_CLIENT(p);
206
207 if (xmlMemTraceBlockAt == ret) {
208 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300209 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
210 (long unsigned)size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000211 xmlMallocBreakpoint();
212 }
213
Owen Taylor3473f882001-02-23 17:55:21 +0000214 TEST_POINT
215
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000216 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000217}
218
219/**
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000220 * xmlMallocAtomicLoc:
David Kilzer886529b2016-04-05 12:05:25 -0700221 * @size: an unsigned int specifying the size in byte to allocate.
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000222 * @file: the file name or NULL
223 * @line: the line number
224 *
225 * a malloc() equivalent, with logging of the allocation info.
226 *
227 * Returns a pointer to the allocated area or NULL in case of lack of memory.
228 */
229
230void *
231xmlMallocAtomicLoc(size_t size, const char * file, int line)
232{
233 MEMHDR *p;
234 void *ret;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000235
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000236 if (!xmlMemInitialized) xmlInitMemory();
237#ifdef DEBUG_MEMORY
238 xmlGenericError(xmlGenericErrorContext,
239 "Malloc(%d)\n",size);
240#endif
241
242 TEST_POINT
Daniel Veillard09459bf2008-07-30 12:58:11 +0000243
David Kilzer886529b2016-04-05 12:05:25 -0700244 if (size > (MAX_SIZE_T - RESERVE_SIZE)) {
245 xmlGenericError(xmlGenericErrorContext,
246 "xmlMallocAtomicLoc : Unsigned overflow prevented\n");
247 xmlMemoryDump();
248 return(NULL);
249 }
250
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000251 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
252
253 if (!p) {
254 xmlGenericError(xmlGenericErrorContext,
David Kilzer886529b2016-04-05 12:05:25 -0700255 "xmlMallocAtomicLoc : Out of free space\n");
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000256 xmlMemoryDump();
257 return(NULL);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000258 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000259 p->mh_tag = MEMTAG;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000260 p->mh_size = size;
261 p->mh_type = MALLOC_ATOMIC_TYPE;
262 p->mh_file = file;
263 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000264 xmlMutexLock(xmlMemMutex);
265 p->mh_number = ++block;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000266 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000267 debugMemBlocks++;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000268 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
269#ifdef MEM_LIST
270 debugmem_list_add(p);
271#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000272 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000273
274#ifdef DEBUG_MEMORY
275 xmlGenericError(xmlGenericErrorContext,
276 "Malloc(%d) Ok\n",size);
277#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000278
William M. Brack0622fe82003-11-29 10:47:56 +0000279 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000280
281 ret = HDR_2_CLIENT(p);
282
283 if (xmlMemTraceBlockAt == ret) {
284 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300285 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt,
286 (long unsigned)size);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000287 xmlMallocBreakpoint();
288 }
289
290 TEST_POINT
291
292 return(ret);
293}
294/**
Owen Taylor3473f882001-02-23 17:55:21 +0000295 * xmlMemMalloc:
296 * @size: an int specifying the size in byte to allocate.
297 *
298 * a malloc() equivalent, with logging of the allocation info.
299 *
300 * Returns a pointer to the allocated area or NULL in case of lack of memory.
301 */
302
303void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000304xmlMemMalloc(size_t size)
Owen Taylor3473f882001-02-23 17:55:21 +0000305{
306 return(xmlMallocLoc(size, "none", 0));
307}
308
309/**
310 * xmlReallocLoc:
311 * @ptr: the initial memory block pointer
312 * @size: an int specifying the size in byte to allocate.
313 * @file: the file name or NULL
314 * @line: the line number
315 *
316 * a realloc() equivalent, with logging of the allocation info.
317 *
318 * Returns a pointer to the allocated area or NULL in case of lack of memory.
319 */
320
321void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000322xmlReallocLoc(void *ptr,size_t size, const char * file, int line)
Owen Taylor3473f882001-02-23 17:55:21 +0000323{
Yegor Yefremov74464452014-10-10 12:23:09 +0200324 MEMHDR *p, *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +0000325 unsigned long number;
Daniel Veillard529233c2004-07-02 12:23:44 +0000326#ifdef DEBUG_MEMORY
327 size_t oldsize;
328#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000329
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000330 if (ptr == NULL)
Aleksey Sanine9f08112004-01-22 22:20:31 +0000331 return(xmlMallocLoc(size, file, line));
332
333 if (!xmlMemInitialized) xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +0000334 TEST_POINT
335
336 p = CLIENT_2_HDR(ptr);
337 number = p->mh_number;
Daniel Veillard18ffe202005-04-14 17:50:59 +0000338 if (xmlMemStopAtBlock == number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000339 if (p->mh_tag != MEMTAG) {
340 Mem_Tag_Err(p);
341 goto error;
342 }
343 p->mh_tag = ~MEMTAG;
William M. Brack0622fe82003-11-29 10:47:56 +0000344 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000345 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000346 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000347#ifdef DEBUG_MEMORY
348 oldsize = p->mh_size;
349#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000350#ifdef MEM_LIST
351 debugmem_list_delete(p);
352#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000353 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000354
Yegor Yefremov74464452014-10-10 12:23:09 +0200355 tmp = (MEMHDR *) realloc(p,RESERVE_SIZE+size);
356 if (!tmp) {
357 free(p);
Owen Taylor3473f882001-02-23 17:55:21 +0000358 goto error;
359 }
Yegor Yefremov74464452014-10-10 12:23:09 +0200360 p = tmp;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000361 if (xmlMemTraceBlockAt == ptr) {
362 xmlGenericError(xmlGenericErrorContext,
Stefan Kostecb5d5a2011-05-06 17:40:10 +0300363 "%p : Realloced(%lu -> %lu) Ok\n",
364 xmlMemTraceBlockAt, (long unsigned)p->mh_size,
365 (long unsigned)size);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000366 xmlMallocBreakpoint();
367 }
Owen Taylor3473f882001-02-23 17:55:21 +0000368 p->mh_tag = MEMTAG;
369 p->mh_number = number;
370 p->mh_type = REALLOC_TYPE;
371 p->mh_size = size;
372 p->mh_file = file;
373 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000374 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000375 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000376 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000377 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
378#ifdef MEM_LIST
379 debugmem_list_add(p);
380#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000381 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000382
383 TEST_POINT
384
Daniel Veillard529233c2004-07-02 12:23:44 +0000385#ifdef DEBUG_MEMORY
386 xmlGenericError(xmlGenericErrorContext,
387 "Realloced(%d to %d) Ok\n", oldsize, size);
388#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000389 return(HDR_2_CLIENT(p));
Daniel Veillard09459bf2008-07-30 12:58:11 +0000390
391error:
Owen Taylor3473f882001-02-23 17:55:21 +0000392 return(NULL);
393}
394
395/**
396 * xmlMemRealloc:
397 * @ptr: the initial memory block pointer
398 * @size: an int specifying the size in byte to allocate.
399 *
400 * a realloc() equivalent, with logging of the allocation info.
401 *
402 * Returns a pointer to the allocated area or NULL in case of lack of memory.
403 */
404
405void *
Daniel Veillard8599e702001-07-17 21:38:51 +0000406xmlMemRealloc(void *ptr,size_t size) {
Owen Taylor3473f882001-02-23 17:55:21 +0000407 return(xmlReallocLoc(ptr, size, "none", 0));
408}
409
410/**
411 * xmlMemFree:
412 * @ptr: the memory block pointer
413 *
414 * a free() equivalent, with error checking.
415 */
416void
417xmlMemFree(void *ptr)
418{
419 MEMHDR *p;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000420 char *target;
Daniel Veillard529233c2004-07-02 12:23:44 +0000421#ifdef DEBUG_MEMORY
422 size_t size;
423#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000424
Daniel Veillard2a512da2007-10-30 20:24:40 +0000425 if (ptr == NULL)
426 return;
427
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000428 if (ptr == (void *) -1) {
429 xmlGenericError(xmlGenericErrorContext,
430 "trying to free pointer from freed area\n");
431 goto error;
432 }
433
434 if (xmlMemTraceBlockAt == ptr) {
435 xmlGenericError(xmlGenericErrorContext,
436 "%p : Freed()\n", xmlMemTraceBlockAt);
437 xmlMallocBreakpoint();
438 }
439
Owen Taylor3473f882001-02-23 17:55:21 +0000440 TEST_POINT
441
Daniel Veillard92ad2102001-03-27 12:47:33 +0000442 target = (char *) ptr;
443
Owen Taylor3473f882001-02-23 17:55:21 +0000444 p = CLIENT_2_HDR(ptr);
445 if (p->mh_tag != MEMTAG) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000446 Mem_Tag_Err(p);
447 goto error;
Owen Taylor3473f882001-02-23 17:55:21 +0000448 }
Daniel Veillard18ffe202005-04-14 17:50:59 +0000449 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000450 p->mh_tag = ~MEMTAG;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000451 memset(target, -1, p->mh_size);
William M. Brack0622fe82003-11-29 10:47:56 +0000452 xmlMutexLock(xmlMemMutex);
453 debugMemSize -= p->mh_size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000454 debugMemBlocks--;
Daniel Veillard529233c2004-07-02 12:23:44 +0000455#ifdef DEBUG_MEMORY
456 size = p->mh_size;
457#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000458#ifdef MEM_LIST
459 debugmem_list_delete(p);
460#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000461 xmlMutexUnlock(xmlMemMutex);
462
Owen Taylor3473f882001-02-23 17:55:21 +0000463 free(p);
464
465 TEST_POINT
466
Daniel Veillard529233c2004-07-02 12:23:44 +0000467#ifdef DEBUG_MEMORY
468 xmlGenericError(xmlGenericErrorContext,
469 "Freed(%d) Ok\n", size);
470#endif
Daniel Veillard09459bf2008-07-30 12:58:11 +0000471
Owen Taylor3473f882001-02-23 17:55:21 +0000472 return;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000473
474error:
Owen Taylor3473f882001-02-23 17:55:21 +0000475 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard26908ab2002-01-01 16:50:03 +0000476 "xmlMemFree(%lX) error\n", (unsigned long) ptr);
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000477 xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000478 return;
479}
480
481/**
482 * xmlMemStrdupLoc:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000483 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000484 * @file: the file name or NULL
485 * @line: the line number
486 *
487 * a strdup() equivalent, with logging of the allocation info.
488 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000489 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000490 */
491
492char *
493xmlMemStrdupLoc(const char *str, const char *file, int line)
494{
495 char *s;
496 size_t size = strlen(str) + 1;
497 MEMHDR *p;
498
499 if (!xmlMemInitialized) xmlInitMemory();
500 TEST_POINT
501
502 p = (MEMHDR *) malloc(RESERVE_SIZE+size);
503 if (!p) {
504 goto error;
505 }
506 p->mh_tag = MEMTAG;
Owen Taylor3473f882001-02-23 17:55:21 +0000507 p->mh_size = size;
508 p->mh_type = STRDUP_TYPE;
509 p->mh_file = file;
510 p->mh_line = line;
William M. Brack0622fe82003-11-29 10:47:56 +0000511 xmlMutexLock(xmlMemMutex);
512 p->mh_number = ++block;
Owen Taylor3473f882001-02-23 17:55:21 +0000513 debugMemSize += size;
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000514 debugMemBlocks++;
Owen Taylor3473f882001-02-23 17:55:21 +0000515 if (debugMemSize > debugMaxMemSize) debugMaxMemSize = debugMemSize;
516#ifdef MEM_LIST
517 debugmem_list_add(p);
518#endif
William M. Brack0622fe82003-11-29 10:47:56 +0000519 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000520
Owen Taylor3473f882001-02-23 17:55:21 +0000521 s = (char *) HDR_2_CLIENT(p);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000522
William M. Brack0622fe82003-11-29 10:47:56 +0000523 if (xmlMemStopAtBlock == p->mh_number) xmlMallocBreakpoint();
Owen Taylor3473f882001-02-23 17:55:21 +0000524
Gaurav Guptab8480ae2014-07-26 21:14:53 +0800525 strcpy(s,str);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000526
Owen Taylor3473f882001-02-23 17:55:21 +0000527 TEST_POINT
528
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000529 if (xmlMemTraceBlockAt == s) {
530 xmlGenericError(xmlGenericErrorContext,
531 "%p : Strdup() Ok\n", xmlMemTraceBlockAt);
532 xmlMallocBreakpoint();
533 }
534
Owen Taylor3473f882001-02-23 17:55:21 +0000535 return(s);
536
537error:
538 return(NULL);
539}
540
541/**
542 * xmlMemoryStrdup:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000543 * @str: the initial string pointer
Owen Taylor3473f882001-02-23 17:55:21 +0000544 *
545 * a strdup() equivalent, with logging of the allocation info.
546 *
Daniel Veillard26908ab2002-01-01 16:50:03 +0000547 * Returns a pointer to the new string or NULL if allocation error occurred.
Owen Taylor3473f882001-02-23 17:55:21 +0000548 */
549
550char *
551xmlMemoryStrdup(const char *str) {
552 return(xmlMemStrdupLoc(str, "none", 0));
553}
554
555/**
556 * xmlMemUsed:
557 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000558 * Provides the amount of memory currently allocated
Owen Taylor3473f882001-02-23 17:55:21 +0000559 *
560 * Returns an int representing the amount of memory allocated.
561 */
562
563int
564xmlMemUsed(void) {
Martin von Gagern8985cde2015-04-13 16:32:14 +0800565 int res;
566
567 xmlMutexLock(xmlMemMutex);
568 res = debugMemSize;
569 xmlMutexUnlock(xmlMemMutex);
570 return(res);
Owen Taylor3473f882001-02-23 17:55:21 +0000571}
572
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000573/**
574 * xmlMemBlocks:
575 *
576 * Provides the number of memory areas currently allocated
577 *
578 * Returns an int representing the number of blocks
579 */
580
581int
582xmlMemBlocks(void) {
Martin von Gagern8985cde2015-04-13 16:32:14 +0800583 int res;
584
585 xmlMutexLock(xmlMemMutex);
586 res = debugMemBlocks;
587 xmlMutexUnlock(xmlMemMutex);
588 return(res);
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000589}
590
Owen Taylor3473f882001-02-23 17:55:21 +0000591#ifdef MEM_LIST
592/**
593 * xmlMemContentShow:
594 * @fp: a FILE descriptor used as the output file
595 * @p: a memory block header
596 *
597 * tries to show some content from the memory block
598 */
599
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000600static void
Owen Taylor3473f882001-02-23 17:55:21 +0000601xmlMemContentShow(FILE *fp, MEMHDR *p)
602{
Gaurav7966a762014-05-09 17:00:08 +0800603 int i,j,k,len;
604 const char *buf;
Owen Taylor3473f882001-02-23 17:55:21 +0000605
606 if (p == NULL) {
607 fprintf(fp, " NULL");
608 return;
609 }
Gaurav7966a762014-05-09 17:00:08 +0800610 len = p->mh_size;
611 buf = (const char *) HDR_2_CLIENT(p);
Owen Taylor3473f882001-02-23 17:55:21 +0000612
613 for (i = 0;i < len;i++) {
614 if (buf[i] == 0) break;
Daniel Veillard9f28f302002-02-15 20:48:08 +0000615 if (!isprint((unsigned char) buf[i])) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000616 }
617 if ((i < 4) && ((buf[i] != 0) || (i == 0))) {
618 if (len >= 4) {
619 MEMHDR *q;
620 void *cur;
621
Daniel Veillardf4721d62006-10-11 21:12:10 +0000622 for (j = 0;(j < len -3) && (j < 40);j += 4) {
Owen Taylor3473f882001-02-23 17:55:21 +0000623 cur = *((void **) &buf[j]);
624 q = CLIENT_2_HDR(cur);
625 p = memlist;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000626 k = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000627 while (p != NULL) {
628 if (p == q) break;
629 p = p->mh_next;
Daniel Veillardf4721d62006-10-11 21:12:10 +0000630 if (k++ > 100) break;
Owen Taylor3473f882001-02-23 17:55:21 +0000631 }
632 if ((p != NULL) && (p == q)) {
633 fprintf(fp, " pointer to #%lu at index %d",
634 p->mh_number, j);
635 return;
636 }
637 }
638 }
639 } else if ((i == 0) && (buf[i] == 0)) {
640 fprintf(fp," null");
641 } else {
Daniel Veillard09459bf2008-07-30 12:58:11 +0000642 if (buf[i] == 0) fprintf(fp," \"%.25s\"", buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000643 else {
644 fprintf(fp," [");
645 for (j = 0;j < i;j++)
646 fprintf(fp,"%c", buf[j]);
647 fprintf(fp,"]");
648 }
649 }
650}
651#endif
652
653/**
Daniel Veillard09459bf2008-07-30 12:58:11 +0000654 * xmlMemDisplayLast:
655 * @fp: a FILE descriptor used as the output file, if NULL, the result is
656 * written to the file .memorylist
657 * @nbBytes: the amount of memory to dump
658 *
659 * the last nbBytes of memory allocated and not freed, useful for dumping
660 * the memory left allocated between two places at runtime.
661 */
662
663void
664xmlMemDisplayLast(FILE *fp, long nbBytes)
665{
666#ifdef MEM_LIST
667 MEMHDR *p;
668 unsigned idx;
669 int nb = 0;
670#endif
671 FILE *old_fp = fp;
672
673 if (nbBytes <= 0)
674 return;
675
676 if (fp == NULL) {
677 fp = fopen(".memorylist", "w");
678 if (fp == NULL)
679 return;
680 }
681
682#ifdef MEM_LIST
683 fprintf(fp," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
684 nbBytes, debugMemSize, debugMaxMemSize);
685 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
686 idx = 0;
687 xmlMutexLock(xmlMemMutex);
688 p = memlist;
689 while ((p) && (nbBytes > 0)) {
690 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
691 (unsigned long)p->mh_size);
692 switch (p->mh_type) {
693 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
694 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
695 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
696 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
697 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
698 default:
699 fprintf(fp,"Unknown memory block, may be corrupted");
700 xmlMutexUnlock(xmlMemMutex);
701 if (old_fp == NULL)
702 fclose(fp);
703 return;
704 }
705 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
706 if (p->mh_tag != MEMTAG)
707 fprintf(fp," INVALID");
708 nb++;
709 if (nb < 100)
710 xmlMemContentShow(fp, p);
711 else
712 fprintf(fp," skip");
713
714 fprintf(fp,"\n");
715 nbBytes -= (unsigned long)p->mh_size;
716 p = p->mh_next;
717 }
718 xmlMutexUnlock(xmlMemMutex);
719#else
720 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
721#endif
722 if (old_fp == NULL)
723 fclose(fp);
724}
725
726/**
Owen Taylor3473f882001-02-23 17:55:21 +0000727 * xmlMemDisplay:
728 * @fp: a FILE descriptor used as the output file, if NULL, the result is
729 * written to the file .memorylist
730 *
731 * show in-extenso the memory blocks allocated
732 */
733
734void
735xmlMemDisplay(FILE *fp)
736{
737#ifdef MEM_LIST
738 MEMHDR *p;
Daniel Veillard144024e2002-02-13 21:14:46 +0000739 unsigned idx;
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000740 int nb = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000741#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
742 time_t currentTime;
743 char buf[500];
744 struct tm * tstruct;
Daniel Veillard942d6c72005-05-08 11:39:56 +0000745#endif
746#endif
747 FILE *old_fp = fp;
Owen Taylor3473f882001-02-23 17:55:21 +0000748
Daniel Veillard942d6c72005-05-08 11:39:56 +0000749 if (fp == NULL) {
750 fp = fopen(".memorylist", "w");
751 if (fp == NULL)
752 return;
753 }
754
755#ifdef MEM_LIST
756#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
Owen Taylor3473f882001-02-23 17:55:21 +0000757 currentTime = time(NULL);
758 tstruct = localtime(&currentTime);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000759 strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
Owen Taylor3473f882001-02-23 17:55:21 +0000760 fprintf(fp," %s\n\n", buf);
761#endif
762
Daniel Veillard09459bf2008-07-30 12:58:11 +0000763
Owen Taylor3473f882001-02-23 17:55:21 +0000764 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
765 debugMemSize, debugMaxMemSize);
766 fprintf(fp,"BLOCK NUMBER SIZE TYPE\n");
767 idx = 0;
William M. Brack0622fe82003-11-29 10:47:56 +0000768 xmlMutexLock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000769 p = memlist;
770 while (p) {
Daniel Veillard144024e2002-02-13 21:14:46 +0000771 fprintf(fp,"%-5u %6lu %6lu ",idx++,p->mh_number,
772 (unsigned long)p->mh_size);
Owen Taylor3473f882001-02-23 17:55:21 +0000773 switch (p->mh_type) {
774 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
775 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000776 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000777 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
Daniel Veillard529233c2004-07-02 12:23:44 +0000778 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
779 default:
William M. Brack13dfa872004-09-18 04:52:08 +0000780 fprintf(fp,"Unknown memory block, may be corrupted");
Daniel Veillard529233c2004-07-02 12:23:44 +0000781 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard942d6c72005-05-08 11:39:56 +0000782 if (old_fp == NULL)
783 fclose(fp);
Daniel Veillard529233c2004-07-02 12:23:44 +0000784 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000785 }
William M. Brack13dfa872004-09-18 04:52:08 +0000786 if (p->mh_file != NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Owen Taylor3473f882001-02-23 17:55:21 +0000787 if (p->mh_tag != MEMTAG)
788 fprintf(fp," INVALID");
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000789 nb++;
790 if (nb < 100)
791 xmlMemContentShow(fp, p);
792 else
793 fprintf(fp," skip");
794
Owen Taylor3473f882001-02-23 17:55:21 +0000795 fprintf(fp,"\n");
796 p = p->mh_next;
797 }
William M. Brack0622fe82003-11-29 10:47:56 +0000798 xmlMutexUnlock(xmlMemMutex);
Owen Taylor3473f882001-02-23 17:55:21 +0000799#else
800 fprintf(fp,"Memory list not compiled (MEM_LIST not defined !)\n");
801#endif
Daniel Veillard942d6c72005-05-08 11:39:56 +0000802 if (old_fp == NULL)
803 fclose(fp);
Owen Taylor3473f882001-02-23 17:55:21 +0000804}
805
806#ifdef MEM_LIST
807
Daniel Veillard01c13b52002-12-10 15:19:08 +0000808static void debugmem_list_add(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000809{
810 p->mh_next = memlist;
811 p->mh_prev = NULL;
812 if (memlist) memlist->mh_prev = p;
813 memlist = p;
814#ifdef MEM_LIST_DEBUG
815 if (stderr)
816 Mem_Display(stderr);
817#endif
818}
819
Daniel Veillard01c13b52002-12-10 15:19:08 +0000820static void debugmem_list_delete(MEMHDR *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000821{
822 if (p->mh_next)
823 p->mh_next->mh_prev = p->mh_prev;
824 if (p->mh_prev)
825 p->mh_prev->mh_next = p->mh_next;
826 else memlist = p->mh_next;
827#ifdef MEM_LIST_DEBUG
828 if (stderr)
829 Mem_Display(stderr);
830#endif
831}
832
833#endif
834
835/*
Daniel Veillard01c13b52002-12-10 15:19:08 +0000836 * debugmem_tag_error:
837 *
838 * internal error function.
Owen Taylor3473f882001-02-23 17:55:21 +0000839 */
Daniel Veillard09459bf2008-07-30 12:58:11 +0000840
Daniel Veillard01c13b52002-12-10 15:19:08 +0000841static void debugmem_tag_error(void *p)
Owen Taylor3473f882001-02-23 17:55:21 +0000842{
843 xmlGenericError(xmlGenericErrorContext,
844 "Memory tag error occurs :%p \n\t bye\n", p);
845#ifdef MEM_LIST
846 if (stderr)
847 xmlMemDisplay(stderr);
848#endif
849}
850
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000851#ifdef MEM_LIST
Daniel Veillardb44025c2001-10-11 22:55:55 +0000852static FILE *xmlMemoryDumpFile = NULL;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000853#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000854
Owen Taylor3473f882001-02-23 17:55:21 +0000855/**
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000856 * xmlMemShow:
857 * @fp: a FILE descriptor used as the output file
858 * @nr: number of entries to dump
859 *
860 * show a show display of the memory allocated, and dump
861 * the @nr last allocated areas which were not freed
862 */
863
864void
Daniel Veillardc064b472003-09-29 10:55:05 +0000865xmlMemShow(FILE *fp, int nr ATTRIBUTE_UNUSED)
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000866{
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000867#ifdef MEM_LIST
868 MEMHDR *p;
869#endif
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000870
871 if (fp != NULL)
872 fprintf(fp," MEMORY ALLOCATED : %lu, MAX was %lu\n",
873 debugMemSize, debugMaxMemSize);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000874#ifdef MEM_LIST
William M. Brack0622fe82003-11-29 10:47:56 +0000875 xmlMutexLock(xmlMemMutex);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000876 if (nr > 0) {
877 fprintf(fp,"NUMBER SIZE TYPE WHERE\n");
878 p = memlist;
879 while ((p) && nr > 0) {
880 fprintf(fp,"%6lu %6lu ",p->mh_number,(unsigned long)p->mh_size);
881 switch (p->mh_type) {
882 case STRDUP_TYPE:fprintf(fp,"strdup() in ");break;
883 case MALLOC_TYPE:fprintf(fp,"malloc() in ");break;
884 case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomicmalloc() in ");break;
885 case REALLOC_TYPE:fprintf(fp,"realloc() in ");break;
886 case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomicrealloc() in ");break;
887 default:fprintf(fp," ??? in ");break;
888 }
889 if (p->mh_file != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +0000890 fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000891 if (p->mh_tag != MEMTAG)
892 fprintf(fp," INVALID");
893 xmlMemContentShow(fp, p);
894 fprintf(fp,"\n");
895 nr--;
896 p = p->mh_next;
897 }
898 }
William M. Brack0622fe82003-11-29 10:47:56 +0000899 xmlMutexUnlock(xmlMemMutex);
Daniel Veillard09459bf2008-07-30 12:58:11 +0000900#endif /* MEM_LIST */
Daniel Veillardfb43bd62003-09-29 09:22:39 +0000901}
902
903/**
Owen Taylor3473f882001-02-23 17:55:21 +0000904 * xmlMemoryDump:
905 *
906 * Dump in-extenso the memory blocks allocated to the file .memorylist
907 */
908
909void
910xmlMemoryDump(void)
911{
Daniel Veillardc064b472003-09-29 10:55:05 +0000912#ifdef MEM_LIST
Owen Taylor3473f882001-02-23 17:55:21 +0000913 FILE *dump;
914
Daniel Veillard5997aca2002-03-18 18:36:20 +0000915 if (debugMaxMemSize == 0)
916 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000917 dump = fopen(".memdump", "w");
Daniel Veillardcd337f02001-11-22 18:20:37 +0000918 if (dump == NULL)
919 xmlMemoryDumpFile = stderr;
Owen Taylor3473f882001-02-23 17:55:21 +0000920 else xmlMemoryDumpFile = dump;
921
922 xmlMemDisplay(xmlMemoryDumpFile);
923
924 if (dump != NULL) fclose(dump);
Daniel Veillardc064b472003-09-29 10:55:05 +0000925#endif /* MEM_LIST */
Owen Taylor3473f882001-02-23 17:55:21 +0000926}
927
928
929/****************************************************************
930 * *
931 * Initialization Routines *
932 * *
933 ****************************************************************/
934
Owen Taylor3473f882001-02-23 17:55:21 +0000935/**
936 * xmlInitMemory:
937 *
938 * Initialize the memory layer.
939 *
940 * Returns 0 on success
941 */
Owen Taylor3473f882001-02-23 17:55:21 +0000942int
943xmlInitMemory(void)
944{
Daniel Veillarde15df582004-07-13 15:25:08 +0000945#ifdef HAVE_STDLIB_H
946 char *breakpoint;
Daniel Veillard09459bf2008-07-30 12:58:11 +0000947#endif
Daniel Veillardf93a8662004-07-01 12:56:30 +0000948#ifdef DEBUG_MEMORY
949 xmlGenericError(xmlGenericErrorContext,
950 "xmlInitMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000951#endif
William M. Brack92029422004-01-04 01:01:14 +0000952 /*
953 This is really not good code (see Bug 130419). Suggestions for
954 improvement will be welcome!
955 */
956 if (xmlMemInitialized) return(-1);
William M. Brack0622fe82003-11-29 10:47:56 +0000957 xmlMemInitialized = 1;
William M. Brack0622fe82003-11-29 10:47:56 +0000958 xmlMemMutex = xmlNewMutex();
Owen Taylor3473f882001-02-23 17:55:21 +0000959
960#ifdef HAVE_STDLIB_H
961 breakpoint = getenv("XML_MEM_BREAKPOINT");
962 if (breakpoint != NULL) {
William M. Brack0622fe82003-11-29 10:47:56 +0000963 sscanf(breakpoint, "%ud", &xmlMemStopAtBlock);
Owen Taylor3473f882001-02-23 17:55:21 +0000964 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000965#endif
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000966#ifdef HAVE_STDLIB_H
967 breakpoint = getenv("XML_MEM_TRACE");
968 if (breakpoint != NULL) {
969 sscanf(breakpoint, "%p", &xmlMemTraceBlockAt);
970 }
Daniel Veillard09459bf2008-07-30 12:58:11 +0000971#endif
972
Owen Taylor3473f882001-02-23 17:55:21 +0000973#ifdef DEBUG_MEMORY
974 xmlGenericError(xmlGenericErrorContext,
975 "xmlInitMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000976#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000977 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000978}
979
980/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000981 * xmlCleanupMemory:
982 *
Daniel Veillard91b955c2004-12-10 10:26:42 +0000983 * Free up all the memory allocated by the library for its own
984 * use. This should not be called by user level code.
William M. Brack72ee48d2003-12-30 08:30:19 +0000985 */
986void
987xmlCleanupMemory(void) {
Daniel Veillardf93a8662004-07-01 12:56:30 +0000988#ifdef DEBUG_MEMORY
989 xmlGenericError(xmlGenericErrorContext,
990 "xmlCleanupMemory()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +0000991#endif
William M. Brack72ee48d2003-12-30 08:30:19 +0000992 if (xmlMemInitialized == 0)
993 return;
994
995 xmlFreeMutex(xmlMemMutex);
Daniel Veillard1a9b7082004-01-02 10:42:01 +0000996 xmlMemMutex = NULL;
William M. Brack72ee48d2003-12-30 08:30:19 +0000997 xmlMemInitialized = 0;
Daniel Veillardf93a8662004-07-01 12:56:30 +0000998#ifdef DEBUG_MEMORY
999 xmlGenericError(xmlGenericErrorContext,
1000 "xmlCleanupMemory() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001001#endif
William M. Brack72ee48d2003-12-30 08:30:19 +00001002}
1003
1004/**
Owen Taylor3473f882001-02-23 17:55:21 +00001005 * xmlMemSetup:
1006 * @freeFunc: the free() function to use
1007 * @mallocFunc: the malloc() function to use
1008 * @reallocFunc: the realloc() function to use
1009 * @strdupFunc: the strdup() function to use
1010 *
1011 * Override the default memory access functions with a new set
1012 * This has to be called before any other libxml routines !
1013 *
1014 * Should this be blocked if there was already some allocations
1015 * done ?
1016 *
1017 * Returns 0 on success
1018 */
1019int
1020xmlMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1021 xmlReallocFunc reallocFunc, xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001022#ifdef DEBUG_MEMORY
1023 xmlGenericError(xmlGenericErrorContext,
1024 "xmlMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001025#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001026 if (freeFunc == NULL)
1027 return(-1);
1028 if (mallocFunc == NULL)
1029 return(-1);
1030 if (reallocFunc == NULL)
1031 return(-1);
1032 if (strdupFunc == NULL)
1033 return(-1);
1034 xmlFree = freeFunc;
1035 xmlMalloc = mallocFunc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001036 xmlMallocAtomic = mallocFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001037 xmlRealloc = reallocFunc;
1038 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001039#ifdef DEBUG_MEMORY
1040 xmlGenericError(xmlGenericErrorContext,
1041 "xmlMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001042#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001043 return(0);
1044}
1045
1046/**
1047 * xmlMemGet:
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001048 * @freeFunc: place to save the free() function in use
1049 * @mallocFunc: place to save the malloc() function in use
1050 * @reallocFunc: place to save the realloc() function in use
1051 * @strdupFunc: place to save the strdup() function in use
Owen Taylor3473f882001-02-23 17:55:21 +00001052 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001053 * Provides the memory access functions set currently in use
Owen Taylor3473f882001-02-23 17:55:21 +00001054 *
1055 * Returns 0 on success
1056 */
1057int
1058xmlMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1059 xmlReallocFunc *reallocFunc, xmlStrdupFunc *strdupFunc) {
1060 if (freeFunc != NULL) *freeFunc = xmlFree;
1061 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1062 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1063 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1064 return(0);
1065}
1066
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001067/**
1068 * xmlGcMemSetup:
1069 * @freeFunc: the free() function to use
1070 * @mallocFunc: the malloc() function to use
1071 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1072 * @reallocFunc: the realloc() function to use
1073 * @strdupFunc: the strdup() function to use
1074 *
1075 * Override the default memory access functions with a new set
1076 * This has to be called before any other libxml routines !
1077 * The mallocAtomicFunc is specialized for atomic block
1078 * allocations (i.e. of areas useful for garbage collected memory allocators
1079 *
1080 * Should this be blocked if there was already some allocations
1081 * done ?
1082 *
1083 * Returns 0 on success
1084 */
1085int
1086xmlGcMemSetup(xmlFreeFunc freeFunc, xmlMallocFunc mallocFunc,
1087 xmlMallocFunc mallocAtomicFunc, xmlReallocFunc reallocFunc,
1088 xmlStrdupFunc strdupFunc) {
Daniel Veillardf93a8662004-07-01 12:56:30 +00001089#ifdef DEBUG_MEMORY
1090 xmlGenericError(xmlGenericErrorContext,
1091 "xmlGcMemSetup()\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001092#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001093 if (freeFunc == NULL)
1094 return(-1);
1095 if (mallocFunc == NULL)
1096 return(-1);
1097 if (mallocAtomicFunc == NULL)
1098 return(-1);
1099 if (reallocFunc == NULL)
1100 return(-1);
1101 if (strdupFunc == NULL)
1102 return(-1);
1103 xmlFree = freeFunc;
1104 xmlMalloc = mallocFunc;
1105 xmlMallocAtomic = mallocAtomicFunc;
1106 xmlRealloc = reallocFunc;
1107 xmlMemStrdup = strdupFunc;
Daniel Veillardf93a8662004-07-01 12:56:30 +00001108#ifdef DEBUG_MEMORY
1109 xmlGenericError(xmlGenericErrorContext,
1110 "xmlGcMemSetup() Ok\n");
Daniel Veillard09459bf2008-07-30 12:58:11 +00001111#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001112 return(0);
1113}
1114
1115/**
1116 * xmlGcMemGet:
1117 * @freeFunc: place to save the free() function in use
1118 * @mallocFunc: place to save the malloc() function in use
1119 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1120 * @reallocFunc: place to save the realloc() function in use
1121 * @strdupFunc: place to save the strdup() function in use
1122 *
1123 * Provides the memory access functions set currently in use
1124 * The mallocAtomicFunc is specialized for atomic block
1125 * allocations (i.e. of areas useful for garbage collected memory allocators
1126 *
1127 * Returns 0 on success
1128 */
1129int
1130xmlGcMemGet(xmlFreeFunc *freeFunc, xmlMallocFunc *mallocFunc,
1131 xmlMallocFunc *mallocAtomicFunc, xmlReallocFunc *reallocFunc,
1132 xmlStrdupFunc *strdupFunc) {
1133 if (freeFunc != NULL) *freeFunc = xmlFree;
1134 if (mallocFunc != NULL) *mallocFunc = xmlMalloc;
1135 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic;
1136 if (reallocFunc != NULL) *reallocFunc = xmlRealloc;
1137 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup;
1138 return(0);
1139}
1140
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001141#define bottom_xmlmemory
1142#include "elfgcchack.h"