https://blog.csdn.net/hsly_support/article/details/7551900
内存池(memory pool)是linux 2.6的一个新特性。内核中有些地方的内存分配是不允许失败的。为了确保这种情况下的成功分配,内核开发者建立了一种称为内存池的抽象。内存池其实就是某种形式的后备高速缓存,它试图始终保存空闲的内存,以便在紧急状态下使用。下边是内存池对象的类型:
- typedef struct mempool_s {
- spinlock_t lock; /*用来保护对象字段的自旋锁*/
- int min_nr; /*内存池中元素的最大个数*/
- int curr_nr; /*当前内存池中元素的个数*/
- void **elements; /*指向一个数组的指针,该数组由指向保留元素的指针组成*/
- void *pool_data; /*池的拥有者可获得的私有数据*/
- mempool_alloc_t *alloc; /*分配一个元素的方法*/
- mempool_free_t *free; /*释放一个元素的方法*/
- wait_queue_head_t wait; /*当内存池为空时使用的等待队列*/
- } mempool_t;
创建内存池对象,mempool_create():
- mempool_t * mempool_create(int min_nr, mempool_alloc_t *alloc_fn, mempool_free_t *free_fn, void *pool_data);
- typedef void *(mempool_alloc_t)(int gfp_mask, void *pool_data);
- typedef void mempool_free_t(void *element, void *pool_data);
在建立内存池之后,可如下所示分配和释放对象:
- void *mempool_alloc(mempool_t *pool, int gfp_mask);
- void mempool_free(void *element, mempool_t *pool);
可以调用下面的函数来调整mempool的大小:
- int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask);
- void mempool_destroy(mempool_t *pool);
下边是自己编写的一个测试程序:
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/slab.h>
- #include <linux/errno.h>
- #include <linux/mempool.h>
- #include <linux/gfp.h>
- #include <linux/delay.h>
- struct my_mempool{
- int val;
- };
- #define MY_MIN_MEMPOOL 20
- struct kmem_cache * mempool_cachep = NULL;
- mempool_t * my_pool = NULL;
- struct my_mempool * mempool_object = NULL;
- static int __init mempool_init(void){
- printk("memory pool test module init!\n");
- mempool_cachep = kmem_cache_create("mempool_cachep", sizeof(struct my_mempool), 0, 0, NULL);
- if(!mempool_cachep)
- return -ENOMEM;
- my_pool = mempool_create(MY_MIN_MEMPOOL, mempool_alloc_slab, mempool_free_slab, mempool_cachep);
- if(!my_pool)
- return -ENOMEM;
- mempool_object = (struct my_mempool *)mempool_alloc(my_pool, GFP_KERNEL);
- if(mempool_object)
- printk("one object has been allocated!\n");
- else
- goto fail;
- printk("memory pool curr_nr is %d, min_nr is %d\n", my_pool->curr_nr, my_pool->min_nr);
- mempool_free(mempool_object, my_pool);
- return 0;
- fail:
- mempool_destroy(my_pool);
- kmem_cache_destroy(mempool_cachep);
- return -1;
- }
- static void __exit mempool_exit(void){
- printk("memory pool test module exit!\n");
- if(my_pool){
- mempool_destroy(my_pool);
- printk("mempool has been destroy!\n");
- }
- if(mempool_cachep){
- kmem_cache_destroy(mempool_cachep);
- printk("cache has been destroy!\n");
- }
- }
- module_init(mempool_init);
- module_exit(mempool_exit);
- MODULE_AUTHOR("embeddedlwp@163.com");
- MODULE_LICENSE("GPL");