Docsity

Implementing Dynamic Memory Allocation: Heap Manager, Exams of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 – F05 (lect 16)
Announcements
●Midterm #2 is Thursday, November 3
– Same time and place as the 1st exam
– If you have a conflict – send me email right away
●Program #4
●Reading
– No required reading
– If you have Bryant & O’Hallaron – see Chapter 10 sect 9 &10
2
CMSC 212 – F05 (lect 16)
Implementing Dynamic Allocation
●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
●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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Implementing Dynamic Memory Allocation: Heap Manager and more Exams Computer Science in PDF only on Docsity!

CMSC 212 – F05 (lect 16)^1

Announcements

l Midterm #2 is Thursday, November 3

  • Same time and place as the 1st^ exam
  • If you have a conflict – send me email right away

l Program

l Reading

  • No required reading
  • If you have Bryant & O’Hallaron – see Chapter 10 sect 9 &

CMSC 212 – F05 (lect 16)^2

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

CMSC 212 – F05 (lect 16)^3

Heap Manager Requirements

l Handle arbitrary sequence of requests to allocate and

deallocate

  • other than a free of an address can't occur before the corresponding malloc.

l Must respond immediately

  • can't wait for other requests (can’t buffer the requests)

l All data it uses must be stored in the heap itself

  • no aux data structures

l Must be aligned so as to hold any data

  • roundup the the next natural alignment if needed

l Can't modify allocated blocks by changing size etc.

  • pointers exist in the program
  • must only modify free blocks of memory

CMSC 212 – F05 (lect 16)^4

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 because of need to cut on “natural boundaries”
  • External - insufficient free space due to many small free blocks as in a series of free and malloc that left the available blocks in a unusable configuration

CMSC 212 – F05 (lect 16)^7

Coalescing Free Blocks

l After free, may have up to three free units in a row

  • one before and one after and the newly freed block

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

CMSC 212 – F05 (lect 16)^8

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- (2sizeof(header)))>>3; heap->allocated = 0; heap->prev = NULL;

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

CMSC 212 – F05 (lect 16)^9

void *mm_alloc(unsigned int size) { header *curr, *temp, next; if (!heap) mm_init(); for (curr=heap;; curr = curr + curr->size + 1) { if (curr->size >= size && !curr->allocated) { temp = curr + ((size + 7) >> 3) + 1; next = curr + curr->size + 1; if (temp < (next - 1)) { / split current node */ temp->size = next - temp - 1; temp->allocated = 0; temp->prev = curr; next->prev = temp; curr->size = temp - curr - 1; curr->allocated = 1; } return ((void *)(curr + 1));

CMSC 212 – F05 (lect 16)^10

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 */ } } }

CMSC 212 – F05 (lect 16)^13

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

l mcheck_pedantic(NULL);

  • from <mcheck.h>

CMSC 212 – F05 (lect 16)^14

Garbage Collection

l What is Garbage?

  • memory the program can't get to anymore
  • Example:
    • ptr = malloc(40);
    • ptr = NULL;

l How to detect Garbage?

  • Need to know what is a pointer
    • know what everything is (Java)
    • guess anything that looks like a pointer is one (C)
      • any 32-bit quantity in globals or on stack is assumed to be a pointer
      • anything in a memory region pointed to
  • Compute reachability graph
    • Follow all pointers

CMSC 212 – F05 (lect 16)^15

Reachability Graph

Not-reachable (garbage) Reachable^ variables (Inside the program)

CMSC 212 – F05 (lect 16)^16

Mark and Sweep Garbage Collection

l Mark Function

  • each allocated block has a "marked" field
  • isPtr returns header for ptr (if in heap) or null

void mark(ptr p) { ptr b; b = isPtr(p); if (!b) return; if (b->marked) return; b->marked = 1; for (i=0; i < b->size; i++) { mark(b + i + 1); } }