Linux Slab分配器(二)--初始化
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分配器(一)--概述
普通高速缓存是一组大小按几何倍数增长的高速缓存的合集,一个普通高速缓存用如下结构描述
- /* Size description struct for general caches. */
- struct cache_sizes {
- size_t cs_size; /*general cache的大小*/
- struct kmem_cache *cs_cachep; /*general cache的cache描述符指针*/
- #ifdef CONFIG_ZONE_DMA
- struct kmem_cache *cs_dmacachep;
- #endif
- };
普通高速缓存的大小由malloc_sizes表来确定
- /*
- * These are the default caches for kmalloc. Custom caches can have other sizes.
- */
- struct cache_sizes malloc_sizes[] = {
- #define CACHE(x) { .cs_size = (x) },
- #include <linux/kmalloc_sizes.h>
- CACHE(ULONG_MAX)
- #undef CACHE
- };
其中<linux/kmalloc_sizes.h>中的内容为
- #if (PAGE_SIZE == 4096)
- CACHE(32)
- #endif
- CACHE(64)
- #if L1_CACHE_BYTES < 64
- CACHE(96)
- #endif
- CACHE(128)
- #if L1_CACHE_BYTES < 128
- CACHE(192)
- #endif
- CACHE(256)
- CACHE(512)
- CACHE(1024)
- CACHE(2048)
- CACHE(4096)
- CACHE(8192)
- CACHE(16384)
- CACHE(32768)
- CACHE(65536)
- CACHE(131072)
- #if KMALLOC_MAX_SIZE >= 262144
- CACHE(262144)
- #endif
- #if KMALLOC_MAX_SIZE >= 524288
- CACHE(524288)
- #endif
- #if KMALLOC_MAX_SIZE >= 1048576
- CACHE(1048576)
- #endif
- #if KMALLOC_MAX_SIZE >= 2097152
- CACHE(2097152)
- #endif
- #if KMALLOC_MAX_SIZE >= 4194304
- CACHE(4194304)
- #endif
- #if KMALLOC_MAX_SIZE >= 8388608
- CACHE(8388608)
- #endif
- #if KMALLOC_MAX_SIZE >= 16777216
- CACHE(16777216)
- #endif
- #if KMALLOC_MAX_SIZE >= 33554432
- CACHE(33554432)
- #endif
|
评论暂时关闭