blob: 16ec60fd253664397c9614a20aa744774bed62ab [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xmlmemory.h: interface for the memory allocation debug.
3 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00004 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00005 */
6
7
8#ifndef _DEBUG_MEMORY_ALLOC_
9#define _DEBUG_MEMORY_ALLOC_
10
11#include <stdio.h>
12#include <libxml/xmlversion.h>
13
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000014/**
15 * DEBUG_MEMORY:
16 *
Daniel Veillard61f26172002-03-12 18:46:39 +000017 * DEBUG_MEMORY replaces the allocator with a collect and debug
18 * shell to the libc allocator.
19 * DEBUG_MEMORY should only be activated when debugging
20 * libxml i.e. if libxml has been configured with --with-debug-mem too.
Owen Taylor3473f882001-02-23 17:55:21 +000021 */
Daniel Veillard48b2f892001-02-25 16:11:03 +000022/* #define DEBUG_MEMORY_FREED */
Owen Taylor3473f882001-02-23 17:55:21 +000023/* #define DEBUG_MEMORY_LOCATION */
24
25#ifdef DEBUG
26#ifndef DEBUG_MEMORY
27#define DEBUG_MEMORY
28#endif
29#endif
30
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000031/**
32 * DEBUG_MEMORY_LOCATION:
33 *
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000034 * DEBUG_MEMORY_LOCATION should be activated only when debugging
Daniel Veillard61f26172002-03-12 18:46:39 +000035 * libxml i.e. if libxml has been configured with --with-debug-mem too.
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000036 */
Owen Taylor3473f882001-02-23 17:55:21 +000037#ifdef DEBUG_MEMORY_LOCATION
Daniel Veillard48b2f892001-02-25 16:11:03 +000038#endif
39
Owen Taylor3473f882001-02-23 17:55:21 +000040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/*
Daniel Veillard61f26172002-03-12 18:46:39 +000045 * The XML memory wrapper support 4 basic overloadable functions.
Owen Taylor3473f882001-02-23 17:55:21 +000046 */
Daniel Veillard9d06d302002-01-22 18:15:52 +000047/**
48 * xmlFreeFunc:
49 * @mem: an already allocated block of memory
50 *
Daniel Veillard61f26172002-03-12 18:46:39 +000051 * Signature for a free() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000052 */
53typedef void (*xmlFreeFunc)(void *mem);
54/**
55 * xmlMallocFunc:
56 * @size: the size requested in bytes
57 *
Daniel Veillard61f26172002-03-12 18:46:39 +000058 * Signature for a malloc() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000059 *
Daniel Veillard61f26172002-03-12 18:46:39 +000060 * Returns a pointer to the newly allocated block or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000061 */
62typedef void *(*xmlMallocFunc)(size_t size);
63
64/**
65 * xmlReallocFunc:
66 * @mem: an already allocated block of memory
67 * @size: the new size requested in bytes
68 *
Daniel Veillard61f26172002-03-12 18:46:39 +000069 * Signature for a realloc() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000070 *
Daniel Veillard61f26172002-03-12 18:46:39 +000071 * Returns a pointer to the newly reallocated block or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000072 */
73typedef void *(*xmlReallocFunc)(void *mem, size_t size);
74
75/**
76 * xmlStrdupFunc:
77 * @str: a zero terminated string
78 *
Daniel Veillard61f26172002-03-12 18:46:39 +000079 * Signature for an strdup() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000080 *
Daniel Veillard61f26172002-03-12 18:46:39 +000081 * Returns the copy of the string or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000082 */
83typedef char *(*xmlStrdupFunc)(const char *str);
Owen Taylor3473f882001-02-23 17:55:21 +000084
85/*
Daniel Veillard61f26172002-03-12 18:46:39 +000086 * The 4 interfaces used for all memory handling within libxml.
Owen Taylor3473f882001-02-23 17:55:21 +000087LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
88LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +000089LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic;
Owen Taylor3473f882001-02-23 17:55:21 +000090LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
91LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +000092 */
Owen Taylor3473f882001-02-23 17:55:21 +000093
94/*
Daniel Veillard61f26172002-03-12 18:46:39 +000095 * The way to overload the existing functions.
Daniel Veillard3c908dc2003-04-19 00:07:51 +000096 * The xmlGc function have an extra entry for atomic block
97 * allocations useful for garbage collected memory allocators
Owen Taylor3473f882001-02-23 17:55:21 +000098 */
99int xmlMemSetup (xmlFreeFunc freeFunc,
100 xmlMallocFunc mallocFunc,
101 xmlReallocFunc reallocFunc,
102 xmlStrdupFunc strdupFunc);
103int xmlMemGet (xmlFreeFunc *freeFunc,
104 xmlMallocFunc *mallocFunc,
105 xmlReallocFunc *reallocFunc,
106 xmlStrdupFunc *strdupFunc);
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000107int xmlGcMemSetup (xmlFreeFunc freeFunc,
108 xmlMallocFunc mallocFunc,
109 xmlMallocFunc mallocAtomicFunc,
110 xmlReallocFunc reallocFunc,
111 xmlStrdupFunc strdupFunc);
112int xmlGcMemGet (xmlFreeFunc *freeFunc,
113 xmlMallocFunc *mallocFunc,
114 xmlMallocFunc *mallocAtomicFunc,
115 xmlReallocFunc *reallocFunc,
116 xmlStrdupFunc *strdupFunc);
Owen Taylor3473f882001-02-23 17:55:21 +0000117
118/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000119 * Initialization of the memory layer.
Owen Taylor3473f882001-02-23 17:55:21 +0000120 */
121int xmlInitMemory (void);
122
123/*
William M. Brackc1939562003-08-05 15:52:22 +0000124 * These are specific to the XML debug memory wrapper.
Owen Taylor3473f882001-02-23 17:55:21 +0000125 */
126int xmlMemUsed (void);
127void xmlMemDisplay (FILE *fp);
128void xmlMemShow (FILE *fp, int nr);
129void xmlMemoryDump (void);
Igor Zlatkovic7ae91bc2002-11-08 17:18:52 +0000130void * xmlMemMalloc (size_t size);
131void * xmlMemRealloc (void *ptr,size_t size);
132void xmlMemFree (void *ptr);
133char * xmlMemoryStrdup (const char *str);
William M. Brackc1939562003-08-05 15:52:22 +0000134void * xmlMallocLoc (size_t size, const char *file, int line);
135void * xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
136void * xmlMallocAtomicLoc (size_t size, const char *file, int line);
137char * xmlMemStrdupLoc (const char *str, const char *file, int line);
138
Owen Taylor3473f882001-02-23 17:55:21 +0000139
140#ifdef DEBUG_MEMORY_LOCATION
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000141/**
142 * xmlMalloc:
143 * @size: number of bytes to allocate
144 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000145 * Wrapper for the malloc() function used in the XML library.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000146 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000147 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000148 */
149#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
150/**
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000151 * xmlMallocAtomic:
152 * @size: number of bytes to allocate
153 *
154 * Wrapper for the malloc() function used in the XML library for allocation
155 * of block not containing pointers to other areas.
156 *
157 * Returns the pointer to the allocated area or NULL in case of error.
158 */
159#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
160/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000161 * xmlRealloc:
162 * @ptr: pointer to the existing allocated area
163 * @size: number of bytes to allocate
164 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000165 * Wrapper for the realloc() function used in the XML library.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000166 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000167 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000168 */
169#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
170/**
171 * xmlMemStrdup:
172 * @str: pointer to the existing string
173 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000174 * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000175 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000176 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000177 */
178#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
Owen Taylor3473f882001-02-23 17:55:21 +0000179
Daniel Veillard8599e702001-07-17 21:38:51 +0000180void * xmlMallocLoc(size_t size, const char *file, int line);
181void * xmlReallocLoc(void *ptr,size_t size, const char *file, int line);
Owen Taylor3473f882001-02-23 17:55:21 +0000182char * xmlMemStrdupLoc(const char *str, const char *file, int line);
183#endif /* DEBUG_MEMORY_LOCATION */
184
185#ifdef __cplusplus
186}
187#endif /* __cplusplus */
188
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +0000189#ifndef __XML_GLOBALS_H
190#ifndef __XML_THREADS_H__
191#include <libxml/threads.h>
192#include <libxml/globals.h>
193#endif
194#endif
195
Owen Taylor3473f882001-02-23 17:55:21 +0000196#endif /* _DEBUG_MEMORY_ALLOC_ */
197