Linux Slab分配器(五)--释放对象
Linux Slab分配器(五)--释放对象
缓存回收对象基于以下原则
1.本地高速缓存的空间还可以容纳空闲对象,则直接将对象放回本地高速缓存
2.本地高速缓存的空间已满,则按batchcount的值将对象从本地高速缓存转移到slab中,转移是基于先进先出的原则的,也就是转移entry数组最前面的batchcount个空闲对象,因为这些对象在数组中存在的时间相对较长,不太可能仍然驻留在CPU高速缓存中
相关阅读:
Linux Slab分配器(一)--概述
Linux Slab分配器(二)--初始化
Linux Slab分配器(三)--创建缓存
Linux Slab分配器(四)--分配对象
Linux Slab分配器(六)--创建slab和销毁slab
释放对象通过函数kmem_cache_free()来完成,下图给出了主要的工作流程
我们以__cache_free函数作为入口进行分析
- static inline void __cache_free(struct kmem_cache *cachep, void *objp)
- {
- struct array_cache *ac = cpu_cache_get(cachep);
- check_irq_off();
- kmemleak_free_recursive(objp, cachep->flags);
- objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
- kmemcheck_slab_free(cachep, objp, obj_size(cachep));
- /*
- * Skip calling cache_free_alien() when the platform is not numa.
- * This will avoid cache misses that happen while accessing slabp (which
- * is per page memory reference) to get nodeid. Instead use a global
- * variable to skip the call, which is mostly likely to be present in
- * the cache.
- */
- if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
- return;
- /*如果本地高速缓存中的空闲对象小于空闲对象上限,则直接用entry中的元素记录对象的地址*/
- if (likely(ac->avail < ac->limit)) {
- STATS_INC_FREEHIT(cachep);
- ac->entry[ac->avail++] = objp;
- return;
- } else {/*否则将本地高速缓存中的空闲对象批量转移到slab中*/
- STATS_INC_FREEMISS(cachep);
- cache_flusharray(cachep, ac);
- ac->entry[ac->avail++] = objp;
- }
- }
|
评论暂时关闭