红联Linux门户
Linux帮助

Linux内存管理之slab机制(销毁slab)

发布时间:2014-11-30 10:36:13来源:linux网站作者:bullbat

总结完了slab创建、对象分配、对象释放,在这里再看看slab的销毁。销毁slab很简单,由函数slab_destroy()实现。


/**
* slab_destroy - destroy and release all objects in a slab
* @cachep: cache pointer being destroyed
* @slabp: slab pointer being destroyed
*
* Destroy all the objs in a slab, and release the mem back to the system.
* Before calling the slab must have been unlinked from the cache.  The
* cache-lock is not held/needed.
*/ 
/*销毁slab,需要释放slab管理对象和slab对象。*/ 
static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp) 

/* 获得slab首页面的虚拟地址 */ 
void *addr = slabp->s_mem - slabp->colouroff; 
/*调试用*/ 
slab_destroy_debugcheck(cachep, slabp); 
if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) { 
/* rcu方式释放,暂时不做分析,主要是做并行优化 */ 
struct slab_rcu *slab_rcu; 
slab_rcu = (struct slab_rcu *)slabp; 
slab_rcu->cachep = cachep; 
slab_rcu->addr = addr; 
call_rcu(&slab_rcu->head, kmem_rcu_free); 
} else { 
/* 释放slab占用的页面到伙伴系统中。如果是内置式,
slab管理对象和slab对象在一起,可以同时释放。*/ 
kmem_freepages(cachep, addr); 
/* 外置式,还需释放slab管理对象 */ 
if (OFF_SLAB(cachep)) 
kmem_cache_free(cachep->slabp_cache, slabp); 

}


其中,涉及到的其他函数在前面相应的地方已经做了分析。