手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux Slab分配器(五)--释放对象

[日期:2012-06-18] 来源:Linux社区  作者:t0nsha [字体:大 中 小]
  1. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)  
  2. {  
  3.     int batchcount;  
  4.     struct kmem_list3 *l3;  
  5.     int node = numa_node_id();  
  6.   
  7.     batchcount = ac->batchcount;  
  8. #if DEBUG   
  9.     BUG_ON(!batchcount || batchcount > ac->avail);  
  10. #endif   
  11.     check_irq_off();  
  12.     l3 = cachep->nodelists[node];  
  13.     spin_lock(&l3->list_lock);  
  14.     if (l3->shared) {/*如果开启了共享本地高速缓存*/  
  15.         /*获取共享的array_cache*/  
  16.         struct array_cache *shared_array = l3->shared;  
  17.         /*计算共享本地高速缓存还可容纳的空闲对象数*/  
  18.         int max = shared_array->limit - shared_array->avail;  
  19.         if (max) {  
  20.             if (batchcount > max)  
  21.                 batchcount = max;  
  22.             /*将batchcount个对象移到共享本地高速缓存中*/  
  23.             memcpy(&(shared_array->entry[shared_array->avail]),  
  24.                    ac->entry, sizeof(void *) * batchcount);  
  25.             shared_array->avail += batchcount;  
  26.             goto free_done;  
  27.         }  
  28.     }  
  29.   
  30.     /*将本地高速缓存的前batchcount个对象放回slab*/  
  31.     free_block(cachep, ac->entry, batchcount, node);  
  32. free_done:  
  33. #if STATS   
  34.     {  
  35.         int i = 0;  
  36.         struct list_head *p;  
  37.   
  38.         p = l3->slabs_free.next;  
  39.         while (p != &(l3->slabs_free)) {  
  40.             struct slab *slabp;  
  41.   
  42.             slabp = list_entry(p, struct slab, list);  
  43.             BUG_ON(slabp->inuse);  
  44.   
  45.             i++;  
  46.             p = p->next;  
  47.         }  
  48.         STATS_SET_FREEABLE(cachep, i);  
  49.     }  
  50. #endif   
  51.     spin_unlock(&l3->list_lock);  
  52.     ac->avail -= batchcount;/*刷新本地高速缓存的avail值*/  
  53.     /*将从batchcount开始的元素搬到前面去*/  
  54.     memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);  
  55. }  
  56.   
  57. /* 
  58.  * Release an obj back to its cache. If the obj has a constructed state, it must 
  59.  * be in this state _before_ it is released.  Called with disabled ints. 
  60.  */  
  61. static inline void __cache_free(struct kmem_cache *cachep, void *objp)  
  62. {  
  63.     struct array_cache *ac = cpu_cache_get(cachep);  
  64.   
  65.     check_irq_off();  
  66.     kmemleak_free_recursive(objp, cachep->flags);  
  67.     objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));  
  68.   
  69.     kmemcheck_slab_free(cachep, objp, obj_size(cachep));  
  70.   
  71.     /* 
  72.      * Skip calling cache_free_alien() when the platform is not numa. 
  73.      * This will avoid cache misses that happen while accessing slabp (which 
  74.      * is per page memory  reference) to get nodeid. Instead use a global 
  75.      * variable to skip the call, which is mostly likely to be present in 
  76.      * the cache. 
  77.      */  
  78.     if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))  
  79.         return;  
  80.   
  81.     /*如果本地高速缓存中的空闲对象小于空闲对象上限,则直接用entry中的元素记录对象的地址*/  
  82.     if (likely(ac->avail < ac->limit)) {  
  83.         STATS_INC_FREEHIT(cachep);  
  84.         ac->entry[ac->avail++] = objp;  
  85.         return;  
  86.     } else {/*否则将本地高速缓存中的空闲对象批量转移到slab中*/  
  87.         STATS_INC_FREEMISS(cachep);  
  88.         cache_flusharray(cachep, ac);  
  89.         ac->entry[ac->avail++] = objp;  
  90.     }  
  91. }  
linux
相关资讯       Linux内存管理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款