160 likes | 411 Views
Chapter 17 Free-Space Management. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. 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
E N D
Chapter 17Free-Space Management Chien-Chung Shen CIS, UD cshen@cis.udel.edu
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
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
Low-level Mechanisms • Splittingand coalescing • Tracking the size of allocated regions • Embedding free listinsidethe free space to keep track of what is free and what isn’t • Growing the heap
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
Tracking Size of Allocated Regions • How does free(void *ptr) know how how many bytes to free? typedefstruct __header_t { intsize; intmagic; } header_t; void free(void *ptr) {header_t*hptr = (void *)ptr- sizeof(header_t); ... } • hptr->size + size of header
Embedding A Free List (1) • Build free list inside free space itself typedefstruct __node_t { intsize; 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;
Embedding A Free List (2) • Request 100 bytes • find a chunk large enough • split the chunk
Embedding A Free List (3) • 3 allocated regions • free(sptr) • sptris 16500 • addfree chunk back to free list by inserting at the head
Embedding A Free List (4) • How many chunks? • 4 • How can you do better? • coalesce • into one
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
Allocation Strategies • Best fit - search through free list and return the smallestblock 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
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
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