Download
chapter 17 free space management n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 17 Free-Space Management PowerPoint Presentation
Download Presentation
Chapter 17 Free-Space Management

Chapter 17 Free-Space Management

174 Vues Download Presentation
Télécharger la présentation

Chapter 17 Free-Space Management

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 17Free-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 • 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

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

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

  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) • sptris 16500 • addfree chunk back to free list by inserting at the head

  10. Embedding A Free List (4) • How many chunks? • 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 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

  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