heap management algorithms
TRANSCRIPT
![Page 1: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/1.jpg)
Heap Allocation Strategies
![Page 2: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/2.jpg)
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…
![Page 3: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/3.jpg)
Dictionary Strategy
• Advantages
• Disadvantages
a (FREE) (FREE) c d e
dictionary
![Page 4: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/4.jpg)
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
![Page 5: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/5.jpg)
Metadata Strategy
• Advantages?
• Disadvantages?
Start of Heap
a (FREE) (FREE) c d e
![Page 6: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/6.jpg)
Two Types of Lists
• Implicit List
• Explicit List
Start of Heap
a (FREE) (FREE) c d e
![Page 7: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/7.jpg)
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
![Page 8: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/8.jpg)
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
![Page 9: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/9.jpg)
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
![Page 10: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/10.jpg)
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
![Page 11: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/11.jpg)
Strategy #3
• Buddy System
– Use segments of 2n of memory, always pairing yourself with a “buddy”.
a
c
d
e
![Page 12: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/12.jpg)
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
![Page 13: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/13.jpg)
Strategy #3
• Advantages?
• Disadvantages?
a
c
d
e
0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 1
![Page 14: Heap Management Algorithms](https://reader031.vdocuments.net/reader031/viewer/2022021006/6203856dda24ad121e4a61ef/html5/thumbnails/14.jpg)
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