xmlmemory.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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); cdecl;
  44. (**
  45. * xmlMallocFunc:
  46. * @size: the size requested in bytes
  47. *
  48. * Signature for a malloc() implementation.
  49. *
  50. * Returns a pointer to the newly allocated block or NULL in case of error.
  51. *)
  52. xmlMallocFunc = function(size: size_t): pointer; cdecl;
  53. (**
  54. * xmlReallocFunc:
  55. * @mem: an already allocated block of memory
  56. * @size: the new size requested in bytes
  57. *
  58. * Signature for a realloc() implementation.
  59. *
  60. * Returns a pointer to the newly reallocated block or NULL in case of error.
  61. *)
  62. xmlReallocFunc = function(mem: pointer; size: size_t): pointer; cdecl;
  63. (**
  64. * xmlStrdupFunc:
  65. * @str: a zero terminated string
  66. *
  67. * Signature for an strdup() implementation.
  68. *
  69. * Returns the copy of the string or NULL in case of error.
  70. *)
  71. xmlStrdupFunc = function(str: pchar): pchar; cdecl;
  72. (*
  73. * The 4 interfaces used for all memory handling within libxml.
  74. LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
  75. LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
  76. LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMallocAtomic;
  77. LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
  78. LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
  79. *)
  80. {$ENDIF}
  81. {$IFDEF FUNCTION}
  82. (*
  83. * The way to overload the existing functions.
  84. * The xmlGc function have an extra entry for atomic block
  85. * allocations useful for garbage collected memory allocators
  86. *)
  87. function xmlMemSetup(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; XMLCALL; XMLPUBFUN;
  88. function xmlMemGet(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; XMLCALL; XMLPUBFUN;
  89. function xmlGcMemSetup(freeFunc: xmlFreeFunc; mallocFunc: xmlMallocFunc; mallocAtomicFunc: xmlMallocFunc; reallocFunc: xmlReallocFunc; strdupFunc: xmlStrdupFunc): cint; XMLCALL; XMLPUBFUN;
  90. function xmlGcMemGet(var freeFunc: xmlFreeFunc; var mallocFunc: xmlMallocFunc; var mallocAtomicFunc: xmlMallocFunc; var reallocFunc: xmlReallocFunc; var strdupFunc: xmlStrdupFunc): cint; XMLCALL; XMLPUBFUN;
  91. (*
  92. * Initialization of the memory layer.
  93. *)
  94. function xmlInitMemory(): cint; XMLCALL; XMLPUBFUN;
  95. (*
  96. * Cleanup of the memory layer.
  97. *)
  98. procedure xmlCleanupMemory(); XMLCALL; XMLPUBFUN;
  99. (*
  100. * These are specific to the XML debug memory wrapper.
  101. *)
  102. function xmlMemUsed(): cint; XMLCALL; XMLPUBFUN;
  103. function xmlMemBlocks(): cint; XMLCALL; XMLPUBFUN;
  104. {XMLPUBFUN void XMLCALL xmlMemDisplay (FILE *fp);
  105. XMLPUBFUN void XMLCALL xmlMemShow (FILE *fp, int nr);}
  106. procedure xmlMemoryDump(); XMLCALL; XMLPUBFUN;
  107. function xmlMemMalloc(size: size_t): pointer; XMLCALL; XMLPUBFUN;
  108. function xmlMemRealloc(ptr: pointer; size: size_t): pointer; XMLCALL; XMLPUBFUN;
  109. procedure xmlMemFree(ptr: pointer); XMLCALL; XMLPUBFUN;
  110. function xmlMemoryStrdup(str: pchar): pchar; XMLCALL; XMLPUBFUN;
  111. function xmlMallocLoc(size: size_t; _file: pchar; line: cint): pointer; XMLCALL; XMLPUBFUN;
  112. function xmlReallocLoc(ptr: pointer; size: size_t; _file: pchar; line: cint): pointer; XMLCALL; XMLPUBFUN;
  113. function xmlMallocAtomicLoc(size: size_t; _file: pchar; line: cint): pointer; XMLCALL; XMLPUBFUN;
  114. function xmlMemStrdupLoc(str: pchar; _file: pchar; line: cint): pchar; XMLCALL; XMLPUBFUN;
  115. {$IFDEF DEBUG_MEMORY_LOCATION}
  116. (**
  117. * xmlMalloc:
  118. * @size: number of bytes to allocate
  119. *
  120. * Wrapper for the malloc() function used in the XML library.
  121. *
  122. * Returns the pointer to the allocated area or NULL in case of error.
  123. *)
  124. //#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)
  125. (**
  126. * xmlMallocAtomic:
  127. * @size: number of bytes to allocate
  128. *
  129. * Wrapper for the malloc() function used in the XML library for allocation
  130. * of block not containing pointers to other areas.
  131. *
  132. * Returns the pointer to the allocated area or NULL in case of error.
  133. *)
  134. //#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)
  135. (**
  136. * xmlRealloc:
  137. * @ptr: pointer to the existing allocated area
  138. * @size: number of bytes to allocate
  139. *
  140. * Wrapper for the realloc() function used in the XML library.
  141. *
  142. * Returns the pointer to the allocated area or NULL in case of error.
  143. *)
  144. //#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)
  145. (**
  146. * xmlMemStrdup:
  147. * @str: pointer to the existing string
  148. *
  149. * Wrapper for the strdup() function, xmlStrdup() is usually preferred.
  150. *
  151. * Returns the pointer to the allocated area or NULL in case of error.
  152. *)
  153. //#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)
  154. {$ENDIF} (* DEBUG_MEMORY_LOCATION *)
  155. {$ENDIF}