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 information
81 #define MEMTAG 0x5aa5U
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) ((void *) (((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 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
176 xmlGenericError(xmlGenericErrorContext
,
177 "xmlMallocLoc : Unsigned overflow\n");
182 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
185 xmlGenericError(xmlGenericErrorContext
,
186 "xmlMallocLoc : Out of free space\n");
192 p
->mh_type
= MALLOC_TYPE
;
195 xmlMutexLock(xmlMemMutex
);
196 p
->mh_number
= ++block
;
197 debugMemSize
+= size
;
199 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
201 debugmem_list_add(p
);
203 xmlMutexUnlock(xmlMemMutex
);
206 xmlGenericError(xmlGenericErrorContext
,
207 "Malloc(%d) Ok\n",size
);
210 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
212 ret
= HDR_2_CLIENT(p
);
214 if (xmlMemTraceBlockAt
== ret
) {
215 xmlGenericError(xmlGenericErrorContext
,
216 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
217 (long unsigned)size
);
218 xmlMallocBreakpoint();
227 * xmlMallocAtomicLoc:
228 * @size: an unsigned int specifying the size in byte to allocate.
229 * @file: the file name or NULL
230 * @line: the line number
232 * a malloc() equivalent, with logging of the allocation info.
234 * Returns a pointer to the allocated area or NULL in case of lack of memory.
238 xmlMallocAtomicLoc(size_t size
, const char * file
, int line
)
243 if (!xmlMemInitialized
) xmlInitMemory();
245 xmlGenericError(xmlGenericErrorContext
,
246 "Malloc(%d)\n",size
);
251 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
252 xmlGenericError(xmlGenericErrorContext
,
253 "xmlMallocAtomicLoc : Unsigned overflow\n");
258 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
261 xmlGenericError(xmlGenericErrorContext
,
262 "xmlMallocAtomicLoc : Out of free space\n");
268 p
->mh_type
= MALLOC_ATOMIC_TYPE
;
271 xmlMutexLock(xmlMemMutex
);
272 p
->mh_number
= ++block
;
273 debugMemSize
+= size
;
275 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
277 debugmem_list_add(p
);
279 xmlMutexUnlock(xmlMemMutex
);
282 xmlGenericError(xmlGenericErrorContext
,
283 "Malloc(%d) Ok\n",size
);
286 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
288 ret
= HDR_2_CLIENT(p
);
290 if (xmlMemTraceBlockAt
== ret
) {
291 xmlGenericError(xmlGenericErrorContext
,
292 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
293 (long unsigned)size
);
294 xmlMallocBreakpoint();
303 * @size: an int specifying the size in byte to allocate.
305 * a malloc() equivalent, with logging of the allocation info.
307 * Returns a pointer to the allocated area or NULL in case of lack of memory.
311 xmlMemMalloc(size_t size
)
313 return(xmlMallocLoc(size
, "none", 0));
318 * @ptr: the initial memory block pointer
319 * @size: an int specifying the size in byte to allocate.
320 * @file: the file name or NULL
321 * @line: the line number
323 * a realloc() equivalent, with logging of the allocation info.
325 * Returns a pointer to the allocated area or NULL in case of lack of memory.
329 xmlReallocLoc(void *ptr
,size_t size
, const char * file
, int line
)
332 unsigned long number
;
338 return(xmlMallocLoc(size
, file
, line
));
340 if (!xmlMemInitialized
) xmlInitMemory();
343 p
= CLIENT_2_HDR(ptr
);
344 number
= p
->mh_number
;
345 if (xmlMemStopAtBlock
== number
) xmlMallocBreakpoint();
346 if (p
->mh_tag
!= MEMTAG
) {
351 xmlMutexLock(xmlMemMutex
);
352 debugMemSize
-= p
->mh_size
;
355 oldsize
= p
->mh_size
;
358 debugmem_list_delete(p
);
360 xmlMutexUnlock(xmlMemMutex
);
362 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
363 xmlGenericError(xmlGenericErrorContext
,
364 "xmlReallocLoc : Unsigned overflow\n");
369 tmp
= (MEMHDR
*) realloc(p
,RESERVE_SIZE
+size
);
375 if (xmlMemTraceBlockAt
== ptr
) {
376 xmlGenericError(xmlGenericErrorContext
,
377 "%p : Realloced(%lu -> %lu) Ok\n",
378 xmlMemTraceBlockAt
, (long unsigned)p
->mh_size
,
379 (long unsigned)size
);
380 xmlMallocBreakpoint();
383 p
->mh_number
= number
;
384 p
->mh_type
= REALLOC_TYPE
;
388 xmlMutexLock(xmlMemMutex
);
389 debugMemSize
+= size
;
391 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
393 debugmem_list_add(p
);
395 xmlMutexUnlock(xmlMemMutex
);
400 xmlGenericError(xmlGenericErrorContext
,
401 "Realloced(%d to %d) Ok\n", oldsize
, size
);
403 return(HDR_2_CLIENT(p
));
411 * @ptr: the initial memory block pointer
412 * @size: an int specifying the size in byte to allocate.
414 * a realloc() equivalent, with logging of the allocation info.
416 * Returns a pointer to the allocated area or NULL in case of lack of memory.
420 xmlMemRealloc(void *ptr
,size_t size
) {
421 return(xmlReallocLoc(ptr
, size
, "none", 0));
426 * @ptr: the memory block pointer
428 * a free() equivalent, with error checking.
431 xmlMemFree(void *ptr
)
442 if (ptr
== (void *) -1) {
443 xmlGenericError(xmlGenericErrorContext
,
444 "trying to free pointer from freed area\n");
448 if (xmlMemTraceBlockAt
== ptr
) {
449 xmlGenericError(xmlGenericErrorContext
,
450 "%p : Freed()\n", xmlMemTraceBlockAt
);
451 xmlMallocBreakpoint();
456 target
= (char *) ptr
;
458 p
= CLIENT_2_HDR(ptr
);
459 if (p
->mh_tag
!= MEMTAG
) {
463 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
465 memset(target
, -1, p
->mh_size
);
466 xmlMutexLock(xmlMemMutex
);
467 debugMemSize
-= p
->mh_size
;
473 debugmem_list_delete(p
);
475 xmlMutexUnlock(xmlMemMutex
);
482 xmlGenericError(xmlGenericErrorContext
,
483 "Freed(%d) Ok\n", size
);
489 xmlGenericError(xmlGenericErrorContext
,
490 "xmlMemFree(%p) error\n", ptr
);
491 xmlMallocBreakpoint();
497 * @str: the initial string pointer
498 * @file: the file name or NULL
499 * @line: the line number
501 * a strdup() equivalent, with logging of the allocation info.
503 * Returns a pointer to the new string or NULL if allocation error occurred.
507 xmlMemStrdupLoc(const char *str
, const char *file
, int line
)
510 size_t size
= strlen(str
) + 1;
513 if (!xmlMemInitialized
) xmlInitMemory();
516 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
517 xmlGenericError(xmlGenericErrorContext
,
518 "xmlMemStrdupLoc : Unsigned overflow\n");
523 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
529 p
->mh_type
= STRDUP_TYPE
;
532 xmlMutexLock(xmlMemMutex
);
533 p
->mh_number
= ++block
;
534 debugMemSize
+= size
;
536 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
538 debugmem_list_add(p
);
540 xmlMutexUnlock(xmlMemMutex
);
542 s
= (char *) HDR_2_CLIENT(p
);
544 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
550 if (xmlMemTraceBlockAt
== s
) {
551 xmlGenericError(xmlGenericErrorContext
,
552 "%p : Strdup() Ok\n", xmlMemTraceBlockAt
);
553 xmlMallocBreakpoint();
564 * @str: the initial string pointer
566 * a strdup() equivalent, with logging of the allocation info.
568 * Returns a pointer to the new string or NULL if allocation error occurred.
572 xmlMemoryStrdup(const char *str
) {
573 return(xmlMemStrdupLoc(str
, "none", 0));
579 * Provides the amount of memory currently allocated
581 * Returns an int representing the amount of memory allocated.
588 xmlMutexLock(xmlMemMutex
);
590 xmlMutexUnlock(xmlMemMutex
);
597 * Provides the number of memory areas currently allocated
599 * Returns an int representing the number of blocks
606 xmlMutexLock(xmlMemMutex
);
607 res
= debugMemBlocks
;
608 xmlMutexUnlock(xmlMemMutex
);
615 * @fp: a FILE descriptor used as the output file
616 * @p: a memory block header
618 * tries to show some content from the memory block
622 xmlMemContentShow(FILE *fp
, MEMHDR
*p
)
628 fprintf(fp
, " NULL");
632 buf
= (const char *) HDR_2_CLIENT(p
);
634 for (i
= 0;i
< len
;i
++) {
635 if (buf
[i
] == 0) break;
636 if (!isprint((unsigned char) buf
[i
])) break;
638 if ((i
< 4) && ((buf
[i
] != 0) || (i
== 0))) {
643 for (j
= 0;(j
< len
-3) && (j
< 40);j
+= 4) {
644 cur
= *((void **) &buf
[j
]);
645 q
= CLIENT_2_HDR(cur
);
651 if (k
++ > 100) break;
653 if ((p
!= NULL
) && (p
== q
)) {
654 fprintf(fp
, " pointer to #%lu at index %d",
660 } else if ((i
== 0) && (buf
[i
] == 0)) {
663 if (buf
[i
] == 0) fprintf(fp
," \"%.25s\"", buf
);
666 for (j
= 0;j
< i
;j
++)
667 fprintf(fp
,"%c", buf
[j
]);
676 * @fp: a FILE descriptor used as the output file, if NULL, the result is
677 * written to the file .memorylist
678 * @nbBytes: the amount of memory to dump
680 * the last nbBytes of memory allocated and not freed, useful for dumping
681 * the memory left allocated between two places at runtime.
685 xmlMemDisplayLast(FILE *fp
, long nbBytes
)
698 fp
= fopen(".memorylist", "w");
704 fprintf(fp
," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
705 nbBytes
, debugMemSize
, debugMaxMemSize
);
706 fprintf(fp
,"BLOCK NUMBER SIZE TYPE\n");
708 xmlMutexLock(xmlMemMutex
);
710 while ((p
) && (nbBytes
> 0)) {
711 fprintf(fp
,"%-5u %6lu %6lu ",idx
++,p
->mh_number
,
712 (unsigned long)p
->mh_size
);
713 switch (p
->mh_type
) {
714 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
715 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
716 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
717 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
718 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
720 fprintf(fp
,"Unknown memory block, may be corrupted");
721 xmlMutexUnlock(xmlMemMutex
);
726 if (p
->mh_file
!= NULL
) fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
727 if (p
->mh_tag
!= MEMTAG
)
728 fprintf(fp
," INVALID");
731 xmlMemContentShow(fp
, p
);
736 nbBytes
-= (unsigned long)p
->mh_size
;
739 xmlMutexUnlock(xmlMemMutex
);
741 fprintf(fp
,"Memory list not compiled (MEM_LIST not defined !)\n");
749 * @fp: a FILE descriptor used as the output file, if NULL, the result is
750 * written to the file .memorylist
752 * show in-extenso the memory blocks allocated
756 xmlMemDisplay(FILE *fp
)
762 #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
771 fp
= fopen(".memorylist", "w");
777 #if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
778 currentTime
= time(NULL
);
779 tstruct
= localtime(¤tTime
);
780 strftime(buf
, sizeof(buf
) - 1, "%I:%M:%S %p", tstruct
);
781 fprintf(fp
," %s\n\n", buf
);
785 fprintf(fp
," MEMORY ALLOCATED : %lu, MAX was %lu\n",
786 debugMemSize
, debugMaxMemSize
);
787 fprintf(fp
,"BLOCK NUMBER SIZE TYPE\n");
789 xmlMutexLock(xmlMemMutex
);
792 fprintf(fp
,"%-5u %6lu %6lu ",idx
++,p
->mh_number
,
793 (unsigned long)p
->mh_size
);
794 switch (p
->mh_type
) {
795 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
796 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
797 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
798 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
799 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
801 fprintf(fp
,"Unknown memory block, may be corrupted");
802 xmlMutexUnlock(xmlMemMutex
);
807 if (p
->mh_file
!= NULL
) fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
808 if (p
->mh_tag
!= MEMTAG
)
809 fprintf(fp
," INVALID");
812 xmlMemContentShow(fp
, p
);
819 xmlMutexUnlock(xmlMemMutex
);
821 fprintf(fp
,"Memory list not compiled (MEM_LIST not defined !)\n");
829 static void debugmem_list_add(MEMHDR
*p
)
831 p
->mh_next
= memlist
;
833 if (memlist
) memlist
->mh_prev
= p
;
835 #ifdef MEM_LIST_DEBUG
841 static void debugmem_list_delete(MEMHDR
*p
)
844 p
->mh_next
->mh_prev
= p
->mh_prev
;
846 p
->mh_prev
->mh_next
= p
->mh_next
;
847 else memlist
= p
->mh_next
;
848 #ifdef MEM_LIST_DEBUG
857 * debugmem_tag_error:
859 * internal error function.
862 static void debugmem_tag_error(void *p
)
864 xmlGenericError(xmlGenericErrorContext
,
865 "Memory tag error occurs :%p \n\t bye\n", p
);
868 xmlMemDisplay(stderr
);
873 static FILE *xmlMemoryDumpFile
= NULL
;
878 * @fp: a FILE descriptor used as the output file
879 * @nr: number of entries to dump
881 * show a show display of the memory allocated, and dump
882 * the @nr last allocated areas which were not freed
886 xmlMemShow(FILE *fp
, int nr ATTRIBUTE_UNUSED
)
893 fprintf(fp
," MEMORY ALLOCATED : %lu, MAX was %lu\n",
894 debugMemSize
, debugMaxMemSize
);
896 xmlMutexLock(xmlMemMutex
);
898 fprintf(fp
,"NUMBER SIZE TYPE WHERE\n");
900 while ((p
) && nr
> 0) {
901 fprintf(fp
,"%6lu %6lu ",p
->mh_number
,(unsigned long)p
->mh_size
);
902 switch (p
->mh_type
) {
903 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
904 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
905 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
906 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
907 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
908 default:fprintf(fp
," ??? in ");break;
910 if (p
->mh_file
!= NULL
)
911 fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
912 if (p
->mh_tag
!= MEMTAG
)
913 fprintf(fp
," INVALID");
914 xmlMemContentShow(fp
, p
);
920 xmlMutexUnlock(xmlMemMutex
);
921 #endif /* MEM_LIST */
927 * Dump in-extenso the memory blocks allocated to the file .memorylist
936 if (debugMaxMemSize
== 0)
938 dump
= fopen(".memdump", "w");
940 xmlMemoryDumpFile
= stderr
;
941 else xmlMemoryDumpFile
= dump
;
943 xmlMemDisplay(xmlMemoryDumpFile
);
945 if (dump
!= NULL
) fclose(dump
);
946 #endif /* MEM_LIST */
950 /****************************************************************
952 * Initialization Routines *
954 ****************************************************************/
959 * Initialize the memory layer.
961 * Returns 0 on success
970 xmlGenericError(xmlGenericErrorContext
,
971 "xmlInitMemory()\n");
974 This is really not good code (see Bug 130419). Suggestions for
975 improvement will be welcome!
977 if (xmlMemInitialized
) return(-1);
978 xmlMemInitialized
= 1;
979 xmlMemMutex
= xmlNewMutex();
982 breakpoint
= getenv("XML_MEM_BREAKPOINT");
983 if (breakpoint
!= NULL
) {
984 sscanf(breakpoint
, "%ud", &xmlMemStopAtBlock
);
988 breakpoint
= getenv("XML_MEM_TRACE");
989 if (breakpoint
!= NULL
) {
990 sscanf(breakpoint
, "%p", &xmlMemTraceBlockAt
);
995 xmlGenericError(xmlGenericErrorContext
,
996 "xmlInitMemory() Ok\n");
1004 * Free up all the memory allocated by the library for its own
1005 * use. This should not be called by user level code.
1008 xmlCleanupMemory(void) {
1010 xmlGenericError(xmlGenericErrorContext
,
1011 "xmlCleanupMemory()\n");
1013 if (xmlMemInitialized
== 0)
1016 xmlFreeMutex(xmlMemMutex
);
1018 xmlMemInitialized
= 0;
1020 xmlGenericError(xmlGenericErrorContext
,
1021 "xmlCleanupMemory() Ok\n");
1027 * @freeFunc: the free() function to use
1028 * @mallocFunc: the malloc() function to use
1029 * @reallocFunc: the realloc() function to use
1030 * @strdupFunc: the strdup() function to use
1032 * Override the default memory access functions with a new set
1033 * This has to be called before any other libxml routines !
1035 * Should this be blocked if there was already some allocations
1038 * Returns 0 on success
1041 xmlMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1042 xmlReallocFunc reallocFunc
, xmlStrdupFunc strdupFunc
) {
1044 xmlGenericError(xmlGenericErrorContext
,
1047 if (freeFunc
== NULL
)
1049 if (mallocFunc
== NULL
)
1051 if (reallocFunc
== NULL
)
1053 if (strdupFunc
== NULL
)
1056 xmlMalloc
= mallocFunc
;
1057 xmlMallocAtomic
= mallocFunc
;
1058 xmlRealloc
= reallocFunc
;
1059 xmlMemStrdup
= strdupFunc
;
1061 xmlGenericError(xmlGenericErrorContext
,
1062 "xmlMemSetup() Ok\n");
1069 * @freeFunc: place to save the free() function in use
1070 * @mallocFunc: place to save the malloc() function in use
1071 * @reallocFunc: place to save the realloc() function in use
1072 * @strdupFunc: place to save the strdup() function in use
1074 * Provides the memory access functions set currently in use
1076 * Returns 0 on success
1079 xmlMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1080 xmlReallocFunc
*reallocFunc
, xmlStrdupFunc
*strdupFunc
) {
1081 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1082 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1083 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1084 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;
1090 * @freeFunc: the free() function to use
1091 * @mallocFunc: the malloc() function to use
1092 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1093 * @reallocFunc: the realloc() function to use
1094 * @strdupFunc: the strdup() function to use
1096 * Override the default memory access functions with a new set
1097 * This has to be called before any other libxml routines !
1098 * The mallocAtomicFunc is specialized for atomic block
1099 * allocations (i.e. of areas useful for garbage collected memory allocators
1101 * Should this be blocked if there was already some allocations
1104 * Returns 0 on success
1107 xmlGcMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1108 xmlMallocFunc mallocAtomicFunc
, xmlReallocFunc reallocFunc
,
1109 xmlStrdupFunc strdupFunc
) {
1111 xmlGenericError(xmlGenericErrorContext
,
1112 "xmlGcMemSetup()\n");
1114 if (freeFunc
== NULL
)
1116 if (mallocFunc
== NULL
)
1118 if (mallocAtomicFunc
== NULL
)
1120 if (reallocFunc
== NULL
)
1122 if (strdupFunc
== NULL
)
1125 xmlMalloc
= mallocFunc
;
1126 xmlMallocAtomic
= mallocAtomicFunc
;
1127 xmlRealloc
= reallocFunc
;
1128 xmlMemStrdup
= strdupFunc
;
1130 xmlGenericError(xmlGenericErrorContext
,
1131 "xmlGcMemSetup() Ok\n");
1138 * @freeFunc: place to save the free() function in use
1139 * @mallocFunc: place to save the malloc() function in use
1140 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1141 * @reallocFunc: place to save the realloc() function in use
1142 * @strdupFunc: place to save the strdup() function in use
1144 * Provides the memory access functions set currently in use
1145 * The mallocAtomicFunc is specialized for atomic block
1146 * allocations (i.e. of areas useful for garbage collected memory allocators
1148 * Returns 0 on success
1151 xmlGcMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1152 xmlMallocFunc
*mallocAtomicFunc
, xmlReallocFunc
*reallocFunc
,
1153 xmlStrdupFunc
*strdupFunc
) {
1154 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1155 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1156 if (mallocAtomicFunc
!= NULL
) *mallocAtomicFunc
= xmlMallocAtomic
;
1157 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1158 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;
1162 #define bottom_xmlmemory
1163 #include "elfgcchack.h"