📄 malloc.c
字号:
static long getpagesize(void);static long getregionsize(void);static void *sbrk(long size);static void *mmap(void *ptr, long size, long prot, long type, long handle, long arg);static long munmap(void *ptr, long size);static void vminfo (unsigned long*free, unsigned long*reserved, unsigned long*committed);static int cpuinfo (int whole, unsigned long*kernel, unsigned long*user);#endif/* __STD_C should be nonzero if using ANSI-standard C compiler, a C++ compiler, or a C compiler sufficiently close to ANSI to get away with it.*/#ifndef __STD_C#if defined(__STDC__) || defined(_cplusplus)#define __STD_C 1#else#define __STD_C 0#endif #endif /*__STD_C*//* Void_t* is the pointer type that malloc should say it returns*/#ifndef Void_t#if (__STD_C || defined(WIN32))#define Void_t void#else#define Void_t char#endif#endif /*Void_t*/#if __STD_C#include <stddef.h> /* for size_t */#else#include <sys/types.h>#endif#ifdef __cplusplusextern "C" {#endif/* define LACKS_UNISTD_H if your system does not have a <unistd.h>. *//* #define LACKS_UNISTD_H */#ifndef LACKS_UNISTD_H#include <unistd.h>#endif/* define LACKS_SYS_PARAM_H if your system does not have a <sys/param.h>. *//* #define LACKS_SYS_PARAM_H */#include <stdio.h> /* needed for malloc_stats */#include <errno.h> /* needed for optional MALLOC_FAILURE_ACTION *//* Debugging: Because freed chunks may be overwritten with bookkeeping fields, this malloc will often die when freed memory is overwritten by user programs. This can be very effective (albeit in an annoying way) in helping track down dangling pointers. If you compile with -DDEBUG, a number of assertion checks are enabled that will catch more memory errors. You probably won't be able to make much sense of the actual assertion errors, but they should help you locate incorrectly overwritten memory. The checking is fairly extensive, and will slow down execution noticeably. Calling malloc_stats or mallinfo with DEBUG set will attempt to check every non-mmapped allocated and free chunk in the course of computing the summmaries. (By nature, mmapped regions cannot be checked very much automatically.) Setting DEBUG may also be helpful if you are trying to modify this code. The assertions in the check routines spell out in more detail the assumptions and invariants underlying the algorithms. Setting DEBUG does NOT provide an automated mechanism for checking that all accesses to malloced memory stay within their bounds. However, there are several add-ons and adaptations of this or other mallocs available that do this.*/#if DEBUG#include <assert.h>#else#define assert(x) ((void)0)#endif/* The unsigned integer type used for comparing any two chunk sizes. This should be at least as wide as size_t, but should not be signed.*/#ifndef CHUNK_SIZE_T#define CHUNK_SIZE_T unsigned long#endif/* The unsigned integer type used to hold addresses when they are are manipulated as integers. Except that it is not defined on all systems, intptr_t would suffice.*/#ifndef PTR_UINT#define PTR_UINT unsigned long#endif/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of chunk sizes. The default version is the same as size_t. While not strictly necessary, it is best to define this as an unsigned type, even if size_t is a signed type. This may avoid some artificial size limitations on some systems. On a 64-bit machine, you may be able to reduce malloc overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the expense of not being able to handle more than 2^32 of malloced space. If this limitation is acceptable, you are encouraged to set this unless you are on a platform requiring 16byte alignments. In this case the alignment requirements turn out to negate any potential advantages of decreasing size_t word size. Implementors: Beware of the possible combinations of: - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits, and might be the same width as int or as long - size_t might have different width and signedness as INTERNAL_SIZE_T - int and long might be 32 or 64 bits, and might be the same width To deal with this, most comparisons and difference computations among INTERNAL_SIZE_Ts should cast them to CHUNK_SIZE_T, being aware of the fact that casting an unsigned int to a wider long does not sign-extend. (This also makes checking for negative numbers awkward.) Some of these casts result in harmless compiler warnings on some systems.*/#ifndef INTERNAL_SIZE_T#define INTERNAL_SIZE_T size_t#endif/* The corresponding word size */#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))/* MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. It must be a power of two at least 2 * SIZE_SZ, even on machines for which smaller alignments would suffice. It may be defined as larger than this though. Note however that code and data structures are optimized for the case of 8-byte alignment.*/#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. Some people think it should. Otherwise, since this malloc returns a unique pointer for malloc(0), so does realloc(p, 0).*//* #define REALLOC_ZERO_BYTES_FREES *//* 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 *//* USE_MALLOC_LOCK causes wrapper functions to surround each callable routine with pthread mutex lock/unlock. USE_MALLOC_LOCK forces USE_PUBLIC_MALLOC_WRAPPERS to be defined*//* #define USE_MALLOC_LOCK *//* If USE_PUBLIC_MALLOC_WRAPPERS is defined, every public routine is actually a wrapper function that first calls MALLOC_PREACTION, then calls the internal routine, and follows it with MALLOC_POSTACTION. This is needed for locking, but you can also use this, without USE_MALLOC_LOCK, for purposes of interception, instrumentation, etc. It is a sad fact that using wrappers often noticeably degrades performance of malloc-intensive programs.*/#ifdef USE_MALLOC_LOCK#define USE_PUBLIC_MALLOC_WRAPPERS#else/* #define USE_PUBLIC_MALLOC_WRAPPERS */#endif/* 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.*/#ifndef USE_PUBLIC_MALLOC_WRAPPERS#define cALLOc public_cALLOc#define fREe public_fREe#define cFREe public_cFREe#define mALLOc public_mALLOc#define mEMALIGn public_mEMALIGn#define rEALLOc public_rEALLOc#define vALLOc public_vALLOc#define pVALLOc public_pVALLOc#define mALLINFo public_mALLINFo#define mALLOPt public_mALLOPt#define mTRIm public_mTRIm#define mSTATs public_mSTATs#define mUSABLe public_mUSABLe#define iCALLOc public_iCALLOc#define iCOMALLOc public_iCOMALLOc#endif#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#else /* USE_DL_PREFIX */#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#endif /* USE_DL_PREFIX *//* 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 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/* 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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -