2 * Summary: interface for the memory allocator
3 * Description: provides interfaces for the memory allocator,
4 * including debugging capabilities.
6 * Copy: See Copyright for the status of this software.
8 * Author: Daniel Veillard
9 * Portion Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
17 #ifndef LIBXML2_XMLMEMORY_H
18 #define LIBXML2_XMLMEMORY_H
21 #include <stdapis/libxml2/libxml2_xmlversion.h>
27 * DEBUG_MEMORY replaces the allocator with a collect and debug
28 * shell to the libc allocator.
29 * DEBUG_MEMORY should only be activated when debugging
30 * libxml i.e. if libxml has been configured with --with-debug-mem too.
32 /* #define DEBUG_MEMORY_FREED */
33 /* #define DEBUG_MEMORY_LOCATION */
42 * DEBUG_MEMORY_LOCATION:
44 * DEBUG_MEMORY_LOCATION should be activated only when debugging
45 * libxml i.e. if libxml has been configured with --with-debug-mem too.
47 #ifdef DEBUG_MEMORY_LOCATION
55 * The XML memory wrapper support 4 basic overloadable functions.
59 * @param mem an already allocated block of memory
61 * Signature for a free() implementation.
63 typedef void (XMLCALL *xmlFreeFunc)(void *mem);
66 * @param size the size requested in bytes
68 * Signature for a malloc() implementation.
70 * Returns a pointer to the newly allocated block or NULL in case of error.
72 typedef void *(XMLCALL *xmlMallocFunc)(size_t size);
76 * @param mem an already allocated block of memory
77 * @param size the new size requested in bytes
79 * Signature for a realloc() implementation.
81 * Returns a pointer to the newly reallocated block or NULL in case of error.
83 typedef void *(XMLCALL *xmlReallocFunc)(void *mem, size_t size);
87 * @param str a zero terminated string
89 * Signature for an strdup() implementation.
91 * Returns the copy of the string or NULL in case of error.
93 typedef char *(XMLCALL *xmlStrdupFunc)(const char *str);
95 /*The 4 interfaces used for all memory handling within libxml.
96 LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
97 LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
98 LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic;
99 LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
100 LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
104 * The way to overload the existing functions.
105 * The xmlGc function have an extra entry for atomic block
106 * allocations useful for garbage collected memory allocators
108 XMLPUBFUN int XMLCALL
109 xmlMemSetup (xmlFreeFunc freeFunc,
110 xmlMallocFunc mallocFunc,
111 xmlReallocFunc reallocFunc,
112 xmlStrdupFunc strdupFunc);
113 XMLPUBFUN int XMLCALL
114 xmlMemGet (xmlFreeFunc *freeFunc,
115 xmlMallocFunc *mallocFunc,
116 xmlReallocFunc *reallocFunc,
117 xmlStrdupFunc *strdupFunc);
119 #ifndef XMLENGINE_EXCLUDE_UNUSED
121 XMLPUBFUN int XMLCALL
122 xmlGcMemSetup (xmlFreeFunc freeFunc,
123 xmlMallocFunc mallocFunc,
124 xmlMallocFunc mallocAtomicFunc,
125 xmlReallocFunc reallocFunc,
126 xmlStrdupFunc strdupFunc);
128 XMLPUBFUN int XMLCALL
129 xmlGcMemGet (xmlFreeFunc *freeFunc,
130 xmlMallocFunc *mallocFunc,
131 xmlMallocFunc *mallocAtomicFunc,
132 xmlReallocFunc *reallocFunc,
133 xmlStrdupFunc *strdupFunc);
134 #endif /* ifndef XMLENGINE_EXCLUDE_UNUSED */
137 * Initialization of the memory layer.
139 XMLPUBFUN int XMLCALL
140 xmlInitMemory (void);
143 * Cleanup of the memory layer.
145 XMLPUBFUN void XMLCALL
146 xmlCleanupMemory (void);
148 * These are specific to the XML debug memory wrapper.
150 XMLPUBFUN int XMLCALL
153 #ifndef XMLENGINE_EXCLUDE_FILE_FUNC
154 XMLPUBFUN void XMLCALL
155 xmlMemDisplay (FILE *fp);
156 XMLPUBFUN void XMLCALL
157 xmlMemShow (FILE *fp, int nr);
160 XMLPUBFUN void XMLCALL
161 xmlMemoryDump (void);
162 XMLPUBFUN void * XMLCALL
163 xmlMemMalloc (size_t size);
164 XMLPUBFUN void * XMLCALL
165 xmlMemRealloc (void *ptr,size_t size);
166 XMLPUBFUN void XMLCALL
167 xmlMemFree (void *ptr);
168 XMLPUBFUN char * XMLCALL
169 xmlMemoryStrdup (const char *str);
170 XMLPUBFUN void * XMLCALL
171 xmlMallocLoc (size_t size, const char *file, int line);
172 XMLPUBFUN void * XMLCALL
173 xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
174 XMLPUBFUN void * XMLCALL
175 xmlMallocAtomicLoc (size_t size, const char *file, int line);
176 XMLPUBFUN char * XMLCALL
177 xmlMemStrdupLoc (const char *str, const char *file, int line);
180 #ifdef DEBUG_MEMORY_LOCATION
183 * @param size number of bytes to allocate
185 * Wrapper for the malloc() function used in the XML library.
187 * Returns the pointer to the allocated area or NULL in case of error.
189 #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
192 * @param size number of bytes to allocate
194 * Wrapper for the malloc() function used in the XML library for allocation
195 * of block not containing pointers to other areas.
197 * Returns the pointer to the allocated area or NULL in case of error.
199 #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
202 * @param ptr pointer to the existing allocated area
203 * @param size number of bytes to allocate
205 * Wrapper for the realloc() function used in the XML library.
207 * Returns the pointer to the allocated area or NULL in case of error.
209 #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
212 * @param str pointer to the existing string
214 * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
216 * Returns the pointer to the allocated area or NULL in case of error.
218 #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
220 #endif /* DEBUG_MEMORY_LOCATION */
224 #endif /* __cplusplus */
227 #endif /* LIBXML2_MEMORY_H */