heap management algorithms

14
Heap Allocation Strategies

Upload: others

Post on 09-Feb-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Heap Management Algorithms

Heap Allocation Strategies

Page 2: Heap Management Algorithms

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

Dictionary Strategy

• Advantages

• Disadvantages

a (FREE) (FREE) c d e

dictionary

Page 4: Heap Management Algorithms

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

Metadata Strategy

• Advantages?

• Disadvantages?

Start of Heap

a (FREE) (FREE) c d e

Page 6: Heap Management Algorithms

Two Types of Lists

• Implicit List

• Explicit List

Start of Heap

a (FREE) (FREE) c d e

Page 7: Heap Management Algorithms

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

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

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

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

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

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

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

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