1 /*
2 * Summary: interface for the memory allocator
3 * Description: provides interfaces for the memory allocator,
4 * including debugging capabilities.
5 *
6 * Copy: See Copyright for the status of this software.
7 *
8 * Author: Daniel Veillard
9 */
10
11
12 #ifndef __DEBUG_MEMORY_ALLOC__
13 #define __DEBUG_MEMORY_ALLOC__
14
15 #include <stdio.h>
16 #include <libxml/xmlversion.h>
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /*
23 * The XML memory wrapper support 4 basic overloadable functions.
24 */
25 /**
26 * xmlFreeFunc:
27 * @mem: an already allocated block of memory
28 *
29 * Signature for a free() implementation.
30 */
31 typedef void (*xmlFreeFunc)(void *mem);
32 /**
33 * xmlMallocFunc:
34 * @size: the size requested in bytes
35 *
36 * Signature for a malloc() implementation.
37 *
38 * Returns a pointer to the newly allocated block or NULL in case of error.
39 */
40 typedef void *(LIBXML_ATTR_ALLOC_SIZE(1) *xmlMallocFunc)(size_t size);
41
42 /**
43 * xmlReallocFunc:
44 * @mem: an already allocated block of memory
45 * @size: the new size requested in bytes
46 *
47 * Signature for a realloc() implementation.
48 *
49 * Returns a pointer to the newly reallocated block or NULL in case of error.
50 */
51 typedef void *(*xmlReallocFunc)(void *mem, size_t size);
52
53 /**
54 * xmlStrdupFunc:
55 * @str: a zero terminated string
56 *
57 * Signature for an strdup() implementation.
58 *
59 * Returns the copy of the string or NULL in case of error.
60 */
61 typedef char *(*xmlStrdupFunc)(const char *str);
62
63 /*
64 * In general the memory allocation entry points are not kept
65 * thread specific but this can be overridden by LIBXML_THREAD_ALLOC_ENABLED
66 * - xmlMalloc
67 * - xmlMallocAtomic
68 * - xmlRealloc
69 * - xmlMemStrdup
70 * - xmlFree
71 */
72 /** DOC_DISABLE */
73 #ifdef LIBXML_THREAD_ALLOC_ENABLED
74 #define XML_GLOBALS_ALLOC \
75 XML_OP(xmlMalloc, xmlMallocFunc, XML_NO_ATTR) \
76 XML_OP(xmlMallocAtomic, xmlMallocFunc, XML_NO_ATTR) \
77 XML_OP(xmlRealloc, xmlReallocFunc, XML_NO_ATTR) \
78 XML_OP(xmlFree, xmlFreeFunc, XML_NO_ATTR) \
79 XML_OP(xmlMemStrdup, xmlStrdupFunc, XML_NO_ATTR)
80 #define XML_OP XML_DECLARE_GLOBAL
81 XML_GLOBALS_ALLOC
82 #undef XML_OP
83 #if defined(LIBXML_THREAD_ENABLED) && !defined(XML_GLOBALS_NO_REDEFINITION)
84 #define xmlMalloc XML_GLOBAL_MACRO(xmlMalloc)
85 #define xmlMallocAtomic XML_GLOBAL_MACRO(xmlMallocAtomic)
86 #define xmlRealloc XML_GLOBAL_MACRO(xmlRealloc)
87 #define xmlFree XML_GLOBAL_MACRO(xmlFree)
88 #define xmlMemStrdup XML_GLOBAL_MACRO(xmlMemStrdup)
89 #endif
90 #else
91 #define XML_GLOBALS_ALLOC
92 /** DOC_ENABLE */
93 XMLPUBVAR xmlMallocFunc xmlMalloc;
94 XMLPUBVAR xmlMallocFunc xmlMallocAtomic;
95 XMLPUBVAR xmlReallocFunc xmlRealloc;
96 XMLPUBVAR xmlFreeFunc xmlFree;
97 XMLPUBVAR xmlStrdupFunc xmlMemStrdup;
98 #endif
99
100 /*
101 * The way to overload the existing functions.
102 * The xmlGc function have an extra entry for atomic block
103 * allocations useful for garbage collected memory allocators
104 */
105 XMLPUBFUN int
106 xmlMemSetup (xmlFreeFunc freeFunc,
107 xmlMallocFunc mallocFunc,
108 xmlReallocFunc reallocFunc,
109 xmlStrdupFunc strdupFunc);
110 XMLPUBFUN int
111 xmlMemGet (xmlFreeFunc *freeFunc,
112 xmlMallocFunc *mallocFunc,
113 xmlReallocFunc *reallocFunc,
114 xmlStrdupFunc *strdupFunc);
115 XMLPUBFUN int
116 xmlGcMemSetup (xmlFreeFunc freeFunc,
117 xmlMallocFunc mallocFunc,
118 xmlMallocFunc mallocAtomicFunc,
119 xmlReallocFunc reallocFunc,
120 xmlStrdupFunc strdupFunc);
121 XMLPUBFUN int
122 xmlGcMemGet (xmlFreeFunc *freeFunc,
123 xmlMallocFunc *mallocFunc,
124 xmlMallocFunc *mallocAtomicFunc,
125 xmlReallocFunc *reallocFunc,
126 xmlStrdupFunc *strdupFunc);
127
128 /*
129 * Initialization of the memory layer.
130 */
131 XML_DEPRECATED
132 XMLPUBFUN int
133 xmlInitMemory (void);
134
135 /*
136 * Cleanup of the memory layer.
137 */
138 XML_DEPRECATED
139 XMLPUBFUN void
140 xmlCleanupMemory (void);
141 /*
142 * These are specific to the XML debug memory wrapper.
143 */
144 XMLPUBFUN size_t
145 xmlMemSize (void *ptr);
146 XMLPUBFUN int
147 xmlMemUsed (void);
148 XMLPUBFUN int
149 xmlMemBlocks (void);
150 XMLPUBFUN void
151 xmlMemDisplay (FILE *fp);
152 XMLPUBFUN void
153 xmlMemDisplayLast(FILE *fp, long nbBytes);
154 XMLPUBFUN void
155 xmlMemShow (FILE *fp, int nr);
156 XMLPUBFUN void
157 xmlMemoryDump (void);
158 XMLPUBFUN void *
159 xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
160 XMLPUBFUN void *
161 xmlMemRealloc (void *ptr,size_t size);
162 XMLPUBFUN void
163 xmlMemFree (void *ptr);
164 XMLPUBFUN char *
165 xmlMemoryStrdup (const char *str);
166 XMLPUBFUN void *
167 xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
168 XMLPUBFUN void *
169 xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
170 XMLPUBFUN void *
171 xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
172 XMLPUBFUN char *
173 xmlMemStrdupLoc (const char *str, const char *file, int line);
174
175
176 /** DOC_DISABLE */
177 #ifdef DEBUG_MEMORY_LOCATION
178 /**
179 * xmlMalloc:
180 * @size: number of bytes to allocate
181 *
182 * Wrapper for the malloc() function used in the XML library.
183 *
184 * Returns the pointer to the allocated area or NULL in case of error.
185 */
186 #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
187 /**
188 * xmlMallocAtomic:
189 * @size: number of bytes to allocate
190 *
191 * Wrapper for the malloc() function used in the XML library for allocation
192 * of block not containing pointers to other areas.
193 *
194 * Returns the pointer to the allocated area or NULL in case of error.
195 */
196 #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
197 /**
198 * xmlRealloc:
199 * @ptr: pointer to the existing allocated area
200 * @size: number of bytes to allocate
201 *
202 * Wrapper for the realloc() function used in the XML library.
203 *
204 * Returns the pointer to the allocated area or NULL in case of error.
205 */
206 #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
207 /**
208 * xmlMemStrdup:
209 * @str: pointer to the existing string
210 *
211 * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
212 *
213 * Returns the pointer to the allocated area or NULL in case of error.
214 */
215 #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
216
217 #endif /* DEBUG_MEMORY_LOCATION */
218 /** DOC_ENABLE */
219
220 #ifdef __cplusplus
221 }
222 #endif /* __cplusplus */
223
224 #endif /* __DEBUG_MEMORY_ALLOC__ */
225