xmlmemory.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Summary: interface for the memory allocator
  3. * Description: provides interfaces for the memory allocator,
  4. * including debugging capabilities.
  5. *
  6. * Copy: See Copyright for the status of this software.
  7. *
  8. * Author: Daniel Veillard
  9. */
  10. #ifndef __DEBUG_MEMORY_ALLOC__
  11. #define __DEBUG_MEMORY_ALLOC__
  12. #include <stdio.h>
  13. #include <libxml/xmlversion.h>
  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. #ifdef DEBUG
  25. #ifndef DEBUG_MEMORY
  26. #define DEBUG_MEMORY
  27. #endif
  28. #endif
  29. /**
  30. * DEBUG_MEMORY_LOCATION:
  31. *
  32. * DEBUG_MEMORY_LOCATION should be activated only when debugging
  33. * libxml i.e. if libxml has been configured with --with-debug-mem too.
  34. */
  35. #ifdef DEBUG_MEMORY_LOCATION
  36. #endif
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /*
  41. * The XML memory wrapper support 4 basic overloadable functions.
  42. */
  43. /**
  44. * xmlFreeFunc:
  45. * @mem: an already allocated block of memory
  46. *
  47. * Signature for a free() implementation.
  48. */
  49. typedef void (XMLCALL *xmlFreeFunc)(void *mem);
  50. /**
  51. * xmlMallocFunc:
  52. * @size: the size requested in bytes
  53. *
  54. * Signature for a malloc() implementation.
  55. *
  56. * Returns a pointer to the newly allocated block or NULL in case of error.
  57. */
  58. typedef void *(LIBXML_ATTR_ALLOC_SIZE(1) XMLCALL *xmlMallocFunc)(size_t size);
  59. /**
  60. * xmlReallocFunc:
  61. * @mem: an already allocated block of memory
  62. * @size: the new size requested in bytes
  63. *
  64. * Signature for a realloc() implementation.
  65. *
  66. * Returns a pointer to the newly reallocated block or NULL in case of error.
  67. */
  68. typedef void *(XMLCALL *xmlReallocFunc)(void *mem, size_t size);
  69. /**
  70. * xmlStrdupFunc:
  71. * @str: a zero terminated string
  72. *
  73. * Signature for an strdup() implementation.
  74. *
  75. * Returns the copy of the string or NULL in case of error.
  76. */
  77. typedef char *(XMLCALL *xmlStrdupFunc)(const char *str);
  78. /*
  79. * The 4 interfaces used for all memory handling within libxml.
  80. LIBXML_DLL_IMPORT xmlFreeFunc xmlFree;
  81. LIBXML_DLL_IMPORT xmlMallocFunc xmlMalloc;
  82. LIBXML_DLL_IMPORT xmlMallocFunc xmlMallocAtomic;
  83. LIBXML_DLL_IMPORT xmlReallocFunc xmlRealloc;
  84. LIBXML_DLL_IMPORT xmlStrdupFunc xmlMemStrdup;
  85. */
  86. /*
  87. * The way to overload the existing functions.
  88. * The xmlGc function have an extra entry for atomic block
  89. * allocations useful for garbage collected memory allocators
  90. */
  91. XMLPUBFUN int XMLCALL
  92. xmlMemSetup (xmlFreeFunc freeFunc,
  93. xmlMallocFunc mallocFunc,
  94. xmlReallocFunc reallocFunc,
  95. xmlStrdupFunc strdupFunc);
  96. XMLPUBFUN int XMLCALL
  97. xmlMemGet (xmlFreeFunc *freeFunc,
  98. xmlMallocFunc *mallocFunc,
  99. xmlReallocFunc *reallocFunc,
  100. xmlStrdupFunc *strdupFunc);
  101. XMLPUBFUN int XMLCALL
  102. xmlGcMemSetup (xmlFreeFunc freeFunc,
  103. xmlMallocFunc mallocFunc,
  104. xmlMallocFunc mallocAtomicFunc,
  105. xmlReallocFunc reallocFunc,
  106. xmlStrdupFunc strdupFunc);
  107. XMLPUBFUN int XMLCALL
  108. xmlGcMemGet (xmlFreeFunc *freeFunc,
  109. xmlMallocFunc *mallocFunc,
  110. xmlMallocFunc *mallocAtomicFunc,
  111. xmlReallocFunc *reallocFunc,
  112. xmlStrdupFunc *strdupFunc);
  113. /*
  114. * Initialization of the memory layer.
  115. */
  116. XMLPUBFUN int XMLCALL
  117. xmlInitMemory (void);
  118. /*
  119. * Cleanup of the memory layer.
  120. */
  121. XMLPUBFUN void XMLCALL
  122. xmlCleanupMemory (void);
  123. /*
  124. * These are specific to the XML debug memory wrapper.
  125. */
  126. XMLPUBFUN int XMLCALL
  127. xmlMemUsed (void);
  128. XMLPUBFUN int XMLCALL
  129. xmlMemBlocks (void);
  130. XMLPUBFUN void XMLCALL
  131. xmlMemDisplay (FILE *fp);
  132. XMLPUBFUN void XMLCALL
  133. xmlMemDisplayLast(FILE *fp, long nbBytes);
  134. XMLPUBFUN void XMLCALL
  135. xmlMemShow (FILE *fp, int nr);
  136. XMLPUBFUN void XMLCALL
  137. xmlMemoryDump (void);
  138. XMLPUBFUN void * XMLCALL
  139. xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
  140. XMLPUBFUN void * XMLCALL
  141. xmlMemRealloc (void *ptr,size_t size);
  142. XMLPUBFUN void XMLCALL
  143. xmlMemFree (void *ptr);
  144. XMLPUBFUN char * XMLCALL
  145. xmlMemoryStrdup (const char *str);
  146. XMLPUBFUN void * XMLCALL
  147. xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
  148. XMLPUBFUN void * XMLCALL
  149. xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
  150. XMLPUBFUN void * XMLCALL
  151. xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
  152. XMLPUBFUN char * XMLCALL
  153. xmlMemStrdupLoc (const char *str, const char *file, int line);
  154. #ifdef DEBUG_MEMORY_LOCATION
  155. /**
  156. * xmlMalloc:
  157. * @size: number of bytes to allocate
  158. *
  159. * Wrapper for the malloc() function used in the XML library.
  160. *
  161. * Returns the pointer to the allocated area or NULL in case of error.
  162. */
  163. #define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
  164. /**
  165. * xmlMallocAtomic:
  166. * @size: number of bytes to allocate
  167. *
  168. * Wrapper for the malloc() function used in the XML library for allocation
  169. * of block not containing pointers to other areas.
  170. *
  171. * Returns the pointer to the allocated area or NULL in case of error.
  172. */
  173. #define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
  174. /**
  175. * xmlRealloc:
  176. * @ptr: pointer to the existing allocated area
  177. * @size: number of bytes to allocate
  178. *
  179. * Wrapper for the realloc() function used in the XML library.
  180. *
  181. * Returns the pointer to the allocated area or NULL in case of error.
  182. */
  183. #define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
  184. /**
  185. * xmlMemStrdup:
  186. * @str: pointer to the existing string
  187. *
  188. * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
  189. *
  190. * Returns the pointer to the allocated area or NULL in case of error.
  191. */
  192. #define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
  193. #endif /* DEBUG_MEMORY_LOCATION */
  194. #ifdef __cplusplus
  195. }
  196. #endif /* __cplusplus */
  197. #ifndef __XML_GLOBALS_H
  198. #ifndef __XML_THREADS_H__
  199. #include <libxml/threads.h>
  200. #include <libxml/globals.h>
  201. #endif
  202. #endif
  203. #endif /* __DEBUG_MEMORY_ALLOC__ */