📄 vm.c
字号:
/************************************************************************/
/* */
/* Copyright 1990-1992 by Accelerated Technology */
/* */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the */
/* subject matter of this material. All manufacturing, reproduction, */
/* use, and sales rights pertaining to this subject matter are */
/* governed by the license agreement. The buyer or recipient of this */
/* package, implicitly accepts the terms of the license. */
/* */
/************************************************************************/
/************************************************************************/
/* */
/* FILE DESCRIPTION */
/* */
/* This file contains routines that facilitate dynamic memory */
/* management. */
/* */
/* ROUTINES */
/* */
/* VM_Initialize Variable Memory Init */
/* VM_Place_On_List Place on a list */
/* VM_Remove_From_List Remove from a list */
/* VM_Allocate_Memory Allocate memory */
/* VM_Deallocate_Memory Deallocate memory */
/* VM_Wait_For_Memory Wait to memory */
/* VM_Available Memory currently available */
/* */
/* NOTES */
/* */
/* Routines except for VM_Max_Memory and VM_Available expect */
/* that interrupts are locked out upon invocation. */
/* */
/************************************************************************/
/* Define necessary include files. */
#include "nu_defs.h" /* General constants */
#include "in_extr.h" /* Initialize references */
#include "vm_defs.h" /* Data structure defns */
#include "sk_extr.h" /* Scheduler routine defs */
/* Define global data structures to the dynamic memory manager. */
/* The pointer "VM_Available_Head" is used to point to the list of
available memory blocks. */
struct VM_BLOCK_HEADER_STRUCT *VM_Available_Head;
/* The pointer "VM_Dynamic_Start" is used to keep track of the first
header in memory. */
struct VM_BLOCK_HEADER_STRUCT *VM_Dynamic_Start;
/* The pointer "VM_Available_Tail" is used to point to the list of
available memory area. */
struct VM_BLOCK_HEADER_STRUCT *VM_Available_Tail;
/* The pointer "VM_Dynamic_End" is used to point to the end of the
available memory area. */
struct VM_BLOCK_TRAILER_STRUCT *VM_Dynamic_End;
/* The pointer "VM_Suspend_Head" is used to point to the list of suspended
tasks on memory. */
struct VM_SUSPENSION_STRUCT *VM_Suspend_Head;
/* The pointer "VM_Suspend_Tail" is used to point to the list of suspended
tasks on memory. */
struct VM_SUSPENSION_STRUCT *VM_Suspend_Tail;
/* The unsigned variable "VM_Total_Available_Memory" contains the total size
of available memory, in terms of unsigned variables. */
unsigned long VM_Total_Available_Memory;
/* Define internal function prototypes. */
void VM_Place_On_List(struct VM_LIST_STRUCT **ptr_head_ptr,
struct VM_LIST_STRUCT **ptr_tail_ptr,
struct VM_LIST_STRUCT *block_ptr);
void VM_Remove_From_List(struct VM_LIST_STRUCT **ptr_head_ptr,
struct VM_LIST_STRUCT **ptr_tail_ptr,
struct VM_LIST_STRUCT *block_ptr);
unsigned int * VM_Allocate_Memory(unsigned long memory_size);
/************************************************************************/
/* */
/* FUNCTION "VM_Initialize" */
/* */
/* */
/* DESCRIPTION */
/* */
/* This function is used to allocate and initialize all of the */
/* dynamic memory control structures. */
/* */
/* AUTHOR */
/* */
/* William E. Lamie, Accelerated Technology */
/* */
/* CALLED FROM */
/* */
/* INP_Initialize High level initialization */
/* */
/* ROUTINES CALLED */
/* */
/* VM_Place_On_List Put on a linked list */
/* */
/* INPUTS */
/* */
/* start_address Starting address */
/* end_address Ending address */
/* */
/* OUTPUTS */
/* */
/* VM_Available_Head Available list pointer */
/* VM_Available_Tail Available list pointer */
/* VM_Suspend_Head Task suspension head */
/* VM_Suspend_Tail Task suspension tail */
/* VM_Total_Available_Memory Total available memory */
/* */
/************************************************************************/
void VM_Initialize(unsigned int *start_address, unsigned int *end_address)
{
struct VM_BLOCK_HEADER_STRUCT
*header_ptr; /* Pointer to block header */
struct VM_BLOCK_TRAILER_STRUCT
*trailer_ptr; /* Pointer to block trailer */
unsigned char *start_byte_addr; /* Starting byte address */
unsigned char *end_byte_addr; /* Ending byte address */
void VM_Place_On_List(); /* Function forward ref */
/* Initialize the control variables. */
VM_Available_Head = NU_NULL;
VM_Available_Tail = NU_NULL;
VM_Suspend_Head = NU_NULL;
VM_Suspend_Tail = NU_NULL;
/* Convert the start and end address into unsigned char pointers to
eliminate the arithmetic. */
start_byte_addr = (unsigned char *) start_address;
end_byte_addr = (unsigned char *) end_address;
/* Determine if the result is greater than 0. If so, there is some
amount of dynamic memory. */
if ((end_byte_addr - start_byte_addr) >
(sizeof(struct VM_BLOCK_HEADER_STRUCT) +
sizeof(struct VM_BLOCK_TRAILER_STRUCT)))
{
/* Calculate the amount of available memory. */
VM_Total_Available_Memory = (end_byte_addr -
start_byte_addr -
sizeof(struct VM_BLOCK_HEADER_STRUCT) -
sizeof(struct VM_BLOCK_TRAILER_STRUCT))/
sizeof(unsigned int);
/* Initialize the header and trailer of the block in memory. */
header_ptr = (struct VM_BLOCK_HEADER_STRUCT *) start_address;
header_ptr -> vm_pattern_1 = TOP_PATTERN;
header_ptr -> vm_next_block = NU_NULL;
header_ptr -> vm_prev_block = NU_NULL;
header_ptr -> vm_task_id = NU_SYSTEM;
header_ptr -> vm_in_use = NU_FALSE;
header_ptr -> vm_block_size = VM_Total_Available_Memory;
header_ptr -> vm_pattern_2 = BOTTOM_PATTERN;
start_address = start_address +
sizeof(struct VM_BLOCK_HEADER_STRUCT)/sizeof(unsigned int)
+ VM_Total_Available_Memory;
trailer_ptr = (struct VM_BLOCK_TRAILER_STRUCT *) start_address;
trailer_ptr -> vm_pattern_1 = TOP_PATTERN;
trailer_ptr -> vm_header_ptr = header_ptr;
trailer_ptr -> vm_pattern_2 = BOTTOM_PATTERN;
/* Now we need to append this 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 *) header_ptr);
/* Mark the beginning and end control blocks of dynamic memory so
we dont try to search for blocks past them. */
VM_Dynamic_Start = header_ptr;
VM_Dynamic_End = trailer_ptr;
}
else
/* Reset the total available variable to 0 for future benefit. */
VM_Total_Available_Memory = 0;
} /* end of VM_Initialize */
/************************************************************************/
/* */
/* FUNCTION "VM_Place_On_List" */
/* */
/* */
/* DESCRIPTION */
/* */
/* This function is used to link a structure on a specified */
/* linked list. */
/* */
/* AUTHOR */
/* */
/* William E. Lamie, Accelerated Technology */
/* */
/* CALLED FROM */
/* */
/* VM_Wait_For_Memory Wait for memory */
/* VM_Allocate_Memory Allocate memory */
/* VM_Deallocate_Memory Deallocate memory */
/* */
/* ROUTINES CALLED */
/* */
/* None */
/* */
/* INPUTS */
/* */
/* ptr_head_ptr Pointer to list head ptr */
/* ptr_tail_ptr Pointer to list tail ptr */
/* block_ptr Pointer to suspend block */
/* */
/* OUTPUTS */
/* */
/* Linked List */
/* */
/************************************************************************/
void VM_Place_On_List(struct VM_LIST_STRUCT **ptr_head_ptr,
struct VM_LIST_STRUCT **ptr_tail_ptr,
struct VM_LIST_STRUCT *block_ptr)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -