Reactos
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
19extern "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 */
31typedef 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 */
40typedef 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 */
51typedef 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 */
61typedef 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 */
105XMLPUBFUN int
106 xmlMemSetup (xmlFreeFunc freeFunc,
107 xmlMallocFunc mallocFunc,
108 xmlReallocFunc reallocFunc,
109 xmlStrdupFunc strdupFunc);
110XMLPUBFUN int
111 xmlMemGet (xmlFreeFunc *freeFunc,
112 xmlMallocFunc *mallocFunc,
113 xmlReallocFunc *reallocFunc,
114 xmlStrdupFunc *strdupFunc);
115XMLPUBFUN int
116 xmlGcMemSetup (xmlFreeFunc freeFunc,
117 xmlMallocFunc mallocFunc,
118 xmlMallocFunc mallocAtomicFunc,
119 xmlReallocFunc reallocFunc,
120 xmlStrdupFunc strdupFunc);
121XMLPUBFUN 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 */
131XML_DEPRECATED
132XMLPUBFUN int
133 xmlInitMemory (void);
134
135/*
136 * Cleanup of the memory layer.
137 */
138XML_DEPRECATED
139XMLPUBFUN void
140 xmlCleanupMemory (void);
141/*
142 * These are specific to the XML debug memory wrapper.
143 */
144XMLPUBFUN size_t
145 xmlMemSize (void *ptr);
146XMLPUBFUN int
147 xmlMemUsed (void);
148XMLPUBFUN int
149 xmlMemBlocks (void);
150XMLPUBFUN void
151 xmlMemDisplay (FILE *fp);
152XMLPUBFUN void
153 xmlMemDisplayLast(FILE *fp, long nbBytes);
154XMLPUBFUN void
155 xmlMemShow (FILE *fp, int nr);
156XMLPUBFUN void
157 xmlMemoryDump (void);
158XMLPUBFUN void *
159 xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
160XMLPUBFUN void *
161 xmlMemRealloc (void *ptr,size_t size);
162XMLPUBFUN void
163 xmlMemFree (void *ptr);
164XMLPUBFUN char *
165 xmlMemoryStrdup (const char *str);
166XMLPUBFUN void *
167 xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
168XMLPUBFUN void *
169 xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
170XMLPUBFUN void *
171 xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
172XMLPUBFUN 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__ */