13 Malloc Basic 1
13 Malloc Basic 1
Announcements
⬛ No lecture on Thursday October 14
Today
⬛Basic concepts ⬛
Implicit free lists
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Carnegie Mellon Memory
Dynamic Memory
Allocation
Application
Dynamic Memory Allocator
Heap
Run-time heap
(created by malloc)
Read/write segment
(.data, .bss)
Read-only segment
(.init, .text, .rodata) Unused
%rsp
(stack
pointer)
brk
Loaded
from
the
executable file
malloc Example
#include <stdio.h>
#include <stdlib.h>
void foo(long n) {
long i, *p;
(2 words)
Free word Allocated word
p2 = malloc(5*SIZ)
p4 = malloc(2*SIZ)
⬛ 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 11
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
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12
Carnegie Mellon
Benchmark Example
allocated amounts
⬛Benchmark ▪ Peak: Max so far of
Allocated
syn-array-short
Step C
▪ Trace provided with
1 a
malloc lab
▪ Allocate & free 10 blocks ▪ a = allocate 2 a
50
▪ f = free
3 a
▪ Bias toward allocate at beginning &
4 a
free at end 1
▪ Blocks number 1–10 5 f
▪ Allocated: Sum of all
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14
Carnegie Mellon
Benchmark Visualization
13 a 8 136 136 40088 90036
0.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Step
0.2
Allocated Peak
as a function of �� (step)
▪ Y-axis normalized — fraction of maximum
Behavior
Typical 1.4
1.2
1.0
Benchmark
0.8
0.6
0.4
0.2
DAllocated ata Peak
Data Fit
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
Fragmentation
Effect 1.4 Perfect Fit
Data Fit
DAllocated ata
Peak + Internal FragPeak
1.2
1.0
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
External #define SIZ sizeof(size_t)
⬛ Occurs when there is enough aggregate heap memory,
but no single free block is large enough
p1 = malloc(4*SIZ)
p2 = malloc(5*SIZ)
p3 = malloc(6*SIZ)
free(p2)
External
Fragmentation
Effect 1.4
Best Fit
Perfect Fit
Data Fit
DAllocated ata
Peak + All Frag (Best Fit)Peak + Internal Frag Peak
1.2
1.0
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(4*SIZ)
48
block size Payload (aligned)
Padding
(for alignment)
free(p0)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23
Carnegie Mellon
Unused
list using length—links all blocks Need to tag each block as allocated/free
Today
⬛Basic concepts ⬛
Implicit free lists
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25
Carnegie Mellon
heap_start heap_end
Headers: labeled with “size in
words/allocated bit” Headers are at
Double-word aligned
non-aligned positions
Allocated blocks: shaded
➔ Payloads are aligned
Free blocks: unshaded
⬛ Initializing header
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29
Carnegie Mellon
block size
End
Block 8/1
▪ Search list from beginning, choose first free block that fits: ▪ Can
take linear time in total number of blocks (allocated and free) ▪ In
practice it can cause “splinters” at beginning of list
⬛ Next fit:
▪ Like first fit, but search list starting where previous search finished ▪
Should often be faster than first fit: avoids re-scanning unhelpful blocks ▪
Some research suggests that fragmentation is worse
⬛ Best fit:
▪ Search the list, choose the best free block: fits, with fewest bytes left over ▪
Keeps fragments small—usually improves memory utilization ▪ Will typically
run slower than first fit
▪ Still a greedy algorithm. No guarantee of optimality
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32
Carnegie Mellon
0.6
0.4
0.2
Comparing
Strategies 1.4
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 16
32 32 16
8
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34
Carnegie Mellon
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 logically
1
free(p) p
gone
32 32 16
48 16
64 logically
block?
free(p) p
▪ How do we know where it starts?
64 16 ▪ How can we determine whether its
allocated?
48 16 gone
8
Size blocks
a = 0: Free block Payload and padding
Format of
Header Size: Total block size
a = 1: Allocated block allocated and free Payload: Application data
a (allocated blocks only)
Boundary tag
Size a (footer)
Quiz
https://canvas.cmu.edu/courses/24383/quizzes/67237
1 word
64
64 16 32 32 32 32 16
m1 1 n 0
m1 1
n 0 m2 1
m1 1 n 1
m2 1
n 1 m2 1
m2 1
m1 1 n+m2
m1 1 0
m1 1 n 1
n 1 m2 0
n+m2 0
m2 0
m1 0
m1 0 n 1 n+m1 0 m2 1
n 1 m2 1 m2 1
m2 1
m1 0
m1 0 n 1
n 1 m2 0
m2 0
n+m1+m2 0
heap_start heap_end
find_fit(asize);
round_up(n, m) =
if (block == NULL) m *((n+m-1)/m)
return NULL;
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 50
Carnegie Mellon
coalesce_block(block);
}
Free
Block
padding
Allocate
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 53
Carnegie Mellon
previous m2 10 m2 10
block
n+m1+m2
block
being
freed
next
block
m1 ?0
m1 ?0 n 01
Header: Use 2 bits (address bits always zero due to alignment):
(previous block allocated)<<1 | (current block
allocated)
⬛ 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.