2012年3月25日 Sunday
1、内核版本号2.6.33.20
2、mempool相关函数的简单应用方法
3、模块源文件mempool.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/slab.h>
- #include <linux/mempool.h>
- #define MIN_ELEMENT 4
- #define TEST_ELEMENT 4
- typedef struct{
- int number;
- char string[128];
- }TMemElement;
- int elementcount=0;
- void *mempool_alloc_test(int gfp_mask,void *pool_data)
- {
- TMemElement *data;
- printk("--->mempool_malloc_test\n");
-
- data=kmalloc(sizeof(TMemElement),gfp_mask);
- if(data!=NULL)data->number=elementcount++;
- return data;
- }
- void mempool_free_test(void *element,void *pool_data)
- {
- printk("--->call mempool_free_test\n");
- if(element!=NULL)kfree(element);
- }
- int mempool_init(void)
- {
- mempool_t *mp;
- TMemElement *element[TEST_ELEMENT];
- int lp;
-
- printk("Module MEMPOOL Test\n");
-
- memset(element,0,sizeof(element));
-
- printk("call mempool_create\n");
- mp=mempool_create(MIN_ELEMENT,
- mempool_alloc_test,
- mempool_free_test,
- NULL);
-
- printk("call mempool_allocate\n");
- for(lp=0;lp<TEST_ELEMENT;lp++){
-
- element[lp]=mempool_alloc(mp,GFP_KERNEL);
- if(element[lp]==NULL){
- printk("allocate fail\n");
- }else{
- sprintf(element[lp]->string,
- "allo data %d\n",
- element[lp]->number);
- printk(element[lp]->string);
- }
- }
-
- printk("mempool free\n");
- for(lp=0;lp<TEST_ELEMENT;lp++){
- if(element[lp]!=NULL)mempool_free(element[lp],mp);
- }
-
- printk("call mempool_destroy\n");
- mempool_destroy(mp);
-
- return 0;
- }
- void mempool_exit(void)
- {
- printk("Module MEMPOOL Test End\n");
- }
- module_init(mempool_init);
- module_exit(mempool_exit);
- MODULE_LICENSE("Dual BSD/GPL");
4、Makefile
- obj-m += mempool.o
- CURRENT_PATH := $(shell pwd)
- LINUX_KERNEL := $(shell uname -r)
- LINUX_KERNEL_PATH := /usr/src/linux-$(LINUX_KERNEL)
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
- clean:
- #make -C $(LINUX_KERNEL_PAHT) M=$(CURRENT_PATH) clean
- rm *.mod.*
- rm *.order
- rm *.o
- rm *.symvers
5、运行方法
- ~/mempool# make
- ~/mempool# insmod mempool.ko
- ~/mempool# dmesg
6、运行结果
- [10373.987583] Module MEMPOOL Test
- [10373.987609] call mempool_create
- [10373.987709] --->mempool_malloc_test
- [10373.989115] --->mempool_malloc_test
- [10373.989172] --->mempool_malloc_test
- [10373.989190] --->mempool_malloc_test
- [10373.989229] call mempool_allocate
- [10373.989247] --->mempool_malloc_test
- [10373.989372] allo data 4
- [10373.989385] --->mempool_malloc_test
- [10373.989399] allo data 5
- [10373.989401] --->mempool_malloc_test
- [10373.989405] allo data 6
- [10373.989406] --->mempool_malloc_test
- [10373.989410] allo data 7
- [10373.989423] mempool free
- [10373.989447] --->call mempool_free_test
- [10373.989477] --->call mempool_free_test
- [10373.989480] --->call mempool_free_test
- [10373.989481] --->call mempool_free_test
- [10373.989493] call mempool_destroy
- [10373.989529] --->call mempool_free_test
- [10373.989540] --->call mempool_free_test
- [10373.989542] --->call mempool_free_test
- [10373.989544] --->call mempool_free_test
7、移除模块
- ~/mempool# rmmod mempool.ko
阅读(416) | 评论(0) | 转发(0) |