Allocator Basics Pages too coarse-grained for allocating individual objects. Instead: flexible-sized, word-aligned blocks. Dynamic Memory Allocation in the Heap Free word (malloc and free) Allocated word Allocated block Free block (4 words) (3 words) Explicit allocators (a.k.a. manual memory management) pointer to newly allocated block number of contiguous bytes required of at least that size void* malloc(size_t size); pointer to allocated block to free void free(void* ptr); 3 Allocator Goals: malloc/free Internal Fragmentation payload smaller than block 1. Programmer does not decide locations of distinct objects. Programmer decides: what size, when needed, when no longer needed block 2. Fast allocation. payload mallocs/second or bytes malloc'd/second 3. High memory utilization. Internal Most of heap contains necessary program data. fragmentation Little wasted space. Causes Enemy: fragmentation – unused memory that cannot be allocated. metadata alignment policy decisions 6
External Fragmentation (64-bit words) Implementation Issues Total free space large enough, 1. Determine how much to free given just a pointer. but no contiguous free block large enough 2. Keep track of free blocks. p1 = malloc(32); 3. Pick a block to allocate. p2 = malloc(40); p3 = malloc(48); 4. Choose what do with extra space when allocating a structure that is smaller than the free block used . free(p2); p4 = malloc(48); 5. Make a freed block available for future reuse. Depends on the pattern of future requests. 7 8 Knowing How Much to Free Keeping Track of Free Blocks Keep length of block in header word preceding block Method 1: Implicit list of all blocks using length Takes extra space! 40 32 48 16 Method 2: Explicit list of free blocks using pointers p0 40 32 48 16 48 p0 = malloc(32); Method 3: Seglist Different free lists for different size blocks block size metadata data payload free(p0); More methods that we will skip… 9 10
Implicit Free List: Block Format Implicit Free List: Heap Layout Block metadata: 1 word Steal LSB for status flag. 1. Block size Special end-heap word LSB = 1: allocated 2. Allocation status block size a Block Header (metadata) Looks like header of LSB = 0: free Store in one header word. block size | block allocated? zero-size allocate block. Start of heap payload (application data, when allocated) 16|0 32|1 64|0 32|1 0|1 optional padding May force Free word Initial word can't internal fragmentation. 16-byte aligned sizes have be part of block. Allocated word 4 zeroes in low-order bits 0000 0000 Allocated word wasted 0001 0000 Payloads start at 16-byte (2-word) alignment. Blocks sizes are multiples of 16 bytes. 0010 0000 0011 0000 … 11 12 Implicit Free List: Finding a Free Block Implicit Free List: Allocating a Free Block First fit: Search list from beginning, choose first free block that fits 16 48 16 Allocated space ≤ free space. Next fit: p = malloc(24); Do first-fit starting where previous search finished Use it all? Split it up? Best fit: 16 32 16 16 Search the list, choose the best free block: fits, with fewest bytes left over p Block Splitting Now showing allocation status flag implicitly with shading. 13 14
Implicit Free List: Freeing a Block Coalescing Free Blocks 32 32 16 16 16 32 16 16 p p Coalesce with following free block. free(p) Clear allocated flag. free(p); 32 48 16 16 logically gone 16 32 16 16 External fragmentation! malloc(40); Enough space, not one block. Coalesce with preceding free block? 15 16 [Knuth73] Bidirectional Coalescing: Boundary Tags Constant-Time Coalescing: 4 cases Header block size a m1 1 m1 1 m1 1 m1 1 payload m1 1 m1 1 m1 1 m1 1 n 1 n 0 n 1 n+m2 0 (application data, Freed Block Freed Block when allocated) n 1 n 0 n 1 m2 1 m2 1 m2 0 optional padding Boundary tag block size a (footer) m2 1 m2 1 m2 0 n+m2 0 m1 0 n+m1 0 m1 0 n+m1+m2 0 m1 0 m1 0 n 1 n 1 32 32 32 32 48 48 32 32 Freed Block Freed Block n 1 n+m1 0 n 1 m2 1 m2 1 m2 0 m2 1 m2 1 m2 0 n+m1+m2 0 17 19
Explicit Free Lists Summary: Implicit Free Lists Implementation: simple Allocated block: Free block: block size a block size a Allocate: O(blocks in heap) next pointer Free: O(1) payload prev pointer (application data, when allocated) Memory utilization: depends on placement policy optional padding Not widely used in practice block size a block size a some special purpose applications (same as implicit free list) Explicit list of free blocks rather than implicit list of all blocks. Splitting, boundary tags, coalescing are general to all allocators. 20 21 Explicit Free Lists: List vs. Memory Order Explicit Free Lists: Allocating a Free Block Abstractly: doubly-linked lists Next Before B A C Previous Concretely: free list blocks in any memory order After (with splitting) Next A B 32 32 32 32 48 48 32 32 32 32 C Previous = malloc(…) List Order ≠ Memory Order 22 23
Freeing with LIFO Policy: Explicit Free Lists: Freeing a Block between allocated blocks Insertion policy : Where in the free list do you add a freed block? Before LIFO (last-in-first-out) policy free( ) Pro: simple and constant time Con: studies suggest fragmentation is worse than address ordered Head Address-ordered policy Insert the freed block at head of free list. Con: linear-time search to insert freed blocks Pro: studies suggest fragmentation is lower than LIFO After Head LIFO Example: 4 cases of freed block neighbor status. 25 26 Summary: Explicit Free Lists Summary: Allocator Policies Implementation: fairly simple All policies offer trade-offs in fragmentation and throughput. Allocate: O( free blocks) vs. O( all blocks) Placement policy: Free: O(1) vs. O(1) First-fit, next-fit, best-fit, etc. Seglists approximate best-fit in low time Memory utilization: Splitting policy: depends on placement policy Always? Sometimes? Size bound? larger minimum block size (next/prev) vs. implicit list Coalescing policy: Used widely in practice , often with more optimizations. Immediate vs. deferred Splitting, boundary tags, coalescing are general to all allocators. 36 41
Recommend
More recommend