嵌入式操作系统内核原理和开发(等值block内存池设计)
作者:网络转载 发布时间:[ 2012/10/18 10:28:39 ] 推荐标签:
内存池设计是嵌入式系统的一个重要环节,之前我们也讨论过相关的内容。但是,看了rawos的代码之后,我觉得rawos的内存池设计更有特点。整个内存池的设计非常健壮,不但考虑了字节对齐的问题,而且还引入了等待调度机制,这是我所没有想到的。所以,在此我很愿意和大家分享这份的代码。闲话不多说,我们看看rawos的mempool数据结构是什么样的,
typedef struct MEM_POOL
{
RAW_COMMON_BLOCK_OBJECT common_block_obj;
/* Define the number of available memory blocks in the pool. */
RAW_U32 raw_block_pool_available;
/* Define the head pointer of the available block pool. */
RAW_U8 *raw_block_pool_available_list;
} MEM_POOL;
内存池的结构非常简单,主要包括了通用阻塞结构、block数值,block起始指针。内存池下面可以包括若干个block,每个block的大小都是相等的,同时block之间是通过链表串联在一起的,这个我们看了后面的代码明白了。mempool的处理函数不多,三个,初始化、申请、释放函数。
RAW_U16 raw_block_pool_create(MEM_POOL *pool_ptr, RAW_U8 *name_ptr, RAW_U32 block_size, RAW_VOID *pool_start, RAW_U32 pool_size)
{
//MEM_POOL *tail_ptr; /* Working block pool pointer */
RAW_U32 blocks; /* Number of blocks in pool */
RAW_U8 *block_ptr; /* Working block pointer */
RAW_U8 *next_block_ptr; /* Next block pointer */
RAW_U8 *end_of_pool; /* End of pool area */
RAW_U8 block_align_mask;
#if (RAW_BLOCK_FUNCTION_CHECK > 0)
/* Check for invalid pool size. */
if (pool_size < (block_size + block_size) ) {
return RAW_BLOCK_SIZE_ERROR;
}
if (pool_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (pool_start == 0) {
return RAW_NULL_POINTER;
}
#endif
block_align_mask = sizeof(void *) - 1u;
if (((RAW_U32)pool_start & block_align_mask)){
return RAW_INVALID_ALIGN;
}
if ((pool_size & block_align_mask)) {
return RAW_INVALID_ALIGN;
}
if ((block_size & block_align_mask)) {
return RAW_INVALID_ALIGN;
}
/*Init the list*/
list_init(&pool_ptr->common_block_obj.block_list);
/* Setup the basic block pool fields. */
pool_ptr ->common_block_obj.name = name_ptr;
pool_ptr ->common_block_obj.block_way = 0;
/* Calculate the end of the pool's memory area. */
end_of_pool = (RAW_U8 *) pool_start + pool_size;
/* Walk through the pool area, setting up the available block list. */
blocks = 0;
block_ptr = (RAW_U8 *) pool_start;
next_block_ptr = block_ptr + block_size;
while (next_block_ptr <= end_of_pool) {
blocks++;
if (next_block_ptr == end_of_pool) {
break;
}
/* Setup the link to the next block. */
*((RAW_U8 * *) block_ptr) = next_block_ptr;
/* Advance to the next block. */
block_ptr = next_block_ptr;
/* Update the next block pointer. */
next_block_ptr = block_ptr + block_size;
}
/* Set the last block's forward pointer to NULL. */
*((RAW_U8 * *) block_ptr) = 0;
/* Save the remaining information in the pool control block. */
pool_ptr ->raw_block_pool_available = blocks;
pool_ptr ->raw_block_pool_available_list = (RAW_U8 *) pool_start;
return RAW_SUCCESS;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11