📄 malloc.c
字号:
#ifndef MALLOC_ALIGNMENT#define MALLOC_ALIGNMENT (2 * SIZE_SZ)#endif/* The corresponding bit mask value */#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)/* REALLOC_ZERO_BYTES_FREES should be set if a call to realloc with zero bytes should be the same as a call to free. This is required by the C standard. Otherwise, since this malloc returns a unique pointer for malloc(0), so does realloc(p, 0).*/#ifndef REALLOC_ZERO_BYTES_FREES#define REALLOC_ZERO_BYTES_FREES 1#endif/* TRIM_FASTBINS controls whether free() of a very small chunk can immediately lead to trimming. Setting to true (1) can reduce memory footprint, but will almost always slow down programs that use a lot of small chunks. Define this only if you are willing to give up some speed to more aggressively reduce system-level memory footprint when releasing memory in programs that use many small chunks. You can get essentially the same effect by setting MXFAST to 0, but this can lead to even greater slowdowns in programs using many small chunks. TRIM_FASTBINS is an in-between compile-time option, that disables only those chunks bordering topmost memory from being placed in fastbins.*/#ifndef TRIM_FASTBINS#define TRIM_FASTBINS 0#endif/* USE_DL_PREFIX will prefix all public routines with the string 'dl'. This is necessary when you only want to use this malloc in one part of a program, using your regular system malloc elsewhere.*//* #define USE_DL_PREFIX *//* Two-phase name translation. All of the actual routines are given mangled names. When wrappers are used, they become the public callable versions. When DL_PREFIX is used, the callable names are prefixed.*/#ifdef USE_DL_PREFIX#define public_cALLOc dlcalloc#define public_fREe dlfree#define public_cFREe dlcfree#define public_mALLOc dlmalloc#define public_mEMALIGn dlmemalign#define public_rEALLOc dlrealloc#define public_vALLOc dlvalloc#define public_pVALLOc dlpvalloc#define public_mALLINFo dlmallinfo#define public_mALLOPt dlmallopt#define public_mTRIm dlmalloc_trim#define public_mSTATs dlmalloc_stats#define public_mUSABLe dlmalloc_usable_size#define public_iCALLOc dlindependent_calloc#define public_iCOMALLOc dlindependent_comalloc#define public_gET_STATe dlget_state#define public_sET_STATe dlset_state#else /* USE_DL_PREFIX */#ifdef _LIBC/* Special defines for the GNU C library. */#define public_cALLOc __libc_calloc#define public_fREe __libc_free#define public_cFREe __libc_cfree#define public_mALLOc __libc_malloc#define public_mEMALIGn __libc_memalign#define public_rEALLOc __libc_realloc#define public_vALLOc __libc_valloc#define public_pVALLOc __libc_pvalloc#define public_mALLINFo __libc_mallinfo#define public_mALLOPt __libc_mallopt#define public_mTRIm __malloc_trim#define public_mSTATs __malloc_stats#define public_mUSABLe __malloc_usable_size#define public_iCALLOc __libc_independent_calloc#define public_iCOMALLOc __libc_independent_comalloc#define public_gET_STATe __malloc_get_state#define public_sET_STATe __malloc_set_state#define malloc_getpagesize __getpagesize()#define open __open#define mmap __mmap#define munmap __munmap#define mremap __mremap#define mprotect __mprotect#define MORECORE (*__morecore)#define MORECORE_FAILURE 0Void_t * __default_morecore (ptrdiff_t);Void_t *(*__morecore)(ptrdiff_t) = __default_morecore;#else /* !_LIBC */#define public_cALLOc calloc#define public_fREe free#define public_cFREe cfree#define public_mALLOc malloc#define public_mEMALIGn memalign#define public_rEALLOc realloc#define public_vALLOc valloc#define public_pVALLOc pvalloc#define public_mALLINFo mallinfo#define public_mALLOPt mallopt#define public_mTRIm malloc_trim#define public_mSTATs malloc_stats#define public_mUSABLe malloc_usable_size#define public_iCALLOc independent_calloc#define public_iCOMALLOc independent_comalloc#define public_gET_STATe malloc_get_state#define public_sET_STATe malloc_set_state#endif /* _LIBC */#endif /* USE_DL_PREFIX */#if !defined _LIBC && (!defined __GNUC__ || __GNUC__<3)#define __builtin_expect(expr, val) (expr)#endif/* HAVE_MEMCPY should be defined if you are not otherwise using ANSI STD C, but still have memcpy and memset in your C library and want to use them in calloc and realloc. Otherwise simple macro versions are defined below. USE_MEMCPY should be defined as 1 if you actually want to have memset and memcpy called. People report that the macro versions are faster than libc versions on some systems. Even if USE_MEMCPY is set to 1, loops to copy/clear small chunks (of <= 36 bytes) are manually unrolled in realloc and calloc.*/#define HAVE_MEMCPY#ifndef USE_MEMCPY#ifdef HAVE_MEMCPY#define USE_MEMCPY 1#else#define USE_MEMCPY 0#endif#endif#if (__STD_C || defined(HAVE_MEMCPY))#ifdef _LIBC# include <string.h>#else#ifdef WIN32/* On Win32 memset and memcpy are already declared in windows.h */#else#if __STD_Cvoid* memset(void*, int, size_t);void* memcpy(void*, const void*, size_t);#elseVoid_t* memset();Void_t* memcpy();#endif#endif#endif#endif/* MALLOC_FAILURE_ACTION is the action to take before "return 0" when malloc fails to be able to return memory, either because memory is exhausted or because of illegal arguments. By default, sets errno if running on STD_C platform, else does nothing.*/#ifndef MALLOC_FAILURE_ACTION#if __STD_C#define MALLOC_FAILURE_ACTION \ errno = ENOMEM;#else#define MALLOC_FAILURE_ACTION#endif#endif/* MORECORE-related declarations. By default, rely on sbrk*/#ifdef LACKS_UNISTD_H#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)#if __STD_Cextern Void_t* sbrk(ptrdiff_t);#elseextern Void_t* sbrk();#endif#endif#endif/* MORECORE is the name of the routine to call to obtain more memory from the system. See below for general guidance on writing alternative MORECORE functions, as well as a version for WIN32 and a sample version for pre-OSX macos.*/#ifndef MORECORE#define MORECORE sbrk#endif/* MORECORE_FAILURE is the value returned upon failure of MORECORE as well as mmap. Since it cannot be an otherwise valid memory address, and must reflect values of standard sys calls, you probably ought not try to redefine it.*/#ifndef MORECORE_FAILURE#define MORECORE_FAILURE (-1)#endif/* If MORECORE_CONTIGUOUS is true, take advantage of fact that consecutive calls to MORECORE with positive arguments always return contiguous increasing addresses. This is true of unix sbrk. Even if not defined, when regions happen to be contiguous, malloc will permit allocations spanning regions obtained from different calls. But defining this when applicable enables some stronger consistency checks and space efficiencies.*/#ifndef MORECORE_CONTIGUOUS#define MORECORE_CONTIGUOUS 1#endif/* Define MORECORE_CANNOT_TRIM if your version of MORECORE cannot release space back to the system when given negative arguments. This is generally necessary only if you are using a hand-crafted MORECORE function that cannot handle negative arguments.*//* #define MORECORE_CANNOT_TRIM *//* MORECORE_CLEARS (default 1) The degree to which the routine mapped to MORECORE zeroes out memory: never (0), only for newly allocated space (1) or always (2). The distinction between (1) and (2) is necessary because on some systems, if the application first decrements and then increments the break value, the contents of the reallocated space are unspecified.*/#ifndef MORECORE_CLEARS#define MORECORE_CLEARS 1#endif/* Define HAVE_MMAP as true to optionally make malloc() use mmap() to allocate very large blocks. These will be returned to the operating system immediately after a free(). Also, if mmap is available, it is used as a backup strategy in cases where MORECORE fails to provide space from system. This malloc is best tuned to work with mmap for large requests. If you do not have mmap, operations involving very large chunks (1MB or so) may be slower than you'd like.*/#ifndef HAVE_MMAP#define HAVE_MMAP 1/* Standard unix mmap using /dev/zero clears memory so calloc doesn't need to.*/#ifndef MMAP_CLEARS#define MMAP_CLEARS 1#endif#else /* no mmap */#ifndef MMAP_CLEARS#define MMAP_CLEARS 0#endif#endif/* MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if sbrk fails, and mmap is used as a backup (which is done only if HAVE_MMAP). The value must be a multiple of page size. This backup strategy generally applies only when systems have "holes" in address space, so sbrk cannot perform contiguous expansion, but there is still space available on system. On systems for which this is known to be useful (i.e. most linux kernels), this occurs only when programs allocate huge amounts of memory. Between this, and the fact that mmap regions tend to be limited, the size should be large, to avoid too many mmap calls and thus avoid running out of kernel resources.*/#ifndef MMAP_AS_MORECORE_SIZE#define MMAP_AS_MORECORE_SIZE (1024 * 1024)#endif/* Define HAVE_MREMAP to make realloc() use mremap() to re-allocate large blocks. This is currently only possible on Linux with kernel versions newer than 1.3.77.*/#ifndef HAVE_MREMAP#ifdef linux#define HAVE_MREMAP 1#else#define HAVE_MREMAP 0#endif#endif /* HAVE_MMAP *//* Define USE_ARENAS to enable support for multiple `arenas'. These are allocated using mmap(), are necessary for threads and occasionally useful to overcome address space limitations affecting sbrk(). */#ifndef USE_ARENAS#define USE_ARENAS HAVE_MMAP#endif/* USE_STARTER determines if and when the special "starter" hook functions are used: not at all (0), during ptmalloc_init (first bit set), or from the beginning until an explicit call to ptmalloc_init (second bit set). This is necessary if thread-related initialization functions (e.g. pthread_key_create) require malloc() calls (set USE_STARTER=1), or if those functions initially cannot be used at all (set USE_STARTER=2 and perform an explicit ptmalloc_init() when the thread library is ready, typically at the start of main()). */#ifndef USE_STARTER# ifndef _LIBC# define USE_STARTER 1# else# if USE___THREAD || (defined USE_TLS && !defined SHARED) /* These routines are never needed in this configuration. */# define USE_STARTER 0# else# define USE_STARTER (USE_TLS ? 4 : 1)# endif# endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -