📄 dlmalloc.c
字号:
#endif/* M_TOP_PAD is the amount of extra `padding' space to allocate or retain whenever sbrk is called. It is used in two ways internally: * When sbrk is called to extend the top of the arena to satisfy a new malloc request, this much padding is added to the sbrk request. * When malloc_trim is called automatically from free(), it is used as the `pad' argument. In both cases, the actual amount of padding is rounded so that the end of the arena is always a system page boundary. The main reason for using padding is to avoid calling sbrk so often. Having even a small pad greatly reduces the likelihood that nearly every malloc request during program start-up (or after trimming) will invoke sbrk, which needlessly wastes time. Automatic rounding-up to page-size units is normally sufficient to avoid measurable overhead, so the default is 0. However, in systems where sbrk is relatively slow, it can pay to increase this value, at the expense of carrying around more memory than the program needs.*/#ifndef DEFAULT_MMAP_THRESHOLD#define DEFAULT_MMAP_THRESHOLD (128 * 1024)#endif/* M_MMAP_THRESHOLD is the request size threshold for using mmap() to service a request. Requests of at least this size that cannot be allocated using already-existing space will be serviced via mmap. (If enough normal freed space already exists it is used instead.) Using mmap segregates relatively large chunks of memory so that they can be individually obtained and released from the host system. A request serviced through mmap is never reused by any other request (at least not directly; the system may just so happen to remap successive requests to the same locations). Segregating space in this way has the benefit that mmapped space can ALWAYS be individually released back to the system, which helps keep the system level memory demands of a long-lived program low. Mapped memory can never become `locked' between other chunks, as can happen with normally allocated chunks, which menas that even trimming via malloc_trim would not release them. However, it has the disadvantages that: 1. The space cannot be reclaimed, consolidated, and then used to service later requests, as happens with normal chunks. 2. It can lead to more wastage because of mmap page alignment requirements 3. It causes malloc performance to be more dependent on host system memory management support routines which may vary in implementation quality and may impose arbitrary limitations. Generally, servicing a request via normal malloc steps is faster than going through a system's mmap. All together, these considerations should lead you to use mmap only for relatively large requests.*/#ifndef DEFAULT_MMAP_MAX#if HAVE_MMAP#define DEFAULT_MMAP_MAX (64)#else#define DEFAULT_MMAP_MAX (0)#endif#endif/* M_MMAP_MAX is the maximum number of requests to simultaneously service using mmap. This parameter exists because: 1. Some systems have a limited number of internal tables for use by mmap. 2. In most systems, overreliance on mmap can degrade overall performance. 3. If a program allocates many large regions, it is probably better off using normal sbrk-based allocation routines that can reclaim and reallocate normal heap memory. Using a small value allows transition into this mode after the first few allocations. Setting to 0 disables all use of mmap. If HAVE_MMAP is not set, the default value is 0, and attempts to set it to non-zero values in mallopt will fail.*//* Special defines for linux libc Except when compiled using these special defines for Linux libc using weak aliases, this malloc is NOT designed to work in multithreaded applications. No semaphores or other concurrency control are provided to ensure that multiple malloc or free calls don't run at the same time, which could be disasterous. A single semaphore could be used across malloc, realloc, and free (which is essentially the effect of the linux weak alias approach). It would be hard to obtain finer granularity.*/#ifdef INTERNAL_LINUX_C_LIB#if __STD_CVoid_t * __default_morecore_init (ptrdiff_t);Void_t *(*__morecore)(ptrdiff_t) = __default_morecore_init;#elseVoid_t * __default_morecore_init ();Void_t *(*__morecore)() = __default_morecore_init;#endif#define MORECORE (*__morecore)#define MORECORE_FAILURE 0#define MORECORE_CLEARS 1#else /* INTERNAL_LINUX_C_LIB */#if __STD_C/* extern Void_t* sbrk(ptrdiff_t);*/#elseextern Void_t* sbrk();#endif#ifndef MORECORE#define MORECORE sbrk#endif#ifndef MORECORE_FAILURE#define MORECORE_FAILURE -1#endif#ifndef MORECORE_CLEARS#define MORECORE_CLEARS 0#endif#endif /* INTERNAL_LINUX_C_LIB */#if defined(INTERNAL_LINUX_C_LIB) && defined(__ELF__)#define cALLOc __libc_calloc#define fREe __libc_free#define mALLOc __libc_malloc#define mEMALIGn __libc_memalign#define rEALLOc __libc_realloc#define vALLOc __libc_valloc#define pvALLOc __libc_pvalloc#define mALLINFo __libc_mallinfo#define mALLOPt __libc_mallopt#pragma weak calloc = __libc_calloc#pragma weak free = __libc_free#pragma weak cfree = __libc_free#pragma weak malloc = __libc_malloc#pragma weak memalign = __libc_memalign#pragma weak realloc = __libc_realloc#pragma weak valloc = __libc_valloc#pragma weak pvalloc = __libc_pvalloc#pragma weak mallinfo = __libc_mallinfo#pragma weak mallopt = __libc_mallopt#else#ifndef cALLOc#define cALLOc calloc#endif#ifndef fREe#define fREe free#endif#ifndef mALLOc#define mALLOc malloc#endif#ifndef mEMALIGn#define mEMALIGn memalign#endif#ifndef rEALLOc#define rEALLOc realloc#endif#ifndef vALLOc#define vALLOc valloc#endif#ifndef pvALLOc#define pvALLOc pvalloc#endif#ifndef mALLINFo#define mALLINFo mallinfo#endif#ifndef mALLOPt#define mALLOPt mallopt#endif#endif/* Public routines */#ifdef DEBUG2#define malloc(size) malloc_dbg(size, __FILE__, __LINE__)#define free(p) free_dbg(p, __FILE__, __LINE__)#define realloc(p, size) realloc_dbg(p, size, __FILE__, __LINE__)#define calloc(n, size) calloc_dbg(n, size, __FILE__, __LINE__)#define memalign(align, size) memalign_dbg(align, size, __FILE__, __LINE__)#define valloc(size) valloc_dbg(size, __FILE__, __LINE__)#define pvalloc(size) pvalloc_dbg(size, __FILE__, __LINE__)#define cfree(p) cfree_dbg(p, __FILE__, __LINE__)#define malloc_trim(pad) malloc_trim_dbg(pad, __FILE__, __LINE__)#define malloc_usable_size(p) malloc_usable_size_dbg(p, __FILE__, __LINE__)#define malloc_stats(void) malloc_stats_dbg(__FILE__, __LINE__)#define mallopt(flag, val) mallopt_dbg(flag, val, __FILE__, __LINE__)#define mallinfo(void) mallinfo_dbg(__FILE__, __LINE__)#if __STD_CVoid_t* malloc_dbg(size_t, const char *, int);void free_dbg(Void_t*, const char *, int);Void_t* realloc_dbg(Void_t*, size_t, const char *, int);Void_t* calloc_dbg(size_t, size_t, const char *, int);Void_t* memalign_dbg(size_t, size_t, const char *, int);Void_t* valloc_dbg(size_t, const char *, int);Void_t* pvalloc_dbg(size_t, const char *, int);void cfree_dbg(Void_t*, const char *, int);int malloc_trim_dbg(size_t, const char *, int);size_t malloc_usable_size_dbg(Void_t*, const char *, int);void malloc_stats_dbg(const char *, int);int mallopt_dbg(int, int, const char *, int);struct mallinfo mallinfo_dbg(const char *, int);#elseVoid_t* malloc_dbg();void free_dbg();Void_t* realloc_dbg();Void_t* calloc_dbg();Void_t* memalign_dbg();Void_t* valloc_dbg();Void_t* pvalloc_dbg();void cfree_dbg();int malloc_trim_dbg();size_t malloc_usable_size_dbg();void malloc_stats_dbg();int mallopt_dbg();struct mallinfo mallinfo_dbg();#endif /* !__STD_C */#else /* !DEBUG2 */#if __STD_CVoid_t* mALLOc(size_t);void fREe(Void_t*);Void_t* rEALLOc(Void_t*, size_t);Void_t* cALLOc(size_t, size_t);Void_t* mEMALIGn(size_t, size_t);Void_t* vALLOc(size_t);Void_t* pvALLOc(size_t);void cfree(Void_t*);int malloc_trim(size_t);size_t malloc_usable_size(Void_t*);void malloc_stats(void);int mALLOPt(int, int);struct mallinfo mALLINFo(void);#elseVoid_t* mALLOc();void fREe();Void_t* rEALLOc();Void_t* cALLOc();Void_t* mEMALIGn();Void_t* vALLOc();Void_t* pvALLOc();void cfree();int malloc_trim();size_t malloc_usable_size();void malloc_stats();int mALLOPt();struct mallinfo mALLINFo();#endif#endif /* !DEBUG2 */#ifdef __cplusplus}; /* end of extern "C" */#endif/* ---------- To make a malloc.h, end cutting here ------------ */#ifdef DEBUG2#ifdef __cplusplusextern "C" {#endif#undef malloc#undef free#undef realloc#undef calloc#undef memalign#undef valloc#undef pvalloc#undef cfree#undef malloc_trim#undef malloc_usable_size#undef malloc_stats#undef mallopt#undef mallinfo#if __STD_CVoid_t* mALLOc(size_t);void fREe(Void_t*);Void_t* rEALLOc(Void_t*, size_t);Void_t* cALLOc(size_t, size_t);Void_t* mEMALIGn(size_t, size_t);Void_t* vALLOc(size_t);Void_t* pvALLOc(size_t);void cfree(Void_t*);int malloc_trim(size_t);size_t malloc_usable_size(Void_t*);void malloc_stats(void);int mALLOPt(int, int);struct mallinfo mALLINFo(void);#elseVoid_t* mALLOc();void fREe();Void_t* rEALLOc();Void_t* cALLOc();Void_t* mEMALIGn();Void_t* vALLOc();Void_t* pvALLOc();void cfree();int malloc_trim();size_t malloc_usable_size();void malloc_stats();int mALLOPt();struct mallinfo mALLINFo();#endif#include <ctype.h> /* isprint() */#ifdef DEBUG3#include <stdlib.h> /* atexit() */#endif#ifdef __cplusplus}; /* end of extern "C" */#endif#endif /* DEBUG2 *//* Emulation of sbrk for WIN32 All code within the ifdef WIN32 is untested by me.*/#ifdef WIN32#define AlignPage(add) (((add) + (malloc_getpagesize-1)) & \ ~(malloc_getpagesize-1))/* resrve 64MB to insure large contiguous space */#define RESERVED_SIZE (1024*1024*64)#define NEXT_SIZE (2048*1024)#define TOP_MEMORY ((unsigned long)2*1024*1024*1024)struct GmListElement;typedef struct GmListElement GmListElement;struct GmListElement{ GmListElement* next; void* base;};static GmListElement* head = 0;static unsigned int gNextAddress = 0;static unsigned int gAddressBase = 0;static unsigned int gAllocatedSize = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -