Monday, August 12, 2013

Variable Vs Fixed size memory pool: Allocation

① Difference between variable size memory pool and fixed size memory pool

Fixed memory pool takes very less processing compared to the variable memory pool.

In variable memory pool, the memory is fragmented into small pieces. So, finally you will end up with fragmentation problem. Also, for each fragment, additional memory of two words (size of header) is getting wasted for management purposes. (Note that in implementation, the return pointer is two words advanced of internal allocated block start address.)
 
So, it is better to try to use fixed size memory pool as can as possible. And, the variable memory pool may increase the interrupt latency when using inside interrupt service routines.

② ITRON implementation of get_mpl()

ER get_mpl(ID mplid, SIZE blksz, VP p_blk)
{
        Check context. If called from ISR,
                return E_CTX;
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        User requested size is rounded up to multiple of Basic unit size(Header size)
        Calculate Internal_required_size( = Rounded up value + Header size)
        current_block = head_pointer->next;
        for (each free memory block from top of the list) {
                /* till the next pointer is NULL */
                /* keep pointer to previous and next nodes */
                if (current_block == NULL)
                        goto No_Block;
                compare the current block size with internal_required_size
                if (current block size >= internal_required_size) {
                        acquired_block = current block;
                        break;
                }
                current block = current block's next pointer;
        }
        if (acquired block size == internal_required_size) {
                Just remove the acquired block from the list
                /* previous node's next = acquired block's next */
        } else {
                /* acquired block is cut into two pieces. new_block is born */
                new_block start address = acquired block ptr + internal_required_size;
                the previous node's next = new_block start address
                new_block's next = acquired block's next pointer
                new_block's size = acquired block's size - internal_required size;
        }
No_Block:
        if (no block available) {
                If (polling mode)
                        Exit critical section and return E_TMOUT;
                If (called from task independent state ex: timer handlers) or
                If (called from dispatch disabled state)
                        Return E_CTX;
                If (Timeout value is specified) {
                        Add task into timer queue;
                        Set TCB flags that 'Waiting for memory block, Check for Timeout'
                } else (Wait forever) {
                        Set TCB flags that 'Waiting forever for memory block'
                }
                Set memory pool ID in TCB
                Also save other parameters requested block size, pointer to return
                        memory block pointer inside context
                Select Queue according to queueing order
                If (Tasks should be granted in FIFO order)
                        Select Flag->Queue[0] as Queue
                Else (Should be granted in Priority order)
                        Select Memorypool->Queue[Current Tasks Priority]
                Change the task from ReadyQueue to the Memory pool Queue
                Schedule the tasks, since this task is going to sleep
                (Once the task is released, execution will return here)
                Return the value set by scheduler(Scheduler directly set return value before return here)
        }
        /* keep the user requested size */
        allocated block's first word(next pointer) = user requested size + header size;
        Advance the acquired block's address by two words and store it in the return pointer;
        Exit critical secion
        Return E_OK;
}

③ ITRON implementation of get_mpf()

ER get_mpf(ID mpfid, VP p_blf)
{
        Sanity check on arguments
        Enter critical section (Interrupt Disable)
        Check head pointer of the memory pool
        if (head points to NULL (no block available)) {
                If (polling mode)
                        Exit critical section and return E_TMOUT;
                If (called from task independent state ex: timer handlers) or
                If (called from dispatch disabled state)
                        Return E_CTX;
                If (Timeout value is specified) {
                        Add task into timer queue;
                        Set TCB flags that 'Waiting for fixed memory block, Check for Timeout'
                } else (Wait forever) {
                        Set TCB flags that 'Waiting forever for fixed memory block'
                }
                Set memory pool ID in TCB
                Also save the parameter 'pointer to return memory block pointer' inside context
                Select Queue according to queueing order
                If (Tasks should be granted in FIFO order)
                        Select Flag->Queue[0] as Queue
                Else (Should be granted in Priority order)
                        Select Memorypool->Queue[Current Tasks Priority]
                Change the task from ReadyQueue to the Memory pool Queue
                Schedule the tasks, since this task is going to sleep
                (Once the task is released, execution will return here)
                Return the value set by scheduler(Scheduler directly set return value before return here)
        }
        /* remove the block at the head from the list */
        pointer to the block = pointer of the block pointed by head
        head = pointer to the next block
        /* pointer to the next block is stored temporarily at the first word of each block */
        Reduce the counter for number of free blocks
        Exit critical secion
        Return E_OK;
}

Getting into reality of Real Time OS! Visit other posts under RTOS lable!!

No comments: