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

Linux Slab分配器(六)--创建slab和销毁slab

[日期:2012-06-18] 来源:Linux社区  作者:vanbreaker [字体:大 中 小]

辅助函数:

为slab分配页框

  1. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)  
  2. {  
  3.     struct page *page;  
  4.     int nr_pages;  
  5.     int i;  
  6.   
  7. #ifndef CONFIG_MMU   
  8.     /* 
  9.      * Nommu uses slab's for process anonymous memory allocations, and thus 
  10.      * requires __GFP_COMP to properly refcount higher order allocations 
  11.      */  
  12.     flags |= __GFP_COMP;  
  13. #endif   
  14.   
  15.     flags |= cachep->gfpflags;  
  16.     if (cachep->flags & SLAB_RECLAIM_ACCOUNT)  
  17.         flags |= __GFP_RECLAIMABLE;  
  18.   
  19.     /*从特定的节点分配2^gfporder个连续页*/  
  20.     page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);  
  21.     if (!page)  
  22.         return NULL;  
  23.   
  24.     nr_pages = (1 << cachep->gfporder);  
  25.     if (cachep->flags & SLAB_RECLAIM_ACCOUNT)  
  26.         add_zone_page_state(page_zone(page),  
  27.             NR_SLAB_RECLAIMABLE, nr_pages);  
  28.     else  
  29.         add_zone_page_state(page_zone(page),  
  30.             NR_SLAB_UNRECLAIMABLE, nr_pages);  
  31.     for (i = 0; i < nr_pages; i++)  
  32.         __SetPageSlab(page + i);  
  33.   
  34.     if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {  
  35.         kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);  
  36.   
  37.         if (cachep->ctor)  
  38.             kmemcheck_mark_uninitialized_pages(page, nr_pages);  
  39.         else  
  40.             kmemcheck_mark_unallocated_pages(page, nr_pages);  
  41.     }  
  42.   
  43.     /*返回首页的虚拟地址*/  
  44.     return page_address(page);  
  45. }  

为slab管理区分配空间:

  1. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,  
  2.                    int colour_off, gfp_t local_flags,  
  3.                    int nodeid)  
  4. {  
  5.     struct slab *slabp;  
  6.   
  7.     /*如果slab管理区位于slab外,则在指定的slabp_cache中分配空间*/  
  8.     if (OFF_SLAB(cachep)) {  
  9.         /* Slab management obj is off-slab. */  
  10.         slabp = kmem_cache_alloc_node(cachep->slabp_cache,  
  11.                           local_flags, nodeid);  
  12.         /* 
  13.          * If the first object in the slab is leaked (it's allocated 
  14.          * but no one has a reference to it), we want to make sure 
  15.          * kmemleak does not treat the ->s_mem pointer as a reference 
  16.          * to the object. Otherwise we will not report the leak. 
  17.          */  
  18.         kmemleak_scan_area(slabp, offsetof(struct slab, list),  
  19.                    sizeof(struct list_head), local_flags);  
  20.         if (!slabp)  
  21.             return NULL;  
  22.     } else {/*slab管理区处于slab中*/  
  23.         /*slab管理区从slab首部偏移颜色值的地方开始*/  
  24.         slabp = objp + colour_off;  
  25.         colour_off += cachep->slab_size;  
  26.     }  
  27.     slabp->inuse = 0;/*对象全为空闲*/  
  28.     slabp->colouroff = colour_off;   /*刷新第一个对象的偏移*/  
  29.     slabp->s_mem = objp + colour_off;/*确定第一个对象的位置*/  
  30.     slabp->nodeid = nodeid;/*标识节点*/  
  31.     slabp->free = 0; /*下一个空闲对象位于s_mem起始处*/  
  32.     return slabp;  
  33. }  
linux
相关资讯       Linux内存管理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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