Lecture 22
Lecture 22
Announcements
• Programming Assignment 5 will be released tomorrow.
• Due Dec. 4th (Wednesday)
2
Carnegie Mellon
Today
• Memory mapping
• Dynamic memory allocation
3
Carnegie Mellon
4
Carnegie Mellon
32 4
TLBT TLBI
4
Carnegie Mellon
32 4
TLBT TLBI
...
4
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
...
40
PPN
4
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
CR3
PTE PTE PTE PTE
Page tables 4
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
CR3
PTE PTE PTE PTE
Page tables 4
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
Page tables 4
Carnegie Mellon
32 4
TLBT TLBI
TLB
hit
TLB
miss ...
Page tables 4
Carnegie Mellon
32 4
TLBT TLBI
L1 d-cache
TLB (64 sets, 8 lines/set)
hit
TLB
miss ... ...
Page tables 4
Carnegie Mellon
Page tables 4
Carnegie Mellon
Page tables 4
Carnegie Mellon
5
Carnegie Mellon
Shared
object
6
Carnegie Mellon
Shared
object
7
Carnegie Mellon
The Problem…
• What if Proc. 2 now wants to modify the shared object, but
doesn’t want the modification to be visible to Proc. 1
8
Carnegie Mellon
The Problem…
• What if Proc. 2 now wants to modify the shared object, but
doesn’t want the modification to be visible to Proc. 1
• Simplest solution: always create duplicate copies of shared
objects at the cost of wasting space. Not ideal.
8
Carnegie Mellon
The Problem…
• What if Proc. 2 now wants to modify the shared object, but
doesn’t want the modification to be visible to Proc. 1
• Simplest solution: always create duplicate copies of shared
objects at the cost of wasting space. Not ideal.
• Idea: Copy-on-write (COW)
• First pretend that both processes will share the objects without
modifying them. If modification happens, create separate copies.
8
Carnegie Mellon
Private
copy-on-write object
9
Carnegie Mellon
Copy-on-write
Write to
private
COW page
Private
copy-on-write object
10
Carnegie Mellon
Write to
private
COW page
Private
copy-on-write object
10
Carnegie Mellon
Write to
private
COW page
Private
copy-on-write object
10
Carnegie Mellon
Write to
private
COW page
Private
copy-on-write object
10
Carnegie Mellon
Write to
• Instruction restarts upon
handler return.
private
COW page
Private
copy-on-write object
10
Carnegie Mellon
Write to
• Instruction restarts upon
handler return.
private
COW page • Copying deferred as
long as possible!
Private
copy-on-write object
10
Carnegie Mellon
Today
• Memory mapping
• Dynamic memory allocation
• Basic concepts
• Implicit free lists
11
Carnegie Mellon
brk
Run-time heap
(created by malloc)
13
Carnegie Mellon
14
Carnegie Mellon
14
Carnegie Mellon
Other functions
• calloc: Version of malloc that initializes allocated block to zero.
14
Carnegie Mellon
malloc Example
15
Carnegie Mellon
16
Carnegie Mellon
return p;
}
void bar() {
int *p = foo(5);
printf(“%d\n”, p[0]);
}
16
Carnegie Mellon
return p;
} Heap (via malloc)
void bar() {
int *p = foo(5);
printf(“%d\n”, p[0]);
}
16
Carnegie Mellon
return p;
} Heap (via malloc)
void bar() {
int *p = foo(5);
printf(“%d\n”, p[0]);
}
16
Carnegie Mellon
return p;
} Heap (via malloc)
void bar() {
int *p = foo(5);
N * 8 bytes
printf(“%d\n”, p[0]);
}
16
Carnegie Mellon
return p;
} Heap (via malloc)
void bar() {
int *p = foo(5);
N * 8 bytes
printf(“%d\n”, p[0]);
}
16
Carnegie Mellon
return p;
}
void bar() {
int *p = foo();
17
Carnegie Mellon
return p;
}
void bar() {
int *p = foo();
17
Carnegie Mellon
}
return p; N * 8 bytes
void bar() {
int *p = foo();
17
Carnegie Mellon
}
return p; N * 8 bytes
void bar() {
int *p = foo();
17
Carnegie Mellon
18
Carnegie Mellon
19
Carnegie Mellon
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
20
Carnegie Mellon
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
20
Carnegie Mellon
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
20
Carnegie Mellon
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
20
Carnegie Mellon
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
20
Carnegie Mellon
Constraints
• Applications
21
Carnegie Mellon
Constraints
• Applications
• Can issue arbitrary sequence of malloc and free requests
21
Carnegie Mellon
Constraints
• Applications
• Can issue arbitrary sequence of malloc and free requests
• free request must be to a malloc’d block
21
Carnegie Mellon
Constraints
• Applications
• Can issue arbitrary sequence of malloc and free requests
• free request must be to a malloc’d block
• Allocators
21
Carnegie Mellon
Constraints
• Applications
• 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
21
Carnegie Mellon
Constraints
• Applications
• 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
21
Carnegie Mellon
Constraints
• Applications
• 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
21
Carnegie Mellon
Constraints
• Applications
• 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
21
Carnegie Mellon
Constraints
• Applications
• 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 place allocated blocks only in free memory
21
Carnegie Mellon
Constraints
• Applications
• 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 place allocated blocks only in free memory
• Must align blocks so they satisfy all alignment requirements
21
Carnegie Mellon
Constraints
• Applications
• 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 place allocated blocks only in free memory
• Must align blocks so they satisfy all alignment requirements
• 8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes
21
Carnegie Mellon
Constraints
• Applications
• 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 place allocated blocks only in free memory
• Must align blocks so they satisfy all alignment requirements
• 8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes
• Can manipulate and modify only free memory
21
Carnegie Mellon
Constraints
• Applications
• 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 place allocated blocks only in free memory
• Must align blocks so they satisfy 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
21
Carnegie Mellon
External Fragmentation
• 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)
22
Carnegie Mellon
External Fragmentation
• 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)
22
Carnegie Mellon
External Fragmentation
• 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)
22
Carnegie Mellon
• Allocation:
• What do we do with the extra space when allocating a structure
that is smaller than the free block it is placed in?
• How do we pick a block to use for allocation -- many might fit?
23
Carnegie Mellon
p0
p0 = malloc(4) 5
free(p0)
24
Carnegie Mellon
Internal Fragmentation
Block
Internal Internal
Payload
fragmentation fragmentation
25
Carnegie Mellon
Internal Fragmentation
Block
Internal Internal
Payload
fragmentation fragmentation
25
Carnegie Mellon
26
Carnegie Mellon
26
Carnegie Mellon
26
Carnegie Mellon
26
Carnegie Mellon
Today
• Memory mapping
• Dynamic memory allocation
• Basic concepts
• Implicit free lists
27
Carnegie Mellon
Implicit List
• For each block we need both size and allocation status
• Could store this information in two words: wasteful!
5 4 6 2
28
Carnegie Mellon
Implicit List
• For each block we need both size and allocation status
• Could store this information in two words: wasteful!
5 4 6 2
1 word
Implicit List
• For each block we need both size and allocation status
• Could store this information in two words: wasteful!
• Standard trick
• If blocks are aligned, some low-order address bits are always 0
• Instead of storing an always-0 bit, use it as a allocated/free flag
• When reading size word, must mask out this bit
29
Carnegie Mellon
Implicit List
• For each block we need both size and allocation status
• Could store this information in two words: wasteful!
• Standard trick
• If blocks are aligned, some low-order address bits are always 0
• Instead of storing an always-0 bit, use it as a allocated/free flag
• When reading size word, must mask out this bit
1 word
Unused
Start
of 8/0 16/1 32/0 16/1 0/1
heap
30
Carnegie Mellon
31
Carnegie Mellon
• 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
31
Carnegie Mellon
• 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
31
Carnegie Mellon
4 4 4 2 2
Freeing a Block
• Simplest implementation:
• Need only clear the “allocated” flag
void free_block(ptr p) { *p = *p & -2 }
• But can lead to “false fragmentation”
4 4 4 2 2
33
Carnegie Mellon
Freeing a Block
• Simplest implementation:
• Need only clear the “allocated” flag
void free_block(ptr p) { *p = *p & -2 }
• But can lead to “false fragmentation”
4 4 4 2 2
free(p) p
4 4 4 2 2
33
Carnegie Mellon
Freeing a Block
• Simplest implementation:
• Need only clear the “allocated” flag
void free_block(ptr p) { *p = *p & -2 }
• But can lead to “false fragmentation”
4 4 4 2 2
free(p) p
4 4 4 2 2
malloc(5) Oops!
33
Carnegie Mellon
Freeing a Block
• Simplest implementation:
• Need only clear the “allocated” flag
void free_block(ptr p) { *p = *p & -2 }
• But can lead to “false fragmentation”
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
33
Carnegie Mellon
Coalescing
• Join (coalesce) with next/previous blocks, if they are free
• Coalescing with next block
4 4 4 2 2
logically
free(p) 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
34
Carnegie Mellon
Coalescing
• How about now?
4 4 4 2 2
free(p) p
8 4 4 2 2
35
Carnegie Mellon
Coalescing
• How about now?
• How do we coalesce with previous block?
4 4 4 2 2
free(p) p
8 4 4 2 2
35
Carnegie Mellon
Coalescing
• How about now?
• How do we coalesce with previous block?
• Linear time solution: scans from beginning
4 4 4 2 2
free(p) p
8 4 4 2 2
35
Carnegie Mellon
4 4 4 4 6 6 4 4
36
Carnegie Mellon
4 4 4 4 6 6 4 4
4 4 4 4 6 6 4 4
4 4 4 4 6 6 4 4
37
Carnegie Mellon
• Splitting policy:
• When do we split free blocks?
• How much internal fragmentation are we willing to tolerate?
37
Carnegie Mellon
• Splitting policy:
• When do we 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. Examples:
• Coalesce as you scan the free list for malloc
• Coalesce when the amount of external fragmentation reaches
some threshold
37
Carnegie Mellon
38
Carnegie Mellon
38
Carnegie Mellon
38
Carnegie Mellon
38
Carnegie Mellon
38
Carnegie Mellon
38