Linux Slab分配器(二)--初始化


在前文中介绍了slab所涉及到的数据结构, slab分配器的初始化工作都是围绕这些数据结构来展开的,主要是针对以下两个问题:

 1.创建kmem_cache高速缓存用来存储所有的cache描述符

 2.创建array_cache和kmem_list3高速缓存用来存储slab数据结构中的这两个关键结构

这里明显有点自相矛盾,那就是slab管理器尚未建立起来,又如何靠slab分配高速缓存来给这些结构分配空间呢?

解决第一个问题的方法是直接静态定义一个名为cache_cache的kmem_cache结构,来管理所有的kmem_cache描述符,对于array_cache和kmem_list3,内核也是先静态定义,然后建立起普通高速缓存(general cache),再使用kmalloc分配普通高速缓存空间来替代之前静态定义的部分。

相关阅读:Linux Slab分配器(一)--概述

普通高速缓存是一组大小按几何倍数增长的高速缓存的合集,一个普通高速缓存用如下结构描述

  1. /* Size description struct for general caches. */  
  2. struct cache_sizes {  
  3.     size_t          cs_size;   /*general cache的大小*/  
  4.     struct kmem_cache   *cs_cachep;         /*general cache的cache描述符指针*/  
  5. #ifdef CONFIG_ZONE_DMA   
  6.     struct kmem_cache   *cs_dmacachep;  
  7. #endif   
  8. };  

普通高速缓存的大小由malloc_sizes表来确定

  1. /* 
  2.  * These are the default caches for kmalloc. Custom caches can have other sizes. 
  3.  */  
  4. struct cache_sizes malloc_sizes[] = {  
  5. #define CACHE(x) { .cs_size = (x) },   
  6. #include <linux/kmalloc_sizes.h>   
  7.     CACHE(ULONG_MAX)  
  8. #undef CACHE   
  9. };  

其中<linux/kmalloc_sizes.h>中的内容为

  1. #if (PAGE_SIZE == 4096)   
  2.     CACHE(32)  
  3. #endif   
  4.     CACHE(64)  
  5. #if L1_CACHE_BYTES < 64   
  6.     CACHE(96)  
  7. #endif   
  8.     CACHE(128)  
  9. #if L1_CACHE_BYTES < 128   
  10.     CACHE(192)  
  11. #endif   
  12.     CACHE(256)  
  13.     CACHE(512)  
  14.     CACHE(1024)  
  15.     CACHE(2048)  
  16.     CACHE(4096)  
  17.     CACHE(8192)  
  18.     CACHE(16384)  
  19.     CACHE(32768)  
  20.     CACHE(65536)  
  21.     CACHE(131072)  
  22. #if KMALLOC_MAX_SIZE >= 262144   
  23.     CACHE(262144)  
  24. #endif   
  25. #if KMALLOC_MAX_SIZE >= 524288   
  26.     CACHE(524288)  
  27. #endif   
  28. #if KMALLOC_MAX_SIZE >= 1048576   
  29.     CACHE(1048576)  
  30. #endif   
  31. #if KMALLOC_MAX_SIZE >= 2097152   
  32.     CACHE(2097152)  
  33. #endif   
  34. #if KMALLOC_MAX_SIZE >= 4194304   
  35.     CACHE(4194304)  
  36. #endif   
  37. #if KMALLOC_MAX_SIZE >= 8388608   
  38.     CACHE(8388608)  
  39. #endif   
  40. #if KMALLOC_MAX_SIZE >= 16777216   
  41.     CACHE(16777216)  
  42. #endif   
  43. #if KMALLOC_MAX_SIZE >= 33554432   
  44.     CACHE(33554432)  
  45. #endif  
  • 1
  • 2
  • 3
  • 下一页

相关内容