Lecture 29-31 - Lecture 29-31 11/8/10 11/12/10...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 29-31 – 11/8/10 – 11/12/10 Announcements Hwk 9 (last programming assignment) posted tomorrow Hwk 8 due tomorrow night Last Week Heap Management Heap Management LC-3 Control Unit Next Week Finish LC-3 Control Unit X86 ISA CS310 Fall 2010 - Boral Heap A segment of memory managed by the OS malloc(n) returns a pointer to a contiguous area of n bytes in memory free(ptr) returns the memory segment pointed to by ptr to the heap Heap in LC-3 A memory block starting at x9000 and growing towards the stack Heap manager Allocates memory segments from the heap in response to malloc requests Adds freed segments to the heap in response to free requests CS310 Fall 2010 - Boral Heap Manager Implementation 1 Use a linked list data structure to track contents of heap struct mem_struct { int start_address; int end_address; struct *mem_struct next; } One linked list for allocated memory; a second for free segments Initially: The free list consists of one element: the entire heap The allocated list is empty Every malloc() call carves a piece out of the heap Adding an element to the allocated list Every free() call Removes an element from the allocated list Adds an element to the free list CS310 Fall 2010 - Boral Example Allocate Free Initially A = malloc(x100); B = malloc(x10); C = malloc(x100); free(B); free(A); D = malloc(x111); free(D); CS310 Fall 2010 - Boral
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Some Issues In the example we had an opportunity to coalesce adjacent free blocks after the free(A) call It’s not too expensive since the free list is kept sorted In order to have a larger single free block available instead of several (adjacent) smaller blocks When allocating, should we find The best fit? The first fit? The frambulating fit? Keeping the free list sorted is expensive At the cost of a slower free() call Is a linked list the best data structure? Where does the heap end anyway? CS310 Fall 2010 - Boral Some Optimizations Embed free list directly into the free memory blocks 1 st cell in the free memory block holds free block size 2 nd cell in the free memory block points to the next free block Adding to the free list means: Modifying the first two cells in the block Linking it with the rest of the free blocks Do we need an allocate list? All we really need to know about each allocated block is its size Can allocate one more cell than requested and keep length there So, A = malloc(x10) Allocates x11 cells Stores x10 in the first cell Returns pointer to the 2 nd cell Requires a corresponding change to free() CS310 Fall 2010 - Boral
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Lecture 29-31 - Lecture 29-31 11/8/10 11/12/10...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online