Heap Management Algorithms
Transcript of Heap Management Algorithms
Heap Allocation Strategies
Strategy #1
• Dictionary Approach
– At a known spot in the heap, store a dictionary of all used and free chunks of memory memory.
Start of Heap
a (FREE) (FREE) c d e
dictionary Inside each dictionary_entry_t…
Dictionary Strategy
• Advantages
• Disadvantages
a (FREE) (FREE) c d e
dictionary
Strategy #2
• Metadata Approach
– Before every allocation, store a small amount of metadata (information about the allocation).
Start of Heap
a (FREE) (FREE) c d
Inside each metadata_t…
e
Metadata Strategy
• Advantages?
• Disadvantages?
Start of Heap
a (FREE) (FREE) c d e
Two Types of Lists
• Implicit List
• Explicit List
Start of Heap
a (FREE) (FREE) c d e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
Bit Map:
0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1
Strategy #3
• Advantages?
• Disadvantages?
a
c
d
e
0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1
MP2
• You will implement your own version of: – malloc(), calloc(), realloc()
– free()
• Initially, the heap size is 0 bytes. Expand it by using a system call: sbrk(). – We will evaluate your program on
• MAX memory usage
• AVG memory usage
• TIME your program takes to run