上面是内存池的创建函数,入参共有五个参数,分别是mempool结构、名称、block大小、pool起始地址、pool大小。函数基本内容如下所示,

  (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;
 
  }