⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 malloc.c

📁 MPI stands for the Message Passing Interface. Written by the MPI Forum (a large committee comprising
💻 C
📖 第 1 页 / 共 5 页
字号:
#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/*  The system page size. To the extent possible, this malloc manages  memory from the system in page-size units.  Note that this value is  cached during initialization into a field of malloc_state. So even  if malloc_getpagesize is a function, it is only called once.  The following mechanics for getpagesize were adapted from bsd/gnu  getpagesize.h. If none of the system-probes here apply, a value of  4096 is used, which should be OK: If they don't apply, then using  the actual value probably doesn't impact performance.*/#ifndef malloc_getpagesize#ifndef LACKS_UNISTD_H#  include <unistd.h>#endif#  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 /* use supplied emulation of getpagesize */#        define malloc_getpagesize getpagesize()#      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 /* just guess */#                define malloc_getpagesize (4096)#              endif#            endif#          endif#        endif#      endif#    endif#  endif#endif/*  This version of malloc supports the standard SVID/XPG mallinfo  routine that returns a struct containing usage properties and  statistics. 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 that are not even meaningful in this version of  malloc.  These fields are are instead filled by mallinfo() with  other numbers that might 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.  The original SVID version of this struct,  defined on most systems with mallinfo, declares all fields as  ints. But some others define as unsigned long. If your system  defines the fields using a type of different width than listed here,  you must #include your system version and #define  HAVE_USR_INCLUDE_MALLOC_H.*//* #define HAVE_USR_INCLUDE_MALLOC_H */#ifdef HAVE_USR_INCLUDE_MALLOC_H#include "/usr/include/malloc.h"#endif/* ---------- description of public routines ------------ *//*  malloc(size_t n)  Returns a pointer to a newly allocated chunk of at least n bytes, or null  if no space is available. Additionally, on failure, errno is  set to ENOMEM on ANSI C systems.  If n is zero, malloc returns a minumum-sized chunk. (The minimum  size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit  systems.)  On most systems, size_t is an unsigned type, so calls  with negative arguments are interpreted as requests for huge amounts  of space, which will often fail. The maximum supported value of n  differs across systems, but is in all cases less than the maximum  representable value of a size_t.*/#if __STD_CVoid_t*  public_mALLOc(size_t);#elseVoid_t*  public_mALLOc();#endif#ifdef libc_hidden_protolibc_hidden_proto (public_mALLOc)#endif/*  free(Void_t* p)  Releases the chunk of memory pointed to by p, that had been previously  allocated using malloc or a related routine such as realloc.  It has no effect if p is null. It can have arbitrary (i.e., bad!)  effects if p has already been freed.  Unless disabled (using mallopt), freeing very large spaces will  when possible, automatically trigger operations that give  back unused memory to the system, thus reducing program footprint.*/#if __STD_Cvoid     public_fREe(Void_t*);#elsevoid     public_fREe();#endif#ifdef libc_hidden_protolibc_hidden_proto (public_fREe)#endif/*  calloc(size_t n_elements, size_t element_size);  Returns a pointer to n_elements * element_size bytes, with all locations  set to zero.*/#if __STD_CVoid_t*  public_cALLOc(size_t, size_t);#elseVoid_t*  public_cALLOc();#endif/*  realloc(Void_t* p, size_t n)  Returns a pointer to a chunk of size n that contains the same data  as does chunk p up to the minimum of (n, p's size) bytes, or null  if no space is available.  The returned pointer may or may not be the same as p. The algorithm  prefers extending p when possible, otherwise it employs the  equivalent of a malloc-copy-free sequence.  If p is null, realloc is equivalent to malloc.  If space is not available, realloc returns null, errno is set (if on  ANSI) and p is NOT freed.  if n is for fewer bytes than already held by p, the newly unused  space is lopped off and freed if possible.  Unless the #define  REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of  zero (re)allocates a minimum-sized chunk.  Large chunks that were internally obtained via mmap will always  be reallocated using malloc-copy-free sequences unless  the system supports MREMAP (currently only linux).  The old unix realloc convention of allowing the last-free'd chunk  to be used as an argument to realloc is not supported.*/#if __STD_CVoid_t*  public_rEALLOc(Void_t*, size_t);#elseVoid_t*  public_rEALLOc();#endif#ifdef libc_hidden_protolibc_hidden_proto (public_rEALLOc)#endif/*  memalign(size_t alignment, size_t n);  Returns a pointer to a newly allocated chunk of n bytes, aligned  in accord with the alignment argument.  The alignment argument should be a power of two. If the argument is  not a power of two, the nearest greater power is used.  8-byte alignment is guaranteed by normal malloc calls, so don't  bother calling memalign with an argument of 8 or less.  Overreliance on memalign is a sure way to fragment space.*/#if __STD_CVoid_t*  public_mEMALIGn(size_t, size_t);#elseVoid_t*  public_mEMALIGn();#endif#ifdef libc_hidden_protolibc_hidden_proto (public_mEMALIGn)#endif/*  valloc(size_t n);  Equivalent to memalign(pagesize, n), where pagesize is the page  size of the system. If the pagesize is unknown, 4096 is used.*/#if __STD_CVoid_t*  public_vALLOc(size_t);#elseVoid_t*  public_vALLOc();#endif/*  mallopt(int parameter_number, int parameter_value)  Sets tunable parameters The format is to provide a  (parameter-number, parameter-value) pair.  mallopt then sets the  corresponding parameter to the argument value if it can (i.e., so  long as the value is meaningful), and returns 1 if successful else  0.  SVID/XPG/ANSI defines four standard param numbers for mallopt,  normally defined in malloc.h.  Only one of these (M_MXFAST) is used  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,  so setting them has no effect. But this malloc also supports four  other options in mallopt. See below for details.  Briefly, supported  parameters are as follows (listed defaults are for "typical"  configurations).  Symbol            param #   default    allowed param values  M_MXFAST          1         64         0-80  (0 disables fastbins)  M_TRIM_THRESHOLD -1         128*1024   any   (-1U disables trimming)  M_TOP_PAD        -2         0          any  M_MMAP_THRESHOLD -3         128*1024   any   (or 0 if no MMAP support)  M_MMAP_MAX       -4         65536      any   (0 disables use of mmap)*/#if __STD_Cint      public_mALLOPt(int, int);#elseint      public_mALLOPt();#endif/*  mallinfo()  Returns (by copy) a struct containing various summary statistics:  arena:     current total non-mmapped bytes allocated from system  ordblks:   the number of free chunks  smblks:    the number of fastbin blocks (i.e., small chunks that               have been freed but not use resused or consolidated)  hblks:     current number of mmapped regions  hblkhd:    total bytes held in mmapped regions  usmblks:   the maximum total allocated space. This will be greater                than current total if trimming has occurred.  fsmblks:   total bytes held in fastbin blocks  uordblks:  current total allocated space (normal or mmapped)  fordblks:  total free space  keepcost:  the maximum number of bytes that could ideally be released               back to system via malloc_trim. ("ideally" means that               it ignores page restrictions etc.)  Because these fields are ints, but internal bookkeeping may  be kept as longs, the reported values may wrap around zero and  thus be inaccurate.*/#if __STD_Cstruct mallinfo public_mALLINFo(void);#elsestruct mallinfo public_mALLINFo();#endif/*  independent_calloc(size_t n_elements, size_t element_size, Void_t* chunks[]);  independent_calloc is similar to calloc, but instead of returning a  single cleared space, it returns an array of pointers to n_elements  independent elements that can hold contents of size elem_size, each  of which starts out cleared, and can be independently freed,  realloc'ed etc. The elements are guaranteed to be adjacently  allocated (this is not guaranteed to occur with multiple callocs or  mallocs), which may also improve cache locality in some  applications.  The "chunks" argument is optional (i.e., may be null, which is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -