13 Malloc Basic
13 Malloc Basic
Instructors:
Brian Railing
Today
Basic concepts
Implicit free lists
malloc Example
#include <stdio.h>
#include <stdlib.h>
void foo(long n) {
long i, *p;
Highest address
within heap
Lowest address (“the break”, adjustable
within heap by sbrk system call)
Allocation Example
(Conceptual)
p1 = malloc(32)
p2 = malloc(40)
free(p2)
p4 = malloc(16)
Constraints
Applications
▪ Can issue arbitrary sequence of malloc and free requests
▪ free request must be to a malloc’d block
Explicit 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 satisfy all alignment requirements
▪ 16-byte (x86-64) alignment on 64-bit systems
▪ Can manipulate and modify only free memory
▪ Can’t move the allocated blocks once they are malloc’d
▪ i.e., compaction is not allowed. Why not?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9
Carnegie Mellon
Throughput:
▪ Number of completed requests per unit time
▪ Example:
▪ 5,000 malloc calls and 5,000 free calls in 10 seconds
▪ Throughput is 1,000 operations/second
Benchmark Example
Step Command Delta Allocated Peak
Benchmark 1 a 0 9904 9904 9904 9904
2 a 1 50084 50084 59988 59988
syn-array-short 3 a 2 20 20 60008 60008
4 a 3 16784 16784 76792 76792
▪ Trace provided with f 3
5 -16784 60008 76792
malloc lab 6 a 4 840 840 60848 76792
▪ Allocate & free 10 blocks 7 a 5 3244 3244 64092 76792
8 f 0 -9904 54188 76792
▪ a = allocate 9 a 6 2012 2012 56200 76792
▪ f = free 10 f 2 -20 56180 76792
11 a 7 33856 33856 90036 90036
▪ Bias toward allocate at
12 f 1 -50084 39952 90036
beginning & free at end 13 a 8 136 136 40088 90036
▪ Blocks number 1–10 14 f 7 -33856 6232 90036
15 f 6 -2012 4220 90036
▪ Allocated: Sum of all 16 a 9 20 20 4240 90036
allocated amounts 17 f 4 -840 3400 90036
▪ Peak: Max so far of 18 f 8 -136 3264 90036
Allocated 19 f 5 -3244 20 90036
20 f 9 -20 0 90036
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12
Carnegie Mellon
Benchmark Visualization
Step Command Delta Allocated Peak 1
1 a 0 9904 9904 9904 9904
2 a 1 50084 50084 59988 59988
1.2
0.8
PeakFit
Data
0.6 Allocated
Data
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Operation / Operation Count
Fragmentation
Poor memory utilization caused by fragmentation
▪ Internal fragmentation
▪ External fragmentation
Internal Fragmentation
For a given block, internal fragmentation occurs if payload is
smaller than block size
Block
Internal Internal
Payload
fragmentation fragmentation
Caused by
▪ Overhead of maintaining heap data structures
▪ Padding for alignment purposes
▪ Explicit policy decisions
(e.g., to return a big block to satisfy a small request)
1.2
1.0
Memory Used / Peak Data
0.8
Peak + Fit
Perfect Internal Frag
PeakFit
Data
0.6
Allocated
Data
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Operation / Operation Count
External Fragmentation
Occurs when there is enough aggregate heap memory,
but no single free block is large enough
p1 = malloc(32)
p2 = malloc(40)
p3 = malloc(48)
free(p2)
1.2
1.0
Memory Used / Peak Data
0.8
PeakFit
Best + All Frag (Best Fit)
Peak + Fit
Perfect Internal Frag
0.6 PeakFit
Data
Data
Allocated
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Operation / Operation Count
Implementation Issues
How do we know how much memory to free given just a
pointer?
p0
p0 = malloc(32)
48
Need space
32 48 32 16
for pointers
Today
Basic concepts
Implicit free lists
End
Unused Block
Start
of 16/0 32/1 64/0 32/1 8/1
heap
heap_start heap_end
block size
End
Unused Block
Comparing Strategies
1.4
1.2
1.0
Memory Used / Peak Data
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Operation / Operation Count
32 32 48 16 8
split_block(p, 32)
32 32 32 16 16 8
64 16 32 32 16
32 32 32 16 16 8
free(p) p
32 32 32 16 16 8
malloc(5*SIZ) Yikes!
There is enough contiguous
free space, but the allocator
won’t be able to find it
32 32 32 16 16 8
logically
free(p) p
gone
32 32 48 16 16 1
64 32 16 16 8
logically
free(p) p
gone
64 48 16 16 8
8 32 32 32 32 48 48 32 32 8
Quiz
https://canvas.cmu.edu/courses/30386/quizzes
asize
asize
dsize
1 word
64 64 16 32 32 32 32 16
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
Heap Structure
Dummy Dummy
Footer Header
Start
of 8/1 16/0 32/1 64/0 32/1 8/1
heap
heap_start heap_end
if (block == NULL)
return NULL;
split_block(block, asize);
return header_to_payload(block);
}
coalesce_block(block);
}
Size a
1 word 1 word
Allocated Free
Block Block
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 51
Carnegie Mellon
block n 11 n 10
being
freed n 10
m2 11 m2 01
next
block
block n 11 n+m2 10
being
freed
m2 10
next
block m2 10 n+m2 10
Splitting policy:
▪ When do we go ahead and split free blocks?
▪ How much internal fragmentation are we willing to tolerate?
Coalescing policy:
▪ Immediate coalescing: coalesce each time free is called
▪ Deferred coalescing: try to improve performance of free by deferring
coalescing until needed.