Docsity

Implementing Dynamic Memory Allocation in CMSC 212: Heap Manager - Prof. Jandelyn Dawn Pla, Exams of Computer Science

Partial preview of the text

Download Implementing Dynamic Memory Allocation in CMSC 212: Heap Manager - Prof. Jandelyn Dawn Pla and more Exams Computer Science in PDF only on Docsity!

Announcements

l

Midterm #2 is Tuesday– April 12– same room as the 1

st^

exam (ARM 0131)

  • time as the 1

st^

exam (6:00-7:15)

  • minimally comprehensive– class Tuesday

l

Reading– Notes

Implementing Dynamic Allocation

l

Where does memory come from?– make a call to OS to add memory to running process

  • On Linux, this is called sbrk(int incr)
    • Adds at least incr bytes to end of memory
      • Key idea, get big chunks of memory from OS
        • OS calls can be100x slower than local subroutines– Hand out small region on demand

l

How to manage it?– Need to keep data structures to

  • track what memory is in use• where are free memory regions
    • Requests to malloc (heap) and new (gc) update structures

Heap Manager Goals

l

Maximize Throughput– need each request to be fast– often ok if average is fast (some calls might be slow)

l

Minimize Wasted Space– What is the peak utilization of memory?– How much space is wasted at the peak?– Avoid

Fragmentation

  • wastes space• Internal - allocated space larger than malloc request• External - insufficient free space due to many small free

blocks

Simple Data Structure for Heap

l

Divide heap into blocks

l

Each Block has a 32 bit header– 29 bits define size of block (in 8 byte chunks)– 1 bit is for free/allocated– 2 bits are un-used

l

Can find next block if know the current block– add address of current block header to size of block– first number means size of chunk that is/was allocated– second number: 0 means free space and 1 means allocated

16/

24/

8/

24/

0/

8/

Coalescing Free Blocks

l

After free, may have up to three free units in a row– one before and one after

l

Merge a sequence of free blocks into a larger one

l

Might causing repeated split/merging– consider {malloc(n),free(n)}

  • if the first malloc involves a split, rest will too

l

Coalescing Implementation– Update first header block to be size of full free space– How to find the previous header?

  • search from start of list• include footer at end of block (also with size)• include explicit back pointer
    • store address of previous header in header

A Heap Implementation

#include <stdio.h>#include <unistd.h>typedef struct _header {

unsigned int size:29;unsigned int unusedBits:2;unsigned int allocated:1;struct _header *prev; } header;#define HEAP_INCR (1024 * 1024)static header *heap;

void mm_init() {

header *end;heap = (header *) sbrk(HEAP_INCR );heap->size = (HEAP_INCR-

(2*sizeof(header)))>>3;

heap->allocated = 0;heap->prev = NULL;end = heap + heap->size - 1;end->allocated = 0;end->size = 0;end->prev = heap; }

mm_allocate (cont)

} else if (!curr->size) {

/* no room in the heap, grow heap */temp = (header ) sbrk(HEAP_INCR);if (!temp) return NULL;curr->size = (HEAP_INCR- sizeof(header))>>3;curr->allocated = 0;/ update new end */ } } }

void mm_free(void *ptr) {

header *curr, *prev, *next, *nn;curr = ((header ) ptr) - 1;prev = curr->prev;next = curr + curr->size + 1;curr->allocated = 0;if (!next->allocated) { / merge next into current */

curr->size += next->size + 1;nn = next + next->size + 1;nn->prev = curr;next = nn;

/* for double merge case */

} if (prev && !prev->allocated) { /* merge current into prev */

prev->size += curr->size + 1;next->prev = prev; } }

How to make heaps safer

l

aux tag storage– create additional (redundant) info elsewhere in heap

l

preamble/trailer with known patterns– allocate 2 * Y bytes extra– fill out first and last Y bytes with a known pattern (non-zero)– return ptr Y bytes into region– on free (or when user requests)

  • verify y bytes at start and end haven't changed
    • mcheck_pedantic(NULL)