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

📄 os_dmem.c.bak

📁 uCosII是一个小型的多任务调度内核
💻 BAK
📖 第 1 页 / 共 2 页
字号:
    /* Adjust the request to a size evenly divisible by the number of bytes
       in an UNSIGNED data element.  Also, check to make sure it is of the
       minimum size.  */
    if (size < pool->dm_min_allocation)
    
        /* Change size to the minimum allocation.  */
        size =  pool->dm_min_allocation;
    else
    
        /* Insure that size is a multiple of the UNSIGNED size.  */
        size = 
           ((size + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) * sizeof(UNSIGNED);

    /* Protect against simultaneous access to the memory pool.  */
    OS_ENTER_CRITICAL();
    
    /* Search the memory list for the first available block of memory that
       satisfies the request.  Note that blocks are merged during the 
       deallocation function.  */
    memory_ptr =  pool->dm_search_ptr;
    do
    {
    
        /* Determine if the block is free and if it can satisfy the request. */
        if (memory_ptr->dm_memory_free)
        
            /* Calculate the free block size.  */
            free_size =  (((CHAR *) (memory_ptr->dm_next_memory)) -
                           ((CHAR *) memory_ptr)) - DM_OVERHEAD;
        else
        
            /* There are no free bytes available.  */
            free_size =  0;
            
        /* Determine if the search should continue.  */
        if (free_size < size)
        
            /* Large enough block has not been found.  Move the search 
               pointer to the next block.  */
            memory_ptr =  memory_ptr->dm_next_memory;
    } while((free_size < size) && (memory_ptr != pool->dm_search_ptr));
    
    /* Determine if the memory is available.  */
    if (free_size >= size)
    {
    
        /* A block that satisfies the request has been found.  */
        
        /* Determine if the block needs to be split.  */
        if (free_size >= (size + DM_OVERHEAD + pool->dm_min_allocation))
        {
        
            /* Yes, split the block.  */
            new_ptr =  (DM_HEADER *) (((CHAR *) memory_ptr) + size +
                                                DM_OVERHEAD);

            /* Mark the new block as free.  */
            new_ptr->dm_memory_free =  TRUE;
            
            /* Put the pool pointer into the new block.  */
            new_ptr->dm_memory_pool =  pool;

            /* Build the necessary pointers.  */
            new_ptr->dm_previous_memory =  memory_ptr;
            new_ptr->dm_next_memory =      memory_ptr->dm_next_memory;
            (new_ptr->dm_next_memory)->dm_previous_memory =  new_ptr;
            memory_ptr->dm_next_memory =   new_ptr;
            
            /* Decrement the available byte count.  */
            pool->dm_available =  pool->dm_available - size - DM_OVERHEAD;
        }
        else
        
            /* Decrement the entire free size from the available bytes 
               count.  */
            pool->dm_available =  pool->dm_available - free_size;

        /* Mark the allocated block as not available.  */
        memory_ptr->dm_memory_free =  FALSE;

        /* Should the search pointer be moved?   */
        if (pool->dm_search_ptr == memory_ptr)
        
            /* Move the search pointer to the next free memory slot.  */
            pool->dm_search_ptr =  memory_ptr->dm_next_memory;
        
        /* Return a memory address to the caller.  */
        *return_pointer =  (VOID *) (((CHAR *) memory_ptr) + DM_OVERHEAD);
    }
    else 
    {
        
	    /* No suspension requested.  Simply return an error status.  */
	    status =            OS_MEM_FULL;        
	    *return_pointer =   NULL;
    }
    
    /* Release protection of the memory pool.  */
    OS_EXIT_CRITICAL();

    /* Return the completion status.  */
    return(status);
}



INT8U OS_FreeMem(VOID *memory)
{

	R1 DM_PCB      *pool;                       /* Pool pointer              */
	R1 DM_HEADER   *header_ptr;                 /* Pointer to memory hdr     */
	R1 DM_HEADER   *new_ptr;                    /* New memory block pointer  */
	INT8U          status;                     /* Completion status         */
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0;
#endif



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

    /* Pickup the associated pool's pointer.  It is inside the header of
       each memory.  */
    header_ptr =  (DM_HEADER *) (((CHAR *) memory) - DM_OVERHEAD);
    pool =        header_ptr->dm_memory_pool;
    
    /* Protect against simultaneous access to the memory pool.  */
    OS_ENTER_CRITICAL();
    
    /* Mark the memory as available.  */
    header_ptr->dm_memory_free =  TRUE;

    /* Adjust the available number of bytes.  */
    pool->dm_available =  pool->dm_available +
                        (((CHAR *) (header_ptr->dm_next_memory)) -
                           ((CHAR *) header_ptr)) - DM_OVERHEAD;

    /* Determine if the block can be merged with the previous neighbor.  */
    if ((header_ptr->dm_previous_memory)->dm_memory_free)
    {
    
        /* Adjust the available number of bytes.  */
        pool->dm_available =  pool->dm_available + DM_OVERHEAD;

        /* Yes, merge block with previous neighbor.  */
        (header_ptr->dm_previous_memory)->dm_next_memory =  
                                header_ptr->dm_next_memory;
        (header_ptr->dm_next_memory)->dm_previous_memory =
                                header_ptr->dm_previous_memory;
                                
        /* Move header pointer to previous.  */
        header_ptr =  header_ptr->dm_previous_memory;
        
        /* Adjust the search pointer to the new merged block.  */
        pool->dm_search_ptr =  header_ptr;
    }
    
    /* Determine if the block can be merged with the next neighbor.  */
    if ((header_ptr->dm_next_memory)->dm_memory_free)
    {
    
        /* Adjust the available number of bytes.  */
        pool->dm_available =  pool->dm_available + DM_OVERHEAD;

        /* Yes, merge block with next neighbor.  */
        new_ptr =  header_ptr->dm_next_memory;
        (new_ptr->dm_next_memory)->dm_previous_memory =
                                                header_ptr;
        header_ptr->dm_next_memory = new_ptr->dm_next_memory;

        /* Adjust the search pointer to the new merged block.  */
        pool->dm_search_ptr =  header_ptr;
    }
    
    /* Release protection of the memory pool.  */
    OS_EXIT_CRITICAL();

    /* Return the completion status.  */
    return(status);
}

void OS_DMemInit(void)
{

    /* Initialize the created dynamic memory pool list to NU_NULL.  */
    DMD_Created_Pools_List =  NULL;
    
    /* Initialize the total number of created pools to 0.  */
    DMD_Total_Pools =  0;
    
}

#define OS_SYSTEM_MEMPOOL_SIZE  0xa00000

void *OS_SysPoolInit(void *first)
{
	INT32U del, mem;
	INT8U err;

	mem = (INT32U)first;
	del = mem%4;
	mem += (4 - del);
	err = OS_Create_Memory_Pool(&OS_system_mempool, "System", (void *)mem, OS_SYSTEM_MEMPOOL_SIZE, 16);
	assert(err == OS_NO_ERR);

	mem += OS_SYSTEM_MEMPOOL_SIZE;
	return (void *)mem;
}

void *OS_malloc(u32_t size)
{
	INT8U err;
	void *pointer;

	err = OS_GetMem(&OS_system_mempool, &pointer, size);
	return (err == OS_NO_ERR?pointer : NULL);
}

void  OS_free(void *ptr)
{
	OS_FreeMem(ptr);
}


⌨️ 快捷键说明

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