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函数作为入口进行分析

  1. static inline void __cache_free(struct kmem_cache *cachep, void *objp)  
  2. {  
  3.     struct array_cache *ac = cpu_cache_get(cachep);  
  4.   
  5.     check_irq_off();  
  6.     kmemleak_free_recursive(objp, cachep->flags);  
  7.     objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));  
  8.   
  9.     kmemcheck_slab_free(cachep, objp, obj_size(cachep));  
  10.   
  11.     /* 
  12.      * Skip calling cache_free_alien() when the platform is not numa. 
  13.      * This will avoid cache misses that happen while accessing slabp (which 
  14.      * is per page memory  reference) to get nodeid. Instead use a global 
  15.      * variable to skip the call, which is mostly likely to be present in 
  16.      * the cache. 
  17.      */  
  18.     if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))  
  19.         return;  
  20.   
  21.     /*如果本地高速缓存中的空闲对象小于空闲对象上限,则直接用entry中的元素记录对象的地址*/  
  22.     if (likely(ac->avail < ac->limit)) {  
  23.         STATS_INC_FREEHIT(cachep);  
  24.         ac->entry[ac->avail++] = objp;  
  25.         return;  
  26.     } else {/*否则将本地高速缓存中的空闲对象批量转移到slab中*/  
  27.         STATS_INC_FREEMISS(cachep);  
  28.         cache_flusharray(cachep, ac);  
  29.         ac->entry[ac->avail++] = objp;  
  30.     }  
  31. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容