The buddy memory allocation algorithm 11 02 2009
The buddy memory allocation algorithm Παρουσίαση 11. 02. 2009: Δρ. Αναγνωστόπουλος Χρήστος
• 1963, H. Markowitz Easy implementation (80286, …) • Little external fragmentation (enough memory is free to satisfy a request, but it is split into chunks none of which is big enough to satisfy the request) • Moderate internal fragmentation (the requested memory is a little larger than a small block, but a lot smaller than a large block) Example: Request of 66 K of memory would be allocated 128 K (waste: 62 K memory) Memory allocation in powers of 2, i. e. , 2 k, k > 0. The programmer decides on: • The upper limit (u) of k • The lower limit (l) of k Example: Memory of 2000 K. The u is k = 10, i. e. , 210 = 1024 K –the biggest “allocatable” block. The l is k = 2, i. e. , 22 = 4 K –the smallest “allocatable” block minimizing internal fragmentation and large enough to avoid overhead.
Algorithm buddy (l, u, request) Begin If (memory. request == allocation) Then Lock memory m of size as the minimal 2 k block that is larger than request. size If m is available Then allocate() Else Split free memory m larger than the request. size into half If m. size is the minimal 2 k block and not lower than l Then allocate() Goto: : Lock Repeat until m is found Else If (memory. request == free) Then Free memory m While neighbor(m) == free or m < u Do m = merge(m, neighbor(m)) /* #compaction = log 2(u/l)*/ End While End If End.
free allocated Running Example I: l = 6 that is 26 = 64 K u = 10 that is 210 = 1024 K Requests: 0. Program A: 64 K Α 1. Program B: 128 K Α Β 2. Program C: 64 K Α C Β 3. Program D: 128 K A C B D 4. Program C: ends A B D 5. Program A: ends 6. Program B: ends 7. Program D: ends ? . Program D: 130 K ? . Program C: 503 K ? . Program C: 260 K D split
free allocated Running Example II: l = 6 that is 26 = 64 K u = 10 that is 210 = 1024 K Requests: 0. Program A: 100 K Α 1. Program B: 240 K Α B 2. Program C: 64 K Α C B 3. Program D: 256 K A C B 4. Program B: ends A C D 5. Program A: ends C D 6. Request E: 75 K C 7. Program C: ends 8. Program E: ends 8. Program D: ends D E D D
The buddy memory allocation algorithm Bib: D. Knuth, “The Art of Computer Programming”, vol. 1, Fundamental Algorithms, 2 nd Ed. , 1997, pp. 435 -455
- Slides: 6