Understanding glibc malloc
日志:
[2019-10-10] 經(jīng)評論 @kwdecsdn 提醒,新增對「Unsorted Bin 中的 chunks 何時移至 small/large chunk 中」的補充解釋。
[2019-02-06] 勘誤與代碼著色優(yōu)化;
[2018-05-22] 內(nèi)容優(yōu)化與排版優(yōu)化;
[2017-03-17] 優(yōu)化排版.
譯者言:
[2018-05-22] 在寫完這篇博客之后,我抽空將 glibc malloc 的源碼閱讀了一遍,并參與編撰了一篇有關(guān)分配器的綜述文獻1,最后我動手實現(xiàn)了自己的分配器。當(dāng)然,這都是 17 年暑期之前的工作了。一年后的今天,我打開這篇藏在記憶角落里的文章,看著它驚人的點擊量,我覺得我有必要認真地校準一下本文,從而盡量為大家提供一篇內(nèi)容正確、閱讀舒適的博文,這樣才對得起大家的厚望。在修訂過程中,為了避免令人尷尬的翻譯腔,我會盡量意譯與技術(shù)無關(guān)的文本,希望大家喜歡!
[2016-07-21] 本篇文章主要完成了「Understanding glibc malloc」的翻譯工作。限于本人翻譯水平與專業(yè)技術(shù)水平(純粹為了了解內(nèi)存分配而翻),本文章必定會有很多不足之處,請大家見諒,也歡迎大家的指正!
文章目錄
前言
堆內(nèi)存(Heap Memory)是一個很有意思的領(lǐng)域。你可能和我一樣,也困惑于下述問題很久了:
最近,我終于有時間去深入了解這些問題。下面就讓我來談?wù)勎业恼{(diào)研成果。
開源社區(qū)公開了很多現(xiàn)成的內(nèi)存分配器(Memory Allocators,以下簡稱為分配器):
dlmalloc – 第一個被廣泛使用的通用動態(tài)內(nèi)存分配器;
ptmalloc2 – glibc 內(nèi)置分配器的原型;
jemalloc – FreeBSD & Firefox 所用分配器;
tcmalloc – Google 貢獻的分配器;
libumem – Solaris 所用分配器;
…
每一種分配器都宣稱自己快(fast)、可拓展(scalable)、效率高(memory efficient)!但是并非所有的分配器都適用于我們的應(yīng)用。內(nèi)存吞吐量大(memory hungry)的應(yīng)用程序,其性能很大程度上取決于分配器的性能。
在這篇文章中,我只談「glibc malloc」分配器。為了方便大家理解「glibc malloc」,我會聯(lián)系最新的源代碼。
歷史:ptmalloc2 基于 dlmalloc 開發(fā),其引入了多線程支持,于 2006 年發(fā)布。發(fā)布之后,ptmalloc2 整合進了 glibc 源碼,此后其所有修改都直接提交到了 glibc malloc 里。因此,ptmalloc2 的源碼和 glibc malloc 的源碼有很多不一致的地方。(譯者注:1996 年出現(xiàn)的 dlmalloc 只有一個主分配區(qū),該分配區(qū)為所有線程所爭用,1997 年發(fā)布的 ptmalloc 在 dlmalloc 的基礎(chǔ)上引入了非主分配區(qū)的概念。)
1. 申請堆的系統(tǒng)調(diào)用
我在之前的文章中提到過,malloc
內(nèi)部通過 brk
或 mmap
系統(tǒng)調(diào)用向內(nèi)核申請堆區(qū)。
譯者注:在內(nèi)存管理領(lǐng)域,我們一般用「堆」指代用于分配動態(tài)內(nèi)存的虛擬地址空間,而用「棧」指代用于分配靜態(tài)內(nèi)存的虛擬地址空間。具體到虛擬內(nèi)存布局(Memory Layout),堆維護在通過 brk
系統(tǒng)調(diào)用申請的「Heap」及通過 mmap
系統(tǒng)調(diào)用申請的「Memory Mapping Segment」中;而棧維護在通過匯編棧指令動態(tài)調(diào)整的「Stack」中。在 Glibc 里,「Heap」用于分配較小的內(nèi)存及主線程使用的內(nèi)存。
下圖為 Linux 內(nèi)核 v2.6.7 之后,32 位模式下的虛擬內(nèi)存布局方式。

2. 多線程支持
Linux 的早期版本采用 dlmalloc 作為它的默認分配器,但是因為 ptmalloc2 提供了多線程支持,所以 后來 Linux 就轉(zhuǎn)而采用 ptmalloc2 了。多線程支持可以提升分配器的性能,進而間接提升應(yīng)用的性能。
在 dlmalloc 中,當(dāng)兩個線程同時 malloc
時,只有一個線程能夠訪問臨界區(qū)(critical section)——這是因為所有線程共享用以緩存已釋放內(nèi)存的「空閑列表數(shù)據(jù)結(jié)構(gòu)」(freelist data structure),所以使用 dlmalloc 的多線程應(yīng)用會在 malloc
上耗費過多時間,從而導(dǎo)致整個應(yīng)用性能的下降。
在 ptmalloc2 中,當(dāng)兩個線程同時調(diào)用 malloc
時,內(nèi)存均會得以立即分配——每個線程都維護著單獨的堆,各個堆被獨立的空閑列表數(shù)據(jù)結(jié)構(gòu)管理,因此各個線程可以并發(fā)地從空閑列表數(shù)據(jù)結(jié)構(gòu)中申請內(nèi)存。這種為每個線程維護獨立堆與空閑列表數(shù)據(jù)結(jié)構(gòu)的行為就「per thread arena」。
2.1. 案例代碼
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
void* threadFunc(void* arg) {
printf("Before malloc in thread 1\n");
getchar();
char* addr = (char*) malloc(1000);
printf("After malloc and before free in thread 1\n");
getchar();
free(addr);
printf("After free in thread 1\n");
getchar();
}
int main() {
pthread_t t1;
void* s;
int ret;
char* addr;
printf("Welcome to per thread arena example::%d\n",getpid());
printf("Before malloc in main thread\n");
getchar();
addr = (char*) malloc(1000);
printf("After malloc and before free in main thread\n");
getchar();
free(addr);
printf("After free in main thread\n");
getchar();
ret = pthread_create(&t1, NULL, threadFunc, NULL);
if(ret)
{
printf("Thread creation error\n");
return -1;
}
ret = pthread_join(t1, &s);
if(ret)
{
printf("Thread join error\n");
return -1;
}
return 0;
}
2.2. 案例輸出
2.2.1. 在主線程 malloc 之前
從如下的輸出結(jié)果中我們可以看到,這里還沒有堆段也沒有每個線程的棧,因為 thread1 還沒有創(chuàng)建!
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
b7e05000-b7e07000 rw-p 00000000 00:00 0
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
2.2.2. 在主線程 malloc 之后
從如下的輸出結(jié)果中我們可以看到,堆段已經(jīng)產(chǎn)生,并且其地址區(qū)間正好在數(shù)據(jù)段(0x0804b000 - 0x0806c000)上面,這表明堆內(nèi)存是移動「Program Break」的位置產(chǎn)生的(也即通過 brk
中斷)。此外,請注意,盡管用戶只申請了 1000 字節(jié)的內(nèi)存,但是實際產(chǎn)生了 132KB 的堆。這個連續(xù)的堆區(qū)域被稱為「arena」。因為這個 arena 是被主線程建立的,因此其被稱為「main arena」。接下來的申請會繼續(xù)分配這個 arena 的 132KB 中剩余的部分。當(dāng)分配完畢時,它可以通過繼續(xù)移動 Program Break 的位置擴容。擴容后,「top chunk」的大小也隨之調(diào)整,以將這塊新增的空間圈進去;相應(yīng)地,arena 也可以在 top chunk 過大時縮小。
注意:top chunk 是一個 arena 位于最頂層的 chunk。有關(guān) top chunk 的更多信息詳見后續(xù)章節(jié)「top chunk」部分。
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
...
sploitfun@sploitfun-VirtualBox:~/lsploits/hof/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804b000-0806c000 rw-p 00000000 00:00 0 [heap]
b7e05000-b7e07000 rw-p 00000000 00:00 0
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
2.2.3. 在主線程 free 之后
從如下的輸出結(jié)果中我們可以看到,當(dāng)分配的內(nèi)存區(qū)域 free
掉時,其并不會立即歸還給操作系統(tǒng),而僅僅是移交給了作為庫函數(shù)的分配器。這塊 free
掉的內(nèi)存添加在了「main arenas bin」中(在 glibc malloc 中,空閑列表數(shù)據(jù)結(jié)構(gòu)被稱為「bin」)。隨后當(dāng)用戶請求內(nèi)存時,分配器就不再向內(nèi)核申請新堆了,而是先試著各個「bin」中查找空閑內(nèi)存。只有當(dāng) bin 中不存在空閑內(nèi)存時,分配器才會繼續(xù)向內(nèi)核申請內(nèi)存。
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
After free in main thread
...
sploitfun@sploitfun-VirtualBox:~/lsploits/hof/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804b000-0806c000 rw-p 00000000 00:00 0 [heap]
b7e05000-b7e07000 rw-p 00000000 00:00 0
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
2.2.4. 在 thread1 malloc 之前
從如下的輸出結(jié)果中我們可以看到,此時 thread1 的堆尚不存在,但其棧已產(chǎn)生。
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
After free in main thread
Before malloc in thread 1
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804b000-0806c000 rw-p 00000000 00:00 0 [heap]
b7604000-b7605000 ---p 00000000 00:00 0
b7605000-b7e07000 rw-p 00000000 00:00 0 [stack:6594]
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
2.2.5. 在 thread1 malloc 之后
從如下的輸出結(jié)果中我們可以看到,thread1 的堆段(b7500000 - b7521000,132KB)建立在了內(nèi)存映射段中,這也表明了堆內(nèi)存是使用 mmap
系統(tǒng)調(diào)用產(chǎn)生的,而非同主線程一樣使用 sbrk
系統(tǒng)調(diào)用。類似地,盡管用戶只請求了 1000B,但是映射到程地址空間的堆內(nèi)存足有 1MB。這 1MB 中,只有 132KB 被設(shè)置了讀寫權(quán)限,并成為該線程的堆內(nèi)存。這段連續(xù)內(nèi)存(132KB)被稱為「thread arena」。
注意:當(dāng)用戶請求超過 128KB(比如 malloc(132*1024)
) 大小并且此時 arena 中沒有足夠的空間來滿足用戶的請求時,內(nèi)存將通過 mmap
系統(tǒng)調(diào)用(不再是 sbrk
)分配,而不論請求是發(fā)自 main arena 還是 thread arena。
ploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
After free in main thread
Before malloc in thread 1
After malloc and before free in thread 1
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804b000-0806c000 rw-p 00000000 00:00 0 [heap]
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b7604000-b7605000 ---p 00000000 00:00 0
b7605000-b7e07000 rw-p 00000000 00:00 0 [stack:6594]
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
2.2.6. 在 thread1 free 之后
從如下的輸出結(jié)果中我們可以看到,free
不會把內(nèi)存歸還給操作系統(tǒng),而是移交給分配器,然后添加在了「thread arenas bin」中。
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ ./mthread
Welcome to per thread arena example::6501
Before malloc in main thread
After malloc and before free in main thread
After free in main thread
Before malloc in thread 1
After malloc and before free in thread 1
After free in thread 1
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$ cat /proc/6501/maps
08048000-08049000 r-xp 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
08049000-0804a000 r--p 00000000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804a000-0804b000 rw-p 00001000 08:01 539625 /home/sploitfun/ptmalloc.ppt/mthread/mthread
0804b000-0806c000 rw-p 00000000 00:00 0 [heap]
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b7604000-b7605000 ---p 00000000 00:00 0
b7605000-b7e07000 rw-p 00000000 00:00 0 [stack:6594]
...
sploitfun@sploitfun-VirtualBox:~/ptmalloc.ppt/mthread$
3. Arena
3.1. Arena 的數(shù)量
在以上的例子中我們可以看到,主線程包含 main arena 而 thread 1 包含它自己的 thread arena。所以線程和 arena 之間是否存在一一映射關(guān)系,而不論線程的數(shù)量有多大?當(dāng)然不是,部分極端的應(yīng)用甚至運行比處理器核數(shù)還多的線程,在這種情況下,每個線程都擁有一個 arena 開銷過高且意義不大。所以,arena 數(shù)量其實是限于系統(tǒng)核數(shù)的。
For 32 bit systems:
Number of arena = 2 * number of cores.
For 64 bit systems:
Number of arena = 8 * number of cores.
3.2. Multiple Arena
舉例而言:讓我們來看一個運行在單核計算機上的 32 位操作系統(tǒng)上的多線程應(yīng)用(4 線程,主線程 + 3 個線程)的例子。這里線程數(shù)量(4)> 2 * 核心數(shù)(1),所以分配器中可能有 Arena(也即標題所稱「multiple arenas」)會被所有線程共享。那么是如何共享的呢?
當(dāng)主線程第一次調(diào)用 malloc
時,已經(jīng)建立的 main arena 會被沒有任何競爭地使用;
當(dāng) thread 1 和 thread 2 第一次調(diào)用 malloc
時,一塊新的 arena 將被創(chuàng)建,且將被沒有任何競爭地使用。此時線程和 arena 之間存在一一映射關(guān)系;
當(dāng) thread3 第一次調(diào)用 malloc
時,arena 的數(shù)量限制被計算出來,結(jié)果顯示已超出,因此嘗試復(fù)用已經(jīng)存在的 arena(也即 Main arena 或 Arena 1 或 Arena 2);
復(fù)用:
一旦遍歷到可用 arena,就開始自旋申請該 arena 的鎖;
如果上鎖成功(比如說 main arena 上鎖成功),就將該 arena 返回用戶;
如果沒找到可用 arena,thread 3 的 malloc
將被阻塞,直到有可用的 arena 為止。
當(dāng)thread 3 調(diào)用 malloc
時(第二次了),分配器會嘗試使用上一次使用的 arena(也即,main arena),從而盡量提高緩存命中率。當(dāng) main arena 可用時就用,否則 thread 3 就一直阻塞,直至 main arena 空閑。因此現(xiàn)在 main arena 實際上是被 main thread 和 thread 3 所共享。
3.3. Multiple Heaps
在「glibc malloc」中主要有 3 種數(shù)據(jù)結(jié)構(gòu):
heap_info ——Heap Header—— 一個 thread arena 可以維護多個堆。每個堆都有自己的堆 Header(注:也即頭部元數(shù)據(jù))。什么時候 Thread Arena 會維護多個堆呢?一般情況下,每個 thread arena 都只維護一個堆,但是當(dāng)這個堆的空間耗盡時,新的堆(而非連續(xù)內(nèi)存區(qū)域)就會被 mmap
到這個 aerna 里;
malloc_state ——Arena header—— 一個 thread arena 可以維護多個堆,這些堆另外共享同一個 arena header。Arena header 描述的信息包括:bins、top chunk、last remainder chunk 等;
malloc_chunk ——Chunk header—— 根據(jù)用戶請求,每個堆被分為若干 chunk。每個 chunk 都有自己的 chunk header。
注意:
Main arena 無需維護多個堆,因此也無需 heap_info。當(dāng)空間耗盡時,與 thread arena 不同,main arena 可以通過 sbrk
拓展堆段,直至堆段「碰」到內(nèi)存映射段;
與 thread arena 不同,main arena 的 arena header 不是保存在通過 sbrk
申請的堆段里,而是作為一個全局變量,可以在 libc.so 的數(shù)據(jù)段中找到。
main arena 和 thread arena 的圖示如下(單堆段):

thread arena 的圖示如下(多堆段):

4. Chunk
堆段中存在的 chunk 類型如下:
Allocated chunk;
Free chunk;
Top chunk;
Last Remainder chunk.
4.1. Allocated chunk
「Allocated chunck」就是已經(jīng)分配給用戶的 chunk,其圖示如下:

圖中左方三個箭頭依次表示:
chunk:該 Allocated chunk 的起始地址;
mem:該 Allocated chunk 中用戶可用區(qū)域的起始地址(= chunk + sizeof(malloc_chunk)
);
next_chunk:下一個 chunck(無論類型)的起始地址。
圖中結(jié)構(gòu)體內(nèi)部各字段的含義依次為:
注意:
4.2. Free chunk
「Free chunck」就是用戶已釋放的 chunk,其圖示如下:

圖中結(jié)構(gòu)體內(nèi)部各字段的含義依次為:
prev_size: 兩個相鄰 free chunk 會被合并成一個,因此該字段總是保存前一個 allocated chunk 的用戶數(shù)據(jù);
size: 該字段保存本 free chunk 的大小;
fd: Forward pointer —— 本字段指向同一 bin 中的下個 free chunk(free chunk 鏈表的前驅(qū)指針);
bk: Backward pointer —— 本字段指向同一 bin 中的上個 free chunk(free chunk 鏈表的后繼指針)。
5. Bins
「bins」 就是空閑列表數(shù)據(jù)結(jié)構(gòu)。它們用以保存 free chunks。根據(jù)其中 chunk 的大小,bins 被分為如下幾種類型:
Fast bin;
Unsorted bin;
Small bin;
Large bin.
保存這些 bins 的字段為:
5.1. Fast Bin
大小為 16 ~ 80 字節(jié)的 chunk 被稱為「fast chunk」。在所有的 bins 中,fast bins 路徑享有最快的內(nèi)存分配及釋放速度。
數(shù)量:10
每個 fast bin 都維護著一條 free chunk 的單鏈表,采用單鏈表是因為鏈表中所有 chunk 的大小相等,增刪 chunk 發(fā)生在鏈表頂端即可;—— LIFO
chunk 大小:8 字節(jié)遞增
fast bins 由一系列所維護 chunk 大小以 8 字節(jié)遞增的 bins 組成。也即,fast bin[0]
維護大小為 16 字節(jié)的 chunk、fast bin[1]
維護大小為 24 字節(jié)的 chunk。依此類推……
指定 fast bin 中所有 chunk 大小相同;
在 malloc 初始化過程中,最大的 fast bin 的大小被設(shè)置為 64 而非 80 字節(jié)。因為默認情況下只有大小 16 ~ 64 的 chunk 被歸為 fast chunk 。
無需合并 —— 兩個相鄰 chunk 不會被合并。雖然這可能會加劇內(nèi)存碎片化,但也大大加速了內(nèi)存釋放的速度!
malloc(fast chunk)
初始情況下 fast chunck 最大尺寸以及 fast bin 相應(yīng)數(shù)據(jù)結(jié)構(gòu)均未初始化,因此即使用戶請求內(nèi)存大小落在 fast chunk 相應(yīng)區(qū)間,服務(wù)用戶請求的也將是 small bin 路徑而非 fast bin 路徑;
初始化后,將在計算 fast bin 索引后檢索相應(yīng) bin;
相應(yīng) bin 中被檢索的第一個 chunk 將被摘除并返回給用戶。
free(fast chunk)

5.2. Unsorted Bin
當(dāng) small chunk 和 large chunk 被 free
掉時,它們并非被添加到各自的 bin 中,而是被添加在 「unsorted bin」 中。這使得分配器可以重新使用最近 free
掉的 chunk,從而消除了尋找合適 bin 的時間開銷,進而加速了內(nèi)存分配及釋放的效率。
譯者注:經(jīng) @kwdecsdn 提醒,這里應(yīng)補充說明「Unsorted Bin 中的 chunks 何時移至 small/large chunk 中」。在內(nèi)存分配的時候,在前后檢索 fast/small bins 未果之后,在特定條件下,會將 unsorted bin 中的 chunks 轉(zhuǎn)移到合適的 bin 中去,small/large。

5.3. Small Bin
大小小于 512 字節(jié)的 chunk 被稱為 「small chunk」,而保存 small chunks 的 bin 被稱為 「small bin」。在內(nèi)存分配回收的速度上,small bin 比 large bin 更快。
5.4. Large Bin
大小大于等于 512 字節(jié)的 chunk 被稱為「large chunk」,而保存 large chunks 的 bin 被稱為 「large bin」。在內(nèi)存分配回收的速度上,large bin 比 small bin 慢。
5.5. Top Chunk
一個 arena 中最頂部的 chunk 被稱為「top chunk」。它不屬于任何 bin 。當(dāng)所有 bin 中都沒有合適空閑內(nèi)存時,就會使用 top chunk 來響應(yīng)用戶請求。
當(dāng) top chunk 的大小比用戶請求的大小大的時候,top chunk 會分割為兩個部分:
當(dāng) top chunk 的大小比用戶請求的大小小的時候,top chunk 就通過 sbrk
(main arena)或 mmap
( thread arena)系統(tǒng)調(diào)用擴容。
5.6. Last Remainder Chunk
「last remainder chunk」即最后一次 small request 中因分割而得到的剩余部分,它有利于改進引用局部性,也即后續(xù)對 small chunk 的 malloc
請求可能最終被分配得彼此靠近。
那么 arena 中的若干 chunks,哪個有資格成為 last remainder chunk 呢?
當(dāng)用戶請求 small chunk 而無法從 small bin 和 unsorted bin 得到服務(wù)時,分配器就會通過掃描 binmaps 找到最小非空 bin。正如前文所提及的,如果這樣的 bin 找到了,其中最合適的 chunk 就會分割為兩部分:返回給用戶的 User chunk 、添加到 unsorted bin 中的 Remainder chunk。這一 Remainder chunk 就將成為 last remainder chunk。
那么引用局部性是如何達成的呢?
當(dāng)用戶的后續(xù)請求 small chunk,并且 last remainder chunk 是 unsorted bin 中唯一的 chunk,該 last remainder chunk 就將分割成兩部分:返回給用戶的 User chunk、添加到 unsorted bin 中的 Remainder chunk(也是 last remainder chunk)。因此后續(xù)的請求的 chunk 最終將被分配得彼此靠近。
劉翔,童薇,劉景寧,馮丹,陳勁龍.動態(tài)內(nèi)存分配器研究綜述[J].計算機學(xué)報,2018,41(10):2359-2378.