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

📄 gfdfilesys.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
	DM_SUSPEND      suspend_block;              /* Allocate suspension block */
	R4 DM_HEADER   *memory_ptr;                 /* Pointer to memory         */
	R3 DM_HEADER   *new_ptr;                    /* New split block pointer   */
	UNSIGNED        free_size;                  /* Size of block found       */
	TC_TCB         *task;                       /* Task pointer              */
	STATUS          status;                     /* Completion status         */


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


#ifdef  NU_ENABLE_STACK_CHECK

    /* Call stack checking function to check for an overflow condition.  */
    TCT_Check_Stack();

#endif

#ifdef  NU_ENABLE_HISTORY

    /* Make an entry that corresponds to this function in the system history
       log.  */
    HIC_Make_History_Entry(NU_ALLOCATE_MEMORY_ID, (UNSIGNED) pool, 
                                (UNSIGNED) return_pointer, (UNSIGNED) size);

#endif

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

    /* 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.  */
//    TCT_Protect(&(pool -> dm_protect));
    
    /* 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 =  (((BYTE_PTR) (memory_ptr -> dm_next_memory)) -
                           ((BYTE_PTR) 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 *) (((BYTE_PTR) memory_ptr) + size +
                                                DM_OVERHEAD);

            /* Mark the new block as free.  */
            new_ptr -> dm_memory_free =  NU_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 =  NU_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 *) (((BYTE_PTR) memory_ptr) + DM_OVERHEAD);
    }
    else 
    {
          
            /* No suspension requested.  Simply return an error status.  */
            status =            NU_NO_MEMORY;        
            *return_pointer =   NU_NULL;
     }
    
    /* Release protection of the memory pool.  */
 //lmq   TCT_Unprotect();
    /* Return the completion status.  */
    return(status);
}
STATUS  SMC_Obtain_Semaphore(NU_SEMAPHORE *semaphore_ptr,  UNSIGNED suspend)
{
	SM_SCB *semaphore;
	semaphore = (SM_SCB *)semaphore_ptr;
    if(semaphore == (SM_SCB *)&NUF_FILE_SYSTEM_MUTEX){
    	twai_sem(1,(TMO)suspend);
    }

}
STATUS  SMC_Release_Semaphore(NU_SEMAPHORE *semaphore_ptr)
{
	SM_SCB *semaphore;
	semaphore = (SM_SCB *)semaphore_ptr;
    if(semaphore = (SM_SCB *)&NUF_FILE_SYSTEM_MUTEX){
    	sig_sem(1);
    }
 
}
STATUS  DMC_Create_Memory_Pool(NU_MEMORY_POOL *pool_ptr, CHAR *name, 
                        VOID *start_address, UNSIGNED pool_size, 
                        UNSIGNED min_allocation, OPTION suspend_type)
{
	UW psr;
	R1 DM_PCB      *pool;                       /* Pool control block ptr    */
	INT             i;                          /* Working index variable    */
	DM_HEADER      *header_ptr;                 /* Partition block header ptr*/


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


#ifdef  NU_ENABLE_STACK_CHECK

    /* Call stack checking function to check for an overflow condition.  */
    TCT_Check_Stack();

#endif

#ifdef  NU_ENABLE_HISTORY

    /* Make an entry that corresponds to this function in the system history
       log.  */
    HIC_Make_History_Entry(NU_CREATE_MEMORY_POOL_ID, (UNSIGNED) pool, 
                        (UNSIGNED) start_address, (UNSIGNED) pool_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 < NU_MAX_NAME; 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);
    
    /* Setup the dynamic memory pool suspension type.  */
    if (suspend_type == NU_FIFO)
       
        /* FIFO suspension is selected, setup the flag accordingly.  */
        pool -> dm_fifo_suspend =  NU_TRUE;
    else
    
        /* Priority suspension is selected.  */
        pool -> dm_fifo_suspend =  NU_FALSE;
        
    /* Clear the suspension list pointer.  */
    pool -> dm_suspension_list =  NU_NULL;
    
    /* Clear the number of tasks waiting on the dynamic memory pool.  */
    pool -> dm_tasks_waiting =  0;

    /* Initialize link pointers.  */
    pool -> dm_created.cs_previous =    NU_NULL;
    pool -> dm_created.cs_next =        NU_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 =  NU_TRUE;
    header_ptr -> dm_next_memory =  (DM_HEADER *) 
           (((BYTE_PTR) 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 =  NU_FALSE;

    /* Initialize the protection structure.  */
    pool -> dm_protect.tc_tcb_pointer =  NU_NULL;

    /* Protect against access to the list of created memory pools.  */
//lmq    TCT_Protect(&DMD_List_Protect);
    
    /* 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.  */
//lmq    TCT_Unprotect();
	
    /* Return successful completion.  */
    return(NU_SUCCESS);
}
STATUS  DMC_Deallocate_Memory(VOID *memory)
{
	UW psr;
	R1 DM_PCB      *pool;                       /* Pool pointer              */
	R3 DM_SUSPEND  *suspend_ptr;                /* Pointer to suspend block  */
	R2 DM_HEADER   *header_ptr;                 /* Pointer to memory hdr     */
	R4 DM_HEADER   *new_ptr;                    /* New memory block pointer  */
	UNSIGNED        size;                       /* Suspended task request    */
	UNSIGNED        free_size;                  /* Amount of free bytes      */
	STATUS          preempt;                    /* Preemption flag           */
	STATUS          status;                     /* Completion status         */



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

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

    /* Adjust the available number of bytes.  */
    pool -> dm_available =  pool -> dm_available +
                        (((BYTE_PTR) (header_ptr -> dm_next_memory)) -
                           ((BYTE_PTR) 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_

⌨️ 快捷键说明

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