13 Malloc Basic 12
13 Malloc Basic 12
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
%rsp
(stack
pointer)
brk
Loaded
from
the
executable file
malloc Example
#include <stdio.h>
#include <stdlib.h>
void foo(long n) {
long i, *p;
Visualization Conventions
⬛Show 8-byte words as squares ⬛
Allocations are double-word aligned
Free block
(2 words)
Free word Allocated word
Allocation Example
(Conceptual) p3 = malloc(6*SIZ)
p1 = malloc(4*SIZ) free(p2)
p2 = malloc(5*SIZ) p4 = malloc(2*SIZ)
#define SIZ sizeof(size_t)
Benchmark Example
▪ Allocated: Sum of all
⬛Benchmark allocated amounts
syn-array-short ▪ Peak: Max so far of
Allocated
▪ Trace provided with
Step C
malloc lab
a
▪ Allocate & free 10 blocks ▪ a = allocate 1
▪ f = free 2 a
50
▪ Bias toward allocate at beginning & 3 a
free at end
4 a
▪ Blocks number 1–10
20 f
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14
Carnegie Mellon
Benchmark Visualization
12 f 1 -50084 39952 90036
0.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Step
as a function of �� (step)
▪ Y-axis normalized — fraction of maximum
Benchmark
Typical
Behavior
1.4
1.2
1.0 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
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
⬛ Purple line: additional heap size due to
allocator’s data + padding for alignment
▪ For this benchmark, 1.5% overhead
▪ Cannot achieve in practice
▪ Especially since cannot move allocated blocks
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
Carnegie Mellon
Fragmentation
External #define SIZ sizeof(size_t)
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
⬛ Green line: additional heap size due to external fragmentation
⬛ Best Fit: One allocation strategy
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)
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
// block_t *block
// bp points to a payload
return (block_t *) ((unsigned char *) bp
- offsetof(block_t, payload));
⬛ Initializing header
block->header = size | alloc;
// block_t *block
Size a
block size
End
Block 8/1
Unused
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 30
Carnegie Mellon
▪ 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
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.