歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux Slab分配器(二)--初始化

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

日期:2017/3/1 10:19:41   编辑:Linux編程
在前文中介紹了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分配器(一)--概述 http://www.linuxidc.com/Linux/2012-06/62965.htm

普通高速緩存是一組大小按幾何倍數增長的高速緩存的合集,一個普通高速緩存用如下結構描述

  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
Copyright © Linux教程網 All Rights Reserved