something about slab layer!!

 总概念: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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值