Address Space Management in OS/161
Core Data Structures
The address space of a program is described by the struct addrspace in OS/161. This structure contains the following fields:
struct addrspace {
struct vm_object_array *as_objects;
int as_heapobj;
vaddr_t as_break;
vaddr_t as_maxbreak;
};
Each struct vm_object in the as_objects array represents a region of memory in the address space.
Key Functions
as_create— create a new empty address spaceas_copy— create a new address space that is an exact copy of an old oneas_activate— make the current address space the one currently “seen” by the processoras_deactivate— unload the current address space so it isn't currently “seen” by the processoras_destroy— dispose of an address spaceas_define_region— set up a region of memory within the address spaceas_prepare_load— called before actually loading from an executable into the address spaceas_complete_load— called when loading from an executable is completeas_define_stack— set up the stack region in the address spaceas_fault— handle fault in (the current) address spaceas_sbrk— adjust the heap, to support thesbrk()system call
For the VM assignment, you mostly don't need to worry about the implementation of these functions. The one exception is as_sbrk.
vm_object_array Functions
The struct vm_object_array is a dynamic array of struct vm_object objects. It is defined using macros from array.h and provides the following functions:
vm_object_array_create— allocate an arrayvm_object_array_destroy— destroy an allocated arrayvm_object_array_init— initialize an array in space externally allocatedvm_object_array_cleanup— clean up an array in space externally allocatedvm_object_array_num— return number of elements in arrayvm_object_array_get— return element numberINDEXvm_object_array_set— set element numberINDEXtoVALvm_object_array_preallocate— allocate space without changing size; may fail and return errorvm_object_array_setsize— change size toNUMelements; may fail and return errorvm_object_array_add— appendVALto end of array; return its index inINDEX_RETifINDEX_RETisn't null; may fail and return errorvm_object_array_remove— excise entryINDEXand slide following entries down to close the resulting gap
Changes needed for VM Assignment to as_sbrk
Current Situation
In addrspace.c, there's currently a temporary hack in as_sbrk that allocates a fixed 2 MB heap:
result = as_define_region(as, base, 512*PAGE_SIZE, 0, 1, 1, 0);
/* ^-- temporary hack: 2MB heap */
This code provides a workaround for the unimplemented vm_object_setsize function, which you will be implementing.
Required Change
Once you have implemented vm_object_setsize, you should
- Remove the fixed-size allocation
- Let
vm_object_setsizehandle all heap size changes dynamically
The heap will then grow and shrink as needed, rather than having a fixed size.
Testing Impact
Remember to test that
- Small heap allocations work
- Large heap allocations work
- The heap can grow beyond 2 MB
- The heap can shrink properly
(When logged in, completion status appears here.)