Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptmalloc中BINS的数量 #622

Open
zh-explorer opened this issue Nov 26, 2019 · 1 comment
Open

ptmalloc中BINS的数量 #622

zh-explorer opened this issue Nov 26, 2019 · 1 comment

Comments

@zh-explorer
Copy link

不懂就问Orz。
ptmalloc的malloc_state结果体的bins字段一共声明了127个bin。其中第一个为unsortbin,后面62个为normal bin。然后是63个large bin。那么第127个bin干什么用去了Orz。
另外,按照注释应该存在第64个larger bin用来放超过范围的其他所有chunk

Bins for sizes < 512 bytes contain chunks of all the same size, spaced
--
8 bytes apart. Larger bins are approximately logarithmically spaced:
 
64 bins of size       8
32 bins of size      64
16 bins of size     512
8 bins of size    4096
4 bins of size   32768
2 bins of size  262144
1 bin  of size what's left

但是在largebin_index 宏中超过大小的所有chunk放在第126个bin,也就是第63个largebin中。

#define largebin_index_64(sz)                                                \
(((((unsigned long) (sz)) >> 6) <= 48) ?  48 + (((unsigned long) (sz)) >> 6) :\
((((unsigned long) (sz)) >> 9) <= 20) ?  91 + (((unsigned long) (sz)) >> 9) :\
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
126)
 
#define largebin_index(sz) \
(SIZE_SZ == 8 ? largebin_index_64 (sz)                                     \
: MALLOC_ALIGNMENT == 16 ? largebin_index_32_big (sz)                     \
: largebin_index_32 (sz))

存在什么说法或者我算的有问题么?

@iromise
Copy link
Member

iromise commented Nov 27, 2019

可能是因为这个?就是可能会对 smallbin 的下标进行调整。

#define SMALLBIN_WIDTH MALLOC_ALIGNMENT
// 是否需要对small bin的下标进行纠正
#define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ)

#define MIN_LARGE_SIZE ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH)
//判断chunk的大小是否在small bin范围内
#define in_smallbin_range(sz)                                                  \
    ((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE)
// 根据chunk的大小得到small bin对应的索引。
#define smallbin_index(sz)                                                     \
    ((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4)                          \
                           : (((unsigned) (sz)) >> 3)) +                       \
     SMALLBIN_CORRECTION)

没仔细看,可能会有问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants