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

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

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

利用页描述结构的lru域建立页框到slab描述符和cache描述符的映射,实际就是使lru.next指向cache描述符,lru.prev指向slab描述符

  1. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,  
  2.                void *addr)  
  3. {  
  4.     int nr_pages;  
  5.     struct page *page;  
  6.   
  7.     page = virt_to_page(addr);  
  8.   
  9.     nr_pages = 1;  
  10.     if (likely(!PageCompound(page)))  
  11.         nr_pages <<= cache->gfporder;/*分配给slab的页框数*/  
  12.   
  13.     do {  
  14.         page_set_cache(page, cache);/*建立到cache的映射*/  
  15.         page_set_slab(page, slab);  /*建立到slab的映射*/  
  16.         page++;  
  17.     } while (--nr_pages);  
  18. }  
  1. static inline void page_set_cache(struct page *page, struct kmem_cache *cache)  
  2. {  
  3.     page->lru.next = (struct list_head *)cache;  
  4. }  
  1. static inline void page_set_slab(struct page *page, struct slab *slab)  
  2. {  
  3.     page->lru.prev = (struct list_head *)slab;  
  4. }  

初始化对象

  1. static void cache_init_objs(struct kmem_cache *cachep,  
  2.                 struct slab *slabp)  
  3. {  
  4.     int i;  
  5.   
  6.     for (i = 0; i < cachep->num; i++) {  
  7.         /*得到第i个对象*/  
  8.         void *objp = index_to_obj(cachep, slabp, i);  
  9. #if DEBUG /*Debug相关操作*/   
  10.         /* need to poison the objs? */  
  11.         if (cachep->flags & SLAB_POISON)  
  12.             poison_obj(cachep, objp, POISON_FREE);  
  13.         if (cachep->flags & SLAB_STORE_USER)  
  14.             *dbg_userword(cachep, objp) = NULL;  
  15.   
  16.         if (cachep->flags & SLAB_RED_ZONE) {  
  17.             *dbg_redzone1(cachep, objp) = RED_INACTIVE;  
  18.             *dbg_redzone2(cachep, objp) = RED_INACTIVE;  
  19.         }  
  20.         /* 
  21.          * Constructors are not allowed to allocate memory from the same 
  22.          * cache which they are a constructor for.  Otherwise, deadlock. 
  23.          * They must also be threaded. 
  24.          */  
  25.         if (cachep->ctor && !(cachep->flags & SLAB_POISON))  
  26.             cachep->ctor(objp + obj_offset(cachep));  
  27.   
  28.         if (cachep->flags & SLAB_RED_ZONE) {  
  29.             if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)  
  30.                 slab_error(cachep, "constructor overwrote the"  
  31.                        " end of an object");  
  32.             if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)  
  33.                 slab_error(cachep, "constructor overwrote the"  
  34.                        " start of an object");  
  35.         }  
  36.         if ((cachep->buffer_size % PAGE_SIZE) == 0 &&  
  37.                 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)  
  38.             kernel_map_pages(virt_to_page(objp),  
  39.                      cachep->buffer_size / PAGE_SIZE, 0);  
  40. #else   
  41.         if (cachep->ctor)/*根据构造函数初始化对象*/  
  42.             cachep->ctor(objp);  
  43. #endif   
  44.         slab_bufctl(slabp)[i] = i + 1;/*确定下一个空闲对象为后面相邻的对象*/  
  45.     }  
  46.     slab_bufctl(slabp)[i - 1] = BUFCTL_END;  
  47. }  
linux
相关资讯       Linux内存管理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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