Advanced Operating Systems: Lecture 20 - Mr. Farhan Zaidi

17 2 0
Advanced Operating Systems: Lecture 20 - Mr. Farhan Zaidi

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Advanced Operating Systems - Lecture 20: Goals of Good malloc/free. This lecture will cover the following: goals of a good allocator; memory fragmentation; internal and external; implementation issues of memory allocators; knowing how much to free; tracking free blocks; implicit list based allocator details;...

CS703 ­ Advanced  Operating Systems By Mr Farhan Zaidi Lecture No.  20 Overview of today’s lecture   Goals of a good allocator Memory Fragmentation   Implementation issues of memory allocators    Internal and external Knowing how much to free Tracking free blocks Implicit list based allocator details Goals of Good malloc/free   Primary goals  Good time performance for malloc and free Ideally should take constant time (not always possible)  Should certainly not take linear time in the number of blocks   Good space utilization User allocated structures should be large fraction of the heap  Want to minimize “fragmentation”  Goals of Good malloc/free ( cont.)   Some other goals  Good locality properties Structures allocated close in time should be close in space  “Similar” objects should be allocated close in space   Robust Can check that free(p1) is on a valid allocated object p1  Can check that memory references are to allocated space  Internal Fragmentation  Poor memory utilization caused by fragmentation   Comes in two forms: internal and external fragmentation Internal fragmentation  For some block, internal fragmentation is the difference between the block size and the payload size block Internal fragmentation 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 p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) oops! External fragmentation depends on the pattern of future requests, and thus is difficult to measure Impossible to “solve” fragmentation  If you read allocation papers or books to find the best allocator(!?!?!) it can be frustrating:    all discussions revolve around tradeoffs the reason? There cannot be a best allocator Theoretical result:  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 p0 free(p0) p1 = malloc(1) Knowing How Much to Free  Standard  method Keep the length of a block in the word preceding the block  This word is often called the header field or header p0 = malloc(4) p0 free(p0) Block size data Keeping Track of Free Blocks  Method 1: Implicit list using lengths links all blocks   Method 2: Explicit list among the free blocks using pointers within the free blocks  4 Method 3: Segregated free list  Different free lists for different size classes Method 4: Blocks sorted by size  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  Need to identify whether each block is free or allocated word size Format of allocated and free blocks a a = 1: allocated block a = 0: free block size: block size payload payload: application data (allocated blocks only) optional padding Implicit List: Finding a Free Block  First fit:  Search list from beginning, choose first free block that fits  Next fit:   Like first-fit, but search list from location of end of previous search Best fit:  Search the list, choose the free block with the closest size that fits Implicit List: Allocating in Free Block  Allocating in a free block - splitting 4 p void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) > 1)

Ngày đăng: 05/07/2022, 12:28

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan