IT3106E SP 04 Malloc-Basic
IT3106E SP 04 Malloc-Basic
Instructors:
Randal E. Bryant and David R. O’Hallaron
Today
¢ Basic concepts
¢ Implicit free lists
0
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 3
Carnegie Mellon
malloc Example
#include <stdio.h>!
#include <stdlib.h>!
!
void foo(int n) {!
int i, *p;!
!
/* Allocate a block of n ints */!
p = (int *) malloc(n * sizeof(int));!
if (p == NULL) {!
perror("malloc");!
exit(0);!
}!
!
/* Initialize allocated block */!
for (i=0; i<n; i++)!
p[i] = i;!
!
!
/* Return allocated block to the heap */!
free(p);!
}!
Alloca/on Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
Constraints
¢ Applica/ons
§ Can issue arbitrary sequence of malloc and free requests
§ free request must be to a malloc’d block
¢ Allocators
§ Can’t control number or size of allocated blocks
§ Must respond immediately to malloc requests
i.e., can’t reorder or buffer requests
§
§ Must allocate blocks from free memory
§ i.e., can only place allocated blocks in free memory
§ Must align blocks so they sa;sfy all alignment requirements
§ 8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes
§ Can manipulate and modify only free memory
§ Can’t move the allocated blocks once they are malloc’d
§ i.e., compac;on is not allowed
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 9
Carnegie Mellon
¢ Throughput:
§ Number of completed requests per unit ;me
§ Example:
§ 5,000 malloc calls and 5,000 free calls in 10 seconds
§ Throughput is 1,000 opera;ons/second
Fragmenta/on
¢ Poor memory u/liza/on caused by fragmenta@on
§ internal fragmenta;on
§ external fragmenta;on
Internal Fragmenta/on
¢ For a given block, internal fragmenta@on occurs if payload is
smaller than block size
Block
Internal Internal
Payload
fragmenta/on fragmenta/on
¢ Caused by
§ Overhead of maintaining heap data structures
§ Padding for alignment purposes
§ Explicit policy decisions
(e.g., to return a big block to sa;sfy a small request)
External Fragmenta/on
¢ Occurs when there is enough aggregate heap memory,
but no single free block is large enough
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
Implementa/on Issues
¢ How do we know how much memory to free given just a
pointer?
p0
p0 = malloc(4) 5
free(p0)
5 4 6 2
5 4 6 2
Today
¢ Basic concepts
¢ Implicit free lists
Unused
Start
of 8/0 16/1 32/0 16/1 0/1
heap
§ Can take linear ;me in total number of blocks (allocated and free)
§ In prac;ce it can cause “splinters” at beginning of list
¢ Next fit:
§ Like first fit, but search list star;ng where previous search finished
§ Should o`en be faster than first fit: avoids re-scanning unhelpful blocks
§ Some research suggests that fragmenta;on is worse
¢ Best fit:
§ Search the list, choose the best free block: fits, with fewest bytes le` over
§ Keeps fragments small—usually improves memory u;liza;on
§ Will typically run slower than first fit
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 21
Carnegie Mellon
4 4 6 2
addblock(p, 4)
4 4 4 2 2
4 4 4 2 2
free(p) p
4 4 4 2 2
malloc(5) Oops!
There is enough free space, but the allocator won’t be able to find it
4 4 4 2 2
logically
p
free(p) gone
4 4 6 2 2
void free_block(ptr p) {
*p = *p & -2; // clear allocated flag
next = p + *p; // find next block
if ((*next & 1) == 0)
*p = *p + *next; // add to this block if
} // not allocated
4 4 4 4 6 6 4 4
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+m1+m2 0
m1 0
n 1
n 1
m2 0
m2 0 n+m1+m2 0
¢ Can it be op/mized?
§ Which blocks need the footer tag?
§ What does that mean?
¢ Splifng policy:
§ When do we go ahead and split free blocks?
§ How much internal fragmenta;on are we willing to tolerate?
¢ Coalescing policy:
§ Immediate coalescing: coalesce each ;me free is called
§ Deferred coalescing: try to improve performance of free by deferring
coalescing un;l needed. Examples:
§ Coalesce as you scan the free list for malloc
§ Coalesce when the amount of external fragmenta;on reaches
some threshold 32
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on
Carnegie Mellon