xmlmemory.h


    1 /*
    2  * xmlmemory.h: interface for the memory allocation debug.
    3  *
    4  * daniel@veillard.com
    5  */
    6 
    7 
    8 #ifndef _DEBUG_MEMORY_ALLOC_
    9 #define _DEBUG_MEMORY_ALLOC_
    10 
    11 #include 
    12 #include 
    13 
    14 /**
    15  * DEBUG_MEMORY:
    16  *
    17  * DEBUG_MEMORY replaces the allocator with a collect and debug
    18  * shell to the libc allocator.
    19  * DEBUG_MEMORY should only be activated when debugging 
    20  * libxml i.e. if libxml has been configured with --with-debug-mem too.
    21  */
    22 /* #define DEBUG_MEMORY_FREED */
    23 /* #define DEBUG_MEMORY_LOCATION */
    24 
    25 #ifdef DEBUG
    26 #ifndef DEBUG_MEMORY
    27 #define DEBUG_MEMORY
    28 #endif
    29 #endif
    30 
    31 /**
    32  * DEBUG_MEMORY_LOCATION:
    33  *
    34  * DEBUG_MEMORY_LOCATION should be activated only when debugging 
    35  * libxml i.e. if libxml has been configured with --with-debug-mem too.
    36  */
    37 #ifdef DEBUG_MEMORY_LOCATION
    38 #endif
    39 
    40 #ifdef __cplusplus
    41 extern "C" {
    42 #endif
    43 
    44 /*
    45  * The XML memory wrapper support 4 basic overloadable functions.
    46  */
    47 /**
    48  * xmlFreeFunc:
    49  * @mem: an already allocated block of memory
    50  *
    51  * Signature for a free() implementation.
    52  */
    53 typedef void (*xmlFreeFunc)(void *mem);  <typedef:xmlFreeFunc>
    54 /**
    55  * xmlMallocFunc:
    56  * @size:  the size requested in bytes
    57  *
    58  * Signature for a malloc() implementation.
    59  *
    60  * Returns a pointer to the newly allocated block or NULL in case of error.
    61  */
    62 typedef void *(*xmlMallocFunc)(size_t size);  <typedef:xmlMallocFunc>
    63 
    64 /**
    65  * xmlReallocFunc:
    66  * @mem: an already allocated block of memory
    67  * @size:  the new size requested in bytes
    68  *
    69  * Signature for a realloc() implementation.
    70  *
    71  * Returns a pointer to the newly reallocated block or NULL in case of error.
    72  */
    73 typedef void *(*xmlReallocFunc)(void *mem, size_t size);  <typedef:xmlReallocFunc>
    74 
    75 /**
    76  * xmlStrdupFunc:
    77  * @str: a zero terminated string
    78  *
    79  * Signature for an strdup() implementation.
    80  *
    81  * Returns the copy of the string or NULL in case of error.
    82  */
    83 typedef char *(*xmlStrdupFunc)(const char *str);  <typedef:xmlStrdupFunc>
    84 
    85 /*
    86  * The 4 interfaces used for all memory handling within libxml.
    87 LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
    88 LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
    89 LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
    90 LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
    91  */
    92 
    93 /*
    94  * The way to overload the existing functions.
    95  */
    96 int     xmlMemSetup	(xmlFreeFunc freeFunc,
    97 			 xmlMallocFunc mallocFunc,
    98 			 xmlReallocFunc reallocFunc,
    99 			 xmlStrdupFunc strdupFunc);
    100 int     xmlMemGet	(xmlFreeFunc *freeFunc,
    101 			 xmlMallocFunc *mallocFunc,
    102 			 xmlReallocFunc *reallocFunc,
    103 			 xmlStrdupFunc *strdupFunc);
    104 
    105 /*
    106  * Initialization of the memory layer.
    107  */
    108 int	xmlInitMemory	(void);
    109 
    110 /*
    111  * Those are specific to the XML debug memory wrapper.
    112  */
    113 int	xmlMemUsed	(void);
    114 void	xmlMemDisplay	(FILE *fp);
    115 void	xmlMemShow	(FILE *fp, int nr);
    116 void	xmlMemoryDump	(void);
    117 void *	xmlMemMalloc	(size_t size);
    118 void *	xmlMemRealloc	(void *ptr,size_t size);
    119 void	xmlMemFree	(void *ptr);
    120 char *	xmlMemoryStrdup	(const char *str);
    121 
    122 #ifdef DEBUG_MEMORY_LOCATION
    123 /**
    124  * xmlMalloc:
    125  * @size:  number of bytes to allocate
    126  *
    127  * Wrapper for the malloc() function used in the XML library.
    128  *
    129  * Returns the pointer to the allocated area or NULL in case of error.
    130  */
    131 #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
    132 /**
    133  * xmlRealloc:
    134  * @ptr:  pointer to the existing allocated area
    135  * @size:  number of bytes to allocate
    136  *
    137  * Wrapper for the realloc() function used in the XML library.
    138  *
    139  * Returns the pointer to the allocated area or NULL in case of error.
    140  */
    141 #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
    142 /**
    143  * xmlMemStrdup:
    144  * @str:  pointer to the existing string
    145  *
    146  * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
    147  *
    148  * Returns the pointer to the allocated area or NULL in case of error.
    149  */
    150 #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
    151 
    152 void *	xmlMallocLoc(size_t size, const char *file, int line);
    153 void *	xmlReallocLoc(void *ptr,size_t size, const char *file, int line);
    154 char *	xmlMemStrdupLoc(const char *str, const char *file, int line);
    155 #endif /* DEBUG_MEMORY_LOCATION */
    156 
    157 #ifdef __cplusplus
    158 }
    159 #endif /* __cplusplus */
    160 
    161 #ifndef __XML_GLOBALS_H
    162 #ifndef __XML_THREADS_H__
    163 #include 
    164 #include 
    165 #endif
    166 #endif
    167 
    168 #endif  /* _DEBUG_MEMORY_ALLOC_ */
    169