blob: 235721ceacb4c1b6f6195731cb89a513e21a95da [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardbe586972003-11-18 20:56:51 +00002 * Summary: interface for the memory allocator
3 * Description: provides interfaces for the memory allocator,
4 * including debugging capabilities.
Owen Taylor3473f882001-02-23 17:55:21 +00005 *
Daniel Veillardbe586972003-11-18 20:56:51 +00006 * Copy: See Copyright for the status of this software.
7 *
8 * Author: Daniel Veillard
Owen Taylor3473f882001-02-23 17:55:21 +00009 */
10
11
Daniel Veillard7a02cfe2003-09-25 12:18:34 +000012#ifndef __DEBUG_MEMORY_ALLOC__
13#define __DEBUG_MEMORY_ALLOC__
Owen Taylor3473f882001-02-23 17:55:21 +000014
15#include <stdio.h>
16#include <libxml/xmlversion.h>
17
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000018/**
19 * DEBUG_MEMORY:
20 *
Daniel Veillard61f26172002-03-12 18:46:39 +000021 * DEBUG_MEMORY replaces the allocator with a collect and debug
22 * shell to the libc allocator.
23 * DEBUG_MEMORY should only be activated when debugging
24 * libxml i.e. if libxml has been configured with --with-debug-mem too.
Owen Taylor3473f882001-02-23 17:55:21 +000025 */
Daniel Veillard48b2f892001-02-25 16:11:03 +000026/* #define DEBUG_MEMORY_FREED */
Owen Taylor3473f882001-02-23 17:55:21 +000027/* #define DEBUG_MEMORY_LOCATION */
28
29#ifdef DEBUG
30#ifndef DEBUG_MEMORY
31#define DEBUG_MEMORY
32#endif
33#endif
34
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000035/**
36 * DEBUG_MEMORY_LOCATION:
37 *
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000038 * DEBUG_MEMORY_LOCATION should be activated only when debugging
Daniel Veillard61f26172002-03-12 18:46:39 +000039 * libxml i.e. if libxml has been configured with --with-debug-mem too.
Daniel Veillardf69bb4b2001-05-19 13:24:56 +000040 */
Owen Taylor3473f882001-02-23 17:55:21 +000041#ifdef DEBUG_MEMORY_LOCATION
Daniel Veillard48b2f892001-02-25 16:11:03 +000042#endif
43
Owen Taylor3473f882001-02-23 17:55:21 +000044#ifdef __cplusplus
45extern "C" {
46#endif
47
48/*
Daniel Veillard61f26172002-03-12 18:46:39 +000049 * The XML memory wrapper support 4 basic overloadable functions.
Owen Taylor3473f882001-02-23 17:55:21 +000050 */
Daniel Veillard9d06d302002-01-22 18:15:52 +000051/**
52 * xmlFreeFunc:
53 * @mem: an already allocated block of memory
54 *
Daniel Veillard61f26172002-03-12 18:46:39 +000055 * Signature for a free() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000056 */
Igor Zlatkovic18a88ce2004-02-03 08:27:55 +000057typedef void (XMLCALL *xmlFreeFunc)(void *mem);
Daniel Veillard9d06d302002-01-22 18:15:52 +000058/**
59 * xmlMallocFunc:
60 * @size: the size requested in bytes
61 *
Daniel Veillard61f26172002-03-12 18:46:39 +000062 * Signature for a malloc() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000063 *
Daniel Veillard61f26172002-03-12 18:46:39 +000064 * Returns a pointer to the newly allocated block or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000065 */
Igor Zlatkovic18a88ce2004-02-03 08:27:55 +000066typedef void *(XMLCALL *xmlMallocFunc)(size_t size);
Daniel Veillard9d06d302002-01-22 18:15:52 +000067
68/**
69 * xmlReallocFunc:
70 * @mem: an already allocated block of memory
71 * @size: the new size requested in bytes
72 *
Daniel Veillard61f26172002-03-12 18:46:39 +000073 * Signature for a realloc() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000074 *
Daniel Veillard61f26172002-03-12 18:46:39 +000075 * Returns a pointer to the newly reallocated block or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000076 */
Igor Zlatkovic18a88ce2004-02-03 08:27:55 +000077typedef void *(XMLCALL *xmlReallocFunc)(void *mem, size_t size);
Daniel Veillard9d06d302002-01-22 18:15:52 +000078
79/**
80 * xmlStrdupFunc:
81 * @str: a zero terminated string
82 *
Daniel Veillard61f26172002-03-12 18:46:39 +000083 * Signature for an strdup() implementation.
Daniel Veillard9d06d302002-01-22 18:15:52 +000084 *
Daniel Veillard61f26172002-03-12 18:46:39 +000085 * Returns the copy of the string or NULL in case of error.
Daniel Veillard9d06d302002-01-22 18:15:52 +000086 */
Igor Zlatkovic18a88ce2004-02-03 08:27:55 +000087typedef char *(XMLCALL *xmlStrdupFunc)(const char *str);
Owen Taylor3473f882001-02-23 17:55:21 +000088
89/*
Daniel Veillard61f26172002-03-12 18:46:39 +000090 * The 4 interfaces used for all memory handling within libxml.
Owen Taylor3473f882001-02-23 17:55:21 +000091LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
92LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
Daniel Veillard3c908dc2003-04-19 00:07:51 +000093LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic;
Owen Taylor3473f882001-02-23 17:55:21 +000094LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
95LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +000096 */
Owen Taylor3473f882001-02-23 17:55:21 +000097
98/*
Daniel Veillard61f26172002-03-12 18:46:39 +000099 * The way to overload the existing functions.
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000100 * The xmlGc function have an extra entry for atomic block
101 * allocations useful for garbage collected memory allocators
Owen Taylor3473f882001-02-23 17:55:21 +0000102 */
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000103XMLPUBFUN int XMLCALL
104 xmlMemSetup (xmlFreeFunc freeFunc,
Owen Taylor3473f882001-02-23 17:55:21 +0000105 xmlMallocFunc mallocFunc,
106 xmlReallocFunc reallocFunc,
107 xmlStrdupFunc strdupFunc);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000108XMLPUBFUN int XMLCALL
109 xmlMemGet (xmlFreeFunc *freeFunc,
Owen Taylor3473f882001-02-23 17:55:21 +0000110 xmlMallocFunc *mallocFunc,
111 xmlReallocFunc *reallocFunc,
112 xmlStrdupFunc *strdupFunc);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000113XMLPUBFUN int XMLCALL
114 xmlGcMemSetup (xmlFreeFunc freeFunc,
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000115 xmlMallocFunc mallocFunc,
116 xmlMallocFunc mallocAtomicFunc,
117 xmlReallocFunc reallocFunc,
118 xmlStrdupFunc strdupFunc);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000119XMLPUBFUN int XMLCALL
120 xmlGcMemGet (xmlFreeFunc *freeFunc,
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000121 xmlMallocFunc *mallocFunc,
122 xmlMallocFunc *mallocAtomicFunc,
123 xmlReallocFunc *reallocFunc,
124 xmlStrdupFunc *strdupFunc);
Owen Taylor3473f882001-02-23 17:55:21 +0000125
126/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000127 * Initialization of the memory layer.
Owen Taylor3473f882001-02-23 17:55:21 +0000128 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000129XMLPUBFUN int XMLCALL
130 xmlInitMemory (void);
Owen Taylor3473f882001-02-23 17:55:21 +0000131
William M. Brack72ee48d2003-12-30 08:30:19 +0000132/*
133 * Cleanup of the memory layer.
134 */
135XMLPUBFUN void XMLCALL
136 xmlCleanupMemory (void);
Owen Taylor3473f882001-02-23 17:55:21 +0000137/*
William M. Brackc1939562003-08-05 15:52:22 +0000138 * These are specific to the XML debug memory wrapper.
Owen Taylor3473f882001-02-23 17:55:21 +0000139 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000140XMLPUBFUN int XMLCALL
141 xmlMemUsed (void);
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000142XMLPUBFUN int XMLCALL
143 xmlMemBlocks (void);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000144XMLPUBFUN void XMLCALL
145 xmlMemDisplay (FILE *fp);
146XMLPUBFUN void XMLCALL
147 xmlMemShow (FILE *fp, int nr);
148XMLPUBFUN void XMLCALL
149 xmlMemoryDump (void);
150XMLPUBFUN void * XMLCALL
151 xmlMemMalloc (size_t size);
152XMLPUBFUN void * XMLCALL
153 xmlMemRealloc (void *ptr,size_t size);
154XMLPUBFUN void XMLCALL
155 xmlMemFree (void *ptr);
156XMLPUBFUN char * XMLCALL
157 xmlMemoryStrdup (const char *str);
158XMLPUBFUN void * XMLCALL
159 xmlMallocLoc (size_t size, const char *file, int line);
160XMLPUBFUN void * XMLCALL
161 xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
162XMLPUBFUN void * XMLCALL
163 xmlMallocAtomicLoc (size_t size, const char *file, int line);
164XMLPUBFUN char * XMLCALL
165 xmlMemStrdupLoc (const char *str, const char *file, int line);
William M. Brackc1939562003-08-05 15:52:22 +0000166
Owen Taylor3473f882001-02-23 17:55:21 +0000167
168#ifdef DEBUG_MEMORY_LOCATION
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000169/**
170 * xmlMalloc:
171 * @size: number of bytes to allocate
172 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000173 * Wrapper for the malloc() function used in the XML library.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000174 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000175 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000176 */
177#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
178/**
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000179 * xmlMallocAtomic:
180 * @size: number of bytes to allocate
181 *
182 * Wrapper for the malloc() function used in the XML library for allocation
183 * of block not containing pointers to other areas.
184 *
185 * Returns the pointer to the allocated area or NULL in case of error.
186 */
187#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
188/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000189 * xmlRealloc:
190 * @ptr: pointer to the existing allocated area
191 * @size: number of bytes to allocate
192 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000193 * Wrapper for the realloc() function used in the XML library.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000194 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000195 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000196 */
197#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
198/**
199 * xmlMemStrdup:
200 * @str: pointer to the existing string
201 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000202 * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000203 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000204 * Returns the pointer to the allocated area or NULL in case of error.
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000205 */
206#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
Owen Taylor3473f882001-02-23 17:55:21 +0000207
Owen Taylor3473f882001-02-23 17:55:21 +0000208#endif /* DEBUG_MEMORY_LOCATION */
209
210#ifdef __cplusplus
211}
212#endif /* __cplusplus */
213
Daniel Veillard6c4ffaf2002-02-11 08:54:05 +0000214#ifndef __XML_GLOBALS_H
215#ifndef __XML_THREADS_H__
216#include <libxml/threads.h>
217#include <libxml/globals.h>
218#endif
219#endif
220
Daniel Veillard7a02cfe2003-09-25 12:18:34 +0000221#endif /* __DEBUG_MEMORY_ALLOC__ */
Owen Taylor3473f882001-02-23 17:55:21 +0000222