CS 703 Advanced Operating Systems By Mr Farhan
CS 703 - Advanced Operating Systems By Mr. Farhan Zaidi
Lecture No. 20
Overview of today’s lecture n n Goals of a good allocator Memory Fragmentation q n Implementation issues of memory allocators q q n Internal and external Knowing how much to free Tracking free blocks Implicit list based allocator details
Goals of Good malloc/free n Primary goals q Good time performance for malloc and free Ideally should take constant time (not always possible) n Should certainly not take linear time in the number of blocks n q Good space utilization User allocated structures should be large fraction of the heap. n Want to minimize “fragmentation”. n
Goals of Good malloc/free ( cont. ) n Some other goals q Good locality properties Structures allocated close in time should be close in space n “Similar” objects should be allocated close in space n q Robust n Can check that free(p 1) is on a valid allocated object p 1 n Can check that memory references are to allocated space
Internal Fragmentation n Poor memory utilization caused by fragmentation. q n Comes in two forms: internal and external fragmentation Internal fragmentation q For some block, internal fragmentation is the difference between the block size and the payload size. block Internal fragmentation q q payload Internal fragmentation Caused by overhead of maintaining heap data structures, padding for alignment purposes, or explicit policy decisions (e. g. , not to split the block). Depends only on the pattern of previous requests, and thus is easy to measure.
External Fragmentation Occurs when there is enough aggregate heap memory, but no single free block is large enough p 1 = malloc(4) p 2 = malloc(5) p 3 = malloc(6) free(p 2) p 4 = malloc(6) oops! External fragmentation depends on the pattern of future requests, and thus is difficult to measure.
Impossible to “solve” fragmentation n If you read allocation papers or books to find the best allocator(!? !? !) it can be frustrating: q q n all discussions revolve around tradeoffs the reason? There cannot be a best allocator Theoretical result: q for any possible allocation algorithm, there exist streams of allocation and deallocation requests that defeat the allocator and force it into severe fragmentation. “pretty well” = ~20% fragmentation under many workloads
p 0 free(p 0) p 1 = malloc(1)
Knowing How Much to Free n Standard q method Keep the length of a block in the word preceding the block. n This word is often called the header field or header p 0 = malloc(4) p 0 5 free(p 0) Block size data
Keeping Track of Free Blocks n Method 1: Implicit list using lengths -- links all blocks 5 n n 6 2 Method 2: Explicit list among the free blocks using pointers within the free blocks 5 n 4 4 6 2 Method 3: Segregated free list q Different free lists for different size classes Method 4: Blocks sorted by size q Can use a balanced tree (e. g. Red-Black tree) with pointers within each free block, and the length used as a key
Method 1: Implicit List n Need to identify whether each block is free or allocated 1 word size Format of allocated and free blocks payload a a = 1: allocated block a = 0: free block size: block size payload: application data (allocated blocks only) optional padding
Implicit List: Finding a Free Block n First fit: q Search list from beginning, choose first free block that fits n Next fit: q n Like first-fit, but search list from location of end of previous search Best fit: q Search the list, choose the free block with the closest size that fits
Implicit List: Allocating in Free Block n Allocating in a free block - splitting 4 4 6 2 p void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; int oldsize = *p & -2; *p = newsize | 1; if (newsize < oldsize) *(p+newsize) = oldsize - newsize; } // add 1 and round up // mask out low bit // set new length // set length in remaining // part of block addblock(p, 4) 4 4 4 2 2
Implicit List: Freeing a Block n Simplest implementation: q Only need to clear allocated flag void free_block(ptr p) { *p = *p & -2} 4 4 2 2 p free(p) 4 malloc(5) 4 Oops! 4 4
Implicit List: Coalescing n Join (coelesce) with next and/or previous block if they are free q Coalescing with next block void free_block(ptr p) { *p = *p & -2; // clear allocated flag next = p + *p; // find next block if ((*next & 1) == 0) *p = *p + *next; // add to this block if } // not allocated 4 4 4 2 2 p free(p) 4 4 6 2
Implicit List: Bidirectional Coalescing n Boundary tags [Knuth 73] q Replicate size/allocated word at bottom of free blocks q Allows us to traverse the “list” backwards, but requires extra space 4 4 6 6 4 4
- Slides: 17