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