1 /* libxml2 - Library for parsing XML documents
2 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
3 *
4 * This file is not part of the GNU gettext program, but is used with
5 * GNU gettext.
6 *
7 * The original copyright notice is as follows:
8 */
9
10 /*
11 * Copyright (C) 1998-2012 Daniel Veillard. All Rights Reserved.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is fur-
18 * nished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
25 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * THE SOFTWARE.
30 *
31 * Author: Daniel Veillard
32 */
33
34 /*
35 * Summary: interface for the memory allocator
36 * Description: provides interfaces for the memory allocator,
37 * including debugging capabilities.
38 */
39
40 #ifndef __DEBUG_MEMORY_ALLOC__
41 #define __DEBUG_MEMORY_ALLOC__
42
43 #include <stdio.h>
44 #include <libxml/xmlversion.h>
45
46 /**
47 * DEBUG_MEMORY:
48 *
49 * DEBUG_MEMORY replaces the allocator with a collect and debug
50 * shell to the libc allocator.
51 * DEBUG_MEMORY should only be activated when debugging
52 * libxml i.e. if libxml has been configured with --with-debug-mem too.
53 */
54 /* #define DEBUG_MEMORY_FREED */
55 /* #define DEBUG_MEMORY_LOCATION */
56
57 #ifdef DEBUG
58 #ifndef DEBUG_MEMORY
59 #define DEBUG_MEMORY
60 #endif
61 #endif
62
63 /**
64 * DEBUG_MEMORY_LOCATION:
65 *
66 * DEBUG_MEMORY_LOCATION should be activated only when debugging
67 * libxml i.e. if libxml has been configured with --with-debug-mem too.
68 */
69 #ifdef DEBUG_MEMORY_LOCATION
70 #endif
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76 /*
77 * The XML memory wrapper support 4 basic overloadable functions.
78 */
79 /**
80 * xmlFreeFunc:
81 * @mem: an already allocated block of memory
82 *
83 * Signature for a free() implementation.
84 */
85 typedef void (XMLCALL *xmlFreeFunc)(void *mem);
86 /**
87 * xmlMallocFunc:
88 * @size: the size requested in bytes
89 *
90 * Signature for a malloc() implementation.
91 *
92 * Returns a pointer to the newly allocated block or NULL in case of error.
93 */
94 typedef void *(LIBXML_ATTR_ALLOC_SIZE(1) XMLCALL *xmlMallocFunc)(size_t size);
95
96 /**
97 * xmlReallocFunc:
98 * @mem: an already allocated block of memory
99 * @size: the new size requested in bytes
100 *
101 * Signature for a realloc() implementation.
102 *
103 * Returns a pointer to the newly reallocated block or NULL in case of error.
104 */
105 typedef void *(XMLCALL *xmlReallocFunc)(void *mem, size_t size);
106
107 /**
108 * xmlStrdupFunc:
109 * @str: a zero terminated string
110 *
111 * Signature for an strdup() implementation.
112 *
113 * Returns the copy of the string or NULL in case of error.
114 */
115 typedef char *(XMLCALL *xmlStrdupFunc)(const char *str);
116
117 /*
118 * The 4 interfaces used for all memory handling within libxml.
119 LIBXML_DLL_IMPORT xmlFreeFunc xmlFree;
120 LIBXML_DLL_IMPORT xmlMallocFunc xmlMalloc;
121 LIBXML_DLL_IMPORT xmlMallocFunc xmlMallocAtomic;
122 LIBXML_DLL_IMPORT xmlReallocFunc xmlRealloc;
123 LIBXML_DLL_IMPORT xmlStrdupFunc xmlMemStrdup;
124 */
125
126 /*
127 * The way to overload the existing functions.
128 * The xmlGc function have an extra entry for atomic block
129 * allocations useful for garbage collected memory allocators
130 */
131 XMLPUBFUN int XMLCALL
132 xmlMemSetup (xmlFreeFunc freeFunc,
133 xmlMallocFunc mallocFunc,
134 xmlReallocFunc reallocFunc,
135 xmlStrdupFunc strdupFunc);
136 XMLPUBFUN int XMLCALL
137 xmlMemGet (xmlFreeFunc *freeFunc,
138 xmlMallocFunc *mallocFunc,
139 xmlReallocFunc *reallocFunc,
140 xmlStrdupFunc *strdupFunc);
141 XMLPUBFUN int XMLCALL
142 xmlGcMemSetup (xmlFreeFunc freeFunc,
143 xmlMallocFunc mallocFunc,
144 xmlMallocFunc mallocAtomicFunc,
145 xmlReallocFunc reallocFunc,
146 xmlStrdupFunc strdupFunc);
147 XMLPUBFUN int XMLCALL
148 xmlGcMemGet (xmlFreeFunc *freeFunc,
149 xmlMallocFunc *mallocFunc,
150 xmlMallocFunc *mallocAtomicFunc,
151 xmlReallocFunc *reallocFunc,
152 xmlStrdupFunc *strdupFunc);
153
154 /*
155 * Initialization of the memory layer.
156 */
157 XMLPUBFUN int XMLCALL
158 xmlInitMemory (void);
159
160 /*
161 * Cleanup of the memory layer.
162 */
163 XMLPUBFUN void XMLCALL
164 xmlCleanupMemory (void);
165 /*
166 * These are specific to the XML debug memory wrapper.
167 */
168 XMLPUBFUN int XMLCALL
169 xmlMemUsed (void);
170 XMLPUBFUN int XMLCALL
171 xmlMemBlocks (void);
172 XMLPUBFUN void XMLCALL
173 xmlMemDisplay (FILE *fp);
174 XMLPUBFUN void XMLCALL
175 xmlMemDisplayLast(FILE *fp, long nbBytes);
176 XMLPUBFUN void XMLCALL
177 xmlMemShow (FILE *fp, int nr);
178 XMLPUBFUN void XMLCALL
179 xmlMemoryDump (void);
180 XMLPUBFUN void * XMLCALL
181 xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
182 XMLPUBFUN void * XMLCALL
183 xmlMemRealloc (void *ptr,size_t size);
184 XMLPUBFUN void XMLCALL
185 xmlMemFree (void *ptr);
186 XMLPUBFUN char * XMLCALL
187 xmlMemoryStrdup (const char *str);
188 XMLPUBFUN void * XMLCALL
189 xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
190 XMLPUBFUN void * XMLCALL
191 xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
192 XMLPUBFUN void * XMLCALL
193 xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
194 XMLPUBFUN char * XMLCALL
195 xmlMemStrdupLoc (const char *str, const char *file, int line);
196
197
198 #ifdef DEBUG_MEMORY_LOCATION
199 /**
200 * xmlMalloc:
201 * @size: number of bytes to allocate
202 *
203 * Wrapper for the malloc() function used in the XML library.
204 *
205 * Returns the pointer to the allocated area or NULL in case of error.
206 */
207 #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
208 /**
209 * xmlMallocAtomic:
210 * @size: number of bytes to allocate
211 *
212 * Wrapper for the malloc() function used in the XML library for allocation
213 * of block not containing pointers to other areas.
214 *
215 * Returns the pointer to the allocated area or NULL in case of error.
216 */
217 #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
218 /**
219 * xmlRealloc:
220 * @ptr: pointer to the existing allocated area
221 * @size: number of bytes to allocate
222 *
223 * Wrapper for the realloc() function used in the XML library.
224 *
225 * Returns the pointer to the allocated area or NULL in case of error.
226 */
227 #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
228 /**
229 * xmlMemStrdup:
230 * @str: pointer to the existing string
231 *
232 * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
233 *
234 * Returns the pointer to the allocated area or NULL in case of error.
235 */
236 #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
237
238 #endif /* DEBUG_MEMORY_LOCATION */
239
240 #ifdef __cplusplus
241 }
242 #endif /* __cplusplus */
243
244 #ifndef __XML_GLOBALS_H
245 #ifndef __XML_THREADS_H__
246 #include <libxml/threads.h>
247 #include <libxml/globals.h>
248 #endif
249 #endif
250
251 #endif /* __DEBUG_MEMORY_ALLOC__ */
252