xmlmemory.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. (**
  11. * DEBUG_MEMORY:
  12. *
  13. * DEBUG_MEMORY replaces the allocator with a collect and debug
  14. * shell to the libc allocator.
  15. * DEBUG_MEMORY should only be activated when debugging
  16. * libxml i.e. if libxml has been configured with --with-debug-mem too.
  17. *)
  18. {.$DEFINE DEBUG_MEMORY_FREED}
  19. {.$DEFINE DEBUG_MEMORY_LOCATION}
  20. {$IFDEF DEBUG}
  21. {$IFNDEF DEBUG_MEMORY}
  22. {$DEFINE DEBUG_MEMORY}
  23. {$ENDIF}
  24. {$ENDIF}
  25. (**
  26. * DEBUG_MEMORY_LOCATION:
  27. *
  28. * DEBUG_MEMORY_LOCATION should be activated only when debugging
  29. * libxml i.e. if libxml has been configured with --with-debug-mem too.
  30. *)
  31. {$IFDEF DEBUG_MEMORY_LOCATION}
  32. {$ENDIF}
  33. {$IFDEF TYPE}
  34. (*
  35. * The XML memory wrapper support 4 basic overloadable functions.
  36. *)
  37. (**
  38. * xmlFreeFunc:
  39. * @mem: an already allocated block of memory
  40. *
  41. * Signature for a free() implementation.
  42. *)
  43. xmlFreeFunc = procedure(mem: pointer); EXTDECL;
  44. {$IFDEF NO_EXTERNAL_VARS}
  45. PxmlFreeFunc = ^xmlFreeFunc;
  46. {$ENDIF}
  47. (**
  48. * xmlMallocFunc:
  49. * @size: the size requested in bytes
  50. *
  51. * Signature for a malloc() implementation.
  52. *
  53. * Returns a pointer to the newly allocated block or NULL in case of error.
  54. *)
  55. xmlMallocFunc = function(size: csize_t): pointer; EXTDECL;
  56. {$IFDEF NO_EXTERNAL_VARS}
  57. PxmlMallocFunc = ^xmlMallocFunc;
  58. {$ENDIF}
  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. xmlReallocFunc = function(mem: pointer; size: csize_t): pointer; EXTDECL;
  69. {$IFDEF NO_EXTERNAL_VARS}
  70. PxmlReallocFunc = ^xmlReallocFunc;
  71. {$ENDIF}
  72. (**
  73. * xmlStrdupFunc:
  74. * @str: a zero terminated string
  75. *
  76. * Signature for an strdup() implementation.
  77. *
  78. * Returns the copy of the string or NULL in case of error.
  79. *)
  80. xmlStrdupFunc = function(str: PAnsiChar): PAnsiChar; EXTDECL;
  81. {$IFDEF NO_EXTERNAL_VARS}
  82. PxmlStrdupFunc = ^xmlStrdupFunc;
  83. {$ENDIF}
  84. (*
  85. * The 4 interfaces used for all memory handling within libxml.
  86. LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
  87. LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
  88. LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic;
  89. LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
  90. LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
  91. *)
  92. {$ENDIF}
  93. {$IFDEF FUNCTION}
  94. (*
  95. * The way to overload the existing functions.
  96. * The xmlGc function have an extra entry for atomic block
  97. * allocations useful for garbage collected memory allocators
  98. *)
  99. function xmlMemSetup(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; EXTDECL; external xml2lib;
  100. function xmlMemGet(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; EXTDECL; external xml2lib;
  101. function xmlGcMemSetup(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; mallocAtomicFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; EXTDECL; external xml2lib;
  102. function xmlGcMemGet(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var mallocAtomicFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; EXTDECL; external xml2lib;
  103. (*
  104. * Initialization of the memory layer.
  105. *)
  106. function xmlInitMemory(): cint; EXTDECL; external xml2lib;
  107. (*
  108. * Cleanup of the memory layer.
  109. *)
  110. procedure xmlCleanupMemory(); EXTDECL; external xml2lib;
  111. (*
  112. * These are specific to the XML debug memory wrapper.
  113. *)
  114. function xmlMemUsed(): cint; EXTDECL; external xml2lib;
  115. function xmlMemBlocks(): cint; EXTDECL; external xml2lib;
  116. procedure xmlMemDisplay(fp: PFILE); EXTDECL; external xml2lib;
  117. procedure xmlMemDisplayLast(fp: PFILE; nbBytes: clong); EXTDECL; external xml2lib;
  118. procedure xmlMemShow(fp: PFILE; nr: cint); EXTDECL; external xml2lib;
  119. procedure xmlMemoryDump(); EXTDECL; external xml2lib;
  120. function xmlMemMalloc(size: csize_t): pointer; EXTDECL; external xml2lib;
  121. function xmlMemRealloc(ptr: pointer; size: csize_t): pointer; EXTDECL; external xml2lib;
  122. procedure xmlMemFree(ptr: pointer); EXTDECL; external xml2lib;
  123. function xmlMemoryStrdup(str: PAnsiChar): PAnsiChar; EXTDECL; external xml2lib;
  124. function xmlMallocLoc(size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL; external xml2lib;
  125. function xmlReallocLoc(ptr: pointer; size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL; external xml2lib;
  126. function xmlMallocAtomicLoc(size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL; external xml2lib;
  127. function xmlMemStrdupLoc(str: PAnsiChar; _file: PAnsiChar; line: cint): PAnsiChar; EXTDECL; external xml2lib;
  128. {$IFDEF DEBUG_MEMORY_LOCATION}
  129. (**
  130. * xmlMalloc:
  131. * @size: number of bytes to allocate
  132. *
  133. * Wrapper for the malloc() function used in the XML library.
  134. *
  135. * Returns the pointer to the allocated area or NULL in case of error.
  136. *)
  137. //#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
  138. (**
  139. * xmlMallocAtomic:
  140. * @size: number of bytes to allocate
  141. *
  142. * Wrapper for the malloc() function used in the XML library for allocation
  143. * of block not containing pointers to other areas.
  144. *
  145. * Returns the pointer to the allocated area or NULL in case of error.
  146. *)
  147. //#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
  148. (**
  149. * xmlRealloc:
  150. * @ptr: pointer to the existing allocated area
  151. * @size: number of bytes to allocate
  152. *
  153. * Wrapper for the realloc() function used in the XML library.
  154. *
  155. * Returns the pointer to the allocated area or NULL in case of error.
  156. *)
  157. //#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
  158. (**
  159. * xmlMemStrdup:
  160. * @str: pointer to the existing string
  161. *
  162. * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
  163. *
  164. * Returns the pointer to the allocated area or NULL in case of error.
  165. *)
  166. //#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
  167. {$ENDIF} (* DEBUG_MEMORY_LOCATION *)
  168. {$ENDIF}
  169. {$IFDEF FUNCTIONVAR}
  170. (*
  171. * The way to overload the existing functions.
  172. * The xmlGc function have an extra entry for atomic block
  173. * allocations useful for garbage collected memory allocators
  174. *)
  175. xmlMemSetup: function(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; EXTDECL;
  176. xmlMemGet: function(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; EXTDECL;
  177. xmlGcMemSetup: function(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; mallocAtomicFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; EXTDECL;
  178. xmlGcMemGet: function(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var mallocAtomicFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; EXTDECL;
  179. (*
  180. * Initialization of the memory layer.
  181. *)
  182. xmlInitMemory: function(): cint; EXTDECL;
  183. (*
  184. * Cleanup of the memory layer.
  185. *)
  186. xmlCleanupMemory: procedure(); EXTDECL;
  187. (*
  188. * These are specific to the XML debug memory wrapper.
  189. *)
  190. xmlMemUsed: function(): cint; EXTDECL;
  191. xmlMemBlocks: function(): cint; EXTDECL;
  192. xmlMemDisplay: procedure(fp: PFILE); EXTDECL;
  193. xmlMemDisplayLast: procedure(fp: PFILE; nbBytes: clong); EXTDECL;
  194. xmlMemShow: procedure(fp: PFILE; nr: cint); EXTDECL;
  195. xmlMemoryDump: procedure(); EXTDECL;
  196. xmlMemMalloc: function(size: csize_t): pointer; EXTDECL;
  197. xmlMemRealloc: function(ptr: pointer; size: csize_t): pointer; EXTDECL;
  198. xmlMemFree: procedure(ptr: pointer); EXTDECL;
  199. xmlMemoryStrdup: function(str: PAnsiChar): PAnsiChar; EXTDECL;
  200. xmlMallocLoc: function(size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL;
  201. xmlReallocLoc: function(ptr: pointer; size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL;
  202. xmlMallocAtomicLoc: function(size: csize_t; _file: PAnsiChar; line: cint): pointer; EXTDECL;
  203. xmlMemStrdupLoc: function(str: PAnsiChar; _file: PAnsiChar; line: cint): PAnsiChar; EXTDECL;
  204. {$ENDIF}