Lec10-Dynamic_memory_1
Lec10-Dynamic_memory_1
CS61, Lecture 10
Prof. Stephen Chong
October 4, 2011
Announcements 1/2
•Assignment 4: Malloc
•Will be released today
•May work in groups of one or two
• Please go to website and enter your group by Sunday
11:59PM
‣ Every group must do this (i.e., even if you are working individually)
•Two deadlines:
• Design checkpoint: Thursday October 13, 10pm
• Final submission: Thursday October 20, 11:59pm
•Encourage you to use version control
•Assignment 3 (Buffer bomb) due Thursday
Stephen Chong, Harvard University 2
Announcements 2/2
Shared libraries
0x40000000
Dynamically allocated
memory Heap (used by malloc)
Read/write segment
Global vars .data, .bss
Read-only segment
Program code .text, .rodata
0x08048000
unused
Stephen Chong, Harvard University 0x00000000 6
The heap
• The heap is the region of a program's memory used for dynamic
allocation.
• Program can allocate and free blocks of memory within the heap.
Heap
Free byte
Allocated block Free block
(4 bytes) (3 bytes) Allocated byte
• Program may request memory, which splits up the the free space.
...
• Program may free up some memory, which increases the free space
...
• Over time the heap will contain a mixture of free and allocated blocks.
...
... ...
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
Internal Internal
fragmentation payload fragmentation
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(6)
No free block big enough!
Stephen Chong, Harvard University 20
Topics for today
p = malloc(4) p
5
data
header
(address p-1)
free(p)
0x0f
0x0f in binary: 0000 1111
allocated = 0
15 bytes total size size = 000 1111 = 0xf = 15 bytes
payload
Implicit list
5 4 6 2
Free Allocated Free Allocated
p
void addblock(ptr_t p, int len) {
int oldsize = *p; // Get old size of free block
*p = len | 0x80; // Set new size + alloc bit
if (len < oldsize)
*(p+len) = oldsize - len; // Set size of remaining
} // part of free block
addblock(p, 4)
2 4 4 2 2
Stephen Chong, Harvard University 29
Implicit List: Freeing a Block
• Simplest implementation:
• Simply clear the allocated bit in the header
/* Here, p points to the block header. */
/* This sets the high-order bit to 0. */
void free_block(ptr_t p) { *p = *p & 0x7f; }
4 4 4 2 2
p
free(p+1)
4 4 4 2 2
4 4 4 2 2
p
free(p+1)
4 4 4 2 2
malloc(5)
• There is enough free space, but the allocator won’t be able to find it!
Stephen Chong, Harvard University 31
Implicit List: Coalescing
• Coalesce with adjacent block(s) if they are free
void free_block(ptr_t p) {
*p = *p & 0x7f; // Clear allocated bit
next = p + *p; // Find next block
if ((*next & 0x80) == 0)
*p = *p + *next; // Add to this block if
} // next is also free
4 4 4 2 2
p
free(p+1)
4 4 6 2
4 4 4 4 6 6 4 4
“Pointers” in footers
• Allows us to traverse the “block list” backwards, but requires extra space
m1 1 m1 1
m1 1 m1 1
n 1 n 0
n 1 n 0
m2 1 m2 1
m2 1 m2 1
m1 1 m1 1
m1 1 m1 1
n 1 n+m2 0
n 1
m2 0
m2 0 n+m2 0
m1 0 n+m1 0
m1 0
n 1
n 1 n+m1 0
m2 1 m2 1
m2 1 m2 1
m1 0 n 0
m1 0
n 1
n 1
m2 0
m2 0 n 0
Forward links
4 4 4 4 6 6 4 4 4 4
Back links
A B C
Forward links
4 4 4 4 6 6 4 4 4 4
Back links
p = malloc(size);
After:
Root
•Insert the freed block at the root of the free block list
After:
Root
Root
After:
Root
Root
• Splice out successor block, coalesce both memory blocks and insert
the new block at the root of the list
After:
Root
Root
After:
Root
5-8
9-inf
•Often have separate size class for every small size (4,5,6,…)
•For larger sizes typically have a size class for each power of 2
•Allocation requirements
•Common memory bugs
•Implicit memory management: Garbage
collection