总概念:the Linux kernel provides the slab layer (also called the slab allocator). The slab layer acts as a generic data structure-caching layer.
The slab layer divides different objects into groups called caches, each of which stores a different type of object. There is one cache per object type
即:把slab layer分为多个caches ,每个caches都由同一种类的object组成
The caches are then divided into slabs (hence the name of this subsystem). The slabs are composed of one or more physically contiguous pages. Typically, slabs are composed of only a single page. Each cache may consist of multiple slabs.
即:每个caches又被分为了许多个的slabs,每个slabs由一个或多个连续的物理页面组成。
Each slab contains some number of objects, which are the data structures being cached. Each slab is in one of three states: full, partial, or empty.
即:每个slab又由许多的objects 组成。一个objects 就对应了一个数据结构!例如:task_struct,struct inode,struct file.
if you are frequently creating many objects of the same type ,consider using the slab cache!
because always needed cache never destroyed.
即:如果你需要经常使用创建许多相同的数据结构,你可以考虑使用slab cache,因为它只是分配,收回内存,(而不destroy!然后再创建新的内存区) ,所以减少了开销!!
用法:creat a cache than creat object .有相关的函数创建cache和object!
创建,销毁cache,:
kmem_cache_t * kmem_cache_create(const char *name, size_t size,
size_t align, unsigned long flags,
void (*ctor)(void*, kmem_cache_t *, unsigned long),
void (*dtor)(void*, kmem_cache_t *, unsigned long))
On success, kmem_cache_create() returns a pointer to the created cache. Otherwise, it returns NULL. This function must not be called from interrupt context because it can sleep.
To destroy a cache, call
int kmem_cache_destroy(kmem_cache_t *cachep)
然后创建object:
After a cache is created, an object is obtained from the cache via
void * kmem_cache_alloc(kmem_cache_t *cachep, int flags)
This function returns a pointer to an object from the given cache cachep. If no free objects are in any slabs in the cache, and the slab layer must obtain new pages via kmem_getpages(), the value of flags is passed to __get_free_pages(). These are the same flags we looked at earlier. You probably want GFP_KERNEL or GFP_ATOMIC.