genalloc — 通用内存分配器

genalloc是Linux内核的通用内存分配器,用于独立于内核的内存管理。它遵循最先适配原则,Android ION内存管理器在处理ION_HEAP_TYPE_CARVEOUT时使用此分配器。文章介绍了genalloc的基础数据结构,包括struct gen_pool,并详细解析了gen_pool_alloc函数的实现,以及简单对比了buddy分配器。
摘要由CSDN通过智能技术生成

genalloc 是 linux 内核提供的通用内存分配器,源码位于 lib/genalloc.c。这个分配器为独立于内核以外的内存块提供分配方法,采用的是最先适配原则,android 最新的ION 内存管理器对 ION_HEAP_TYPE_CARVEOUT 类型的内存就是采用的这个分配器。

1、基础数据结构

首先看下分配器用到的几个数据结构,struct gen_pool 用来描述一个内存池:

struct gen_pool {
	rwlock_t lock;             /* 链表读写锁 */
	struct list_head chunks;   /* 内存池中内存块的链表 */
	int min_alloc_order;       /* 内存池最小分配单元的阶数,大小为 2^min_alloc_order */
};
在使用的时候需要向内存池中加入内存块,一个内存块即一大块连续的物理内存,用  struct gen_pool_chunk 来描述:

struct gen_pool_chunk {
	spinlock_t lock;              /* 操作内存块时用到的自旋锁 */
	struct list_head next_chunk;  /* 加入内存池的节点 */
	unsigned long start_addr;     /* 内存块的起始地址 */
	unsigned long end_addr;       /* 内存块的结束地址 */
	unsigned long bits[0];        /* 内存块的位图 */
};

2、函数接口及调用方法

genalloc 用到的函数接口有下面几个:

/* 创建一个内存池,主要工作是完成 struct gen_pool 的初始化 */
struct gen_pool *gen_pool_create(int min_alloc_order, int nid);
/* 向内存池中加入内存块,addr 为起始地址,size 为大小 */
int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size, int nid);
/* 销毁一个内存池 */
void gen_pool_destroy(struct gen_pool *pool);
/* 内存池分配内存的函数 */
unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size);
/* 内存池释放内存的函数 */
void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size);
对通用内存分配器的一般使用方法如下:

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值