第42章 トップチャンク( Top Chunk )のマクロ

トップチャンクはメモリーの終端にあるチャンクです。

malloc.c(https://github.com/MacKomatsu/glibc/blob/release/2.27/master/malloc/malloc.c). 

1526 /*
1527    Top
1528
1529     The top-most available chunk (i.e., the one bordering the end of
1530     available memory) is treated specially. It is never included in
1531     any bin, is used only if no other chunk is available, and is
1532     released back to the system if it is very large (see
1533     M_TRIM_THRESHOLD).  Because top initially
1534     points to its own bin with initial zero size, thus forcing
1535     extension on the first malloc request, we avoid having any special
1536     code in malloc to check whether it even exists yet. But we still
1537     need to do so when getting memory from system, so we make
1538     initial_top treat the bin as a legal but unusable chunk during the
1539     interval between initialization and the first call to
1540     sysmalloc. (This is somewhat delicate, since it relies on
1541     the 2 preceding words to be zero during this interval as well.)
1542  */
1543
1544 /* Conveniently, the unsorted bin can be used as dummy top on first call */
1545 #define initial_top(M)              (unsorted_chunks (M))

このチャンクは他にフリーチャンクが無く、新規割り当てをする時に使います。

initial_top マクロはビンの初期化時に使われてます。

Copyright 2018-2019, by Masaki Komatsu