linux驱动38:后备高速缓存

设备驱动程序常常会反复地分配同一大小的内存块,为这些反复使用的内存块增加某些特殊的内存池——后备高速缓存。

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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值