⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_dmem.c.bak

📁 uCosII是一个小型的多任务调度内核
💻 BAK
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------
[BASIC]  
{                                                              
	[FILENAME]  dmem.c                                                   
	[CONTENT]   Dynamic memory manegement
	[AUTHOR]    Wangyf 
	[VERSION]   070716.01
	[COMPANY]   CEC Huada Electronic Design CO.,LTD                                      
}

[MOD]
{
	2007.07.16  Create this file
	
}
----------------------------------------------------------------*/   
#include <assert.h>
#include "ucos_ii.h"
#include "os_dmem.h"

/* DMD_Created_Pools_List is the head pointer of the linked list of 
   created dynamic memory pools.  If the list is NU_NULL, there are no 
   dynamic memory pools created.  */
   
CS_NODE *DMD_Created_Pools_List;

/* DMD_Total_Pools contains the number of currently created 
   dynamic memory pools.  */
UNSIGNED        DMD_Total_Pools;

/* Control block of system memory pool */
DM_PCB OS_system_mempool;

void CSC_Place_On_List(CS_NODE **head, CS_NODE *new_node)
{

    /* Determine if the list in non-empty.  */
    if (*head)
    {
        /* The list is not empty.  Add the new node to the end of
           the list.  */
        new_node->cs_previous =               (*head)->cs_previous;
        (new_node->cs_previous)->cs_next =  new_node;
        new_node->cs_next =                   (*head);
        (new_node->cs_next)->cs_previous =  new_node;
    }
    else
    {
    
        /* The list is empty, setup the head and the new node.  */
        (*head) =  new_node;
        new_node->cs_previous =  new_node;
        new_node->cs_next =      new_node;
    }
}

void CSC_Remove_From_List(CS_NODE **head, CS_NODE *node)
{

    /* Determine if this is the only node in the system.  */
    if (node->cs_previous == node)
    {
        /* Yes, this is the only node in the system.  Clear the node's
           pointers and the head pointer.  */
        (*head) =              NULL;
    }
    else
    {
        /* Unlink the node from a multiple node list.  */
        (node->cs_previous)->cs_next =  node->cs_next;
        (node->cs_next)->cs_previous =  node->cs_previous;
        
        /* Check to see if the node to delete is at the head of the
           list. */
        if (node == *head)
            /* Move the head pointer to the node after.  */
            *head =  node->cs_next;
    }
}


INT8U  OS_Create_Memory_Pool(DM_PCB *pool_ptr, CHAR *name, 
                        void *start_address, UNSIGNED pool_size, 
                        UNSIGNED min_allocation)
{
	R1 DM_PCB      *pool;                       /* Pool control block ptr    */
	int             i;                          /* Working index variable    */
	DM_HEADER      *header_ptr;                 /* Partition block header ptr*/
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0;
#endif


    /* Move input pool pointer into internal pointer.  */
    pool =  (DM_PCB *) pool_ptr;


	#if KL_CHK_ARG_EN
	if (pool_ptr == NULL) return KL_INVALID_MEMPOOL;
	if ((start_address == NULL)||(start_address%4 != 0)) return KL_INVALID_MEMORY;
	if (pool_size <= min_allocation) return KL_INVALID_SIZE;
	#endif


    /* First, clear the partition pool ID just in case it is an old 
       pool control block.  */
    pool->dm_id =             0;
    
    /* Fill in the partition pool name.  */
    for (i = 0; i < OS_DMEM_NAME_SIZE; i++)
        pool->dm_name[i] =  name[i];    

    /* Convert the pool's size into something that is evenly divisible by
       the sizeof an UNSIGNED data element.  */
    pool_size =  (pool_size/sizeof(UNSIGNED)) * sizeof(UNSIGNED);

    /* Save the starting address and size parameters in the dynamic memory 
       control block.  */
    pool->dm_start_address =   start_address;
    pool->dm_pool_size =       pool_size;
    pool->dm_min_allocation =    
                ((min_allocation + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) *
                                                              sizeof(UNSIGNED);
    

    /* Initialize link pointers.  */
    pool->dm_created.cs_previous =    NULL;
    pool->dm_created.cs_next =        NULL;

    /* Build a single block that has all of the memory.  */
    header_ptr =  (DM_HEADER *) start_address;

    /* Initialize the memory parameters.  */
    pool->dm_available =       pool_size - (2 * DM_OVERHEAD);
    pool->dm_memory_list =     header_ptr;
    pool->dm_search_ptr =      header_ptr;
    
    /* Build the block header.  */
    header_ptr->dm_memory_pool =  pool;
    header_ptr->dm_memory_free =  TRUE;
    header_ptr->dm_next_memory =  (DM_HEADER *) 
           (((CHAR *) header_ptr) + pool->dm_available + DM_OVERHEAD);     
    header_ptr->dm_previous_memory =  header_ptr->dm_next_memory;
    
    /* Build the small trailer block that prevents block merging when the
       pool wraps around.  Note that the list is circular so searching can
       wrap across the physical end of the memory pool.  */
    header_ptr =  header_ptr->dm_next_memory;
    header_ptr->dm_next_memory =  (DM_HEADER *) start_address;
    header_ptr->dm_previous_memory =  (DM_HEADER *) start_address;
    header_ptr->dm_memory_pool =  pool;
    header_ptr->dm_memory_free =  FALSE;

    /* Protect against access to the list of created memory pools.  */
    OS_ENTER_CRITICAL();
    
    /* At this point the dynamic memory pool is completely built.  The ID can 
       now be set and it can be linked into the created dynamic memory
       pool list. */
    pool->dm_id =  DM_DYNAMIC_ID;

    /* Link the memory pool into the list of created memory pools and 
       increment the total number of pools in the system.  */
    CSC_Place_On_List(&DMD_Created_Pools_List, &(pool->dm_created));
    DMD_Total_Pools++;

    /* Release protection against access to the list of created memory
       pools.  */
    OS_EXIT_CRITICAL();

    /* Return successful completion.  */
    return(OS_NO_ERR);
}


INT8U OS_DelMemPool(DM_PCB *pool_ptr)
{

	R1 DM_PCB      *pool;                       /* Pool control block ptr    */
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0;
#endif

    /* Move input pool pointer into internal pointer.  */
    pool =  (DM_PCB *) pool_ptr;

    /* Protect against simultaneous access to the memory pool.  */
    OS_ENTER_CRITICAL();

    /* Clear the memory pool ID.  */
    pool->dm_id =  0;

    /* Remove the memory pool from the list of created memory pools.  */
    CSC_Remove_From_List(&DMD_Created_Pools_List, &(pool->dm_created));

    /* Decrement the total number of created memory pools.  */
    DMD_Total_Pools--;

    /* Release protection against access to the list of created memory
       pools. */
    OS_EXIT_CRITICAL();

    /* Return a successful completion.  */
    return(OS_NO_ERR);
}


INT8U OS_GetMem(DM_PCB *pool_ptr, void **return_pointer, 
                                        UNSIGNED size)
{

	R1 DM_PCB      *pool;                       /* Pool control block ptr    */
	R1 DM_HEADER   *memory_ptr;                 /* Pointer to memory         */
	R1 DM_HEADER   *new_ptr;                    /* New split block pointer   */
	UNSIGNED        free_size;                  /* Size of block found       */
	INT8U          status;                     /* Completion status         */
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0;
#endif


    /* Move input pool pointer into internal pointer.  */
    pool =  (DM_PCB *) pool_ptr;

    /* Initialize the status as successful.  */
    status =  OS_NO_ERR;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -