Operating Systems

Table of Contents

A2 Allocator

Basic info

first layer:

sometimes run out of space on heap, need more. need to tell OS that more virtual memory is needed — ask OS for more virtual memory using brk() (‘please extend the heap’). mmap() (‘create new virtual region in the address space’) is a generalised brk(). munmap() is undo for mmap().

every time you need memory, the mmu will try to translate and get page fault, which is done with page fault handler.

virtual memory regions are basically contract between OS and application on legal ranges of memory that we can access

demand paging:

  1. Program calls brk() to grow its heap

  2. brk() enlarges virtual memory address space. new pages are not mapped onto physical memory.

  3. Program tries to access new memory, processor page faults.

  4. Kernel assigns page frame to process, creates page table entry, and resumes execution. Program lives happily ever after in userland.

Assignment

allocate user space allocator. gets memory from kernel, divides that, etc.

requesting memory from kernel

getting started

void *mymalloc(size_t size) {
    return sbrk(size);
}

problems:

design options in-band list metadata screenshot.png

each node contains metadata (size, next, prev, is_free) and space for the object.

when freeing, merge with adjacent free areas. avoid fragmentation — non-contiguous free areas.

list metadata example

struct obj_metadata {
    size_t size;
    struct obj_metadata *next;
    struct obj_metadata *prev;
    int is_free;
};

don’t use artificial limit on max number of objects. should have pointer to heap_start, and pointer to freelist (both void). dynamically grow. would give penalty.

Grading