ptmalloc heap_free
arena.c
static int heap_trim(heap_info *heap, size_t pad);函数有一段没读懂
prev_size = prev_heap->size - (MINSIZE - 2 * SIZE_SZ);
后面才看《Glibc 内存管理Ptmalloc2 源代码分析》才发现没理解top_chunk
-------------------------------------------------------------------------
fencepost chunk1 size:(MINSIZE - 2 * SIZE_SZ)
-------------------------------------------------------------------------
fencepost chunk2 size:2 * SIZE_SZ
---------------------------------------------------------------------------
mem chunk
----------------------------------------------------------------------------
top chunk最后两个chunk为 fencepost,总大小是MINSIZE。
#define NBINS 128
#define NSMALLBINS 64
#define SMALLBIN_WIDTH MALLOC_ALIGNMENT
#define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ)
#define MIN_LARGE_SIZE ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH)
第一反应 SMALLBIN_CORRECTION为true/false,用在min_large_size:NSMALLBINS - SMALLBIN_CORRECTION开始没看懂,后面才反应过来true/false可以用1/0表示。
MIN_LARGE_SIZE简化为NSMALLBINS*SMALLBIN_WIDTH,64位系统为64*0x10,值为0x400。(SMALLBIN_CORRECTION通常为0)