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