0% found this document useful (0 votes)
44 views24 pages

Chapter 17. Free Space Management

Uploaded by

prernatarani021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views24 pages

Chapter 17. Free Space Management

Uploaded by

prernatarani021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

17.

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.

30-byte heap: free used free


0 10 20 30

addr:0 addr:20
free list: head len:10 len:10 NULL

Youjip Won 2
Splitting(Cont.)

 Two 10-bytes free segment with 1-byte request

30-byte heap: free used free


0 10 20 30

addr:0 addr:20
free list: head len:10 len:10 NULL

𝒔𝒑𝒍𝒊𝒕𝒕𝒊𝒏𝒈 𝟏𝟎 − 𝒃𝒚𝒕𝒆 𝒇𝒓𝒆𝒆 𝒔𝒆𝒈𝒎𝒆𝒏𝒕

30-byte heap: free used free


0 10 20 21 30

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.

 Coalescing: Merge returning a free chunk with existing chunks into a


large single free chunk if addresses of them are nearby.

addr:10 addr:0 addr:20


head len:10 Len:10 len:10 NULL

𝒄𝒐𝒂𝒍𝒆𝒔𝒄𝒊𝒏𝒈 𝒇𝒓𝒆𝒆 𝒄𝒉𝒖𝒏𝒌𝒔

addr:0
head len:30 NULL

Youjip Won 4
Tracking The Size of Allocated Regions

 The interface to free(void *ptr) does not take a size parameter.


 How does the library know the size of memory region that will be back
into free list?

 Most allocators store extra information in a header block.

ptr = malloc(20);

The header used by malloc library

ptr

The 20 bytes returned to caller

An Allocated Region Plus Header

Youjip Won 5
The Header of Allocated Memory Chunk

 The header minimally contains the size of the allocated memory


region.

 The header may also contain


 Additional pointers to speed up deallocation

 A magic number for integrity checking

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

 Simple pointer arithmetic to find the header pointer.

void free(void *ptr) {


header_t *hptr = (void *)ptr – sizeof(header_t);
}

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.)

 Description of a node of the list

typedef struct __node_t {


int size;
struct __node_t *next;
} nodet_t;

 Building heap and putting a free list


 Assume that the heap is built vi mmap() system call.
// mmap() returns a pointer to a chunk of free space
node_t *head = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
head->size = 4096 - sizeof(node_t);
head->next = NULL;

Youjip Won 9
A Heap With One Free Chunk

// mmap() returns a pointer to a chunk of free space


node_t *head = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, -1, 0);
head->size = 4096 - sizeof(node_t);
head->next = NULL;

[virtual address: 16KB]


header: size field
size: 4088

head next: 0 header: next field(NULL is 0)

■ ■ ■
the rest of the 4KB chunk

Youjip Won 10
Embedding A Free List: Allocation

 If a chunk of memory is requested, the library will first find a chunk


that is large enough to accommodate the request.

 The library will


 Split the large free chunk into two.
 One for the request and the remaining free chunk

 Shrink the size of free chunk in the list.

Youjip Won 11
Embedding A Free List: Allocation(Cont.)

 Example: a request for 100 bytes by ptr = malloc(100)


 Allocating 108 bytes out of the existing one free chunk.

 shrinking the one free chunk to 3980(4088 minus 108).

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

■ ■ ■ the free 3980 byte chunk

Youjip Won 12
Free Space With Chunks Allocated

size: 100 [virtual address: 16KB]


8 bytes header
magic: 1234567

■ ■ ■
100 bytes still allocated

size: 100
magic: 1234567
sptr
■ ■ ■
100 bytes still allocated
(but about to be freed)
size: 100
magic: 1234567

■ ■ ■ 100 bytes still allocated


head
size: 3764
next: 0

■ ■ ■ The free 3764-byte chunk

Free Space With Three Chunks Allocated

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

■ ■ ■ The free 3764-byte chunk

Youjip Won 14
Free Space With Freed Chunks

 Let’s assume that the last two in-use chunks are freed.

[virtual address: 16KB]


 External Fragmentation occurs. size: 100
next: 16492
 Coalescing is needed in the list.
■ ■ ■
(now free)

size: 100
next: 16708

■ ■ ■
(now free)
head
size: 100
next: 16384

■ ■ ■
(now free)

size: 3764
next: 0

■ ■ ■ The free 3764-byte chunk

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.

(not in use) (not in use)

Heap Heap Heap Heap


break sbrk()
break
(not in use)
(not in use)

Address Space Address Space Heap

Physical Memory

Youjip Won 16
Managing Free Space: Basic Strategies

 Best Fit:
 Finding free chunks that are big or bigger than the request

 Returning the one of smallest in the chunks in the group of candidates

 Worst Fit:
 Finding the largest free chunks and allocation the amount of the request

 Keeping the remaining chunk on the free list.

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

 Allocation Request Size 15

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?

 Slab allocator handles this issue.

Youjip Won 20
Other Approaches: Segregated List(Cont.)

 Slab Allocator
 Allocate a number of object caches.
 The objects are likely to e requested frequently.

 e.g., locks, file-system inodes, etc.

 Request some memory from a more general memory allocator when a


given cache is running low on free space.

Youjip Won 21
Other Approaches: Buddy Allocation

 Binary Buddy Allocation


 The allocator divides free space by two until a block that is big enough
to accommodate the request is found.

64 KB

32 KB 32 KB

16 KB 16 KB

8 KB 8 KB

64KB free space for 7KB request

Youjip Won 22
Other Approaches: Buddy Allocation(Cont.)

 Buddy allocation can suffer from internal fragmentation.

 Buddy system makes coalescing simple.


 Coalescing two blocks in to the next level of block.

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

You might also like