Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xmlmemory.h: interface for the memory allocation debug. |
| 3 | * |
| 4 | * Daniel.Veillard@w3.org |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | #ifndef _DEBUG_MEMORY_ALLOC_ |
| 9 | #define _DEBUG_MEMORY_ALLOC_ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <libxml/xmlversion.h> |
| 13 | |
| 14 | /* |
| 15 | * DEBUG_MEMORY_LOCATION should be activated only done when debugging |
| 16 | * libxml. |
| 17 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 18 | /* #define DEBUG_MEMORY_FREED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | /* #define DEBUG_MEMORY_LOCATION */ |
| 20 | |
| 21 | #ifdef DEBUG |
| 22 | #ifndef DEBUG_MEMORY |
| 23 | #define DEBUG_MEMORY |
| 24 | #endif |
| 25 | #endif |
| 26 | |
| 27 | #ifdef DEBUG_MEMORY_LOCATION |
| 28 | #define MEM_LIST /* keep a list of all the allocated memory blocks */ |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 29 | #define DEBUG_MEMORY_FREED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 32 | #ifdef DEBUG_MEMORY_FREED |
| 33 | #define MEM_CLEANUP(p,l) memset((p), -1, (l)); |
| 34 | #else |
| 35 | #define MEM_CLEANUP(p,l) |
| 36 | #endif |
| 37 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /* |
| 43 | * The XML memory wrapper support 4 basic overloadable functions |
| 44 | */ |
| 45 | typedef void (*xmlFreeFunc)(void *); |
| 46 | typedef void *(*xmlMallocFunc)(int); |
| 47 | typedef void *(*xmlReallocFunc)(void *, int); |
| 48 | typedef char *(*xmlStrdupFunc)(const char *); |
| 49 | |
| 50 | /* |
| 51 | * The 4 interfaces used for all memory handling within libxml |
| 52 | */ |
| 53 | LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree; |
| 54 | LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc; |
| 55 | LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc; |
| 56 | LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup; |
| 57 | |
| 58 | /* |
| 59 | * The way to overload the existing functions |
| 60 | */ |
| 61 | int xmlMemSetup (xmlFreeFunc freeFunc, |
| 62 | xmlMallocFunc mallocFunc, |
| 63 | xmlReallocFunc reallocFunc, |
| 64 | xmlStrdupFunc strdupFunc); |
| 65 | int xmlMemGet (xmlFreeFunc *freeFunc, |
| 66 | xmlMallocFunc *mallocFunc, |
| 67 | xmlReallocFunc *reallocFunc, |
| 68 | xmlStrdupFunc *strdupFunc); |
| 69 | |
| 70 | /* |
| 71 | * Initialization of the memory layer |
| 72 | */ |
| 73 | int xmlInitMemory (void); |
| 74 | |
| 75 | /* |
| 76 | * Those are specific to the XML debug memory wrapper |
| 77 | */ |
| 78 | int xmlMemUsed (void); |
| 79 | void xmlMemDisplay (FILE *fp); |
| 80 | void xmlMemShow (FILE *fp, int nr); |
| 81 | void xmlMemoryDump (void); |
| 82 | int xmlInitMemory (void); |
| 83 | |
| 84 | #ifdef DEBUG_MEMORY_LOCATION |
| 85 | #define xmlMalloc(x) xmlMallocLoc((x), __FILE__, __LINE__) |
| 86 | #define xmlRealloc(p, x) xmlReallocLoc((p), (x), __FILE__, __LINE__) |
| 87 | #define xmlMemStrdup(x) xmlMemStrdupLoc((x), __FILE__, __LINE__) |
| 88 | |
| 89 | void * xmlMallocLoc(int size, const char *file, int line); |
| 90 | void * xmlReallocLoc(void *ptr,int size, const char *file, int line); |
| 91 | char * xmlMemStrdupLoc(const char *str, const char *file, int line); |
| 92 | #endif /* DEBUG_MEMORY_LOCATION */ |
| 93 | |
| 94 | #ifdef __cplusplus |
| 95 | } |
| 96 | #endif /* __cplusplus */ |
| 97 | |
| 98 | #endif /* _DEBUG_MEMORY_ALLOC_ */ |
| 99 | |