xmlmemory.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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: pchar): pchar; 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 xmlMemShow(fp: PFILE; nr: cint); EXTDECL; external xml2lib;
  118. procedure xmlMemoryDump(); EXTDECL; external xml2lib;
  119. function xmlMemMalloc(size: csize_t): pointer; EXTDECL; external xml2lib;
  120. function xmlMemRealloc(ptr: pointer; size: csize_t): pointer; EXTDECL; external xml2lib;
  121. procedure xmlMemFree(ptr: pointer); EXTDECL; external xml2lib;
  122. function xmlMemoryStrdup(str: pchar): pchar; EXTDECL; external xml2lib;
  123. function xmlMallocLoc(size: csize_t; _file: pchar; line: cint): pointer; EXTDECL; external xml2lib;
  124. function xmlReallocLoc(ptr: pointer; size: csize_t; _file: pchar; line: cint): pointer; EXTDECL; external xml2lib;
  125. function xmlMallocAtomicLoc(size: csize_t; _file: pchar; line: cint): pointer; EXTDECL; external xml2lib;
  126. function xmlMemStrdupLoc(str: pchar; _file: pchar; line: cint): pchar; EXTDECL; external xml2lib;
  127. {$IFDEF DEBUG_MEMORY_LOCATION}
  128. (**
  129. * xmlMalloc:
  130. * @size: number of bytes to allocate
  131. *
  132. * Wrapper for the malloc() function used in the XML library.
  133. *
  134. * Returns the pointer to the allocated area or NULL in case of error.
  135. *)
  136. //#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
  137. (**
  138. * xmlMallocAtomic:
  139. * @size: number of bytes to allocate
  140. *
  141. * Wrapper for the malloc() function used in the XML library for allocation
  142. * of block not containing pointers to other areas.
  143. *
  144. * Returns the pointer to the allocated area or NULL in case of error.
  145. *)
  146. //#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
  147. (**
  148. * xmlRealloc:
  149. * @ptr: pointer to the existing allocated area
  150. * @size: number of bytes to allocate
  151. *
  152. * Wrapper for the realloc() function used in the XML library.
  153. *
  154. * Returns the pointer to the allocated area or NULL in case of error.
  155. *)
  156. //#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
  157. (**
  158. * xmlMemStrdup:
  159. * @str: pointer to the existing string
  160. *
  161. * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
  162. *
  163. * Returns the pointer to the allocated area or NULL in case of error.
  164. *)
  165. //#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
  166. {$ENDIF} (* DEBUG_MEMORY_LOCATION *)
  167. {$ENDIF}