CS 134

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 space
  • as_copy — create a new address space that is an exact copy of an old one
  • as_activate — make the current address space the one currently “seen” by the processor
  • as_deactivate — unload the current address space so it isn't currently “seen” by the processor
  • as_destroy — dispose of an address space
  • as_define_region — set up a region of memory within the address space
  • as_prepare_load — called before actually loading from an executable into the address space
  • as_complete_load — called when loading from an executable is complete
  • as_define_stack — set up the stack region in the address space
  • as_fault — handle fault in (the current) address space
  • as_sbrk — adjust the heap, to support the sbrk() 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 array
  • vm_object_array_destroy — destroy an allocated array
  • vm_object_array_init — initialize an array in space externally allocated
  • vm_object_array_cleanup — clean up an array in space externally allocated
  • vm_object_array_num — return number of elements in array
  • vm_object_array_get — return element number INDEX
  • vm_object_array_set — set element number INDEX to VAL
  • vm_object_array_preallocate — allocate space without changing size; may fail and return error
  • vm_object_array_setsize — change size to NUM elements; may fail and return error
  • vm_object_array_add — append VAL to end of array; return its index in INDEX_RET if INDEX_RET isn't null; may fail and return error
  • vm_object_array_remove — excise entry INDEX and 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

  1. Remove the fixed-size allocation
  2. Let vm_object_setsize handle 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.)