6
The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστόπουλος Χρήστος

The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

Embed Size (px)

Citation preview

Page 1: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

The buddy memory allocation algorithm

Παρουσίαση 11.02.2009: Δρ. Αναγνωστόπουλος Χρήστος

Page 2: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

•1963, H. MarkowitzEasy 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 66K of memory would be allocated 128K (waste: 62K memory)Memory allocation in powers of 2, i.e., 2k, k > 0. The programmer decides on:

•The upper limit (u) of k•The lower limit (l) of k

Example: Memory of 2000K. The u is k = 10, i.e., 210 = 1024K –the biggest “allocatable” block.The l is k = 2, i.e., 22 = 4K –the smallest “allocatable” block minimizing internal fragmentation and large enough to avoid overhead.

Page 3: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

Algorithm buddy (l, u, request)Begin If (memory.request == allocation) Then Lock memory m of size as the minimal 2k 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 2k 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 = log2(u/l)*/ End While End IfEnd.

Page 4: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

Running Example I:l = 6 that is 26 = 64K u = 10 that is 210 = 1024K

Requests:

0. Program A: 64K

1. Program B: 128K

2. Program C: 64K

3. Program D: 128K

4. Program C: ends

5. Program A: ends

6. Program B: ends

7. Program D: ends

Α

Α Β

Α C Β

A B D

B D

D

A C B D split

freeallocated

1024K

?. Program D: 130K?. Program C: 503K?. Program C: 260K

Page 5: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

Running Example II:l = 6 that is 26 = 64K u = 10 that is 210 = 1024K

Requests:

0. Program A: 100K

1. Program B: 240K

2. Program C: 64K

3. Program D: 256K

4. Program B: ends

5. Program A: ends

6. Request E: 75K

7. Program C: ends

8. Program E: ends

8. Program D: ends

Α

Α B

Α C

A

A C D

freeallocated

1024K

B

B

C

C

D

D

C E D

E D

D

Page 6: The buddy memory allocation algorithm Παρουσίαση 11.02.2009: Δρ. Αναγνωστό π ουλος Χρήστος

The buddy memory allocation algorithm Bib:

D. Knuth, “The Art of Computer Programming”, vol. 1, Fundamental Algorithms, 2nd Ed., 1997, pp. 435-455