Slab 分配器

 1、什麼是Slab 分配器:

       以下摘自維基百科://en.wikipedia.org/wiki/Slab_allocation

        Slab  firstly introduced in kernel 2.2, it’s now one of three memory allocator implementations together with SLOB and SLUB. The three allocators provides a kind of front-end to the zoned buddy allocator for those sections of the kernel that require more flexible memory allocation than the standard 4KB page size

        With slab allocation, a cache for a certain type or size of data object has a number of pre-allocated “slabs” of memory; within each slab there are memory chunks of fixed size suitable for the objects.The slab allocator keeps track of these chunks, so that when it receives a request to allocate memory for a data object of a certain type, usually it can satisfy the request with a free slot (chunk) from an existing slab. When the allocator is asked to free the object’s memory, it just adds the slot to the containing slab’s list of free (unused) slots. The next call to create an object of the same type (or allocate memory of the same size) will return that memory slot (or some other free slot) and remove it from the list of free slots. This process eliminates the need to search for suitable memory space and greatly alleviates memory fragmentation. In this context, a slab is one or more contiguous pages in the memory containing pre-allocated memory chunks.

                                                                 

         

 2、如何理解這個slab 機制:    

        我對Slab 分配器的理解是,slab 層應該是軟體層面的一種記憶體管理機制,它把不同的對象劃分為不同的高速快取組(空閑鏈表),每一個高速快取組針對一種類型的對象,例如一個只存task_struct的高速快取組。
         然後某一個高速快取組有包含不同狀態的slab(滿,半滿,空),然後slab是由一個或多個物理上連續的頁組成的。
        Slab allocation 相當於給記憶體做了一層快取,釋放對象也不是真的釋放掉,而是給個標記,如果下次同類型的對象要申請空間,直接在slab裡面找標記為空閑的某塊區域,直接分配。這樣可以高效的應對小空間對象頻繁的申請和釋放。