📄 malloc.h
字号:
/* #define REALLOC_ZERO_BYTES_FREES *//* WIN32 causes an emulation of sbrk to be compiled in mmap-based options are not currently supported in WIN32.*//* #define WIN32 */#ifdef WIN32#define MORECORE wsbrk#define HAVE_MMAP 0#define LACKS_UNISTD_H#define LACKS_SYS_PARAM_H/* Include 'windows.h' to get the necessary declarations for the Microsoft Visual C++ data structures and routines used in the 'sbrk' emulation. Define WIN32_LEAN_AND_MEAN so that only the essential Microsoft Visual C++ header files are included.*/#define WIN32_LEAN_AND_MEAN#include <windows.h>#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 here. USE_MEMCPY should be defined as 1 if you actually want to have memset and memcpy called. People report that the macro versions are often enough faster than libc versions on many systems that it is better to use them.*/#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))#if __STD_C#else#ifdef WIN32// On Win32 platforms, 'memset()' and 'memcpy()' are already declared in// 'windows.h'#elseVoid_t* memset();Void_t* memcpy();#endif#endif#endif#if USE_MEMCPY/* The following macros are only invoked with (2n+1)-multiples of INTERNAL_SIZE_T units, with a positive integer n. This is exploited for fast inline execution when n is small. */#define MALLOC_ZERO(charp, nbytes) \do { \ INTERNAL_SIZE_T mzsz = (nbytes); \ if(mzsz <= 9*sizeof(mzsz)) { \ INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \ if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \ *mz++ = 0; \ if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \ *mz++ = 0; \ if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \ *mz++ = 0; }}} \ *mz++ = 0; \ *mz++ = 0; \ *mz = 0; \ } else memset((charp), 0, mzsz); \} while(0)#define MALLOC_COPY(dest,src,nbytes) \do { \ INTERNAL_SIZE_T mcsz = (nbytes); \ if(mcsz <= 9*sizeof(mcsz)) { \ INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \ INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \ if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \ *mcdst++ = *mcsrc++; \ if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \ *mcdst++ = *mcsrc++; \ if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \ *mcdst++ = *mcsrc++; }}} \ *mcdst++ = *mcsrc++; \ *mcdst++ = *mcsrc++; \ *mcdst = *mcsrc ; \ } else memcpy(dest, src, mcsz); \} while(0)#else /* !USE_MEMCPY *//* Use Duff's device for good zeroing/copying performance. */#define MALLOC_ZERO(charp, nbytes) \do { \ INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \ long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \ if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \ switch (mctmp) { \ case 0: for(;;) { *mzp++ = 0; \ case 7: *mzp++ = 0; \ case 6: *mzp++ = 0; \ case 5: *mzp++ = 0; \ case 4: *mzp++ = 0; \ case 3: *mzp++ = 0; \ case 2: *mzp++ = 0; \ case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \ } \} while(0)#define MALLOC_COPY(dest,src,nbytes) \do { \ INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \ INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \ long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \ if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \ switch (mctmp) { \ case 0: for(;;) { *mcdst++ = *mcsrc++; \ case 7: *mcdst++ = *mcsrc++; \ case 6: *mcdst++ = *mcsrc++; \ case 5: *mcdst++ = *mcsrc++; \ case 4: *mcdst++ = *mcsrc++; \ case 3: *mcdst++ = *mcsrc++; \ case 2: *mcdst++ = *mcsrc++; \ case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \ } \} while(0)#endif/* Define HAVE_MMAP to optionally make malloc() use mmap() to allocate very large blocks. These will be returned to the operating system immediately after a free().*/#undef HAVE_MMAP /* Not available for ARMboot *//* 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.*/#undef HAVE_MREMAP /* Not available for ARMboot */#if HAVE_MMAP#include <unistd.h>#include <fcntl.h>#include <sys/mman.h>#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)#define MAP_ANONYMOUS MAP_ANON#endif#endif /* HAVE_MMAP *//* Access to system page size. To the extent possible, this malloc manages memory from the system in page-size units. The following mechanics for getpagesize were adapted from bsd/gnu getpagesize.h*/#define LACKS_UNISTD_H /* Shortcut for ARMboot */#define malloc_getpagesize 4096#ifndef LACKS_UNISTD_H# include <unistd.h>#endif#ifndef malloc_getpagesize# ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */# ifndef _SC_PAGE_SIZE# define _SC_PAGE_SIZE _SC_PAGESIZE# endif# endif# ifdef _SC_PAGE_SIZE# define malloc_getpagesize sysconf(_SC_PAGE_SIZE)# else# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE) extern size_t getpagesize();# define malloc_getpagesize getpagesize()# else# ifdef WIN32# define malloc_getpagesize (4096) /* TBD: Use 'GetSystemInfo' instead */# else# ifndef LACKS_SYS_PARAM_H# include <sys/param.h># endif# ifdef EXEC_PAGESIZE# define malloc_getpagesize EXEC_PAGESIZE# else# ifdef NBPG# ifndef CLSIZE# define malloc_getpagesize NBPG# else# define malloc_getpagesize (NBPG * CLSIZE)# endif# else# ifdef NBPC# define malloc_getpagesize NBPC# else# ifdef PAGESIZE# define malloc_getpagesize PAGESIZE# else# define malloc_getpagesize (4096) /* just guess */# endif# endif# endif# endif# endif# endif# endif#endif/* This version of malloc supports the standard SVID/XPG mallinfo routine that returns a struct containing the same kind of information you can get from malloc_stats. It should work on any SVID/XPG compliant system that has a /usr/include/malloc.h defining struct mallinfo. (If you'd like to install such a thing yourself, cut out the preliminary declarations as described above and below and save them in a malloc.h file. But there's no compelling reason to bother to do this.) The main declaration needed is the mallinfo struct that is returned (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a bunch of fields, most of which are not even meaningful in this version of malloc. Some of these fields are are instead filled by mallinfo() with other numbers that might possibly be of interest. HAVE_USR_INCLUDE_MALLOC_H should be set if you have a /usr/include/malloc.h file that includes a declaration of struct mallinfo. If so, it is included; else an SVID2/XPG2 compliant version is declared below. These must be precisely the same for mallinfo() to work.*//* #define HAVE_USR_INCLUDE_MALLOC_H */#if HAVE_USR_INCLUDE_MALLOC_H#include "/usr/include/malloc.h"#else/* SVID2/XPG mallinfo structure */struct mallinfo { int arena; /* total space allocated from system */ int ordblks; /* number of non-inuse chunks */ int smblks; /* unused -- always zero */ int hblks; /* number of mmapped regions */ int hblkhd; /* total space in mmapped regions */ int usmblks; /* unused -- always zero */ int fsmblks; /* unused -- always zero */ int uordblks; /* total allocated space */ int fordblks; /* total non-inuse space */ int keepcost; /* top-most, releasable (via malloc_trim) space */};/* SVID2/XPG mallopt options */#define M_MXFAST 1 /* UNUSED in this malloc */#define M_NLBLKS 2 /* UNUSED in this malloc */#define M_GRAIN 3 /* UNUSED in this malloc */#define M_KEEP 4 /* UNUSED in this malloc */#endif/* mallopt options that actually do something */#define M_TRIM_THRESHOLD -1#define M_TOP_PAD -2#define M_MMAP_THRESHOLD -3#define M_MMAP_MAX -4#ifndef DEFAULT_TRIM_THRESHOLD#define DEFAULT_TRIM_THRESHOLD (128 * 1024)#endif/* M_TRIM_THRESHOLD is the maximum amount of unused top-most memory to keep before releasing via malloc_trim in free().
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -