2 * xmlmemory.c: libxml memory allocator wrapper.
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
32 /* #define DEBUG_MEMORY */
37 * keep track of all allocated blocks for error reporting
38 * Always build the memory list !
40 #ifdef DEBUG_MEMORY_LOCATION
42 #define MEM_LIST /* keep a list of all the allocated memory blocks */
46 #include <libxml/globals.h> /* must come before xmlmemory.h */
47 #include <libxml/xmlmemory.h>
48 #include <libxml/xmlerror.h>
49 #include <libxml/threads.h>
51 static int xmlMemInitialized
= 0;
52 static unsigned long debugMemSize
= 0;
53 static unsigned long debugMemBlocks
= 0;
54 static unsigned long debugMaxMemSize
= 0;
55 static xmlMutexPtr xmlMemMutex
= NULL
;
57 void xmlMallocBreakpoint(void);
59 /************************************************************************
61 * Macros, variables and associated types *
63 ************************************************************************/
65 #if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED)
78 * Each of the blocks allocated begin with a header containing informations
84 #define REALLOC_TYPE 2
86 #define MALLOC_ATOMIC_TYPE 4
87 #define REALLOC_ATOMIC_TYPE 5
89 typedef struct memnod
{
92 unsigned long mh_number
;
95 struct memnod
*mh_next
;
96 struct memnod
*mh_prev
;
104 #define ALIGN_SIZE 16
106 #define ALIGN_SIZE sizeof(double)
108 #define HDR_SIZE sizeof(MEMHDR)
109 #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \
110 / ALIGN_SIZE ) * ALIGN_SIZE)
112 #define MAX_SIZE_T ((size_t)-1)
114 #define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE))
115 #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE))
118 static unsigned int block
=0;
119 static unsigned int xmlMemStopAtBlock
= 0;
120 static void *xmlMemTraceBlockAt
= NULL
;
122 static MEMHDR
*memlist
= NULL
;
125 static void debugmem_tag_error(void *addr
);
127 static void debugmem_list_add(MEMHDR
*);
128 static void debugmem_list_delete(MEMHDR
*);
130 #define Mem_Tag_Err(a) debugmem_tag_error(a);
137 * xmlMallocBreakpoint:
139 * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block
140 * number reaches the specified value this function is called. One need to add a breakpoint
141 * to it to get the context in which the given block is allocated.
145 xmlMallocBreakpoint(void) {
146 xmlGenericError(xmlGenericErrorContext
,
147 "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock
);
152 * @size: an int specifying the size in byte to allocate.
153 * @file: the file name or NULL
154 * @line: the line number
156 * a malloc() equivalent, with logging of the allocation info.
158 * Returns a pointer to the allocated area or NULL in case of lack of memory.
162 xmlMallocLoc(size_t size
, const char * file
, int line
)
167 if (!xmlMemInitialized
) xmlInitMemory();
169 xmlGenericError(xmlGenericErrorContext
,
170 "Malloc(%d)\n",size
);
175 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
178 xmlGenericError(xmlGenericErrorContext
,
179 "xmlMallocLoc : Out of free space\n");
185 p
->mh_type
= MALLOC_TYPE
;
188 xmlMutexLock(xmlMemMutex
);
189 p
->mh_number
= ++block
;
190 debugMemSize
+= size
;
192 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
194 debugmem_list_add(p
);
196 xmlMutexUnlock(xmlMemMutex
);
199 xmlGenericError(xmlGenericErrorContext
,
200 "Malloc(%d) Ok\n",size
);
203 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
205 ret
= HDR_2_CLIENT(p
);
207 if (xmlMemTraceBlockAt
== ret
) {
208 xmlGenericError(xmlGenericErrorContext
,
209 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
210 (long unsigned)size
);
211 xmlMallocBreakpoint();
220 * xmlMallocAtomicLoc:
221 * @size: an unsigned int specifying the size in byte to allocate.
222 * @file: the file name or NULL
223 * @line: the line number
225 * a malloc() equivalent, with logging of the allocation info.
227 * Returns a pointer to the allocated area or NULL in case of lack of memory.
231 xmlMallocAtomicLoc(size_t size
, const char * file
, int line
)
236 if (!xmlMemInitialized
) xmlInitMemory();
238 xmlGenericError(xmlGenericErrorContext
,
239 "Malloc(%d)\n",size
);
244 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
245 xmlGenericError(xmlGenericErrorContext
,
246 "xmlMallocAtomicLoc : Unsigned overflow prevented\n");
251 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
254 xmlGenericError(xmlGenericErrorContext
,
255 "xmlMallocAtomicLoc : Out of free space\n");
261 p
->mh_type
= MALLOC_ATOMIC_TYPE
;
264 xmlMutexLock(xmlMemMutex
);
265 p
->mh_number
= ++block
;
266 debugMemSize
+= size
;
268 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
270 debugmem_list_add(p
);
272 xmlMutexUnlock(xmlMemMutex
);
275 xmlGenericError(xmlGenericErrorContext
,
276 "Malloc(%d) Ok\n",size
);
279 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
281 ret
= HDR_2_CLIENT(p
);
283 if (xmlMemTraceBlockAt
== ret
) {
284 xmlGenericError(xmlGenericErrorContext
,
285 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
286 (long unsigned)size
);
287 xmlMallocBreakpoint();
296 * @size: an int specifying the size in byte to allocate.
298 * a malloc() equivalent, with logging of the allocation info.
300 * Returns a pointer to the allocated area or NULL in case of lack of memory.
304 xmlMemMalloc(size_t size
)
306 return(xmlMallocLoc(size
, "none", 0));
311 * @ptr: the initial memory block pointer
312 * @size: an int specifying the size in byte to allocate.
313 * @file: the file name or NULL
314 * @line: the line number
316 * a realloc() equivalent, with logging of the allocation info.
318 * Returns a pointer to the allocated area or NULL in case of lack of memory.
322 xmlReallocLoc(void *ptr
,size_t size
, const char * file
, int line
)
325 unsigned long number
;
331 return(xmlMallocLoc(size
, file
, line
));
333 if (!xmlMemInitialized
) xmlInitMemory();
336 p
= CLIENT_2_HDR(ptr
);
337 number
= p
->mh_number
;
338 if (xmlMemStopAtBlock
== number
) xmlMallocBreakpoint();
339 if (p
->mh_tag
!= MEMTAG
) {
344 xmlMutexLock(xmlMemMutex
);
345 debugMemSize
-= p
->mh_size
;
348 oldsize
= p
->mh_size
;
351 debugmem_list_delete(p
);
353 xmlMutexUnlock(xmlMemMutex
);
355 tmp
= (MEMHDR
*) realloc(p
,RESERVE_SIZE
+size
);
361 if (xmlMemTraceBlockAt
== ptr
) {
362 xmlGenericError(xmlGenericErrorContext
,
363 "%p : Realloced(%lu -> %lu) Ok\n",
364 xmlMemTraceBlockAt
, (long unsigned)p
->mh_size
,
365 (long unsigned)size
);
366 xmlMallocBreakpoint();
369 p
->mh_number
= number
;
370 p
->mh_type
= REALLOC_TYPE
;
374 xmlMutexLock(xmlMemMutex
);
375 debugMemSize
+= size
;
377 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
379 debugmem_list_add(p
);
381 xmlMutexUnlock(xmlMemMutex
);
386 xmlGenericError(xmlGenericErrorContext
,
387 "Realloced(%d to %d) Ok\n", oldsize
, size
);
389 return(HDR_2_CLIENT(p
));
397 * @ptr: the initial memory block pointer
398 * @size: an int specifying the size in byte to allocate.
400 * a realloc() equivalent, with logging of the allocation info.
402 * Returns a pointer to the allocated area or NULL in case of lack of memory.
406 xmlMemRealloc(void *ptr
,size_t size
) {
407 return(xmlReallocLoc(ptr
, size
, "none", 0));
412 * @ptr: the memory block pointer
414 * a free() equivalent, with error checking.
417 xmlMemFree(void *ptr
)
428 if (ptr
== (void *) -1) {
429 xmlGenericError(xmlGenericErrorContext
,
430 "trying to free pointer from freed area\n");
434 if (xmlMemTraceBlockAt
== ptr
) {
435 xmlGenericError(xmlGenericErrorContext
,
436 "%p : Freed()\n", xmlMemTraceBlockAt
);
437 xmlMallocBreakpoint();
442 target
= (char *) ptr
;
444 p
= CLIENT_2_HDR(ptr
);
445 if (p
->mh_tag
!= MEMTAG
) {
449 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
451 memset(target
, -1, p
->mh_size
);
452 xmlMutexLock(xmlMemMutex
);
453 debugMemSize
-= p
->mh_size
;
459 debugmem_list_delete(p
);
461 xmlMutexUnlock(xmlMemMutex
);
468 xmlGenericError(xmlGenericErrorContext
,
469 "Freed(%d) Ok\n", size
);
475 xmlGenericError(xmlGenericErrorContext
,
476 "xmlMemFree(%lX) error\n", (unsigned long) ptr
);
477 xmlMallocBreakpoint();
483 * @str: the initial string pointer
484 * @file: the file name or NULL
485 * @line: the line number
487 * a strdup() equivalent, with logging of the allocation info.
489 * Returns a pointer to the new string or NULL if allocation error occurred.
493 xmlMemStrdupLoc(const char *str
, const char *file
, int line
)
496 size_t size
= strlen(str
) + 1;
499 if (!xmlMemInitialized
) xmlInitMemory();
502 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
508 p
->mh_type
= STRDUP_TYPE
;
511 xmlMutexLock(xmlMemMutex
);
512 p
->mh_number
= ++block
;
513 debugMemSize
+= size
;
515 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
517 debugmem_list_add(p
);
519 xmlMutexUnlock(xmlMemMutex
);
521 s
= (char *) HDR_2_CLIENT(p
);
523 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
529 if (xmlMemTraceBlockAt
== s
) {
530 xmlGenericError(xmlGenericErrorContext
,
531 "%p : Strdup() Ok\n", xmlMemTraceBlockAt
);
532 xmlMallocBreakpoint();
543 * @str: the initial string pointer
545 * a strdup() equivalent, with logging of the allocation info.
547 * Returns a pointer to the new string or NULL if allocation error occurred.
551 xmlMemoryStrdup(const char *str
) {
552 return(xmlMemStrdupLoc(str
, "none", 0));
558 * Provides the amount of memory currently allocated
560 * Returns an int representing the amount of memory allocated.
567 xmlMutexLock(xmlMemMutex
);
569 xmlMutexUnlock(xmlMemMutex
);
576 * Provides the number of memory areas currently allocated
578 * Returns an int representing the number of blocks
585 xmlMutexLock(xmlMemMutex
);
586 res
= debugMemBlocks
;
587 xmlMutexUnlock(xmlMemMutex
);
594 * @fp: a FILE descriptor used as the output file
595 * @p: a memory block header
597 * tries to show some content from the memory block
601 xmlMemContentShow(FILE *fp
, MEMHDR
*p
)
607 fprintf(fp
, " NULL");
611 buf
= (const char *) HDR_2_CLIENT(p
);
613 for (i
= 0;i
< len
;i
++) {
614 if (buf
[i
] == 0) break;
615 if (!isprint((unsigned char) buf
[i
])) break;
617 if ((i
< 4) && ((buf
[i
] != 0) || (i
== 0))) {
622 for (j
= 0;(j
< len
-3) && (j
< 40);j
+= 4) {
623 cur
= *((void **) &buf
[j
]);
624 q
= CLIENT_2_HDR(cur
);
630 if (k
++ > 100) break;
632 if ((p
!= NULL
) && (p
== q
)) {
633 fprintf(fp
, " pointer to #%lu at index %d",
639 } else if ((i
== 0) && (buf
[i
] == 0)) {
642 if (buf
[i
] == 0) fprintf(fp
," \"%.25s\"", buf
);
645 for (j
= 0;j
< i
;j
++)
646 fprintf(fp
,"%c", buf
[j
]);
655 * @fp: a FILE descriptor used as the output file, if NULL, the result is
656 * written to the file .memorylist
657 * @nbBytes: the amount of memory to dump
659 * the last nbBytes of memory allocated and not freed, useful for dumping
660 * the memory left allocated between two places at runtime.
664 xmlMemDisplayLast(FILE *fp
, long nbBytes
)
677 fp
= fopen(".memorylist", "w");
683 fprintf(fp
," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
684 nbBytes
, debugMemSize
, debugMaxMemSize
);
685 fprintf(fp
,"BLOCK NUMBER SIZE TYPE\n");
687 xmlMutexLock(xmlMemMutex
);
689 while ((p
) && (nbBytes
> 0)) {
690 fprintf(fp
,"%-5u %6lu %6lu ",idx
++,p
->mh_number
,
691 (unsigned long)p
->mh_size
);
692 switch (p
->mh_type
) {
693 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
694 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
695 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
696 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
697 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
699 fprintf(fp
,"Unknown memory block, may be corrupted");
700 xmlMutexUnlock(xmlMemMutex
);
705 if (p
->mh_file
!= NULL
) fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
706 if (p
->mh_tag
!= MEMTAG
)
707 fprintf(fp
," INVALID");
710 xmlMemContentShow(fp
, p
);
715 nbBytes
-= (unsigned long)p
->mh_size
;
718 xmlMutexUnlock(xmlMemMutex
);
720 fprintf(fp
,"Memory list not compiled (MEM_LIST not defined !)\n");
728 * @fp: a FILE descriptor used as the output file, if NULL, the result is
729 * written to the file .memorylist
731 * show in-extenso the memory blocks allocated
735 xmlMemDisplay(FILE *fp
)
741 #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
750 fp
= fopen(".memorylist", "w");
756 #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
757 currentTime
= time(NULL
);
758 tstruct
= localtime(¤tTime
);
759 strftime(buf
, sizeof(buf
) - 1, "%I:%M:%S %p", tstruct
);
760 fprintf(fp
," %s\n\n", buf
);
764 fprintf(fp
," MEMORY ALLOCATED : %lu, MAX was %lu\n",
765 debugMemSize
, debugMaxMemSize
);
766 fprintf(fp
,"BLOCK NUMBER SIZE TYPE\n");
768 xmlMutexLock(xmlMemMutex
);
771 fprintf(fp
,"%-5u %6lu %6lu ",idx
++,p
->mh_number
,
772 (unsigned long)p
->mh_size
);
773 switch (p
->mh_type
) {
774 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
775 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
776 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
777 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
778 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
780 fprintf(fp
,"Unknown memory block, may be corrupted");
781 xmlMutexUnlock(xmlMemMutex
);
786 if (p
->mh_file
!= NULL
) fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
787 if (p
->mh_tag
!= MEMTAG
)
788 fprintf(fp
," INVALID");
791 xmlMemContentShow(fp
, p
);
798 xmlMutexUnlock(xmlMemMutex
);
800 fprintf(fp
,"Memory list not compiled (MEM_LIST not defined !)\n");
808 static void debugmem_list_add(MEMHDR
*p
)
810 p
->mh_next
= memlist
;
812 if (memlist
) memlist
->mh_prev
= p
;
814 #ifdef MEM_LIST_DEBUG
820 static void debugmem_list_delete(MEMHDR
*p
)
823 p
->mh_next
->mh_prev
= p
->mh_prev
;
825 p
->mh_prev
->mh_next
= p
->mh_next
;
826 else memlist
= p
->mh_next
;
827 #ifdef MEM_LIST_DEBUG
836 * debugmem_tag_error:
838 * internal error function.
841 static void debugmem_tag_error(void *p
)
843 xmlGenericError(xmlGenericErrorContext
,
844 "Memory tag error occurs :%p \n\t bye\n", p
);
847 xmlMemDisplay(stderr
);
852 static FILE *xmlMemoryDumpFile
= NULL
;
857 * @fp: a FILE descriptor used as the output file
858 * @nr: number of entries to dump
860 * show a show display of the memory allocated, and dump
861 * the @nr last allocated areas which were not freed
865 xmlMemShow(FILE *fp
, int nr ATTRIBUTE_UNUSED
)
872 fprintf(fp
," MEMORY ALLOCATED : %lu, MAX was %lu\n",
873 debugMemSize
, debugMaxMemSize
);
875 xmlMutexLock(xmlMemMutex
);
877 fprintf(fp
,"NUMBER SIZE TYPE WHERE\n");
879 while ((p
) && nr
> 0) {
880 fprintf(fp
,"%6lu %6lu ",p
->mh_number
,(unsigned long)p
->mh_size
);
881 switch (p
->mh_type
) {
882 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
883 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
884 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
885 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
886 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
887 default:fprintf(fp
," ??? in ");break;
889 if (p
->mh_file
!= NULL
)
890 fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
891 if (p
->mh_tag
!= MEMTAG
)
892 fprintf(fp
," INVALID");
893 xmlMemContentShow(fp
, p
);
899 xmlMutexUnlock(xmlMemMutex
);
900 #endif /* MEM_LIST */
906 * Dump in-extenso the memory blocks allocated to the file .memorylist
915 if (debugMaxMemSize
== 0)
917 dump
= fopen(".memdump", "w");
919 xmlMemoryDumpFile
= stderr
;
920 else xmlMemoryDumpFile
= dump
;
922 xmlMemDisplay(xmlMemoryDumpFile
);
924 if (dump
!= NULL
) fclose(dump
);
925 #endif /* MEM_LIST */
929 /****************************************************************
931 * Initialization Routines *
933 ****************************************************************/
938 * Initialize the memory layer.
940 * Returns 0 on success
949 xmlGenericError(xmlGenericErrorContext
,
950 "xmlInitMemory()\n");
953 This is really not good code (see Bug 130419). Suggestions for
954 improvement will be welcome!
956 if (xmlMemInitialized
) return(-1);
957 xmlMemInitialized
= 1;
958 xmlMemMutex
= xmlNewMutex();
961 breakpoint
= getenv("XML_MEM_BREAKPOINT");
962 if (breakpoint
!= NULL
) {
963 sscanf(breakpoint
, "%ud", &xmlMemStopAtBlock
);
967 breakpoint
= getenv("XML_MEM_TRACE");
968 if (breakpoint
!= NULL
) {
969 sscanf(breakpoint
, "%p", &xmlMemTraceBlockAt
);
974 xmlGenericError(xmlGenericErrorContext
,
975 "xmlInitMemory() Ok\n");
983 * Free up all the memory allocated by the library for its own
984 * use. This should not be called by user level code.
987 xmlCleanupMemory(void) {
989 xmlGenericError(xmlGenericErrorContext
,
990 "xmlCleanupMemory()\n");
992 if (xmlMemInitialized
== 0)
995 xmlFreeMutex(xmlMemMutex
);
997 xmlMemInitialized
= 0;
999 xmlGenericError(xmlGenericErrorContext
,
1000 "xmlCleanupMemory() Ok\n");
1006 * @freeFunc: the free() function to use
1007 * @mallocFunc: the malloc() function to use
1008 * @reallocFunc: the realloc() function to use
1009 * @strdupFunc: the strdup() function to use
1011 * Override the default memory access functions with a new set
1012 * This has to be called before any other libxml routines !
1014 * Should this be blocked if there was already some allocations
1017 * Returns 0 on success
1020 xmlMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1021 xmlReallocFunc reallocFunc
, xmlStrdupFunc strdupFunc
) {
1023 xmlGenericError(xmlGenericErrorContext
,
1026 if (freeFunc
== NULL
)
1028 if (mallocFunc
== NULL
)
1030 if (reallocFunc
== NULL
)
1032 if (strdupFunc
== NULL
)
1035 xmlMalloc
= mallocFunc
;
1036 xmlMallocAtomic
= mallocFunc
;
1037 xmlRealloc
= reallocFunc
;
1038 xmlMemStrdup
= strdupFunc
;
1040 xmlGenericError(xmlGenericErrorContext
,
1041 "xmlMemSetup() Ok\n");
1048 * @freeFunc: place to save the free() function in use
1049 * @mallocFunc: place to save the malloc() function in use
1050 * @reallocFunc: place to save the realloc() function in use
1051 * @strdupFunc: place to save the strdup() function in use
1053 * Provides the memory access functions set currently in use
1055 * Returns 0 on success
1058 xmlMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1059 xmlReallocFunc
*reallocFunc
, xmlStrdupFunc
*strdupFunc
) {
1060 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1061 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1062 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1063 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;
1069 * @freeFunc: the free() function to use
1070 * @mallocFunc: the malloc() function to use
1071 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1072 * @reallocFunc: the realloc() function to use
1073 * @strdupFunc: the strdup() function to use
1075 * Override the default memory access functions with a new set
1076 * This has to be called before any other libxml routines !
1077 * The mallocAtomicFunc is specialized for atomic block
1078 * allocations (i.e. of areas useful for garbage collected memory allocators
1080 * Should this be blocked if there was already some allocations
1083 * Returns 0 on success
1086 xmlGcMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1087 xmlMallocFunc mallocAtomicFunc
, xmlReallocFunc reallocFunc
,
1088 xmlStrdupFunc strdupFunc
) {
1090 xmlGenericError(xmlGenericErrorContext
,
1091 "xmlGcMemSetup()\n");
1093 if (freeFunc
== NULL
)
1095 if (mallocFunc
== NULL
)
1097 if (mallocAtomicFunc
== NULL
)
1099 if (reallocFunc
== NULL
)
1101 if (strdupFunc
== NULL
)
1104 xmlMalloc
= mallocFunc
;
1105 xmlMallocAtomic
= mallocAtomicFunc
;
1106 xmlRealloc
= reallocFunc
;
1107 xmlMemStrdup
= strdupFunc
;
1109 xmlGenericError(xmlGenericErrorContext
,
1110 "xmlGcMemSetup() Ok\n");
1117 * @freeFunc: place to save the free() function in use
1118 * @mallocFunc: place to save the malloc() function in use
1119 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1120 * @reallocFunc: place to save the realloc() function in use
1121 * @strdupFunc: place to save the strdup() function in use
1123 * Provides the memory access functions set currently in use
1124 * The mallocAtomicFunc is specialized for atomic block
1125 * allocations (i.e. of areas useful for garbage collected memory allocators
1127 * Returns 0 on success
1130 xmlGcMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1131 xmlMallocFunc
*mallocAtomicFunc
, xmlReallocFunc
*reallocFunc
,
1132 xmlStrdupFunc
*strdupFunc
) {
1133 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1134 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1135 if (mallocAtomicFunc
!= NULL
) *mallocAtomicFunc
= xmlMallocAtomic
;
1136 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1137 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;
1141 #define bottom_xmlmemory
1142 #include "elfgcchack.h"