xref: /aosp_15_r20/external/libxml2/include/libxml/xmlmemory.h (revision 7c5688314b92172186c154356a6374bf7684c3ca)
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 XML_DEPRECATED
116 XMLPUBFUN int
117 	xmlGcMemSetup	(xmlFreeFunc freeFunc,
118 			 xmlMallocFunc mallocFunc,
119 			 xmlMallocFunc mallocAtomicFunc,
120 			 xmlReallocFunc reallocFunc,
121 			 xmlStrdupFunc strdupFunc);
122 XML_DEPRECATED
123 XMLPUBFUN int
124 	xmlGcMemGet	(xmlFreeFunc *freeFunc,
125 			 xmlMallocFunc *mallocFunc,
126 			 xmlMallocFunc *mallocAtomicFunc,
127 			 xmlReallocFunc *reallocFunc,
128 			 xmlStrdupFunc *strdupFunc);
129 
130 /*
131  * Initialization of the memory layer.
132  */
133 XML_DEPRECATED
134 XMLPUBFUN int
135 	xmlInitMemory	(void);
136 
137 /*
138  * Cleanup of the memory layer.
139  */
140 XML_DEPRECATED
141 XMLPUBFUN void
142                 xmlCleanupMemory        (void);
143 /*
144  * These are specific to the XML debug memory wrapper.
145  */
146 XMLPUBFUN size_t
147 	xmlMemSize	(void *ptr);
148 XMLPUBFUN int
149 	xmlMemUsed	(void);
150 XMLPUBFUN int
151 	xmlMemBlocks	(void);
152 XML_DEPRECATED
153 XMLPUBFUN void
154 	xmlMemDisplay	(FILE *fp);
155 XML_DEPRECATED
156 XMLPUBFUN void
157 	xmlMemDisplayLast(FILE *fp, long nbBytes);
158 XML_DEPRECATED
159 XMLPUBFUN void
160 	xmlMemShow	(FILE *fp, int nr);
161 XML_DEPRECATED
162 XMLPUBFUN void
163 	xmlMemoryDump	(void);
164 XMLPUBFUN void *
165 	xmlMemMalloc	(size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
166 XMLPUBFUN void *
167 	xmlMemRealloc	(void *ptr,size_t size);
168 XMLPUBFUN void
169 	xmlMemFree	(void *ptr);
170 XMLPUBFUN char *
171 	xmlMemoryStrdup	(const char *str);
172 XML_DEPRECATED
173 XMLPUBFUN void *
174 	xmlMallocLoc	(size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
175 XML_DEPRECATED
176 XMLPUBFUN void *
177 	xmlReallocLoc	(void *ptr, size_t size, const char *file, int line);
178 XML_DEPRECATED
179 XMLPUBFUN void *
180 	xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
181 XML_DEPRECATED
182 XMLPUBFUN char *
183 	xmlMemStrdupLoc	(const char *str, const char *file, int line);
184 
185 #ifdef __cplusplus
186 }
187 #endif /* __cplusplus */
188 
189 #endif  /* __DEBUG_MEMORY_ALLOC__ */
190 
191