歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux內存管理之slab機制(銷毀slab)

總結完了slab創建、對象分配、對象釋放,在這裡再看看slab的銷毀。銷毀slab很簡單,由函數slab_destroy()實現。

相關閱讀: http://www.linuxidc.com/Linux/2012-01/51241.htm http://www.linuxidc.com/Linux/2012-01/51240.htm http://www.linuxidc.com/Linux/2012-01/51239.htm http://www.linuxidc.com/Linux/2012-01/51242.htm

  1. /** 
  2.  * slab_destroy - destroy and release all objects in a slab 
  3.  * @cachep: cache pointer being destroyed 
  4.  * @slabp: slab pointer being destroyed 
  5.  * 
  6.  * Destroy all the objs in a slab, and release the mem back to the system. 
  7.  * Before calling the slab must have been unlinked from the cache.  The 
  8.  * cache-lock is not held/needed. 
  9.  */  
  10.  /*銷毀slab,需要釋放slab管理對象和slab對象。*/  
  11. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)  
  12. {  
  13.     /* 獲得slab首頁面的虛擬地址 */  
  14.     void *addr = slabp->s_mem - slabp->colouroff;  
  15.     /*調試用*/  
  16.     slab_destroy_debugcheck(cachep, slabp);  
  17.     if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {  
  18.          /* rcu方式釋放,暫時不做分析,主要是做並行優化 */  
  19.         struct slab_rcu *slab_rcu;  
  20.   
  21.         slab_rcu = (struct slab_rcu *)slabp;  
  22.         slab_rcu->cachep = cachep;  
  23.         slab_rcu->addr = addr;  
  24.         call_rcu(&slab_rcu->head, kmem_rcu_free);  
  25.     } else {  
  26.         /* 釋放slab占用的頁面到伙伴系統中。如果是內置式, 
  27.         slab管理對象和slab對象在一起,可以同時釋放。*/  
  28.         kmem_freepages(cachep, addr);  
  29.         /* 外置式,還需釋放slab管理對象 */  
  30.         if (OFF_SLAB(cachep))  
  31.             kmem_cache_free(cachep->slabp_cache, slabp);  
  32.     }  
  33. }  

其中,涉及到的其他函數在前面相應的地方已經做了分析。

Copyright © Linux教程網 All Rights Reserved