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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章