设备驱动程序常常会反复地分配同一大小的内存块,为这些反复使用的内存块增加某些特殊的内存池——后备高速缓存。
linux内核的高速缓存管理称为slab分配器,头文件<linux/slab.h>,类型struct kmem_cache。
一、api接口:
1、创建高速缓存对象
struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t offset, unsigned long flags, void (*constructor)(void *));
name:名称
size:内存块大小
offset:页面第一个对象的偏移量,通常为0
flags:控制如何分配
constructor:可选,初始化内存块函数,会多次调用(一个内存块调用一次)
2、分配内存对象
void *kmem_cache_alloc(struct kmem_cache *cache, gfp_t flags);
参数flags和传递给kmalloc的相同
3、释放内存对象
void kmem_cache_free(struct kmem_cache *cache, void *mem_obj);
4、释放高速缓存
void kmem_cache_destroy(struct kmem_cache *cache);
二、demo
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
typedef struct
{
int num;
char str[32];
}TEST_S;
static struct kmem_cache *pKmemCache = NULL;
static int cacheCnt = 0;
void constructor(void *args)
{
//printk("constructor. \n");
TEST_S *test = (TEST_S *)args;
test->num = 7;
memset(test->str, 0x0, 32);
cacheCnt++;
}
static int __init hello_init(void)
{
printk("hello_init. \n");
pKmemCache = kmem_cache_create("test", sizeof(TEST_S), 0, SLAB_HWCACHE_ALIGN, constructor);
if (!pKmemCache)
{
printk("kmem_cache_create failed. \n");
return -ENOMEM;
}
void *p = kmem_cache_alloc(pKmemCache, GFP_KERNEL);
if (!p)
{
printk("kmem_cache_alloc failed. \n");
return -1;
}
TEST_S *test = (TEST_S *)p;
printk("num:%d\n", test->num);
printk("cacheCnt:%d\n", cacheCnt);
kmem_cache_free(pKmemCache, p);
return 0;
}
static void __exit hello_exit(void)
{
printk("hello_exit. \n");
if (pKmemCache)
{
kmem_cache_destroy(pKmemCache);
pKmemCache = NULL;
}
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);