嵌入式操作系统内核原理和开发(等值block内存池设计)
·
嵌入式操作系统内核原理和开发(等值block内存池设计)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
内存池设计是嵌入式系统的一个重要环节,之前我们也讨论过相关的内容。但是,看了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;
- 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;
- }
(1)判断内存池、指针参数合法性;
(2)检验指针是否n字节对齐,n取决于地址的大小;
(3)构建block链表,前后相连,最后一个block指向NULL指针;
(4)将pool首地址赋值给raw_block_pool_available_list,函数返回。
- RAW_U16 raw_block_allocate(MEM_POOL *pool_ptr, RAW_VOID **block_ptr, RAW_U32 wait_option)
- {
- RAW_U16 status;
- RAW_U8 *work_ptr;
- RAW_SR_ALLOC();
- #if (RAW_BLOCK_FUNCTION_CHECK > 0)
- if (pool_ptr == 0) {
- return RAW_NULL_OBJECT;
- }
- if (block_ptr == 0) {
- return RAW_NULL_POINTER;
- }
- if (raw_int_nesting) {
- if (wait_option != RAW_NO_WAIT) {
- return RAW_NOT_CALLED_BY_ISR;
- }
- }
- #endif
- RAW_CRITICAL_ENTER();
- /* Determine if there is an available block. */
- if (pool_ptr ->raw_block_pool_available) {
- /* Yes, a block is available. Decrement the available count. */
- pool_ptr ->raw_block_pool_available--;
- /* Pickup the current block pointer. */
- work_ptr = pool_ptr ->raw_block_pool_available_list;
- /* Return the first available block to the caller. */
- *((RAW_U8 **)block_ptr) = work_ptr;
- /* Modify the available list to point at the next block in the pool. */
- pool_ptr ->raw_block_pool_available_list = *((RAW_U8 **)work_ptr);
- /* Set status to success. */
- status = RAW_SUCCESS;
- }
- /*if no block memory is available then do it depend wait_option*/
- else {
- if (wait_option == RAW_NO_WAIT) {
- *((RAW_U8 **)block_ptr) = 0;
- RAW_CRITICAL_EXIT();
- return RAW_NO_PEND_WAIT;
- }
- /*system is locked so task can not be blocked just return immediately*/
- if (raw_sched_lock) {
- *((RAW_U8 **)block_ptr) = 0;
- RAW_CRITICAL_EXIT();
- return RAW_SCHED_DISABLE;
- }
- raw_pend_object(&pool_ptr->common_block_obj, raw_task_active, wait_option);
- RAW_CRITICAL_EXIT();
- raw_sched();
- RAW_CRITICAL_ENTER();
- *((RAW_U8 **)block_ptr) = 0;
- status = block_state_post_process(raw_task_active, block_ptr);
- RAW_CRITICAL_EXIT();
- }
- return status;
- }
- RAW_U16 raw_block_release(MEM_POOL *pool_ptr, RAW_VOID *block_ptr)
- {
- LIST *block_list_head;
- RAW_U8 *work_ptr; /* Working block pointer */
- RAW_U8 need_schedule = 0;
- RAW_SR_ALLOC();
- #if (RAW_BLOCK_FUNCTION_CHECK > 0)
- if (block_ptr == 0) {
- return RAW_NULL_OBJECT;
- }
- if (pool_ptr == 0) {
- return RAW_NULL_OBJECT;
- }
- #endif
- block_list_head = &pool_ptr->common_block_obj.block_list;
- RAW_CRITICAL_ENTER();
- work_ptr = ((RAW_U8 *) block_ptr);
- if (is_list_empty(block_list_head)) {
- /* Put the block back in the available list. */
- *((RAW_U8 **) work_ptr) = pool_ptr ->raw_block_pool_available_list;
- /* Adjust the head pointer. */
- pool_ptr ->raw_block_pool_available_list = work_ptr;
- /* Increment the count of available blocks. */
- pool_ptr ->raw_block_pool_available++;
- }
- else {
- need_schedule = 1;
- wake_send_msg(list_entry(block_list_head->next, RAW_TASK_OBJ, task_list), block_ptr);
- }
- RAW_CRITICAL_EXIT();
- if (need_schedule) {
- raw_sched();
- }
- /* Return completion status. */
- return RAW_SUCCESS;
- }
openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。
更多推荐


所有评论(0)