xmlmemory.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /*
  18. * The XML memory wrapper support 4 basic overloadable functions.
  19. */
  20. /**
  21. * xmlFreeFunc:
  22. * @mem: an already allocated block of memory
  23. *
  24. * Signature for a free() implementation.
  25. */
  26. typedef void (*xmlFreeFunc)(void *mem);
  27. /**
  28. * xmlMallocFunc:
  29. * @size: the size requested in bytes
  30. *
  31. * Signature for a malloc() implementation.
  32. *
  33. * Returns a pointer to the newly allocated block or NULL in case of error.
  34. */
  35. typedef void *(LIBXML_ATTR_ALLOC_SIZE(1) *xmlMallocFunc)(size_t size);
  36. /**
  37. * xmlReallocFunc:
  38. * @mem: an already allocated block of memory
  39. * @size: the new size requested in bytes
  40. *
  41. * Signature for a realloc() implementation.
  42. *
  43. * Returns a pointer to the newly reallocated block or NULL in case of error.
  44. */
  45. typedef void *(*xmlReallocFunc)(void *mem, size_t size);
  46. /**
  47. * xmlStrdupFunc:
  48. * @str: a zero terminated string
  49. *
  50. * Signature for an strdup() implementation.
  51. *
  52. * Returns the copy of the string or NULL in case of error.
  53. */
  54. typedef char *(*xmlStrdupFunc)(const char *str);
  55. /*
  56. * In general the memory allocation entry points are not kept
  57. * thread specific but this can be overridden by LIBXML_THREAD_ALLOC_ENABLED
  58. * - xmlMalloc
  59. * - xmlMallocAtomic
  60. * - xmlRealloc
  61. * - xmlMemStrdup
  62. * - xmlFree
  63. */
  64. /** DOC_DISABLE */
  65. #ifdef LIBXML_THREAD_ALLOC_ENABLED
  66. #define XML_GLOBALS_ALLOC \
  67. XML_OP(xmlMalloc, xmlMallocFunc, XML_NO_ATTR) \
  68. XML_OP(xmlMallocAtomic, xmlMallocFunc, XML_NO_ATTR) \
  69. XML_OP(xmlRealloc, xmlReallocFunc, XML_NO_ATTR) \
  70. XML_OP(xmlFree, xmlFreeFunc, XML_NO_ATTR) \
  71. XML_OP(xmlMemStrdup, xmlStrdupFunc, XML_NO_ATTR)
  72. #define XML_OP XML_DECLARE_GLOBAL
  73. XML_GLOBALS_ALLOC
  74. #undef XML_OP
  75. #if defined(LIBXML_THREAD_ENABLED) && !defined(XML_GLOBALS_NO_REDEFINITION)
  76. #define xmlMalloc XML_GLOBAL_MACRO(xmlMalloc)
  77. #define xmlMallocAtomic XML_GLOBAL_MACRO(xmlMallocAtomic)
  78. #define xmlRealloc XML_GLOBAL_MACRO(xmlRealloc)
  79. #define xmlFree XML_GLOBAL_MACRO(xmlFree)
  80. #define xmlMemStrdup XML_GLOBAL_MACRO(xmlMemStrdup)
  81. #endif
  82. #else
  83. #define XML_GLOBALS_ALLOC
  84. /** DOC_ENABLE */
  85. XMLPUBVAR xmlMallocFunc xmlMalloc;
  86. XMLPUBVAR xmlMallocFunc xmlMallocAtomic;
  87. XMLPUBVAR xmlReallocFunc xmlRealloc;
  88. XMLPUBVAR xmlFreeFunc xmlFree;
  89. XMLPUBVAR xmlStrdupFunc xmlMemStrdup;
  90. #endif
  91. /*
  92. * The way to overload the existing functions.
  93. * The xmlGc function have an extra entry for atomic block
  94. * allocations useful for garbage collected memory allocators
  95. */
  96. XMLPUBFUN int
  97. xmlMemSetup (xmlFreeFunc freeFunc,
  98. xmlMallocFunc mallocFunc,
  99. xmlReallocFunc reallocFunc,
  100. xmlStrdupFunc strdupFunc);
  101. XMLPUBFUN int
  102. xmlMemGet (xmlFreeFunc *freeFunc,
  103. xmlMallocFunc *mallocFunc,
  104. xmlReallocFunc *reallocFunc,
  105. xmlStrdupFunc *strdupFunc);
  106. XMLPUBFUN int
  107. xmlGcMemSetup (xmlFreeFunc freeFunc,
  108. xmlMallocFunc mallocFunc,
  109. xmlMallocFunc mallocAtomicFunc,
  110. xmlReallocFunc reallocFunc,
  111. xmlStrdupFunc strdupFunc);
  112. XMLPUBFUN int
  113. xmlGcMemGet (xmlFreeFunc *freeFunc,
  114. xmlMallocFunc *mallocFunc,
  115. xmlMallocFunc *mallocAtomicFunc,
  116. xmlReallocFunc *reallocFunc,
  117. xmlStrdupFunc *strdupFunc);
  118. /*
  119. * Initialization of the memory layer.
  120. */
  121. XML_DEPRECATED
  122. XMLPUBFUN int
  123. xmlInitMemory (void);
  124. /*
  125. * Cleanup of the memory layer.
  126. */
  127. XML_DEPRECATED
  128. XMLPUBFUN void
  129. xmlCleanupMemory (void);
  130. /*
  131. * These are specific to the XML debug memory wrapper.
  132. */
  133. XMLPUBFUN size_t
  134. xmlMemSize (void *ptr);
  135. XMLPUBFUN int
  136. xmlMemUsed (void);
  137. XMLPUBFUN int
  138. xmlMemBlocks (void);
  139. XML_DEPRECATED
  140. XMLPUBFUN void
  141. xmlMemDisplay (FILE *fp);
  142. XML_DEPRECATED
  143. XMLPUBFUN void
  144. xmlMemDisplayLast(FILE *fp, long nbBytes);
  145. XML_DEPRECATED
  146. XMLPUBFUN void
  147. xmlMemShow (FILE *fp, int nr);
  148. XML_DEPRECATED
  149. XMLPUBFUN void
  150. xmlMemoryDump (void);
  151. XMLPUBFUN void *
  152. xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);
  153. XMLPUBFUN void *
  154. xmlMemRealloc (void *ptr,size_t size);
  155. XMLPUBFUN void
  156. xmlMemFree (void *ptr);
  157. XMLPUBFUN char *
  158. xmlMemoryStrdup (const char *str);
  159. XML_DEPRECATED
  160. XMLPUBFUN void *
  161. xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
  162. XML_DEPRECATED
  163. XMLPUBFUN void *
  164. xmlReallocLoc (void *ptr, size_t size, const char *file, int line);
  165. XML_DEPRECATED
  166. XMLPUBFUN void *
  167. xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);
  168. XML_DEPRECATED
  169. XMLPUBFUN char *
  170. xmlMemStrdupLoc (const char *str, const char *file, int line);
  171. #ifdef __cplusplus
  172. }
  173. #endif /* __cplusplus */
  174. #endif /* __DEBUG_MEMORY_ALLOC__ */