Path: blob/master/libs/xml2/include/libxml/xmlmemory.h
4394 views
/*1* Summary: interface for the memory allocator2* Description: provides interfaces for the memory allocator,3* including debugging capabilities.4*5* Copy: See Copyright for the status of this software.6*7* Author: Daniel Veillard8*/91011#ifndef __DEBUG_MEMORY_ALLOC__12#define __DEBUG_MEMORY_ALLOC__1314#include <stdio.h>15#include <libxml/xmlversion.h>1617#ifdef __cplusplus18extern "C" {19#endif2021/*22* The XML memory wrapper support 4 basic overloadable functions.23*/24/**25* xmlFreeFunc:26* @mem: an already allocated block of memory27*28* Signature for a free() implementation.29*/30typedef void (*xmlFreeFunc)(void *mem);31/**32* xmlMallocFunc:33* @size: the size requested in bytes34*35* Signature for a malloc() implementation.36*37* Returns a pointer to the newly allocated block or NULL in case of error.38*/39typedef void *(LIBXML_ATTR_ALLOC_SIZE(1) *xmlMallocFunc)(size_t size);4041/**42* xmlReallocFunc:43* @mem: an already allocated block of memory44* @size: the new size requested in bytes45*46* Signature for a realloc() implementation.47*48* Returns a pointer to the newly reallocated block or NULL in case of error.49*/50typedef void *(*xmlReallocFunc)(void *mem, size_t size);5152/**53* xmlStrdupFunc:54* @str: a zero terminated string55*56* Signature for an strdup() implementation.57*58* Returns the copy of the string or NULL in case of error.59*/60typedef char *(*xmlStrdupFunc)(const char *str);6162/*63* In general the memory allocation entry points are not kept64* thread specific but this can be overridden by LIBXML_THREAD_ALLOC_ENABLED65* - xmlMalloc66* - xmlMallocAtomic67* - xmlRealloc68* - xmlMemStrdup69* - xmlFree70*/71/** DOC_DISABLE */72#ifdef LIBXML_THREAD_ALLOC_ENABLED73#define XML_GLOBALS_ALLOC \74XML_OP(xmlMalloc, xmlMallocFunc, XML_NO_ATTR) \75XML_OP(xmlMallocAtomic, xmlMallocFunc, XML_NO_ATTR) \76XML_OP(xmlRealloc, xmlReallocFunc, XML_NO_ATTR) \77XML_OP(xmlFree, xmlFreeFunc, XML_NO_ATTR) \78XML_OP(xmlMemStrdup, xmlStrdupFunc, XML_NO_ATTR)79#define XML_OP XML_DECLARE_GLOBAL80XML_GLOBALS_ALLOC81#undef XML_OP82#if defined(LIBXML_THREAD_ENABLED) && !defined(XML_GLOBALS_NO_REDEFINITION)83#define xmlMalloc XML_GLOBAL_MACRO(xmlMalloc)84#define xmlMallocAtomic XML_GLOBAL_MACRO(xmlMallocAtomic)85#define xmlRealloc XML_GLOBAL_MACRO(xmlRealloc)86#define xmlFree XML_GLOBAL_MACRO(xmlFree)87#define xmlMemStrdup XML_GLOBAL_MACRO(xmlMemStrdup)88#endif89#else90#define XML_GLOBALS_ALLOC91/** DOC_ENABLE */92XMLPUBVAR xmlMallocFunc xmlMalloc;93XMLPUBVAR xmlMallocFunc xmlMallocAtomic;94XMLPUBVAR xmlReallocFunc xmlRealloc;95XMLPUBVAR xmlFreeFunc xmlFree;96XMLPUBVAR xmlStrdupFunc xmlMemStrdup;97#endif9899/*100* The way to overload the existing functions.101* The xmlGc function have an extra entry for atomic block102* allocations useful for garbage collected memory allocators103*/104XMLPUBFUN int105xmlMemSetup (xmlFreeFunc freeFunc,106xmlMallocFunc mallocFunc,107xmlReallocFunc reallocFunc,108xmlStrdupFunc strdupFunc);109XMLPUBFUN int110xmlMemGet (xmlFreeFunc *freeFunc,111xmlMallocFunc *mallocFunc,112xmlReallocFunc *reallocFunc,113xmlStrdupFunc *strdupFunc);114XMLPUBFUN int115xmlGcMemSetup (xmlFreeFunc freeFunc,116xmlMallocFunc mallocFunc,117xmlMallocFunc mallocAtomicFunc,118xmlReallocFunc reallocFunc,119xmlStrdupFunc strdupFunc);120XMLPUBFUN int121xmlGcMemGet (xmlFreeFunc *freeFunc,122xmlMallocFunc *mallocFunc,123xmlMallocFunc *mallocAtomicFunc,124xmlReallocFunc *reallocFunc,125xmlStrdupFunc *strdupFunc);126127/*128* Initialization of the memory layer.129*/130XML_DEPRECATED131XMLPUBFUN int132xmlInitMemory (void);133134/*135* Cleanup of the memory layer.136*/137XML_DEPRECATED138XMLPUBFUN void139xmlCleanupMemory (void);140/*141* These are specific to the XML debug memory wrapper.142*/143XMLPUBFUN size_t144xmlMemSize (void *ptr);145XMLPUBFUN int146xmlMemUsed (void);147XMLPUBFUN int148xmlMemBlocks (void);149XMLPUBFUN void150xmlMemDisplay (FILE *fp);151XMLPUBFUN void152xmlMemDisplayLast(FILE *fp, long nbBytes);153XMLPUBFUN void154xmlMemShow (FILE *fp, int nr);155XMLPUBFUN void156xmlMemoryDump (void);157XMLPUBFUN void *158xmlMemMalloc (size_t size) LIBXML_ATTR_ALLOC_SIZE(1);159XMLPUBFUN void *160xmlMemRealloc (void *ptr,size_t size);161XMLPUBFUN void162xmlMemFree (void *ptr);163XMLPUBFUN char *164xmlMemoryStrdup (const char *str);165XMLPUBFUN void *166xmlMallocLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);167XMLPUBFUN void *168xmlReallocLoc (void *ptr, size_t size, const char *file, int line);169XMLPUBFUN void *170xmlMallocAtomicLoc (size_t size, const char *file, int line) LIBXML_ATTR_ALLOC_SIZE(1);171XMLPUBFUN char *172xmlMemStrdupLoc (const char *str, const char *file, int line);173174175/** DOC_DISABLE */176#ifdef DEBUG_MEMORY_LOCATION177/**178* xmlMalloc:179* @size: number of bytes to allocate180*181* Wrapper for the malloc() function used in the XML library.182*183* Returns the pointer to the allocated area or NULL in case of error.184*/185#define xmlMalloc(size) xmlMallocLoc((size), __FILE__, __LINE__)186/**187* xmlMallocAtomic:188* @size: number of bytes to allocate189*190* Wrapper for the malloc() function used in the XML library for allocation191* of block not containing pointers to other areas.192*193* Returns the pointer to the allocated area or NULL in case of error.194*/195#define xmlMallocAtomic(size) xmlMallocAtomicLoc((size), __FILE__, __LINE__)196/**197* xmlRealloc:198* @ptr: pointer to the existing allocated area199* @size: number of bytes to allocate200*201* Wrapper for the realloc() function used in the XML library.202*203* Returns the pointer to the allocated area or NULL in case of error.204*/205#define xmlRealloc(ptr, size) xmlReallocLoc((ptr), (size), __FILE__, __LINE__)206/**207* xmlMemStrdup:208* @str: pointer to the existing string209*210* Wrapper for the strdup() function, xmlStrdup() is usually preferred.211*212* Returns the pointer to the allocated area or NULL in case of error.213*/214#define xmlMemStrdup(str) xmlMemStrdupLoc((str), __FILE__, __LINE__)215216#endif /* DEBUG_MEMORY_LOCATION */217/** DOC_ENABLE */218219#ifdef __cplusplus220}221#endif /* __cplusplus */222223#endif /* __DEBUG_MEMORY_ALLOC__ */224225226