chipset: msm8x25
codebase: android4.1
一、 初始化:
int __init memory_pool_init(void)
{
int i;
alloc_root = RB_ROOT;
mutex_init(&alloc_mutex);
for (i = 0; i < ARRAY_SIZE(mpools); i++) {
mutex_init(&mpools[i].pool_mutex);
mpools[i].gpool = NULL;
}
return 0;
}
Mpools结构体如下,最多能存放8个,存放类型由平台自己决定:
#define MAX_MEMPOOLS 8
struct mem_pool mpools[MAX_MEMPOOLS];
struct mem_pool {
struct mutex pool_mutex;
struct gen_pool *gpool;
unsigned long paddr; //存放的是物理或者虚拟地址都可以。
unsigned long size; //pool 的size大小。
unsigned long free; //还有多少空闲部分可用。
unsigned int id;
};
本平台定义的type如下:
enum {
MEMTYPE_NONE = -1,
MEMTYPE_SMI_KERNEL = 0,
MEMTYPE_SMI,
MEMTYPE_EBI0,
MEMTYPE_EBI1,
MEMTYPE_MAX,
};
下面函数是和平台相关,其中调用了kernel中的initialize_memory_pool函数,
当然自己使用的时候也可用按照这种写法:
static void __init initialize_mempools(void)
{
struct mem_pool *mpool;
int memtype;
struct memtype_reserve *mt;
//保留内存相关信息,其实type为MEMTYPE_EBI0部分才有size,
因为平台用的就是EBI1接口的DDR。
mt = &reserve_info->memtype_reserve_table[0];
for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
if (!mt->size)
continue;
//依次将平台所用到的保留内存信息保存到mpool中。
mpool = initialize_memory_pool(mt->start, mt->size, memtype);
if (!mpool)
pr_warning("failed to create %s mempool\n",
memtype_name[memtype]);
}
}
好了,看公共的函数initialize_memory_pool:
struct mem_pool *initialize_memory_pool(unsigned long start,
unsigned long size, int mem_type)
{
int id = mem_type;
//类型不符合或者size小于4k就返回
if (id >= MAX_MEMPOOLS || size <= PAGE_SIZE || size % PAGE_SIZE)
return NULL;
mutex_lock(&mpools[id].pool_mutex);
mpools[id].paddr = start; //保留内存的虚拟地址,注意是虚拟地址。
mpools[id].size = size; //能使用的总size
mpools[id].free = size; //空闲size,一开始肯定和总size一样。
mpools[id].id = id;
mutex_unlock(&mpools[id].pool_mutex);
pr_info("memory pool