VPP代码阅读中文注解---dlmalloc.h

2023-11-06

* Quickstart

  This library is all in one file to simplify the most common usage:
  ftp it, compile it (-O3), and link it into another program. All of
  the compile-time options default to reasonable values for use on
  most platforms.  You might later want to step through various
  compile-time and dynamic tuning options.

  For convenience, an include file for code using this malloc is at:
     ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h
  You don't really need this .h file unless you call functions not
  defined in your system include files.  The .h file contains only the
  excerpts from this file needed for using this malloc on ANSI C/C++
  systems, so long as you haven't changed compile-time options about
  naming and tuning parameters.  If you do, then you can create your
  own malloc.h that does include all settings by cutting at the point
  indicated below. Note that you may already by default be using a C
  library containing a malloc that is based on some version of this
  malloc (for example in linux). You might still want to use the one
  in this file to customize settings or to avoid overheads associated
  with library versions.

这个库由1个单文件构成,编译时使用-O3优化等级。使用时需要与用户自己的代码进行链接。

一般情况下,不需要直接包含此头文件。系统中可能已经有地方包含了此头文件。

 

Supported pointer/size_t representation:       4 or 8 bytes
       size_t MUST be an unsigned type of the same width as
       pointers. (If you are using an ancient system that declares
       size_t as a signed type, or need it to be a different width
       than pointers, you can use a previous release of this malloc
       (e.g. 2.7.2) supporting these.)

size_t和和指针变量的长度必须保持一致。都是4字节或者8字节。如果你的系统不满足这个约束,那么本库需要回退到2.7.2以前的版本才支持。

 

Alignment:                                     8 bytes (minimum)
       This suffices for nearly all current machines and C compilers.
       However, you can define MALLOC_ALIGNMENT to be wider than this
       if necessary (up to 128bytes), at the expense of using more space.

可以定义64字节或者128字节对齐。128字节对齐会稍微浪费更多的空间

 

Minimum overhead per allocated chunk:   4 or  8 bytes (if 4byte sizes)
                                          8 or 16 bytes (if 8byte sizes)
       Each malloced chunk has a hidden word of overhead holding size
       and status information, and additional cross-check word
       if FOOTERS is defined.

每一次申请内存至少要比用户需求多消耗4字节~到16字节内存。用于维护此内存块的信息。

 

Minimum allocated size: 4-byte ptrs:  16 bytes    (including overhead)
                          8-byte ptrs:  32 bytes    (including overhead)

       Even a request for zero bytes (i.e., malloc(0)) returns a
       pointer to something of the minimum allocatable size.
       The maximum overhead wastage (i.e., number of extra bytes
       allocated than were requested in malloc) is less than or equal
       to the minimum size, except for requests >= mmap_threshold that
       are serviced via mmap(), where the worst case wastage is about
       32 bytes plus the remainder from a system page (the minimal
       mmap unit); typically 4096 or 8192 bytes.

最下的申请内存长度为16字节或者32字节(已包含内存块自身维护数据在内)

 

Security: static-safe; optionally more or less
       The "security" of malloc refers to the ability of malicious
       code to accentuate the effects of errors (for example, freeing
       space that is not currently malloc'ed or overwriting past the
       ends of chunks) in code that calls malloc.  This malloc
       guarantees not to modify any memory locations below the base of
       heap, i.e., static variables, even in the presence of usage
       errors.  The routines additionally detect most improper frees
       and reallocs.  All this holds as long as the static bookkeeping
       for malloc itself is not corrupted by some other means.  This
       is only one aspect of security -- these checks do not, and
       cannot, detect all possible programming errors.

       If FOOTERS is defined nonzero, then each allocated chunk
       carries an additional check word to verify that it was malloced
       from its space.  These check words are the same within each
       execution of a program using malloc, but differ across
       executions, so externally crafted fake chunks cannot be
       freed. This improves security by rejecting frees/reallocs that
       could corrupt heap memory, in addition to the checks preventing
       writes to statics that are always on.  This may further improve
       security at the expense of time and space overhead.  (Note that
       FOOTERS may also be worth using with MSPACES.)

       By default detected errors cause the program to abort (calling
       "abort()"). You can override this to instead proceed past
       errors by defining PROCEED_ON_ERROR.  In this case, a bad free
       has no effect, and a malloc that encounters a bad address
       caused by user overwrites will ignore the bad address by
       dropping pointers and indices to all known memory. This may
       be appropriate for programs that should continue if at all
       possible in the face of programming errors, although they may
       run out of memory because dropped memory is never reclaimed.

       If you don't like either of these options, you can define
       CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything
       else. And if if you are sure that your program using malloc has
       no errors or vulnerabilities, you can define INSECURE to 1,
       which might (or might not) provide a small performance improvement.

       It is also possible to limit the maximum total allocatable
       space, using malloc_set_footprint_limit. This is not
       designed as a security feature in itself (calls to set limits
       are not screened or privileged), but may be useful as one
       aspect of a secure implementation.

安全性考虑。

静态安全性,或多或少有一些。

如应对释放没有申请的内存,或者内存写越界这样的问题,称为安全性问题。

在本算法中,静态变量是不会被乱写的。内部会检测释放和重分配是否合法。这些检查无法避免所有的错误。

如果FOOTERS被定义了的话,则被分配的内存增加了一个额外的信息用来做校验,这些数据在一次内存分配后,下一次内存分配前不会修订。如果在释放的时候发现这个数据被改了,则拒绝释放,否则会破坏堆内存。

缺省情况下,如果malloc发现内存被篡改,则会推出进程(abort)。用户也可以自定义处理方式,让程序继续运行而不推出,但这样做,会导致后期内存逐渐耗尽,因为被篡改的内存不会回收。空闲内存会逐渐减少。

最后最好限制一下可以申请的内存空间的总大小。

Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero
       When USE_LOCKS is defined, each public call to malloc, free,
       etc is surrounded with a lock. By default, this uses a plain
       pthread mutex, win32 critical section, or a spin-lock if if
       available for the platform and not disabled by setting
       USE_SPIN_LOCKS=0.  However, if USE_RECURSIVE_LOCKS is defined,
       recursive versions are used instead (which are not required for
       base functionality but may be needed in layered extensions).
       Using a global lock is not especially fast, and can be a major
       bottleneck.  It is designed only to provide minimal protection
       in concurrent environments, and to provide a basis for
       extensions.  If you are using malloc in a concurrent program,
       consider instead using nedmalloc
       (http://www.nedprod.com/programs/portable/nedmalloc/) or
       ptmalloc (See http://www.malloc.de), which are derived from
       versions of this malloc.

在高并发环境下,考虑使用其他的malloc库而不是当前的这个库,因为这个库的锁的粒度比较粗,性能较低。

 

System requirements: Any combination of MORECORE and/or MMAP/MUNMAP
       This malloc can use unix sbrk or any emulation (invoked using
       the CALL_MORECORE macro) and/or mmap/munmap or any emulation
       (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system
       memory.  On most unix systems, it tends to work best if both
       MORECORE and MMAP are enabled.  On Win32, it uses emulations
       based on VirtualAlloc. It also uses common C library functions
       like memset.

  Compliance: I believe it is compliant with the Single Unix Specification
       (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably
       others as well.

系统需求和兼容性。仅依赖于2到3个系统调用或者等价体。兼容C标准和单一UNIX规范。

 

* Overview of algorithms

  This is not the fastest, most space-conserving, most portable, or
  most tunable malloc ever written. However it is among the fastest
  while also being among the most space-conserving, portable and
  tunable.  Consistent balance across these factors results in a good
  general-purpose allocator for malloc-intensive programs.

  In most ways, this malloc is a best-fit allocator. Generally, it
  chooses the best-fitting existing chunk for a request, with ties
  broken in approximately least-recently-used order. (This strategy
  normally maintains low fragmentation.) However, for requests less
  than 256bytes, it deviates from best-fit when there is not an
  exactly fitting available chunk by preferring to use space adjacent
  to that used for the previous small request, as well as by breaking
  ties in approximately most-recently-used order. (These enhance
  locality of series of small allocations.)  And for very large requests
  (>= 256Kb by default), it relies on system memory mapping
  facilities, if supported.  (This helps avoid carrying around and
  possibly fragmenting memory used only for large chunks.)

  All operations (except malloc_stats and mallinfo) have execution
  times that are bounded by a constant factor of the number of bits in
  a size_t, not counting any clearing in calloc or copying in realloc,
  or actions surrounding MORECORE and MMAP that have times
  proportional to the number of non-contiguous regions returned by
  system allocation routines, which is often just 1. In real-time
  applications, you can optionally suppress segment traversals using
  NO_SEGMENT_TRAVERSAL, which assures bounded execution even when
  system allocators return non-contiguous spaces, at the typical
  expense of carrying around more memory and increased fragmentation.

  The implementation is not very modular and seriously overuses
  macros. Perhaps someday all C compilers will do as good a job
  inlining modular code as can now be done by brute-force expansion,
  but now, enough of them seem not to.

  Some compilers issue a lot of warnings about code that is
  dead/unreachable only on some platforms, and also about intentional
  uses of negation on unsigned types. All known cases of each can be
  ignored.

  For a longer but out of date high-level description, see
     http://gee.cs.oswego.edu/dl/html/malloc.html

这是一个性价比较高的内存分配算法。

它尽量从最近刚释放的内存中分配,增加cache命中率

大多数函数的执行时间是常量时间。

* MSPACES
  If MSPACES is defined, then in addition to malloc, free, etc.,
  this file also defines mspace_malloc, mspace_free, etc. These
  are versions of malloc routines that take an "mspace" argument
  obtained using create_mspace, to control all internal bookkeeping.
  If ONLY_MSPACES is defined, only these versions are compiled.
  So if you would like to use this allocator for only some allocations,
  and your system malloc for others, you can compile with
  ONLY_MSPACES and then do something like...
    static mspace mymspace = create_mspace(0,0); // for example
    #define mymalloc(bytes)  mspace_malloc(mymspace, bytes)

  (Note: If you only need one instance of an mspace, you can instead
  use "USE_DL_PREFIX" to relabel the global malloc.)

  You can similarly create thread-local allocators by storing
  mspaces as thread-locals. For example:
    static __thread mspace tlms = 0;
    void*  tlmalloc(size_t bytes) {
      if (tlms == 0) tlms = create_mspace(0, 0);
      return mspace_malloc(tlms, bytes);
    }
    void  tlfree(void* mem) { mspace_free(tlms, mem); }

  Unless FOOTERS is defined, each mspace is completely independent.
  You cannot allocate from one and free to another (although
  conformance is only weakly checked, so usage errors are not always
  caught). If FOOTERS is defined, then each chunk carries around a tag
  indicating its originating mspace, and frees are directed to their
  originating spaces. Normally, this requires use of locks.

内存的申请和释放可以由多个mspace来进行管理

 

/* --- begin vpp customizations --- */

#if CLIB_DEBUG > 0
#define FOOTERS 1  /* extra debugging */
#define DLM_MAGIC_CONSTANT 0xdeaddabe
#endif
#define USE_LOCKS 1
#define DLM_ABORT {extern void os_panic(void); os_panic(); abort();}
#define ONLY_MSPACES 1

/* --- end vpp customizations --- */

在开启debug的情况下,每个内存块记录魔鬼数字,用于检查内存是否被写越界。

缺省的错误处理方法是使进程退出

/*
  malloc(size_t n)
  Returns a pointer to a newly allocated chunk of at least n bytes, or
  null if no space is available, in which case errno is set to ENOMEM
  on ANSI C systems.

  If n is zero, malloc returns a minimum-sized chunk. (The minimum
  size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
  systems.)  Note that size_t is an unsigned type, so calls with
  arguments that would be negative if signed 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.
*/
DLMALLOC_EXPORT void* dlmalloc(size_t);

申请给定大小的内存空间,返回内存空间的首地址。大小参数不要填成了负数,否则会当成正数解释,而去获取一个很大的内存,导致失败。

 

/*
  free(void* 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. If p was not malloced or already
  freed, free(p) will by default cause the current program to abort.
*/
DLMALLOC_EXPORT void  dlfree(void*);

释放之前申请的内存块。如果重复释放或者释放不存在的内存块,则进程一般情况下会退出。

 

/*
  calloc(size_t n_elements, size_t element_size);
  Returns a pointer to n_elements * element_size bytes, with all locations
  set to zero.
*/
DLMALLOC_EXPORT void* dlcalloc(size_t, size_t);

申请1个数组的内存,并初始化所有元素为0

 

/*
  realloc(void* 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 in most cases 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.  realloc with a size
  argument of zero (re)allocates a minimum-sized chunk.

  The old unix realloc convention of allowing the last-free'd chunk
  to be used as an argument to realloc is not supported.
*/
DLMALLOC_EXPORT void* dlrealloc(void*, size_t);

重新申请内存大小,即内存伸缩。可以简单理解成申请一块新内存,将旧内存的数据拷贝到新内存,释放旧内存。

 

/*
  realloc_in_place(void* p, size_t n)
  Resizes the space allocated for p to size n, only if this can be
  done without moving p (i.e., only if there is adjacent space
  available if n is greater than p's current allocated size, or n is
  less than or equal to p's size). This may be used instead of plain
  realloc if an alternative allocation strategy is needed upon failure
  to expand space; for example, reallocation of a buffer that must be
  memory-aligned or cleared. You can use realloc_in_place to trigger
  these alternatives only when needed.

  Returns p if successful; otherwise null.
*/
DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t);

与上一个函数语义相同,不同的地方是,直接在当前内存上进行伸缩,而不是重新申请内存,效率较高。但这个容易失败,特别是内存扩展的时候,如果旁边的内存块不是空闲的话,想一想?

 

/*
  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.
*/
DLMALLOC_EXPORT void* dlmemalign(size_t, size_t);

申请一个有字节对齐要求的内存块。

 

/*
  int posix_memalign(void** pp, size_t alignment, size_t n);
  Allocates a chunk of n bytes, aligned in accord with the alignment
  argument. Differs from memalign only in that it (1) assigns the
  allocated memory to *pp rather than returning it, (2) fails and
  returns EINVAL if the alignment is not a power of two (3) fails and
  returns ENOMEM if memory cannot be allocated.
*/
DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t);

与上一个函数类似,内存块由输出参数返回,函数本身返回错误代码。

/*
  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.
*/
DLMALLOC_EXPORT void* dlvalloc(size_t);

同memalign,  pagesize参数取系统默认值。

 

/*
  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.  To workaround the fact that mallopt is specified to use int,
  not size_t parameters, the value -1 is specially treated as the
  maximum unsigned size_t value.

  SVID/XPG/ANSI defines four standard param numbers for mallopt,
  normally defined in malloc.h.  None of these are use in this malloc,
  so setting them has no effect. But this malloc also supports 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_TRIM_THRESHOLD     -1   2*1024*1024   any   (-1 disables)
  M_GRANULARITY        -2     page size   any power of 2 >= page size
  M_MMAP_THRESHOLD     -3      256*1024   any   (or 0 if no MMAP support)
*/
DLMALLOC_EXPORT int dlmallopt(int, int);

设置malloc的全局参数,会影响malloc的行为。

 

/*
  malloc_footprint();
  Returns the number of bytes obtained from the system.  The total
  number of bytes allocated by malloc, realloc etc., is less than this
  value. Unlike mallinfo, this function returns only a precomputed
  result, so can be called frequently to monitor memory consumption.
  Even if locks are otherwise defined, this function does not use them,
  so results might not be up to date.
*/
DLMALLOC_EXPORT size_t dlmalloc_footprint(void);

当前malloc内存用量,这个统计不是实时的,有点延迟,大致准确。但是多线程安全。

/*
  malloc_max_footprint();
  Returns the maximum number of bytes obtained from the system. This
  value will be greater than current footprint if deallocated space
  has been reclaimed by the system. The peak number of bytes allocated
  by malloc, realloc etc., is less than this value. Unlike mallinfo,
  this function returns only a precomputed result, so can be called
  frequently to monitor memory consumption.  Even if locks are
  otherwise defined, this function does not use them, so results might
  not be up to date.
*/
DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void);

malloc用量的历史最大值

 

/*
  malloc_footprint_limit();
  Returns the number of bytes that the heap is allowed to obtain from
  the system, returning the last value returned by
  malloc_set_footprint_limit, or the maximum size_t value if
  never set. The returned value reflects a permission. There is no
  guarantee that this number of bytes can actually be obtained from
  the system.
*/
DLMALLOC_EXPORT size_t dlmalloc_footprint_limit();

malloc用量的理论极限值。

 

/*
  malloc_set_footprint_limit();
  Sets the maximum number of bytes to obtain from the system, causing
  failure returns from malloc and related functions upon attempts to
  exceed this value. The argument value may be subject to page
  rounding to an enforceable limit; this actual value is returned.
  Using an argument of the maximum possible size_t effectively
  disables checks. If the argument is less than or equal to the
  current malloc_footprint, then all future allocations that require
  additional system memory will fail. However, invocation cannot
  retroactively deallocate existing used memory.
*/
DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes);

设置malloc用量的理论极限值

 

/*
  malloc_inspect_all(void(*handler)(void *start,
                                    void *end,
                                    size_t used_bytes,
                                    void* callback_arg),
                      void* arg);
  Traverses the heap and calls the given handler for each managed
  region, skipping all bytes that are (or may be) used for bookkeeping
  purposes.  Traversal does not include include chunks that have been
  directly memory mapped. Each reported region begins at the start
  address, and continues up to but not including the end address.  The
  first used_bytes of the region contain allocated data. If
  used_bytes is zero, the region is unallocated. The handler is
  invoked with the given callback argument. If locks are defined, they
  are held during the entire traversal. It is a bad idea to invoke
  other malloc functions from within the handler.

  For example, to count the number of in-use chunks with size greater
  than 1000, you could write:
  static int count = 0;
  void count_chunks(void* start, void* end, size_t used, void* arg) {
    if (used >= 1000) ++count;
  }
  then:
    malloc_inspect_all(count_chunks, NULL);

  malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.
*/

遍历所有的malloc内存块,执行指定的函数。

 

/*
  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:    always zero.
  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:   always zero
  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.
*/
DLMALLOC_EXPORT struct dlmallinfo dlmallinfo(void);

获取malloc相关的统计信息

 

/*
  independent_calloc(size_t n_elements, size_t element_size, void* 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
  probably the most typical usage). If it is null, the returned array
  is itself dynamically allocated and should also be freed when it is
  no longer needed. Otherwise, the chunks array must be of at least
  n_elements in length. It is filled in with the pointers to the
  chunks.

  In either case, independent_calloc returns this pointer array, or
  null if the allocation failed.  If n_elements is zero and "chunks"
  is null, it returns a chunk representing an array with zero elements
  (which should be freed if not wanted).

  Each element must be freed when it is no longer needed. This can be
  done all at once using bulk_free.

  independent_calloc simplifies and speeds up implementations of many
  kinds of pools.  It may also be useful when constructing large data
  structures that initially have a fixed number of fixed-sized nodes,
  but the number is not known at compile time, and some of the nodes
  may later need to be freed. For example:

  struct Node { int item; struct Node* next; };

  struct Node* build_list() {
    struct Node** pool;
    int n = read_number_of_nodes_needed();
    if (n <= 0) return 0;
    pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
    if (pool == 0) die();
    // organize into a linked list...
    struct Node* first = pool[0];
    for (i = 0; i < n-1; ++i)
      pool[i]->next = pool[i+1];
    free(pool);     // Can now free the array (or not, if it is needed later)
    return first;
  }
*/
DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**);

申请数组内存,以指针数组的形式返回。实际上申请了2级内存。

/*
  independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);

  independent_comalloc allocates, all at once, a set of n_elements
  chunks with sizes indicated in the "sizes" array.    It returns
  an array of pointers to these elements, each of which 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). If it is null
  the returned array is itself dynamically allocated and should also
  be freed when it is no longer needed. Otherwise, the chunks array
  must be of at least n_elements in length. It is filled in with the
  pointers to the chunks.

  In either case, independent_comalloc returns this pointer array, or
  null if the allocation failed.  If n_elements is zero and chunks is
  null, it returns a chunk representing an array with zero elements
  (which should be freed if not wanted).

  Each element must be freed when it is no longer needed. This can be
  done all at once using bulk_free.

  independent_comallac differs from independent_calloc in that each
  element may have a different size, and also that it does not
  automatically clear elements.

  independent_comalloc can be used to speed up allocation in cases
  where several structs or objects must always be allocated at the
  same time.  For example:

  struct Head { ... }
  struct Foot { ... }

  void send_message(char* msg) {
    int msglen = strlen(msg);
    size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
    void* chunks[3];
    if (independent_comalloc(3, sizes, chunks) == 0)
      die();
    struct Head* head = (struct Head*)(chunks[0]);
    char*        body = (char*)(chunks[1]);
    struct Foot* foot = (struct Foot*)(chunks[2]);
    // ...
  }

  In general though, independent_comalloc is worth using only for
  larger values of n_elements. For small values, you probably won't
  detect enough difference from series of malloc calls to bother.

  Overuse of independent_comalloc can increase overall memory usage,
  since it cannot reuse existing noncontiguous small chunks that
  might be available for some of the elements.
*/
DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**);

独立的通用内存申请。比独立的指针数组二级内存申请更加灵活。

相当于指针数组中,每一个指针元素指向的内存大小可以不同。

 

/*
  bulk_free(void* array[], size_t n_elements)
  Frees and clears (sets to null) each non-null pointer in the given
  array.  This is likely to be faster than freeing them one-by-one.
  If footers are used, pointers that have been allocated in different
  mspaces are not freed or cleared, and the count of all such pointers
  is returned.  For large arrays of pointers with poor locality, it
  may be worthwhile to sort this array before calling bulk_free.
*/
DLMALLOC_EXPORT size_t  dlbulk_free(void**, size_t n_elements);

释放一组内存,这些内存由指针数组记录。

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VPP代码阅读中文注解---dlmalloc.h 的相关文章

  • docker swarm

    文章目录 1 docker swarm 常用命令 2 docker swarm 官网地址 3 初始化节点 3 1 初始化节点 3 2 生成 manager 或者 worker 令牌 3 3 查看 swarm 信息 3 4 查看节点信息 4
  • mysql 存储过程编写中踩的坑-----工作项目实战一

    如要转载请注明 https blog csdn net qwert789p article details 103627980 创作不易 且行且珍惜 1 项目前提 由于计费系统先上线 CRM项目后上线 前期计费系统有的建立客户账户 商品以及
  • dsadd user 命令详解

    From Internet 现在说下DSADD批量创建用户的方法 首先在使用DSADD之前先讲下LDAP协议 目录服务使用LDAP这个公用协议来查找和定位对象 LDAP可以描述对象在那个域 对象在那个OU 对象自己的名字 通常它的语法为 O
  • Appium自动化测试<三>

    本文接着Appium自动化测试 二 写 文章戳这里 夜神模拟器开启坐标 手机桌面 gt 工具 gt 设置 gt 开发者选项 gt 打开 显示点按操作反馈 指针位置 打开之后 我们鼠标滑动的时候就可以看到指定元素的位置坐标 一 Appium
  • chatgpt赋能python:Python:一门强大的编程语言

    Python 一门强大的编程语言 Python是一门面向对象 解释型的高级动态编程语言 广泛应用于Web开发 数据科学 人工智能 计算机网络等各个领域 在过去的10年中 Python经历了快速的发展 并成为了世界上最受欢迎的编程语言之一 P
  • 如何在Silverlight 控件中设置默认.xaml 页面

    每个Silverlight 项目可以有多个 xaml 文件 但每次只能看到一个 xaml 页面 当你在Visual Studio 中创建一个Silverlight 项目是 将创建一个默认的名字叫做 MainPage xaml 的 xaml
  • SolidWorks二次开发,工程图螺纹孔标注

  • Python使用tensorflow实现图像识别(猫狗大战)-01

    Python使用tensorflow实现图像识别 猫狗大战 01 import data py import tensorflow as tf import numpy as np import os 引入tensorflow numpy
  • 三种socket超时方法

    文章目录 概述 通过alarm定时器实现 通过select实现 通过SO RCVTIMEO和SO SNDTIMEO实现 概述 socket超时有三种处理办法 使用alarm定时器 设置一定的超时时间 通过产生SIGALRM中断信号实现soc
  • qt creator编译qt工程时报错:undefined reference to

    qt creator编译qt工程时报错 undefined reference to qt creator编译qt工程时报错 该错误指向了本地文件 且经过调研知道 该类错误一般都是链接问题 仔细查看一下pro和pri文件 发现SystemL
  • LeetCode 704. 二分查找 27. 移除元素

    文章目录 前言 LeetCode 704 二分查找 1 题目 2 思路 3 代码 LeetCode 27 移除元素 1 题目 2 思路 3 代码 总结 前言 为了巩固所学的知识 作者尝试着开始发布一些学习笔记类的博客 方便日后回顾 当然 如
  • 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案

    这是 Flutter 问题系列第 72 篇 如果觉得有用的话 欢迎关注专栏 Flutter SDK 2 5 0 Dart SDK 2 14 Android Studio 2021 2 1 Pathc 1 一 问题描述 项目中要自定义相机页
  • C++之仿函数

    最近再看STL源码的时候看到里面的实现用了大量的仿函数 然后上网搜集了一些关于仿函数的知识 仿函数 Functor 又称为函数对象 Function Object 是一个能行使函数功能的类 仿函数的语法几乎和我们普通的函数调用一样 不过作为
  • 网络编程学习:UDP发送消息new String(packet.getData(),0,packet.getLength())报错

    最近鄙人在使用IDEA学习网络编程的UDP消息发送时 遇到了以下的报错提醒 在使用IDEA时 为了方便我们习惯性的设置了自动导包 com sun org apache xpath internal operations String 和 j

随机推荐

  • Swift的二分法查找实践

    Swift的二分法查找实践 Swift的二分法查找实践 在这篇教程中我们会使用计算机科学里一个基础的算法 二分法查找 binary search 我们也会实现一个二分法查找的项目 快速自动匹配 介绍 在一个数组里查找某个元素 最直接的方法是
  • Cocos Shader实现HSL和RGB颜色模型

    HSL和RGB是两种常见的颜色模型 被广泛应用于计算机图形学 数字图像处理 Web设计等领域 在进行颜色处理时 经常需要将HSL和RGB相互转换 本demo演示如何通过Cocos Creator 3 7游戏引擎 用HSL模型来调节图片颜色
  • PDU会话建立流程(8)-SM Policy Association的建立

    相关文章会在公众号同步更新 公众号 5G通信大家学 持续更新的相关5G内容都是直接根据3GPP整理 保证更新内容的准确性 避免通过二手 甚至多手的资料 以讹传讹误导网友 在介绍完流程详解后 会整理专题内容 比如切片 服务发现 QoS流端到端
  • JPBM4.4基础及数据库说明

    JPBM4 4基础及数据库说明 对jBPM4 4数据库的几张表简单介绍 A 资源库和运行时表结构 JBPM4 DEPLOYMENT 流程定义表 JBPM4 DEPLOYPROP 流程定义属性表 JBPM4 EXECUTION 流程实例表 主
  • nacos--基础--2.5--部署--集群--传统

    nacos 基础 2 5 部署 集群 传统 前提 使用hd用户登陆 完成基础环境搭建 https blog csdn net zhou920786312 article details 118212302 1 机器 IP 端口 备注 192
  • 云计算是什么

    博客主页 微笑的段嘉许博客主页 欢迎关注 点赞 收藏 留言 本文由微笑的段嘉许原创 51CTO首发时间 2023年2月1日 坚持和努力一定能换来诗与远方 作者水平很有限 如果发现错误 一定要及时告知作者哦 感谢感谢 不要偷走我小火车哦 文章
  • text11

    word 剪切板 ul li span style font family none background color rgb 255 255 255 span li li span style font family none backg
  • 比较编译技术--Java 代码的静态编译和动态编译中的问题

    本文是关于实时 Java 的 系列文章 共 5 部分 的第二篇 考察了 Java 语言的本地代码编译所涉及的一些问题 单独使用动态 即时 编译或静态 提前 编译都不能满足所有 Java 应用程序的需求 作者在各种执行环境中对这两种编译技术进
  • Linux命令_awk & 字符串过滤

    前言 被称为 Linux 文本操作三剑客之一的 awk 不但可以处理复杂文件输出 更是一门编程语言 支持对文本每行每列精确读出 格式化输出 正则匹配 if 判断 数组 循环 目录 1 语法 1 1 选项参数 1 2 内置变量 1 3 运算符
  • Integer.parseInt(s)与Integer.valueOf(s)的区别详解

    一 Integer parseInt s 用法 String s1 1000 String s2 1000 int n1 Integer parseInt s1 int n2 Integer parseInt s2 if n1 n2 Sys
  • Pycharm一复制就出现光标变成黑块,无法编辑

    光标变黑块是因为输入方式变成了改写方式 INSERT可以切换插入 改写模式 大多笔记本上是INS键 在键盘上 外接键盘一般是Insert键 但有时光按insert也不行 试试下面这个方法 file gt settings gt Plugin
  • 博客记录

    文章目录 一 3d点云 1 激光SLAM 1 从零开始做自动驾驶定位 2 点云匹配定位 1 NDT 二 编程 1 CMakeLists 三 Github 1 自动驾驶相关 四 Markdown 1 emoji 用来记录一下自己在学习的过程中
  • linux下文件的访问时间、修改时间和改变时间

    参考 linux下文件的创建时间 访问时间 修改时间和改变时间 刀刀的专栏 博客频道 CSDN NET http blog csdn net zyz511919766 article details 14452027 归纳一下 linux下
  • MyBatis动态拼接SQL

    通过使用MyBatis提供的标签方法可以实现动态SQL拼接 1 if标签
  • oracle时间获取记录

    select trunc sysdate from dual 2017 9 11 select trunc sysdate 1 from dual 2017 9 12 select sysdate from dual 从系统获取时间2017
  • VsCode怎么打开settings.json文件?

    Mac command shift p 输入setting json 选择 首选项 打开设置 json 即可
  • 使用Git进行版本控制,并将代码托管到GitHub的完整流程

    Git是一个分布式版本控制系统 它可以记录文件的修改历史 并且可以管理多人协作开发的项目 Git的基本工作流程如下 在开发者的本地电脑上安装Git 并克隆一个GitHub上已存在的项目到本地 在本地进行代码的修改和提交 这些修改都会被存储在
  • Android studio编译中Connect to maven.google.com:443[xxx] failed: connect timed out的解决方法

    前言 最近徒弟分到了一个rn的模块开发工作 但rn 大家懂的 Android studio有时需要科学上网下载必要的rn库 又是一个徒弟哀嚎的日子 问题 连接maven google com超时 具体编译报错如下 gt Could not
  • R语言-- R语言数据类型(3)

    列表 创建 1 1 创建一个列表L1 记录三位同学的姓名 A B C 编号 1 2 3 数学成绩 80 90 95 语文成绩 75 95 80 L1 lt list 姓名 c A B C 编号 c 1 2 3 数学 c 80 90 95 语
  • VPP代码阅读中文注解---dlmalloc.h

    Quickstart This library is all in one file to simplify the most common usage ftp it compile it O3 and link it into anoth