|
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 <libc/stddef.h> |
|
16 #include "Libxml2_xmlversion.h" |
|
17 |
|
18 #ifndef XMLENGINE_EXCLUDE_FILE_FUNC |
|
19 #include <stdio.h> |
|
20 #endif |
|
21 |
|
22 /** |
|
23 * DEBUG_MEMORY: |
|
24 * |
|
25 * DEBUG_MEMORY replaces the allocator with a collect and debug |
|
26 * shell to the libc allocator. |
|
27 * DEBUG_MEMORY should only be activated when debugging |
|
28 * libxml i.e. if libxml has been configured with --with-debug-mem too. |
|
29 */ |
|
30 /* #define DEBUG_MEMORY_FREED */ |
|
31 /* #define DEBUG_MEMORY_LOCATION */ |
|
32 |
|
33 #ifdef DEBUG |
|
34 #ifndef DEBUG_MEMORY |
|
35 #define DEBUG_MEMORY |
|
36 #endif |
|
37 #endif |
|
38 |
|
39 /** |
|
40 * DEBUG_MEMORY_LOCATION: |
|
41 * |
|
42 * DEBUG_MEMORY_LOCATION should be activated only when debugging |
|
43 * libxml i.e. if libxml has been configured with --with-debug-mem too. |
|
44 */ |
|
45 #ifdef DEBUG_MEMORY_LOCATION |
|
46 #endif |
|
47 |
|
48 #ifdef __cplusplus |
|
49 extern "C" { |
|
50 #endif |
|
51 |
|
52 /* |
|
53 * The XML memory wrapper support 4 basic overloadable functions. |
|
54 */ |
|
55 /** |
|
56 * xmlFreeFunc: |
|
57 * @mem: an already allocated block of memory |
|
58 * |
|
59 * Signature for a free() implementation. |
|
60 */ |
|
61 typedef void (XMLCALL *xmlFreeFunc)(void *mem); |
|
62 /** |
|
63 * xmlMallocFunc: |
|
64 * @size: the size requested in bytes |
|
65 * |
|
66 * Signature for a malloc() implementation. |
|
67 * |
|
68 * Returns a pointer to the newly allocated block or NULL in case of error. |
|
69 */ |
|
70 typedef void *(XMLCALL *xmlMallocFunc)(size_t size); |
|
71 |
|
72 /** |
|
73 * xmlReallocFunc: |
|
74 * @mem: an already allocated block of memory |
|
75 * @size: the new size requested in bytes |
|
76 * |
|
77 * Signature for a realloc() implementation. |
|
78 * |
|
79 * Returns a pointer to the newly reallocated block or NULL in case of error. |
|
80 */ |
|
81 typedef void *(XMLCALL *xmlReallocFunc)(void *mem, size_t size); |
|
82 |
|
83 /** |
|
84 * xmlStrdupFunc: |
|
85 * @str: a zero terminated string |
|
86 * |
|
87 * Signature for an strdup() implementation. |
|
88 * |
|
89 * Returns the copy of the string or NULL in case of error. |
|
90 */ |
|
91 typedef char *(XMLCALL *xmlStrdupFunc)(const char *str); |
|
92 |
|
93 /*The 4 interfaces used for all memory handling within libxml. |
|
94 LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree; |
|
95 LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc; |
|
96 LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic; |
|
97 LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc; |
|
98 LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup; |
|
99 */ |
|
100 |
|
101 /* |
|
102 * The way to overload the existing functions. |
|
103 * The xmlGc function have an extra entry for atomic block |
|
104 * allocations useful for garbage collected memory allocators |
|
105 */ |
|
106 XMLPUBFUN int XMLCALL |
|
107 xmlMemSetup (xmlFreeFunc freeFunc, |
|
108 xmlMallocFunc mallocFunc, |
|
109 xmlReallocFunc reallocFunc, |
|
110 xmlStrdupFunc strdupFunc); |
|
111 XMLPUBFUN int XMLCALL |
|
112 xmlMemGet (xmlFreeFunc *freeFunc, |
|
113 xmlMallocFunc *mallocFunc, |
|
114 xmlReallocFunc *reallocFunc, |
|
115 xmlStrdupFunc *strdupFunc); |
|
116 |
|
117 #ifndef XMLENGINE_EXCLUDE_UNUSED |
|
118 |
|
119 XMLPUBFUN int XMLCALL |
|
120 xmlGcMemSetup (xmlFreeFunc freeFunc, |
|
121 xmlMallocFunc mallocFunc, |
|
122 xmlMallocFunc mallocAtomicFunc, |
|
123 xmlReallocFunc reallocFunc, |
|
124 xmlStrdupFunc strdupFunc); |
|
125 |
|
126 XMLPUBFUN int XMLCALL |
|
127 xmlGcMemGet (xmlFreeFunc *freeFunc, |
|
128 xmlMallocFunc *mallocFunc, |
|
129 xmlMallocFunc *mallocAtomicFunc, |
|
130 xmlReallocFunc *reallocFunc, |
|
131 xmlStrdupFunc *strdupFunc); |
|
132 #endif /* ifndef XMLENGINE_EXCLUDE_UNUSED */ |
|
133 |
|
134 /* |
|
135 * Initialization of the memory layer. |
|
136 */ |
|
137 XMLPUBFUN int XMLCALL |
|
138 xmlInitMemory (void); |
|
139 |
|
140 /* |
|
141 * Cleanup of the memory layer. |
|
142 */ |
|
143 XMLPUBFUN void XMLCALL |
|
144 xmlCleanupMemory (void); |
|
145 /* |
|
146 * These are specific to the XML debug memory wrapper. |
|
147 */ |
|
148 XMLPUBFUN int XMLCALL |
|
149 xmlMemUsed (void); |
|
150 |
|
151 #ifndef XMLENGINE_EXCLUDE_FILE_FUNC |
|
152 XMLPUBFUN void XMLCALL |
|
153 xmlMemDisplay (FILE *fp); |
|
154 XMLPUBFUN void XMLCALL |
|
155 xmlMemShow (FILE *fp, int nr); |
|
156 #endif |
|
157 |
|
158 XMLPUBFUN void XMLCALL |
|
159 xmlMemoryDump (void); |
|
160 XMLPUBFUN void * XMLCALL |
|
161 xmlMemMalloc (size_t size); |
|
162 XMLPUBFUN void * XMLCALL |
|
163 xmlMemRealloc (void *ptr,size_t size); |
|
164 XMLPUBFUN void XMLCALL |
|
165 xmlMemFree (void *ptr); |
|
166 XMLPUBFUN char * XMLCALL |
|
167 xmlMemoryStrdup (const char *str); |
|
168 XMLPUBFUN void * XMLCALL |
|
169 xmlMallocLoc (size_t size, const char *file, int line); |
|
170 XMLPUBFUN void * XMLCALL |
|
171 xmlReallocLoc (void *ptr, size_t size, const char *file, int line); |
|
172 XMLPUBFUN void * XMLCALL |
|
173 xmlMallocAtomicLoc (size_t size, const char *file, int line); |
|
174 XMLPUBFUN char * XMLCALL |
|
175 xmlMemStrdupLoc (const char *str, const char *file, int line); |
|
176 |
|
177 |
|
178 #ifdef DEBUG_MEMORY_LOCATION |
|
179 /** |
|
180 * xmlMalloc: |
|
181 * @size: number of bytes to allocate |
|
182 * |
|
183 * Wrapper for the malloc() function used in the XML library. |
|
184 * |
|
185 * Returns the pointer to the allocated area or NULL in case of error. |
|
186 */ |
|
187 #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__) |
|
188 /** |
|
189 * xmlMallocAtomic: |
|
190 * @size: number of bytes to allocate |
|
191 * |
|
192 * Wrapper for the malloc() function used in the XML library for allocation |
|
193 * of block not containing pointers to other areas. |
|
194 * |
|
195 * Returns the pointer to the allocated area or NULL in case of error. |
|
196 */ |
|
197 #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__) |
|
198 /** |
|
199 * xmlRealloc: |
|
200 * @ptr: pointer to the existing allocated area |
|
201 * @size: number of bytes to allocate |
|
202 * |
|
203 * Wrapper for the realloc() function used in the XML library. |
|
204 * |
|
205 * Returns the pointer to the allocated area or NULL in case of error. |
|
206 */ |
|
207 #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__) |
|
208 /** |
|
209 * xmlMemStrdup: |
|
210 * @str: pointer to the existing string |
|
211 * |
|
212 * Wrapper for the strdup() function, xmlStrdup() is usually preferred. |
|
213 * |
|
214 * Returns the pointer to the allocated area or NULL in case of error. |
|
215 */ |
|
216 #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__) |
|
217 |
|
218 #endif /* DEBUG_MEMORY_LOCATION */ |
|
219 |
|
220 #ifdef __cplusplus |
|
221 } |
|
222 #endif /* __cplusplus */ |
|
223 |
|
224 #ifndef __XML_GLOBALS_H |
|
225 #ifndef __XML_THREADS_H__ |
|
226 //#include "Libxml2_threads.h" |
|
227 #include "Libxml2_globals.h" |
|
228 #endif |
|
229 #endif |
|
230 |
|
231 #endif /* __DEBUG_MEMORY_ALLOC__ */ |
|
232 |
|
233 |