2 * xmlmemory.c: libxml memory allocator wrapper.
15 /* #define DEBUG_MEMORY */
20 * keep track of all allocated blocks for error reporting
21 * Always build the memory list !
23 #ifdef DEBUG_MEMORY_LOCATION
25 #define MEM_LIST /* keep a list of all the allocated memory blocks */
29 #include <libxml/globals.h> /* must come before xmlmemory.h */
30 #include <libxml/xmlmemory.h>
31 #include <libxml/xmlerror.h>
32 #include <libxml/threads.h>
34 static int xmlMemInitialized
= 0;
35 static unsigned long debugMemSize
= 0;
36 static unsigned long debugMemBlocks
= 0;
37 static unsigned long debugMaxMemSize
= 0;
38 static xmlMutexPtr xmlMemMutex
= NULL
;
40 void xmlMallocBreakpoint(void);
42 /************************************************************************
44 * Macros, variables and associated types *
46 ************************************************************************/
48 #if !defined(LIBXML_THREAD_ENABLED) && !defined(LIBXML_THREAD_ALLOC_ENABLED)
61 * Each of the blocks allocated begin with a header containing information
64 #define MEMTAG 0x5aa5U
67 #define REALLOC_TYPE 2
69 #define MALLOC_ATOMIC_TYPE 4
70 #define REALLOC_ATOMIC_TYPE 5
72 typedef struct memnod
{
75 unsigned long mh_number
;
78 struct memnod
*mh_next
;
79 struct memnod
*mh_prev
;
89 #define ALIGN_SIZE sizeof(double)
91 #define HDR_SIZE sizeof(MEMHDR)
92 #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \
93 / ALIGN_SIZE ) * ALIGN_SIZE)
95 #define MAX_SIZE_T ((size_t)-1)
97 #define CLIENT_2_HDR(a) ((void *) (((char *) (a)) - RESERVE_SIZE))
98 #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE))
101 static unsigned int block
=0;
102 static unsigned int xmlMemStopAtBlock
= 0;
103 static void *xmlMemTraceBlockAt
= NULL
;
105 static MEMHDR
*memlist
= NULL
;
108 static void debugmem_tag_error(void *addr
);
110 static void debugmem_list_add(MEMHDR
*);
111 static void debugmem_list_delete(MEMHDR
*);
113 #define Mem_Tag_Err(a) debugmem_tag_error(a);
120 * xmlMallocBreakpoint:
122 * Breakpoint to use in conjunction with xmlMemStopAtBlock. When the block
123 * number reaches the specified value this function is called. One need to add a breakpoint
124 * to it to get the context in which the given block is allocated.
128 xmlMallocBreakpoint(void) {
129 xmlGenericError(xmlGenericErrorContext
,
130 "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock
);
135 * @size: an int specifying the size in byte to allocate.
136 * @file: the file name or NULL
137 * @line: the line number
139 * a malloc() equivalent, with logging of the allocation info.
141 * Returns a pointer to the allocated area or NULL in case of lack of memory.
145 xmlMallocLoc(size_t size
, const char * file
, int line
)
150 if (!xmlMemInitialized
) xmlInitMemory();
152 xmlGenericError(xmlGenericErrorContext
,
153 "Malloc(%d)\n",size
);
158 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
159 xmlGenericError(xmlGenericErrorContext
,
160 "xmlMallocLoc : Unsigned overflow\n");
165 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
168 xmlGenericError(xmlGenericErrorContext
,
169 "xmlMallocLoc : Out of free space\n");
175 p
->mh_type
= MALLOC_TYPE
;
178 xmlMutexLock(xmlMemMutex
);
179 p
->mh_number
= ++block
;
180 debugMemSize
+= size
;
182 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
184 debugmem_list_add(p
);
186 xmlMutexUnlock(xmlMemMutex
);
189 xmlGenericError(xmlGenericErrorContext
,
190 "Malloc(%d) Ok\n",size
);
193 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
195 ret
= HDR_2_CLIENT(p
);
197 if (xmlMemTraceBlockAt
== ret
) {
198 xmlGenericError(xmlGenericErrorContext
,
199 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
200 (long unsigned)size
);
201 xmlMallocBreakpoint();
210 * xmlMallocAtomicLoc:
211 * @size: an unsigned int specifying the size in byte to allocate.
212 * @file: the file name or NULL
213 * @line: the line number
215 * a malloc() equivalent, with logging of the allocation info.
217 * Returns a pointer to the allocated area or NULL in case of lack of memory.
221 xmlMallocAtomicLoc(size_t size
, const char * file
, int line
)
226 if (!xmlMemInitialized
) xmlInitMemory();
228 xmlGenericError(xmlGenericErrorContext
,
229 "Malloc(%d)\n",size
);
234 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
235 xmlGenericError(xmlGenericErrorContext
,
236 "xmlMallocAtomicLoc : Unsigned overflow\n");
241 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
244 xmlGenericError(xmlGenericErrorContext
,
245 "xmlMallocAtomicLoc : Out of free space\n");
251 p
->mh_type
= MALLOC_ATOMIC_TYPE
;
254 xmlMutexLock(xmlMemMutex
);
255 p
->mh_number
= ++block
;
256 debugMemSize
+= size
;
258 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
260 debugmem_list_add(p
);
262 xmlMutexUnlock(xmlMemMutex
);
265 xmlGenericError(xmlGenericErrorContext
,
266 "Malloc(%d) Ok\n",size
);
269 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
271 ret
= HDR_2_CLIENT(p
);
273 if (xmlMemTraceBlockAt
== ret
) {
274 xmlGenericError(xmlGenericErrorContext
,
275 "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt
,
276 (long unsigned)size
);
277 xmlMallocBreakpoint();
286 * @size: an int specifying the size in byte to allocate.
288 * a malloc() equivalent, with logging of the allocation info.
290 * Returns a pointer to the allocated area or NULL in case of lack of memory.
294 xmlMemMalloc(size_t size
)
296 return(xmlMallocLoc(size
, "none", 0));
301 * @ptr: the initial memory block pointer
302 * @size: an int specifying the size in byte to allocate.
303 * @file: the file name or NULL
304 * @line: the line number
306 * a realloc() equivalent, with logging of the allocation info.
308 * Returns a pointer to the allocated area or NULL in case of lack of memory.
312 xmlReallocLoc(void *ptr
,size_t size
, const char * file
, int line
)
315 unsigned long number
;
321 return(xmlMallocLoc(size
, file
, line
));
323 if (!xmlMemInitialized
) xmlInitMemory();
326 p
= CLIENT_2_HDR(ptr
);
327 number
= p
->mh_number
;
328 if (xmlMemStopAtBlock
== number
) xmlMallocBreakpoint();
329 if (p
->mh_tag
!= MEMTAG
) {
334 xmlMutexLock(xmlMemMutex
);
335 debugMemSize
-= p
->mh_size
;
338 oldsize
= p
->mh_size
;
341 debugmem_list_delete(p
);
343 xmlMutexUnlock(xmlMemMutex
);
345 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
346 xmlGenericError(xmlGenericErrorContext
,
347 "xmlReallocLoc : Unsigned overflow\n");
352 tmp
= (MEMHDR
*) realloc(p
,RESERVE_SIZE
+size
);
358 if (xmlMemTraceBlockAt
== ptr
) {
359 xmlGenericError(xmlGenericErrorContext
,
360 "%p : Realloced(%lu -> %lu) Ok\n",
361 xmlMemTraceBlockAt
, (long unsigned)p
->mh_size
,
362 (long unsigned)size
);
363 xmlMallocBreakpoint();
366 p
->mh_number
= number
;
367 p
->mh_type
= REALLOC_TYPE
;
371 xmlMutexLock(xmlMemMutex
);
372 debugMemSize
+= size
;
374 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
376 debugmem_list_add(p
);
378 xmlMutexUnlock(xmlMemMutex
);
383 xmlGenericError(xmlGenericErrorContext
,
384 "Realloced(%d to %d) Ok\n", oldsize
, size
);
386 return(HDR_2_CLIENT(p
));
394 * @ptr: the initial memory block pointer
395 * @size: an int specifying the size in byte to allocate.
397 * a realloc() equivalent, with logging of the allocation info.
399 * Returns a pointer to the allocated area or NULL in case of lack of memory.
403 xmlMemRealloc(void *ptr
,size_t size
) {
404 return(xmlReallocLoc(ptr
, size
, "none", 0));
409 * @ptr: the memory block pointer
411 * a free() equivalent, with error checking.
414 xmlMemFree(void *ptr
)
425 if (ptr
== (void *) -1) {
426 xmlGenericError(xmlGenericErrorContext
,
427 "trying to free pointer from freed area\n");
431 if (xmlMemTraceBlockAt
== ptr
) {
432 xmlGenericError(xmlGenericErrorContext
,
433 "%p : Freed()\n", xmlMemTraceBlockAt
);
434 xmlMallocBreakpoint();
439 target
= (char *) ptr
;
441 p
= CLIENT_2_HDR(ptr
);
442 if (p
->mh_tag
!= MEMTAG
) {
446 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
448 memset(target
, -1, p
->mh_size
);
449 xmlMutexLock(xmlMemMutex
);
450 debugMemSize
-= p
->mh_size
;
456 debugmem_list_delete(p
);
458 xmlMutexUnlock(xmlMemMutex
);
465 xmlGenericError(xmlGenericErrorContext
,
466 "Freed(%d) Ok\n", size
);
472 xmlGenericError(xmlGenericErrorContext
,
473 "xmlMemFree(%p) error\n", ptr
);
474 xmlMallocBreakpoint();
480 * @str: the initial string pointer
481 * @file: the file name or NULL
482 * @line: the line number
484 * a strdup() equivalent, with logging of the allocation info.
486 * Returns a pointer to the new string or NULL if allocation error occurred.
490 xmlMemStrdupLoc(const char *str
, const char *file
, int line
)
493 size_t size
= strlen(str
) + 1;
496 if (!xmlMemInitialized
) xmlInitMemory();
499 if (size
> (MAX_SIZE_T
- RESERVE_SIZE
)) {
500 xmlGenericError(xmlGenericErrorContext
,
501 "xmlMemStrdupLoc : Unsigned overflow\n");
506 p
= (MEMHDR
*) malloc(RESERVE_SIZE
+size
);
512 p
->mh_type
= STRDUP_TYPE
;
515 xmlMutexLock(xmlMemMutex
);
516 p
->mh_number
= ++block
;
517 debugMemSize
+= size
;
519 if (debugMemSize
> debugMaxMemSize
) debugMaxMemSize
= debugMemSize
;
521 debugmem_list_add(p
);
523 xmlMutexUnlock(xmlMemMutex
);
525 s
= (char *) HDR_2_CLIENT(p
);
527 if (xmlMemStopAtBlock
== p
->mh_number
) xmlMallocBreakpoint();
533 if (xmlMemTraceBlockAt
== s
) {
534 xmlGenericError(xmlGenericErrorContext
,
535 "%p : Strdup() Ok\n", xmlMemTraceBlockAt
);
536 xmlMallocBreakpoint();
547 * @str: the initial string pointer
549 * a strdup() equivalent, with logging of the allocation info.
551 * Returns a pointer to the new string or NULL if allocation error occurred.
555 xmlMemoryStrdup(const char *str
) {
556 return(xmlMemStrdupLoc(str
, "none", 0));
562 * Provides the amount of memory currently allocated
564 * Returns an int representing the amount of memory allocated.
571 xmlMutexLock(xmlMemMutex
);
573 xmlMutexUnlock(xmlMemMutex
);
580 * Provides the number of memory areas currently allocated
582 * Returns an int representing the number of blocks
589 xmlMutexLock(xmlMemMutex
);
590 res
= debugMemBlocks
;
591 xmlMutexUnlock(xmlMemMutex
);
598 * @fp: a FILE descriptor used as the output file
599 * @p: a memory block header
601 * tries to show some content from the memory block
605 xmlMemContentShow(FILE *fp
, MEMHDR
*p
)
611 fprintf(fp
, " NULL");
615 buf
= (const char *) HDR_2_CLIENT(p
);
617 for (i
= 0;i
< len
;i
++) {
618 if (buf
[i
] == 0) break;
619 if (!isprint((unsigned char) buf
[i
])) break;
621 if ((i
< 4) && ((buf
[i
] != 0) || (i
== 0))) {
626 for (j
= 0;(j
< len
-3) && (j
< 40);j
+= 4) {
627 cur
= *((void **) &buf
[j
]);
628 q
= CLIENT_2_HDR(cur
);
634 if (k
++ > 100) break;
636 if ((p
!= NULL
) && (p
== q
)) {
637 fprintf(fp
, " pointer to #%lu at index %d",
643 } else if ((i
== 0) && (buf
[i
] == 0)) {
646 if (buf
[i
] == 0) fprintf(fp
," \"%.25s\"", buf
);
649 for (j
= 0;j
< i
;j
++)
650 fprintf(fp
,"%c", buf
[j
]);
659 * @fp: a FILE descriptor used as the output file, if NULL, the result is
660 * written to the file .memorylist
661 * @nbBytes: the amount of memory to dump
663 * the last nbBytes of memory allocated and not freed, useful for dumping
664 * the memory left allocated between two places at runtime.
668 xmlMemDisplayLast(FILE *fp
, long nbBytes
)
681 fp
= fopen(".memorylist", "w");
687 fprintf(fp
," Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
688 nbBytes
, debugMemSize
, debugMaxMemSize
);
689 fprintf(fp
,"BLOCK NUMBER SIZE TYPE\n");
691 xmlMutexLock(xmlMemMutex
);
693 while ((p
) && (nbBytes
> 0)) {
694 fprintf(fp
,"%-5u %6lu %6lu ",idx
++,p
->mh_number
,
695 (unsigned long)p
->mh_size
);
696 switch (p
->mh_type
) {
697 case STRDUP_TYPE
:fprintf(fp
,"strdup() in ");break;
698 case MALLOC_TYPE
:fprintf(fp
,"malloc() in ");break;
699 case REALLOC_TYPE
:fprintf(fp
,"realloc() in ");break;
700 case MALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicmalloc() in ");break;
701 case REALLOC_ATOMIC_TYPE
:fprintf(fp
,"atomicrealloc() in ");break;
703 fprintf(fp
,"Unknown memory block, may be corrupted");
704 xmlMutexUnlock(xmlMemMutex
);
709 if (p
->mh_file
!= NULL
) fprintf(fp
,"%s(%u)", p
->mh_file
, p
->mh_line
);
710 if (p
->mh_tag
!= MEMTAG
)
711 fprintf(fp
," INVALID");
714 xmlMemContentShow(fp
, p
);
719 nbBytes
-= (unsigned long)p
->mh_size
;
722 xmlMutexUnlock(xmlMemMutex
);
724 fprintf(fp
,"Memory list not compiled (MEM_LIST not defined !)\n");
732 * @fp: a FILE descriptor used as the output file, if NULL, the result is
733 * written to the file .memorylist
735 * show in-extenso the memory blocks allocated
739 xmlMemDisplay(FILE *fp
)
752 fp
= fopen(".memorylist", "w");
758 currentTime
= time(NULL
);
759 tstruct
= localtime(¤tTime
);
760 strftime(buf
, sizeof(buf
) - 1, "%I:%M:%S %p", tstruct
);
761 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 * DEPRECATED: This function will be made private. Call xmlInitParser to
939 * initialize the library.
941 * Initialize the memory layer.
943 * Returns 0 on success
950 xmlGenericError(xmlGenericErrorContext
,
951 "xmlInitMemory()\n");
954 This is really not good code (see Bug 130419). Suggestions for
955 improvement will be welcome!
957 if (xmlMemInitialized
) return(-1);
958 xmlMemInitialized
= 1;
959 xmlMemMutex
= xmlNewMutex();
961 breakpoint
= getenv("XML_MEM_BREAKPOINT");
962 if (breakpoint
!= NULL
) {
963 sscanf(breakpoint
, "%ud", &xmlMemStopAtBlock
);
965 breakpoint
= getenv("XML_MEM_TRACE");
966 if (breakpoint
!= NULL
) {
967 sscanf(breakpoint
, "%p", &xmlMemTraceBlockAt
);
971 xmlGenericError(xmlGenericErrorContext
,
972 "xmlInitMemory() Ok\n");
980 * DEPRECATED: This function will be made private. Call xmlCleanupParser
981 * to free global state but see the warnings there. xmlCleanupParser
982 * should be only called once at program exit. In most cases, you don't
983 * have call cleanup functions at all.
985 * Free up all the memory allocated by the library for its own
986 * use. This should not be called by user level code.
989 xmlCleanupMemory(void) {
991 xmlGenericError(xmlGenericErrorContext
,
992 "xmlCleanupMemory()\n");
994 if (xmlMemInitialized
== 0)
997 xmlFreeMutex(xmlMemMutex
);
999 xmlMemInitialized
= 0;
1001 xmlGenericError(xmlGenericErrorContext
,
1002 "xmlCleanupMemory() Ok\n");
1008 * @freeFunc: the free() function to use
1009 * @mallocFunc: the malloc() function to use
1010 * @reallocFunc: the realloc() function to use
1011 * @strdupFunc: the strdup() function to use
1013 * Override the default memory access functions with a new set
1014 * This has to be called before any other libxml routines !
1016 * Should this be blocked if there was already some allocations
1019 * Returns 0 on success
1022 xmlMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1023 xmlReallocFunc reallocFunc
, xmlStrdupFunc strdupFunc
) {
1025 xmlGenericError(xmlGenericErrorContext
,
1028 if (freeFunc
== NULL
)
1030 if (mallocFunc
== NULL
)
1032 if (reallocFunc
== NULL
)
1034 if (strdupFunc
== NULL
)
1037 xmlMalloc
= mallocFunc
;
1038 xmlMallocAtomic
= mallocFunc
;
1039 xmlRealloc
= reallocFunc
;
1040 xmlMemStrdup
= strdupFunc
;
1042 xmlGenericError(xmlGenericErrorContext
,
1043 "xmlMemSetup() Ok\n");
1050 * @freeFunc: place to save the free() function in use
1051 * @mallocFunc: place to save the malloc() function in use
1052 * @reallocFunc: place to save the realloc() function in use
1053 * @strdupFunc: place to save the strdup() function in use
1055 * Provides the memory access functions set currently in use
1057 * Returns 0 on success
1060 xmlMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1061 xmlReallocFunc
*reallocFunc
, xmlStrdupFunc
*strdupFunc
) {
1062 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1063 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1064 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1065 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;
1071 * @freeFunc: the free() function to use
1072 * @mallocFunc: the malloc() function to use
1073 * @mallocAtomicFunc: the malloc() function to use for atomic allocations
1074 * @reallocFunc: the realloc() function to use
1075 * @strdupFunc: the strdup() function to use
1077 * Override the default memory access functions with a new set
1078 * This has to be called before any other libxml routines !
1079 * The mallocAtomicFunc is specialized for atomic block
1080 * allocations (i.e. of areas useful for garbage collected memory allocators
1082 * Should this be blocked if there was already some allocations
1085 * Returns 0 on success
1088 xmlGcMemSetup(xmlFreeFunc freeFunc
, xmlMallocFunc mallocFunc
,
1089 xmlMallocFunc mallocAtomicFunc
, xmlReallocFunc reallocFunc
,
1090 xmlStrdupFunc strdupFunc
) {
1092 xmlGenericError(xmlGenericErrorContext
,
1093 "xmlGcMemSetup()\n");
1095 if (freeFunc
== NULL
)
1097 if (mallocFunc
== NULL
)
1099 if (mallocAtomicFunc
== NULL
)
1101 if (reallocFunc
== NULL
)
1103 if (strdupFunc
== NULL
)
1106 xmlMalloc
= mallocFunc
;
1107 xmlMallocAtomic
= mallocAtomicFunc
;
1108 xmlRealloc
= reallocFunc
;
1109 xmlMemStrdup
= strdupFunc
;
1111 xmlGenericError(xmlGenericErrorContext
,
1112 "xmlGcMemSetup() Ok\n");
1119 * @freeFunc: place to save the free() function in use
1120 * @mallocFunc: place to save the malloc() function in use
1121 * @mallocAtomicFunc: place to save the atomic malloc() function in use
1122 * @reallocFunc: place to save the realloc() function in use
1123 * @strdupFunc: place to save the strdup() function in use
1125 * Provides the memory access functions set currently in use
1126 * The mallocAtomicFunc is specialized for atomic block
1127 * allocations (i.e. of areas useful for garbage collected memory allocators
1129 * Returns 0 on success
1132 xmlGcMemGet(xmlFreeFunc
*freeFunc
, xmlMallocFunc
*mallocFunc
,
1133 xmlMallocFunc
*mallocAtomicFunc
, xmlReallocFunc
*reallocFunc
,
1134 xmlStrdupFunc
*strdupFunc
) {
1135 if (freeFunc
!= NULL
) *freeFunc
= xmlFree
;
1136 if (mallocFunc
!= NULL
) *mallocFunc
= xmlMalloc
;
1137 if (mallocAtomicFunc
!= NULL
) *mallocAtomicFunc
= xmlMallocAtomic
;
1138 if (reallocFunc
!= NULL
) *reallocFunc
= xmlRealloc
;
1139 if (strdupFunc
!= NULL
) *strdupFunc
= xmlMemStrdup
;