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

📄 vm.c

📁 NecluesRTX RTOS的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
                                   
                    /* New header is right after the new trailer.  */
                    new_header_ptr =  (struct VM_BLOCK_HEADER_STRUCT *) 
                                      memory_ptr;
                    
                    /* Modify all of the allocation structures, starting with
                       the new block. */
                    
                    new_trailer_ptr -> vm_pattern_1 =  TOP_PATTERN;
                    new_trailer_ptr -> vm_header_ptr = header_ptr;
                    new_trailer_ptr -> vm_pattern_2 =  BOTTOM_PATTERN;
                    
                    new_header_ptr -> vm_pattern_1 =   TOP_PATTERN;
                    new_header_ptr -> vm_next_block =  NU_NULL;
                    new_header_ptr -> vm_prev_block =  NU_NULL;
                    new_header_ptr -> vm_task_id =     NU_SYSTEM;
                    new_header_ptr -> vm_in_use =      NU_FALSE;

                    /* New block size must account for the new header and 
                       trailer structures as well as the current allocation
                       request.  */
                    new_header_ptr -> vm_block_size =  
                             header_ptr -> vm_block_size - memory_size -
                             (sizeof(struct VM_BLOCK_HEADER_STRUCT)/
                              sizeof(unsigned int)) -
                             (sizeof(struct VM_BLOCK_TRAILER_STRUCT)/
                              sizeof(unsigned int));
                    new_header_ptr -> vm_pattern_2 =   BOTTOM_PATTERN;
        
                    header_ptr -> vm_block_size =      memory_size;
                    trailer_ptr -> vm_header_ptr =     new_header_ptr;
        
                    /* Return the amount of memory available in the new
                       block.  */
                    VM_Total_Available_Memory =  VM_Total_Available_Memory +
                                             new_header_ptr -> vm_block_size;

                    /* Now we need to append this new block to the available 
                       memory list.  */
                    VM_Place_On_List(
                         (struct VM_LIST_STRUCT **) &VM_Available_Head,
                         (struct VM_LIST_STRUCT **) &VM_Available_Tail,
                         (struct VM_LIST_STRUCT *)  new_header_ptr);
                }

                /* Mark the appropriate information in the header.  */
                header_ptr -> vm_task_id =  SKP_Get_Task_ID();
                header_ptr -> vm_in_use =   NU_TRUE;

                /* Set up memory pointer again for the return.  */
                memory_ptr =  (unsigned int *) header_ptr;
                memory_ptr =  memory_ptr +
                                  (sizeof(struct VM_BLOCK_HEADER_STRUCT)/ 
                                     sizeof(unsigned int));
            }
            else
            
                /* Move to the next block.  */
                header_ptr =  header_ptr -> vm_next_block;
        }
    }    

    /* Return the memory pointer to the caller.  */
    return(memory_ptr);
}                                           /* end of VM_Allocate_Memory */



/************************************************************************/
/*                                                                      */
/*  FUNCTION                                "VM_Deallocate_Memory"      */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This function deallocates a dynamic memory.                     */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*      William E. Lamie,  Accelerated Technology                       */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Service routines that request memory deallocation.              */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      IND_Major_System_Error              Memory slicked              */
/*      VM_Place_On_List                    Place partition on avail lst*/
/*      VM_Remove_From_List                 Remove from wait list       */
/*      VM_Allocate_Memory                  Allocate dynamic memory     */
/*      SKP_Ready_Task                      Place the task on ready lst */
/*      SKD_Leave_Task                      Leave the task if necessary */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*      memory_ptr                          Pointer to partition        */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*      return(status)                                                  */
/*                                                                      */
/************************************************************************/
signed int  VM_Deallocate_Memory(unsigned int *memory_ptr)
{

unsigned int      *temp_ptr;                /* Pointer to the memory    */
struct VM_BLOCK_HEADER_STRUCT 
                  *other_header_ptr,        /* Pointer to other control */
                  *header_ptr;              /* Pointer to control block */
struct VM_BLOCK_TRAILER_STRUCT 
                  *other_trailer_ptr,       /* Pointer to other control */
                  *trailer_ptr;             /* Pointer to the trailer   */
struct VM_SUSPENSION_STRUCT 
                  *suspend_ptr,             /* Pointer to suspend blocks*/
                  *temp_susp_ptr; 
signed int         context_switches;        /* Context switch counter   */
signed int         status;                  /* Return status indication */

    /* Get the pointers to the header and trailer of the block.  */
    temp_ptr =    memory_ptr - 
                   sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int);
    header_ptr =  (struct VM_BLOCK_HEADER_STRUCT *) temp_ptr;
    temp_ptr =    memory_ptr + header_ptr -> vm_block_size;
    trailer_ptr = (struct VM_BLOCK_TRAILER_STRUCT *) temp_ptr;
    
    /* First, determine if this memory pointer is a valid dynamic memory
       block and an allocated one. */
    if ((header_ptr -> vm_pattern_1 == TOP_PATTERN) &&
        (header_ptr -> vm_pattern_2 == BOTTOM_PATTERN) &&
        (header_ptr -> vm_in_use == NU_TRUE))
    {

        /* Check the trailing structure of the block.  */
        if ((trailer_ptr -> vm_pattern_1 != TOP_PATTERN) ||
            (trailer_ptr -> vm_pattern_2 != BOTTOM_PATTERN))
        {
        
            /* System error, something got slicked.  */
            IND_Major_System_Error(NU_MEMORY_SLICKED);
        }

        /* Valid memory header, free up the memory.  */
        header_ptr -> vm_in_use =  NU_FALSE;
        VM_Total_Available_Memory =  VM_Total_Available_Memory +
                                            header_ptr -> vm_block_size;
             
        /* Check to see if the block can be combined with previous block. */
        if (header_ptr > VM_Dynamic_Start)
        {

            /* Point to the trailer of the previous block.  */
            temp_ptr = memory_ptr - 
                   sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int) -
                   sizeof(struct VM_BLOCK_TRAILER_STRUCT)/sizeof(unsigned int);
            other_trailer_ptr =  (struct VM_BLOCK_TRAILER_STRUCT *) temp_ptr;
        
            /* Check the previous trailer for errors.  */
            if ((other_trailer_ptr -> vm_pattern_1 != TOP_PATTERN) ||
                (other_trailer_ptr -> vm_pattern_2 != BOTTOM_PATTERN))
            {  
            
                /* Major problem here...  someone has slicked some pointers. */
                IND_Major_System_Error(NU_MEMORY_SLICKED);
            }
            
            /* Point to the header of the previous block.  */
            other_header_ptr =  other_trailer_ptr -> vm_header_ptr;
            
            /* Check the previous header for errors.  */
            if ((other_header_ptr -> vm_pattern_1 != TOP_PATTERN) ||
                (other_header_ptr -> vm_pattern_2 != BOTTOM_PATTERN))
            {  
            
                /* Major problem here...  someone has slicked some pointers. */
                IND_Major_System_Error(NU_MEMORY_SLICKED);
            }

            /* Determine if the previous block is free.   */
            if (other_header_ptr -> vm_in_use == NU_FALSE)
            {
            
                /* The previous header is free, merge it with the current 
                   block.  */

                /* Update the block size to include the deallocated block. */
                other_header_ptr -> vm_block_size =  
                   other_header_ptr -> vm_block_size + 
                   sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int) +
                   sizeof(struct VM_BLOCK_TRAILER_STRUCT)/sizeof(unsigned int)+
                   header_ptr -> vm_block_size;

                 /* Update the deallocated tail pointer to point at the
                    previous header.  */
                 trailer_ptr -> vm_header_ptr =  other_header_ptr;
                 
                 /* Remove the previous block from the available list, 
                    temporarily.  The current header is always added at the
                    end.  */
                 VM_Remove_From_List(
                     (struct VM_LIST_STRUCT **) &VM_Available_Head,
                     (struct VM_LIST_STRUCT **) &VM_Available_Tail,
                     (struct VM_LIST_STRUCT *)  other_header_ptr);
                     
                 /* Slick the header and trailer pointers so subsequent
                    deallocation requests to the same block do not find
                    good headers and trailers.  */
                 header_ptr -> vm_pattern_1 =    NU_NULL;
                 header_ptr -> vm_pattern_2 =    NU_NULL;
                 header_ptr -> vm_in_use =       NU_FALSE;
                 other_trailer_ptr -> vm_pattern_1=  NU_NULL;
                 other_trailer_ptr -> vm_pattern_2=  NU_NULL;

                 /* Adjust the header.  */
                 header_ptr =  other_header_ptr;
                 
                 /* Add a little extra to the total available memory to 
                    account for the two control structures.  */
                 VM_Total_Available_Memory =  VM_Total_Available_Memory +
                   sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int) +
                   sizeof(struct VM_BLOCK_TRAILER_STRUCT)/sizeof(unsigned int);
            }
        }
        
        /* Now check to see if the block can be combined with the following
           block.  */
        if (trailer_ptr < VM_Dynamic_End)
        {
        
            /* Point to the header of the next block.  */
            temp_ptr =  ((unsigned int *) trailer_ptr) + 
                 sizeof(struct VM_BLOCK_TRAILER_STRUCT)/sizeof(unsigned int);
            other_header_ptr =  (struct VM_BLOCK_HEADER_STRUCT *) temp_ptr;
        
            /* Check the next header for errors.  */
            if ((other_header_ptr -> vm_pattern_1 != TOP_PATTERN) ||
                (other_header_ptr -> vm_pattern_2 != BOTTOM_PATTERN))
            {  
            
                /* Major problem here...  someone has slicked some pointers. */
                IND_Major_System_Error(NU_MEMORY_SLICKED);
            }
            
            /* Point to the trailer of the next block.  */
            temp_ptr =  temp_ptr + 
                   sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int) +
                   other_header_ptr -> vm_block_size;
            other_trailer_ptr =  (struct VM_BLOCK_TRAILER_STRUCT *) temp_ptr;
            
            /* Check the next trailer for errors.  */
            if ((other_trailer_ptr -> vm_pattern_1 != TOP_PATTERN) ||
                (other_trailer_ptr -> vm_pattern_2 != BOTTOM_PATTERN))
            {  

⌨️ 快捷键说明

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