SLAB,SLUB,SLOB
SLAB分配器
作为基础,当然要学习。
我们知道,BUDDY算法是基于页的分配管理,在内存中大部分使用却是以字节进行分配的,slab就是解决小内存分配的;
重点关注:
- SLAB是如何分配和释放小内存块的?
 - 缓存着色啥意思?
 
用户使用
#include <linux/slab.h>/** 创建和释放slab描述符* kmem_cache_create - Create a cache.* @name: /proc/slabinfo . procfs下可见* @size: 缓存对象大小* @align: 缓存对象需要对齐的字节数* @flags: SLAB flags* @ctor: A constructor for the objects. 对象构造函数,直接为NULL即可。* Return: a pointer to the cache on success, NULL on failure.*/struct kmem_cache *kmem_cache_create(const char *name, unsigned int size, unsigned int align,slab_flags_t flags, void (*ctor)(void *));void kmem_cache_destroy(struct kmem_cache *s);/** 分配和释放缓存对象* kmem_cache_alloc - Allocate an object* @cachep: The cache to allocate from.* @flags: See kmalloc().** Allocate an object from this cache. The flags are only relevant* if the cache has no available objects.** Return: pointer to the new object or %NULL in case of error*/void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags);void kmem_cache_free(struct kmem_cache *cachep, void *objp);
如果单纯使用,那么以上接口足够。 但内部实现仍然懵逼
slab描述符
// linux-5.7.14/include/linux/slab_def.h#include <linux/slab_def.h>/** struct array_cache** Purpose:* - LIFO ordering, to hand out cache-warm objects from _alloc* - reduce the number of linked list operations* - reduce spinlock operations** The limit is stored in the per-cpu structure to reduce the data cache* footprint.**/struct array_cache {unsigned int avail;unsigned int limit;unsigned int batchcount;unsigned int touched;void *entry[];};struct kmem_cache {struct array_cache __percpu *cpu_cache; // percpu 本地CPU对象缓冲池
描述符分配流程
细节分配参考: SLUB分配算法
kmem_cache_createkmem_cache_create_usercopyget_online_cpus() + get_online_mems() // cpu和mem加锁kmem_cache_sanity_check__kmem_cache_alias(name, size, align, flags, ctor);create_cache(cache_name, size,calculate_alignment(flags, align, size),flags, useroffset, usersize, ctor, NULL, NULL);put_online_mems() + put_online_cpus() // cpu和mem释放锁
__kmem_cache_alias 
该函数检查已创建的slab是否存在与当前想要创建的slab的对象大小相匹配的,如果有则通过别名合并到一个缓存中进行访问  
__kmem_cache_alias(name, size, align, flags, ctor);find_mergeable(size, align, flags, name, ctor); // 查找可合并slab的kmem_cache结构
SLUB分配器
https://blog.csdn.net/juS3Ve/article/details/79124544?utm_source=blogxgwz2
看了下ubuntu18.04 /boot/config-5.4.0-52-generic下的配置,也是用SLUB,这里也学习下这个玩意。
