Example With Different Sizes

Download Report

Transcript Example With Different Sizes

Storage Management
Different-sized Items
Heap Memory with Different-sized Items
Light blue indicates
allocated items
Free some items
Light blue indicates
allocated items
Free some items
Light blue indicates
allocated items
Although we have 50 free bytes, we can’t satisfy a
request for more than 30 bytes
Free another item
We need to recognize when two adjacent items are free.
Free List Head
How can we recognize that two adjacent items are free?
Suppose all free nodes are on a free linked list. Since the
free bytes aren’t being used, they easily can be made
“free” nodes.
Light blue indicates
allocated items
We could follow the free list and see if any node’s
address + size is the address of the recent free node.
Free another item
Free List Head
Light blue indicates
allocated items
Combine them.
Free another item
Free List Head
Light blue indicates
allocated items
Combine them.
What if there is a free
item after a newly freed
item?
Free List Head
Of course, the item being freed could precede a free
item already in the list
What if there is a free
item after a newly freed
item?
Of course, the item being freed could precede a free
item already in the list
Free List Head
We can check the next item to see if its indicator
indicates free.
We also need to update the free list. Do we really need
to search the entire list?
What could we change to avoid that search?
What if the newly freed
item is surrounded by
free items?
Free List Head
The newly freed item might be between two already free
items.
What if the newly freed
item is surrounded by
free items?
The newly freed item might be between two already free
items.
Free List Head
So, we need to always check before and after it.
Which free item to use
when allocating?
Suppose we need to allocate an item of 40 bytes, where
should we place it?
Free List Head
Algorithms:
First Fit - scan the free item list looking for the first free item of N or more
bytes. Allocate N bytes from the free item and the remainder might be free
space.
Best Fit - scan the free item list looking for the first free item with the
minimum number of bytes >= N. How can we optimize this search?
Can there be an issue with
the left over free space?
Free List Head
Light blue indicates
allocated items
What happens if we need to allocate a 48 byte item?
Can there be an issue with
the left over free space?
What happens if we need to allocate a 48 byte item?
?
Free List Head
The free portion is too small for the standard free node
format.
What should we do?
Light blue indicates
allocated items