記錄一下,看源碼的時(shí)候也不用到處翻了
- bin_at
#define bin_at(m, i) \
(mbinptr) (((char *) &((m)->bins[((i) - 1) * 2])) \
- offsetof (struct malloc_chunk, fd))
宏 bin_at(m, i) 通過(guò) bin index 獲得 bin 的鏈表頭,m 指的是分配區(qū),i 是索引
- unsorted_chunks
#define unsorted_chunks(M) (bin_at (M, 1))
由于 unsorted_chunks 是第一個(gè) bin 所以 索引始終為 1
- binmap
#define NBINS 128
#define BINMAPSHIFT 5
#define BITSPERMAP (1U << BINMAPSHIFT) // 32
#define BINMAPSIZE (NBINS / BITSPERMAP)// 128 / 32 = 4
unsigned int binmap[BINMAPSIZE]; // 32b * 4 = 128b
一共有 128 個(gè) bin,0 和 127 不算,也就是有 126 個(gè) bin,其中第一個(gè) bin 是 unsorted_bin
binmap 字段是一個(gè) int 數(shù)組,ptmalloc 用一個(gè) bit 來(lái)標(biāo)識(shí)該 bit 對(duì)應(yīng)的 bin 中是否包含空閑 chunk
- mark_bin
#define mark_bin(m, i) ((m)->binmap[idx2block (i)] |= idx2bit (i))
mark_bin 設(shè)置第 i 個(gè) bin 在 binmap 中對(duì)應(yīng)的 bit 位為 1
- last
#define last(b) ((b)->fd)
之前沒(méi)看,以為是最后一個(gè) bin
- __glibc_unlikely
# define __glibc_unlikely(cond) __builtin_expect ((cond), 0)
與 glibc 不像的。。。條件。如果 cond 為 1, 返回 1,否則返回 0
- in_smallbin_range
#define in_smallbin_range(sz) \
((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE)
在 SIZE_SZ 為 4B 的平臺(tái)上,small bins 中的 chunk 大小是以 8B 為公差的等差數(shù)列,最大的 chunk 大小為 504B,最小的 chunk 大小為 16B,所以實(shí)際共 62 個(gè) bin。分別為 16B、 24B、32B,??,504B。在 SIZE_SZ 為 8B 的平臺(tái)上,small bins 中的 chunk 大小是以 16B 為公差的等差數(shù)列,最大的 chunk 大小為 1008B,最小的 chunk 大小為 32B,所以實(shí)際共 62 個(gè) bin。分別為 32B、48B、64B,??,1008B。
- idx2block(i)
#define idx2block(i) ((i) >> BINMAPSHIFT)
可以使用宏 idx2block 計(jì)算出該 bin 在 binmap 對(duì)應(yīng)的 bit 屬于哪個(gè) block。idx2bit 宏取第 i 位為 1,其它位都為 0 的掩碼,舉個(gè)例子:idx2bit(3) 為 “0000 1000”(只顯示 8 位)。
- malloc_consolidate
這個(gè)函數(shù)非常 diao 了
沒(méi)有分析過(guò)他,改天分析。。。書(shū)上說(shuō),這個(gè)函數(shù)的作用主要是合并 fastbin 中的 bin,將這些空閑 chunk 加入 unsorted bin 中。
- chunk_main_arena
#define NON_MAIN_ARENA 0x4
#define chunk_main_arena(p) (((p)->mchunk_size & NON_MAIN_ARENA) == 0)
如果 chunk size 的第三位是 1,,也就是說(shuō)這個(gè) chunk 不是主分配區(qū)。
- get_max_fast
#define set_max_fast(s) \
global_max_fast = (((s) == 0) \
? SMALLBIN_WIDTH : ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK))
#define get_max_fast() global_max_fast
看到上面有一個(gè) set_max_fast,有這樣一段代碼:
set_max_fast (DEFAULT_MXFAST);
其中:
DEFAULT_MXFAST 64 (for 32bit), 128 (for 64bit)
所以一般情況下 get_max_fast() 64 (for 32bit), 128 (for 64bit)
- arena_get(ptr, size)
#define arena_get(ptr, size) do { \
ptr = thread_arena; \
arena_lock (ptr, size); \
} while (0)
得到這個(gè)線程的分配區(qū),并且給這個(gè)分配區(qū)上鎖
- smallbin_index
#define smallbin_index(sz) \
((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4) : (((unsigned) (sz)) >> 3))\
+ SMALLBIN_CORRECTION)
- fastbin
#define fastbin(ar_ptr, idx) ((ar_ptr)->fastbinsY[idx])
返回 fastbin 地址
- chunk_at_offset
#define chunk_at_offset(p, s) ((mchunkptr) (((char *) (p)) + (s)))
當(dāng)前 fastbin 的地址 + 自己的 size 就能得到下一個(gè) chunk 的地址
- clear_fastchunks
#define clear_fastchunks(M) catomic_or (&(M)->flags, FASTCHUNKS_BIT)
清除有 fastbin chunk 的標(biāo)志
- atomic_exchange_acq
讀取第一個(gè)參數(shù)指針?biāo)傅闹?/p>
- check_inuse_chunk
# define check_inuse_chunk(A, P)
這個(gè)就是 define 了一下其實(shí)什么都沒(méi)干