Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD

Similar presentations


Presentation on theme: "Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD"— Presentation transcript:

1 Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD cshen@cis.udel.edu

2 Problem In malloc library or within OS itself Fixed-sized units vs. variable-sized units External fragmentation –free space gets chopped into little pieces of different sizes and is thus fragmented –subsequent requests (e.g., 15) may fail because there is no single contiguous space that can satisfy the request, even though the total amount of free space exceeds the size of the request –minimize fragmentation

3 User-level Memory Allocation User-level memory allocation library with malloc() and free() Heap Free list –data structure used to manage free space in heap –contains references to all of the free chunks of space in the managed region of memory

4 Low-level Mechanisms Splitting and coalescing Tracking the size of allocated regions Embedding free list inside the free space to keep track of what is free and what isn’t Growing the heap

5 Splitting and Coalescing A free list contains a set of elements that describe the free space still remaining in the heap Request 1 byte –splitting Free 10 bytes –coalescing

6 Tracking Size of Allocated Region s How does free(void *ptr) know how how many bytes to free? typedef struct __header_t { int size; int magic; } header_t; void free(void *ptr) { header_t *hptr = (void *)ptr - sizeof(header_t);... } hptr->size + size of header

7 Embedding A Free List (1) Build free list inside free space itself typedef struct __node_t { int size; struct __node_t *next; } node_t; // 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; head->next = NULL;

8 Embedding A Free List (2) Request 100 bytes –find a chunk large enough –split the chunk

9 Embedding A Free List (3) 3 allocated regions free(sptr) –sptr is 16500 –add free chunk back to free list by inserting at the head

10 Embedding A Free List (4) How many chunks? –4–4 How can you do better? –coalesce –into one

11 Growing Heap When running out of heap, memory allocation library makes system call (e.g., sbrk() ) to grow the heap To serve sbrk(), OS –finds free physical pages –maps them into the address space of the requesting process –returns the value of the end of the new heap

12 Allocation Strategies Best fit - search through free list and return the smallest block of free memory that is bigger than the requested size Worst fit – opposite of best fit First fit – find the first block that is big enough and return the requested amount Next fit – not begin the first-fit search at the beginning of the list Their pros and cons

13 Buddy Allocation search for free space recursively divides free space by two until a block that is big enough to accommodate the request is found

14 Buddy Allocation

15 Segregated Lists If a particular application has one (or a few) popular- sized request that it makes, keep a separate list just to manage objects of that size; all other requests are forwarded to a more general memory allocator –benefit: fragmentation is much less of a concern; allocation and free requests can be served quite quickly Slab allocator


Download ppt "Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD"

Similar presentations


Ads by Google