Dynamic Memory Allocation CSCE 410/611 Dynamic Memory Allocation Memory Allocation: where? Fragmentation external vs. internal Implementation Approaches Naïve vs. Buddy System Dynamic Memory Management
Dynamic Memory Allocation – when? CSCE 410/611 Dynamic Memory Allocation – when? When does new memory get allocated in the kernel? New process gets created (fork()) New program gets loaded into memory (execve()) Process stack grows What about thread stacks? Process expands heap (malloc()) Process “creates” (attaches to) shared memory segment (shmat()) Map a file to memory (mmap()) Note: These memory segments “belong to” the user! For internal memory management OS uses Slab Allocator. Dynamic Memory Management
libraries & dev.drivers CSCE 410/611 Memory Allocation memory space user program area libraries & dev.drivers Monitor Job1 Job2 Job3 Job4 Dynamic Memory Management
Memory Allocation X First Fit Worst Fit Best Fit CSCE 410/611 Dynamic Memory Management
(External) Fragmentation CSCE 410/611 (External) Fragmentation allocate() ?! Dynamic Memory Management
(Internal) Fragmentation CSCE 410/611 (Internal) Fragmentation Internal Fragmentation Dynamic Memory Management
(Internal) Fragmentation CSCE 410/611 (Internal) Fragmentation Dynamic Memory Management
Memory Allocation: Data Structures CSCE 410/611 Memory Allocation: Data Structures global memory descriptor next memory regions start end memory space start end next free list Dynamic Memory Management
(Binary) Buddy System Allocation CSCE 410/611 (Binary) Buddy System Allocation Maintain multiple free lists, one for each power-of-2 size. Allocation: Increase size of request to next power of 2*. Look up segment in free lists. If exists, allocate. If none exists, split next larger segment in half, put first half (the “buddy”) on free list, and return second half. Harry Markowitz 1927- 1990 Nobel Memorial Prize in Economics (nobelprize.org) De-Allocation: Return segment to free list. Check if buddy is free. If so, coalesce. References: Donald Knuth: The Art of Computer Programming Volume 1: Fundamental Algorithms. Second Edition (Reading, Massachusetts: Addison-Wesley, 1997), pp. 435-455. ISBN 0-201-89683-4 Dynamic Memory Management
How to find a buddy? The buddy of each block can be found with an Exclusive OR of the block’s address and the block’s size. Ex) 32KB size = 1000 0000 0000 0000 First 32KB Block = 0000 0000 0000 0000 XOR 1000 0000 0000 0000
Allocation at Different Levels user level explicit user-level allocation: malloc(size) allocate virtually contiguous sequence of bytes at user level user-level library kernel level byte-sized allocations: kmalloc(size, gfp_mask) allocate physically contiguous sequence of bytes vmalloc(size, gfp_mask) allocate virtually contiguous sequence of bytes Slab Allocator (+ caching) alloc_pages() and __get_free_pages() allocate frames or pages, at low level useful to allocate contiguous frames/pages. Buddy System!
Summary: Dynamic Memory Allocation CSCE 410/611 Summary: Dynamic Memory Allocation Memory Allocation Internal and External Fragmentation Implementation Approaches Free Lists Buddy System Dynamic Memory Management