Threadx 字节内存池内存分配_tx_byte_allocate

_tx_byte_allocate

_tx_byte_allocate用于内存分配,内存分配原理参考上篇博文:Threadx 内存管理-内存字节池

UINT    _tx_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr,
                          ULONG memory_size,  ULONG wait_option)
{

    TX_INTERRUPT_SAVE_AREA

    REG_1   UINT        status;                 /* Return status              */
    REG_2   TX_THREAD   *thread_ptr;            /* Working thread pointer     */
    REG_3   CHAR_PTR    work_ptr;               /* Working byte pointer       */


    /* Pickup the thread pointer.  */
    #def 临时记录申请内存分配的当前线程
    thread_ptr =  _tx_thread_current_ptr;

    /* Round the memory size up to the next size that is evenly divisible by
       an ULONG.  This guarantees proper alignment.  */
    #def 计算申请内存大小,ULONG大小对齐
    memory_size = ((memory_size + sizeof(ULONG) - 1) / sizeof(ULONG)) * sizeof(ULONG);
	
    /* Disable interrupts.  */
    #def 禁止中断,防止打断下面操作
    TX_DISABLE

    /* Loop to handle cases where the owner of the pool changed.  */
    #def 循环操作
    do
    {

        /* Indicate that this thread is the current owner.  */
        #def 先标记内存池拥有者为当前线程
        pool_ptr -> tx_byte_pool_owner =  thread_ptr;

        /* Restore interrupts.  */
        #def 恢复中断,后面操作可能被高优先级打断,而高优先级线程也可能在这个内存池申请分配内存
        TX_RESTORE

        /* At this point, the executing thread owns the pool and can perform a search
           for free memory.  */
        work_ptr =  _tx_byte_pool_search(pool_ptr, memory_size);

        /* Lockout interrupts.  */
        TX_DISABLE

    }
    #def 如果申请失败,并且内存池拥有者不在是thread_ptr线程,说明被搞优先级线程抢占,并且在这个内存池申请分配内存;这个线程恢复后,循环继续,再次尝试内存申请
    while ((!work_ptr) && (pool_ptr -> tx_byte_pool_owner != thread_ptr));

    /* Determine if memory was found.  */
    #def 申请到了内存,函数返回成功
    if (work_ptr)
    {

        /* Copy the pointer into the return destination.  */
        *memory_ptr = (VOID *) work_ptr;

        /* Set the status to success.  */
        status =  TX_SUCCESS;
    }
    else
    {
	     #def 内存不够,申请失败,如果wait_option不为0,挂起线程到tx_byte_pool_suspension_list,后面
	     #def 其它线程释放内存后,在_tx_byte_release中重新申请内存,恢复线程
        /* No memory of sufficient size was found...  */

        /* Determine if the request specifies suspension.  */
        if (wait_option)
        {
			#def 挂起线程
            /* Prepare for suspension of this thread.  */

            /* Setup cleanup routine pointer.  */
            thread_ptr -> tx_suspend_cleanup =  _tx_byte_pool_cleanup;

            /* Setup cleanup information, i.e. this pool control
               block.  */
            #def 记录内存池管理结构指针
            thread_ptr -> tx_suspend_control_block = (VOID_PTR) pool_ptr;

            /* Save the return memory pointer address as well.  */
            #def 记录应用程序传递的用来保存申请到的内存起始地址指针
            thread_ptr -> tx_additional_suspend_info = (VOID_PTR) memory_ptr;

            /* Save the byte size requested.  */
            #def 记录需要申请分配内存大小
            thread_ptr -> tx_suspend_info =  memory_size;

            /* Setup suspension list.  */
            #def 挂起到tx_byte_pool_suspension_list线程,按照fifo,并没有按照优先级高低顺序
            if (pool_ptr -> tx_byte_pool_suspension_list)
            {

                /* This list is not NULL, add current thread to the end. */
                thread_ptr -> tx_suspended_next =
                    pool_ptr -> tx_byte_pool_suspension_list;
                thread_ptr -> tx_suspended_previous =
                    (pool_ptr -> tx_byte_pool_suspension_list) -> tx_suspended_previous;
                ((pool_ptr -> tx_byte_pool_suspension_list) -> tx_suspended_previous) -> tx_suspended_next =
                    thread_ptr;
                (pool_ptr -> tx_byte_pool_suspension_list) -> tx_suspended_previous =   thread_ptr;
            }
            else
            {

                /* No other threads are suspended.  Setup the head pointer and
                   just setup this threads pointers to itself.  */
                pool_ptr -> tx_byte_pool_suspension_list =  thread_ptr;
                thread_ptr -> tx_suspended_next =           thread_ptr;
                thread_ptr -> tx_suspended_previous =       thread_ptr;
            }

            /* Increment the suspended thread count.  */
            pool_ptr -> tx_byte_pool_suspended_count++;

            /* Set the state to suspended.  */
            thread_ptr -> tx_state =       TX_BYTE_MEMORY;

            /* Set the suspending flag.  */
            thread_ptr -> tx_suspending =  TX_TRUE;

            /* Temporarily disable preemption.  */
            _tx_thread_preempt_disable++;

            /* Save the timeout value.  */
            thread_ptr -> tx_thread_timer.tx_remaining_ticks =  wait_option;

            /* Restore interrupts.  */
            TX_RESTORE

            /* See if we need to start a timer.  */
            #def 不等于TX_WAIT_FOREVER,开启定时器,定时器超时会清除申请内存相关操作,并返回申请失败
            if (wait_option != TX_WAIT_FOREVER)
            {

                /* A timeout is required.  */

                /* Activate the thread timer for the timeout.  */
                _tx_timer_activate(&(thread_ptr -> tx_thread_timer));
            }

            /* Call actual thread suspension routine.  */
            _tx_thread_suspend(thread_ptr);

            /* Return the completion status.  */
            #def 线程恢复后,从这里返回,tx_suspend_status在_tx_byte_release或_tx_byte_pool_cleanup中设置
            return (thread_ptr -> tx_suspend_status);
        }
        else
        {

            /* Immediate return, return error completion.  */
            status =  TX_NO_MEMORY;
        }
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return completion status.  */
    return (status);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章