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

Linux内存管理之slab机制(分配对象)

[日期:2012-01-11] 来源:Linux社区  作者:bullbat [字体:大 中 小]

几个涉及到的辅助函数

  1. /* 
  2.  * Transfer objects in one arraycache to another. 
  3.  * Locking must be handled by the caller. 
  4.  * 
  5.  * Return the number of entries transferred. 
  6.  */  
  7. static int transfer_objects(struct array_cache *to,  
  8.         struct array_cache *from, unsigned int max)  
  9. {  
  10.     /* Figure out how many entries to transfer */  
  11.     int nr = min(min(from->avail, max), to->limit - to->avail);  
  12.   
  13.     if (!nr)  
  14.         return 0;  
  15.     /*拷贝*/  
  16.     memcpy(to->entry + to->avail, from->entry + from->avail -nr,  
  17.             sizeof(void *) *nr);  
  18.     /*两边数据更新*/  
  19.     from->avail -= nr;  
  20.     to->avail += nr;  
  21.     to->touched = 1;  
  22.     return nr;  
  23. }  
 
  1. /*从slab中提取一个空闲对象*/  
  2. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,  
  3.                 int nodeid)  
  4. {  
  5.     /* 获得一个空闲的对象,free是本slab中第一个空闲对象的索引 */  
  6.     void *objp = index_to_obj(cachep, slabp, slabp->free);  
  7.     kmem_bufctl_t next;  
  8.     /* 更新在用对象计数 */  
  9.     slabp->inuse++;  
  10.     /* 获得下一个空闲对象的索引 */  
  11.     next = slab_bufctl(slabp)[slabp->free];  
  12. #if DEBUG   
  13.     slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;  
  14.     WARN_ON(slabp->nodeid != nodeid);  
  15. #endif   
  16.     /* free指向下一个空闲的对象 */  
  17.     slabp->free = next;  
  18.   
  19.     return objp;  
  20. }  
 
  1. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,  
  2.                  unsigned int idx)  
  3. {   /* s_mem是slab中第一个对象的起始地址,buffer_size是每个对象的大小 
  4.     ,这里根据对象索引计算对象的地址 */  
  5.     return slab->s_mem + cache->buffer_size * idx;  
  6. }  
 
  1. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)  
  2. {  
  3.     return (kmem_bufctl_t *) (slabp + 1);  
  4. }  
 
  1. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)  
  2. {  
  3.     return cachep->array[smp_processor_id()];  
  4. }  

总结:从slab分配器中分配空间实际工作很简单,先查看本地CPU cache,然后是本地共享CPU cache,最后是三链。前面三个都没有空间时,需要从新分配slab。可以看出,从slab分配器中分配内存空间一般不会申请不到空间,也就是说返回空的可能性很小。 linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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