Chapter 17. Free Space Management
Chapter 17. Free Space Management
Free-Space Management
Operating System: Three Easy Pieces
Youjip Won 1
Splitting
Finding a free chunk of memory that can satisfy the request and
splitting it into two.
When request for memory allocation is smaller than the size of free
chunks.
addr:0 addr:20
free list: head len:10 len:10 NULL
Youjip Won 2
Splitting(Cont.)
addr:0 addr:20
free list: head len:10 len:10 NULL
addr:0 addr:21
free list: head len:10 len:10 NULL
Youjip Won 3
Coalescing
If a user requests memory that is bigger than free chunk size, the list
will not find such a free chunk.
addr:0
head len:30 NULL
Youjip Won 4
Tracking The Size of Allocated Regions
ptr = malloc(20);
ptr
Youjip Won 5
The Header of Allocated Memory Chunk
hptr
size: 20
typedef struct __header_t {
magic: 1234567
ptr int size;
int magic;
The 20 bytes } header_t;
returned to caller
A Simple Header
Specific Contents Of The Header
Youjip Won 6
The Header of Allocated Memory Chunk(Cont.)
The size for free region is the size of the header plus the size of the space
allocated to the user.
If a user request N bytes, the library searches for a free chunk of size N
plus the size of the header
Youjip Won 7
Embedding A Free List
The memory-allocation library initializes the heap and puts the first
element of the free list in the free space.
The library can’t use malloc() to build a list within itself.
Youjip Won 8
Embedding A Free List(Cont.)
Youjip Won 9
A Heap With One Free Chunk
■ ■ ■
the rest of the 4KB chunk
Youjip Won 10
Embedding A Free List: Allocation
Youjip Won 11
Embedding A Free List: Allocation(Cont.)
A 4KB Heap With One Free Chunk A Heap : After One Allocation
head size: 100
size: 4088
magic: 1234567
next: 0 ptr
the rest of ■ ■ ■
the 100 bytes now allocated
the 4KB chunk
■ ■ ■
head
size: 3980
next: 0
Youjip Won 12
Free Space With Chunks Allocated
■ ■ ■
100 bytes still allocated
size: 100
magic: 1234567
sptr
■ ■ ■
100 bytes still allocated
(but about to be freed)
size: 100
magic: 1234567
Youjip Won 13
Free Space With free()
Example: free(sptr)
[virtual address: 16KB]
The 100 bytes chunks is back size: 100
magic: 1234567
into the free list.
■ ■ ■
100 bytes still allocated
The free list will start with a head
size: 100
small chunk. sptr
next: 16708
(now a free chunk of
The list header will point the ■ ■ ■
memory)
small chunk size: 100
magic: 1234567
■ ■ ■
100 bytes still allocated
size: 3764
next: 0
Youjip Won 14
Free Space With Freed Chunks
Let’s assume that the last two in-use chunks are freed.
size: 100
next: 16708
■ ■ ■
(now free)
head
size: 100
next: 16384
■ ■ ■
(now free)
size: 3764
next: 0
Youjip Won 15
Growing The Heap
Most allocators start with a small-sized heap and then request more
memory from the OS when they run out.
e.g., sbrk(), brk() in most UNIX systems.
Physical Memory
Youjip Won 16
Managing Free Space: Basic Strategies
Best Fit:
Finding free chunks that are big or bigger than the request
Worst Fit:
Finding the largest free chunks and allocation the amount of the request
Youjip Won 17
Managing Free Space: Basic Strategies(Cont.)
First Fit:
Finding the first chunk that is big enough for the request
Returning the requested amount and remaining the rest of the chunk.
Next Fit:
Finding the first chunk that is big enough for the request.
Searching at where one was looking at instead of the begging of the list.
Youjip Won 18
Examples of Basic Strategies
head 10 30 20 NULL
Result of Best-fit
head 10 30 5 NULL
Result of Worst-fit
head 10 15 20 NULL
Youjip Won 19
Other Approaches: Segregated List
Segregated List:
Keeping free chunks in different size in a separate list for the size of
popular request.
New Complication:
How much memory should dedicate to the pool of memory that serves
specialized requests of a given size?
Youjip Won 20
Other Approaches: Segregated List(Cont.)
Slab Allocator
Allocate a number of object caches.
The objects are likely to e requested frequently.
Youjip Won 21
Other Approaches: Buddy Allocation
64 KB
32 KB 32 KB
16 KB 16 KB
8 KB 8 KB
Youjip Won 22
Other Approaches: Buddy Allocation(Cont.)
Youjip Won 23
Disclaimer: This lecture slide set was initially developed for Operating System course in
Computer Science Dept. at Hanyang University. This lecture slide set is for OSTEP book
written by Remzi and Andrea at University of Wisconsin.
Youjip Won 24