Carnegie Mellon
1
Dynamic Memory Alloca/on: Advanced Concepts 15-213: - - PowerPoint PPT Presentation
Carnegie Mellon Dynamic Memory Alloca/on: Advanced Concepts 15-213: Introduc0on to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy Bryant
Carnegie Mellon
1
Carnegie Mellon
2
Explicit ¡free ¡lists
Segregated ¡free ¡lists ¡ Garbage ¡collec/on ¡ Memory-‑related ¡perils ¡and ¡piBalls ¡
Carnegie Mellon
3
Method ¡1: ¡Implicit ¡free ¡list ¡using ¡length—links ¡all ¡blocks ¡ Method ¡2: ¡Explicit ¡free ¡list ¡among ¡the ¡free ¡blocks ¡using ¡pointers ¡ Method ¡3: ¡Segregated ¡free ¡list ¡
Method ¡4: ¡Blocks ¡sorted ¡by ¡size ¡
5 4 ¡ 2 ¡ 6 ¡ 5 4 ¡ 2 ¡ 6 ¡
Carnegie Mellon
4
Maintain ¡list(s) ¡of ¡free ¡blocks, ¡not ¡all ¡blocks ¡
Size ¡ Payload ¡and ¡ padding ¡ a ¡ Size ¡ a ¡ Size ¡ a ¡ Size ¡ a ¡ Next ¡ Prev ¡
Allocated ¡(as ¡before) ¡ Free ¡
Carnegie Mellon
5
Logically: ¡ Physically: ¡blocks ¡can ¡be ¡in ¡any ¡order ¡
A ¡ B ¡ C ¡ 4 ¡ 4 ¡ 4 ¡ 4 ¡ 6 ¡ 6 ¡ 4 ¡ 4 ¡ 4 ¡ 4 ¡ Forward ¡(next) ¡links ¡ Back ¡(prev) ¡links ¡ A ¡ B ¡ C ¡
Carnegie Mellon
6
conceptual ¡graphic ¡
Carnegie Mellon
7
InserAon ¡policy: ¡Where ¡in ¡the ¡free ¡list ¡do ¡you ¡put ¡a ¡newly ¡
Carnegie Mellon
8
Insert ¡the ¡freed ¡block ¡at ¡the ¡root ¡of ¡the ¡list ¡
conceptual ¡graphic ¡
Carnegie Mellon
9
Splice ¡out ¡predecessor ¡block, ¡coalesce ¡both ¡memory ¡blocks, ¡
conceptual ¡graphic ¡
Carnegie Mellon
10
Splice ¡out ¡successor ¡block, ¡coalesce ¡both ¡memory ¡blocks ¡and ¡
conceptual ¡graphic ¡
Carnegie Mellon
11
Splice ¡out ¡predecessor ¡and ¡successor ¡blocks, ¡coalesce ¡all ¡3 ¡
conceptual ¡graphic ¡
Carnegie Mellon
12
Comparison ¡to ¡implicit ¡list: ¡
Most ¡common ¡use ¡of ¡linked ¡lists ¡is ¡in ¡conjunc/on ¡with ¡
Carnegie Mellon
13
Method ¡1: ¡Implicit ¡list ¡using ¡length—links ¡all ¡blocks ¡ Method ¡2: ¡Explicit ¡list ¡among ¡the ¡free ¡blocks ¡using ¡pointers ¡ Method ¡3: ¡Segregated ¡free ¡list ¡
Method ¡4: ¡Blocks ¡sorted ¡by ¡size ¡
5 4 ¡ 2 ¡ 6 ¡ 5 4 ¡ 2 ¡ 6 ¡
Carnegie Mellon
14
Explicit ¡free ¡lists
Segregated ¡free ¡lists ¡ Garbage ¡collec/on ¡ Memory-‑related ¡perils ¡and ¡piBalls ¡
Carnegie Mellon
15
Each ¡size ¡class ¡of ¡blocks ¡has ¡its ¡own ¡free ¡list ¡ O\en ¡have ¡separate ¡classes ¡for ¡each ¡small ¡size ¡ For ¡larger ¡sizes: ¡One ¡class ¡for ¡each ¡two-‑power ¡size ¡
1-‑2 ¡ 3 ¡ 4 ¡ 5-‑8 ¡ 9-‑inf ¡
Carnegie Mellon
16
Given ¡an ¡array ¡of ¡free ¡lists, ¡each ¡one ¡for ¡some ¡size ¡class ¡ To ¡allocate ¡a ¡block ¡of ¡size ¡n: ¡
If ¡no ¡block ¡is ¡found: ¡
Carnegie Mellon
17
To ¡free ¡a ¡block: ¡
Advantages ¡of ¡seglist ¡allocators ¡
Carnegie Mellon
18
D. ¡Knuth, ¡“The ¡Art ¡of ¡Computer ¡Programming”, ¡2nd ¡edi/on, ¡
Wilson ¡et ¡al, ¡“Dynamic ¡Storage ¡AllocaAon: ¡A ¡Survey ¡and ¡
Carnegie Mellon
19
Explicit ¡free ¡lists
Segregated ¡free ¡lists ¡ Garbage ¡collec/on ¡ Memory-‑related ¡perils ¡and ¡piBalls ¡
Carnegie Mellon
20
Garbage ¡collecAon: ¡automa/c ¡reclama/on ¡of ¡heap-‑allocated ¡
Common ¡in ¡func/onal ¡languages, ¡scrip/ng ¡languages, ¡and ¡
Variants ¡(“conserva/ve” ¡garbage ¡collectors) ¡exist ¡for ¡C ¡and ¡C++ ¡
void foo() { int *p = malloc(128); return; /* p block is now garbage */ }
Carnegie Mellon
21
How ¡does ¡the ¡memory ¡manager ¡know ¡when ¡memory ¡can ¡be ¡
Must ¡make ¡certain ¡assump/ons ¡about ¡pointers ¡
Carnegie Mellon
22
Mark-‑and-‑sweep ¡collec/on ¡(McCarthy, ¡1960) ¡
Reference ¡coun/ng ¡(Collins, ¡1960) ¡
Copying ¡collec/on ¡(Minsky, ¡1963) ¡
Genera/onal ¡Collectors ¡(Lieberman ¡and ¡Hewii, ¡1983) ¡
For ¡more ¡informa/on: ¡ ¡
Carnegie Mellon
23
We ¡view ¡memory ¡as ¡a ¡directed ¡graph ¡
Root ¡nodes ¡ Heap ¡nodes ¡ Not-‑reachable ¡ (garbage) ¡ reachable ¡
A ¡node ¡(block) ¡is ¡reachable ¡ ¡if ¡there ¡is ¡a ¡path ¡from ¡any ¡root ¡to ¡that ¡node. ¡ Non-‑reachable ¡nodes ¡are ¡garbage ¡(cannot ¡be ¡needed ¡by ¡the ¡applica/on) ¡
Carnegie Mellon
24
Can ¡build ¡on ¡top ¡of ¡malloc/free ¡package ¡
When ¡out ¡of ¡space: ¡
Mark ¡bit ¡set ¡
free ¡ free ¡
Note: ¡arrows ¡ here ¡denote ¡ memory ¡refs, ¡not ¡ free ¡list ¡ptrs. ¡ ¡
Carnegie Mellon
25
Applica/on ¡
Each ¡block ¡will ¡have ¡a ¡header ¡word ¡
Instruc/ons ¡used ¡by ¡the ¡Garbage ¡Collector ¡
Carnegie Mellon
26
ptr mark(ptr p) { if (!is_ptr(p)) return; // do nothing if not pointer if (markBitSet(p)) return; // check if already marked setMarkBit(p); // set the mark bit for (i=0; i < length(p); i++) // call mark on all words mark(p[i]); // in the block return; }
ptr sweep(ptr p, ptr end) { while (p < end) { if markBitSet(p) clearMarkBit(); else if (allocateBitSet(p)) free(p); p += length(p); }
Carnegie Mellon
27
A ¡“conserva/ve ¡garbage ¡collector” ¡for ¡C ¡programs ¡
So ¡how ¡to ¡find ¡the ¡beginning ¡of ¡the ¡block? ¡
Header ¡ ptr ¡ Head ¡ Data ¡ Le\ ¡ Right ¡
Size ¡
Le\: ¡smaller ¡addresses ¡ Right: ¡larger ¡addresses ¡
Carnegie Mellon
28
Explicit ¡free ¡lists
Segregated ¡free ¡lists ¡ Garbage ¡collec/on ¡ Memory-‑related ¡perils ¡and ¡piBalls ¡
Carnegie Mellon
29
Dereferencing ¡bad ¡pointers ¡ Reading ¡unini/alized ¡memory ¡ Overwri/ng ¡memory ¡ Referencing ¡nonexistent ¡variables ¡ Freeing ¡blocks ¡mul/ple ¡/mes ¡ Referencing ¡freed ¡blocks ¡ Failing ¡to ¡free ¡blocks ¡
Carnegie Mellon
30
Operators ¡ ¡ ¡ ¡ ¡AssociaAvity ¡ () [] -> . lee ¡to ¡right ¡ ! ~ ++ -- + - * & (type) sizeof right ¡to ¡lee ¡ * / % lee ¡to ¡right ¡ + - lee ¡to ¡right ¡ << >> lee ¡to ¡right ¡ < <= > >= lee ¡to ¡right ¡ == != lee ¡to ¡right ¡ & lee ¡to ¡right ¡ ^ lee ¡to ¡right ¡ | lee ¡to ¡right ¡ && lee ¡to ¡right ¡ || lee ¡to ¡right ¡ ?: right ¡to ¡lee ¡ = += -= *= /= %= &= ^= != <<= >>= right ¡to ¡lee ¡ , lee ¡to ¡right ¡
¡->, ¡(), ¡and ¡[] ¡have ¡high ¡precedence, ¡with ¡* ¡and ¡& ¡just ¡below ¡ Unary ¡+, -, ¡and ¡* ¡have ¡higher ¡precedence ¡than ¡binary ¡forms ¡
Source: ¡K&R ¡page ¡53 ¡
Carnegie Mellon
31
int *p int *p[13] int *(p[13]) int **p int (*p)[13] int *f() int (*f)() int (*(*f())[13])() int (*(*x[3])())[5] p ¡is ¡a ¡pointer ¡to ¡int ¡ p ¡is ¡an ¡array[13] ¡of ¡pointer ¡to ¡int ¡ p ¡is ¡an ¡array[13] ¡of ¡pointer ¡to ¡int ¡ p ¡is ¡a ¡pointer ¡to ¡a ¡pointer ¡to ¡an ¡int ¡ p ¡is ¡a ¡pointer ¡to ¡an ¡array[13] ¡of ¡int ¡ f ¡is ¡a ¡func0on ¡returning ¡a ¡pointer ¡to ¡int ¡ f ¡is ¡a ¡pointer ¡to ¡a ¡func0on ¡returning ¡int ¡ f ¡is ¡a ¡func0on ¡returning ¡ptr ¡to ¡an ¡array[13] ¡
x ¡is ¡an ¡array[3] ¡of ¡pointers ¡ ¡to ¡func0ons ¡ ¡ returning ¡pointers ¡to ¡array[5] ¡of ¡ints ¡ Source: ¡K&R ¡Sec ¡5.12 ¡
Carnegie Mellon
32
The ¡classic ¡scanf ¡bug ¡
Carnegie Mellon
33
Assuming ¡that ¡heap ¡data ¡is ¡ini/alized ¡to ¡zero ¡
Carnegie Mellon
34
Alloca/ng ¡the ¡(possibly) ¡wrong ¡sized ¡object ¡
Carnegie Mellon
35
Off-‑by-‑one ¡error ¡
Carnegie Mellon
36
Not ¡checking ¡the ¡max ¡string ¡size ¡ Basis ¡for ¡classic ¡buffer ¡overflow ¡aiacks ¡
Carnegie Mellon
37
Misunderstanding ¡pointer ¡arithme/c ¡
Carnegie Mellon
38
Referencing ¡a ¡pointer ¡instead ¡of ¡the ¡object ¡it ¡points ¡to ¡
Carnegie Mellon
39
Forgerng ¡that ¡local ¡variables ¡disappear ¡when ¡a ¡func/on ¡
Carnegie Mellon
40
Nasty! ¡
Carnegie Mellon
41
Evil! ¡ ¡
Carnegie Mellon
42
Slow, ¡long-‑term ¡killer! ¡ ¡
Carnegie Mellon
43
Freeing ¡only ¡part ¡of ¡a ¡data ¡structure ¡
Carnegie Mellon
44
Conven/onal ¡debugger ¡(gdb) ¡
Debugging ¡malloc ¡(UToronto ¡CSRI ¡malloc) ¡
Carnegie Mellon
45
Some ¡malloc ¡implementa/ons ¡contain ¡checking ¡code ¡
Binary ¡translator: ¡ ¡valgrind ¡(Linux), ¡Purify ¡
Garbage ¡collec/on ¡(Boehm-‑Weiser ¡Conserva/ve ¡GC) ¡