1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2025 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>
//! Implementation of [`AllocBitmap`].
extern crate alloc;
use alloc::vec::Vec;
use super::bitmap_word::{BITMAP_WORD_BITS_LOG2, BitmapWord, BitmapWordBlocksLsbsMaskTable};
use super::sparse_bitmap::{SparseAllocBitmap, SparseAllocBitmapUnion, SparseAllocBitmapUnionWordIterator};
use crate::{
fs::{
NvFsError, NvFsIoError,
cocoonfs::{
FormatError, extents,
extents_layout::ExtentsLayout,
layout::{self, BlockCount as _},
},
},
nvfs_err_internal,
utils_common::{
alloc::try_alloc_vec,
bitmanip::{BitManip as _, UBitManip as _},
},
};
use core::cmp;
/// Details of an [`extents`](extents::PhysicalExtents) allocation request.
///
/// Allocation requests are always specified in terms of the total effective
/// payload capacity to be collectively provided by the allocated extents at
/// least. (Non-trivial) [`ExtentsLayout`] constraints may be imposed on the
/// group of to be allocated extents, possibly specifying the various possible
/// headers' lengths each, as well as alignment and size constraints.
#[derive(Clone)]
pub struct ExtentsAllocationRequest {
/// Total effective payload length to allocate at least.
pub total_effective_payload_len: u64,
/// [`ExtentsLayout`] format applying to the to be allocated group of
/// extents.
layout: ExtentsLayout,
}
impl ExtentsAllocationRequest {
/// Instantiate a new [`ExtentsAllocationRequest`].
///
/// # Arguments:
///
/// * `total_effective_payload_len` - The total effective payload storage
/// length to be provided at least by the to be allocated group of
/// extents.
/// * `layout` - [`ExtentsLayout`] constraints applying to the to be
/// allocated group of extents.
pub fn new(total_effective_payload_len: u64, layout: &ExtentsLayout) -> Self {
Self {
total_effective_payload_len,
layout: layout.clone(),
}
}
/// Get the associated [`ExtentsLayout`] constraint.
pub fn get_layout(&self) -> &ExtentsLayout {
&self.layout
}
/// Create a [extents reallocation request](ExtentsReallocationRequest).
///
/// When resizing some existing filesystem entity, it might be worthwhile to
/// retain some or all of its backing extents on storage as appropriate.
/// In order to accomodate for a new effective payload length of
/// `total_effective_payload_len`, the `preexisting_extents` might
/// either
///
/// * need to get shrunken in order to free up excess space, as indicated by
/// a return value of [`ExtentsReallocationRequest::Shrink`],
/// * kept as-is, as indicated by a return value of
/// [`ExtentsReallocationRequest::Keep`],
/// * or need to get extended, as indicated by
/// [`ExtentsReallocationRequest::Grow`] with its associated
/// [`ExtentsAllocationRequest`] suitable for allocating the extension
/// extents.
///
/// In either case, it is assumed that the relative order of the retained
/// `preexisting_extents` is preserved and, in case of a growing
/// operation, that they will all be ordered before any additionally
/// allocated extents in the resulting extents group. In particular, it is
/// assumed the the extents group's [common extents
/// header](ExtentsLayout::extents_hdr_len) will be placed into the first
/// extent thereof, if any.
///
/// Furthermore, all `preexisting_extents` must conform to the constraints
/// imposed by the specified [`layout`](ExtentsLayout):
///
/// * their boundaries must be aligned according to
/// [ExtentsLayout::extent_alignment_allocation_blocks_log2] and
/// * their lengths must all be within the bounds as given by
/// [`ExtentsLayout::min_extents_allocation_blocks()`] and
/// [ExtentsLayout::max_extent_allocation_blocks].
///
/// # Arguments:
///
/// * `preexisting_extents` - The preexisting extents to reallocate.
/// * `total_effective_payload_len` - The total effective payload storage
/// length to be provided at least by the to be reallocated group of
/// extents.
/// * `layout` - [`ExtentsLayout`] constraints applying to the to be
/// reallocated group of extents.
pub fn new_reallocate(
preexisting_extents: &extents::PhysicalExtents,
total_effective_payload_len: u64,
layout: &ExtentsLayout,
) -> Result<ExtentsReallocationRequest, NvFsError> {
let mut remaining_effective_payload_len = total_effective_payload_len;
let mut is_first = true;
for (preexisting_extent_index, preexisting_extent) in preexisting_extents.iter().enumerate() {
// Determine the maximum possible extent length that could get allocated to move
// towards request completion at the current point.
let remaining_max_extent_allocation_blocks =
layout.extent_payload_len_to_allocation_blocks(remaining_effective_payload_len, is_first);
if remaining_max_extent_allocation_blocks.0 >= preexisting_extent.block_count() {
// Excess space could still cause an underflow, so saturate.
remaining_effective_payload_len = remaining_effective_payload_len
.saturating_sub(layout.extent_effective_payload_len(preexisting_extent.block_count(), is_first));
} else {
// The pre-existing allocation is too large and should get truncated.
if preexisting_extent.block_count() > layout.max_extent_allocation_blocks {
// The current extent is even larger than the maximum allowed limit. In
// principle we could split the extent logically and
// continue, but don't even bother.
return Err(NvFsError::from(FormatError::InvalidExtents));
}
// When here, the current extent provides enough space to accomodate
// the remaining_effective_payload_len.
return Ok(ExtentsReallocationRequest::Shrink {
last_retained_extent_index: preexisting_extent_index,
last_retained_extent_allocation_blocks: remaining_max_extent_allocation_blocks.0,
});
}
is_first = false;
}
if remaining_effective_payload_len == 0 {
Ok(ExtentsReallocationRequest::Keep)
} else {
let mut layout = layout.clone();
if !is_first {
// Don't account for the extents_hdr when extending an existing allocation.
layout.extents_hdr_len = 0;
}
Ok(ExtentsReallocationRequest::Grow {
request: Self {
total_effective_payload_len: remaining_effective_payload_len,
layout,
},
})
}
}
/// Determine the upper bound on the next extent's length to allocate in
/// working towards completing the [`ExtentsAllocationRequest`].
///
/// Assuming that the already allocated extents, if any, collectively
/// provide a total effective payload capacity of
/// `allocated_effective_payload_len`, determine the upper bound on the
/// length of the next extent to be allocated in working towards
/// completing the [`ExtentsAllocationRequest`].
///
/// # Arguments:
///
/// * `allocated_effective_payload_len` - The assumed total effective
/// payload length already allocated. Must not be greater than
/// `self.total_effective_payload_len`.
fn remaining_max_extent_allocation_blocks(
&self,
allocated_effective_payload_len: u64,
) -> (layout::AllocBlockCount, bool) {
debug_assert!(allocated_effective_payload_len <= self.total_effective_payload_len);
if allocated_effective_payload_len == self.total_effective_payload_len {
return (layout::AllocBlockCount::from(0), true);
}
let is_first = allocated_effective_payload_len == 0;
let remaining_effective_payload_len = self.total_effective_payload_len - allocated_effective_payload_len;
self.layout
.extent_payload_len_to_allocation_blocks(remaining_effective_payload_len, is_first)
}
}
/// Details of an [`extents`](extents::PhysicalExtents) reallocation request,
/// created through [`ExtentsAllocationRequest::new_reallocate()`].
pub enum ExtentsReallocationRequest {
/// Keep the preexisting extents as-is.
Keep,
/// Shring the preexisting extents.
Shrink {
/// Index of last preexisting extent to retain in full or part.
last_retained_extent_index: usize,
/// Number of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) to
/// retain in the last extent identified by
/// `last_retained_extent_index`.
///
/// Can be zero!
last_retained_extent_allocation_blocks: layout::AllocBlockCount,
},
/// Extend the preexisting extents by some newly allocated ones.
Grow {
/// [`ExtentsAllocationRequest`] suitable for allocating the extension.
request: ExtentsAllocationRequest,
},
}
/// Progress tracking data for an [`ExtentsAllocationRequest`].
struct ExtentsAllocationRequestProgress<'a> {
/// The [`ExtentsAllocationRequest`] being worked on.
request: &'a ExtentsAllocationRequest,
/// Amount of effective payload capacity less than or equal to the requested
/// [`ExtentsAllocationRequest::total_effective_payload_len`] allocated so
/// far.
///
/// If the current value is strictly less than
/// `request.total_effective_payload_len`, then the
/// [`allocated_excess_effective_payload_len`](Self::allocated_excess_effective_payload_len) is zero.
allocated_effective_payload_len: u64,
/// Amount of effective payload capacity beyond the requested
/// [`ExtentsAllocationRequest::total_effective_payload_len`] allocated so
/// far.
///
/// If non-zero, then
/// [`allocated_effective_payload_len`](Self::allocated_effective_payload_len)
/// equals `request.total_effective_payload_len`.
allocated_excess_effective_payload_len: u64,
/// Cached value of
/// [`ExtentsLayout::extents_hdr_placement_cost_is_invariant()`].
extents_hdr_placement_cost_is_invariant: bool,
}
impl<'a> ExtentsAllocationRequestProgress<'a> {
/// Instantiate a [`ExtentsAllocationRequestProgress`].
fn new(request: &'a ExtentsAllocationRequest) -> Self {
let extents_hdr_placement_cost_is_invariant = request.layout.extents_hdr_placement_cost_is_invariant();
Self {
request,
allocated_effective_payload_len: 0,
allocated_excess_effective_payload_len: 0,
extents_hdr_placement_cost_is_invariant,
}
}
/// Determine the remaining effective payload length to allocate.
fn remaining_effective_payload_len(&self) -> u64 {
let remaining_effective_len = self.request.total_effective_payload_len - self.allocated_effective_payload_len;
debug_assert!(remaining_effective_len == 0 || self.allocated_excess_effective_payload_len == 0);
remaining_effective_len
}
/// Refit an already allocated extent to a smaller effective payload length.
///
/// It is assumed that the desired target
/// `extent_accounted_target_effective_payload_len` is
///
/// * less or equal than the total effective payload storage capacity
/// provided by the allocated extent's current length
/// * and equals that part of the current extent's total provided effective
/// payload storage capacity that is being considered to currently have
/// been accounted for at [`Self::allocated_effective_payload_len`] and
/// [`Self::allocated_excess_effective_payload_len`] respectively.
///
/// For clarity on the latter point: in general, an extent's accounted
/// effective payload length is always equal to its total capacity, but
/// callers may artificially reduce it by some amount they somehow
/// gained elsewhere and not accounted for yet, e.g. via header
/// placement optimizations, for the purpose of invoking this function.
/// Note that this effectively transfers the "accounted" status from a
/// fraction of the extent's total effective payload capacity over to that
/// gained payload.
///
/// In a first step of the refitting procedure, the input
/// `extent_accounted_target_effective_payload_len` gets further reduced as
/// much as possible while still staying in the
/// `max_final_remaining_effective_payload_len` realm, updating the
/// accounting in the course. That is, it gets reduced by the maximum amount
/// such that
/// [`remaining_effective_payload_len()`](Self::remaining_effective_payload_len) would not
/// exceed `max_final_remaining_effective_payload_len`. The intended usecase
/// is to not overallocate too much by upwards alignment when doing full
/// [`BitmapWord`] allocations in case the remainder, including headers,
/// could fit into a subword allocation already.
///
/// Afterwards, the minimum extent length compatible with the constraints
/// imposed by the [`ExtentsAllocationRequest`]'s associated
/// [`ExtentsLayout`] and capable of storing the resulting effective
/// payload length is determined. Any additional payload capacity due to
/// alignment is being accounted for and the resulting fitted extent length
/// returned.
///
/// # Arguments:
///
/// * `extent_accounted_target_effective_payload_len` - The part of the
/// extent's total effective payload capacity being considered by the
/// caller as having been accounted for.
/// * `extent_stores_extents_hdr` - Whether or not the extent in question
/// stores the [common extents header](ExtentsLayout::extents_hdr_len).
/// * `max_final_remaining_effective_payload_len` - Upper bound on the value
/// [`remaining_effective_payload_len()`](Self::remaining_effective_payload_len)
/// may have after the refitting.
/// * `min_extent_alignment_allocation_blocks_log2` - Alignment constrains
/// on the refitted extent.
fn fit_allocated_extent_to_effective_payload_len(
&mut self,
mut extent_accounted_target_effective_payload_len: u64,
extent_stores_extents_hdr: bool,
max_final_remaining_effective_payload_len: u64,
min_extent_alignment_allocation_blocks_log2: u32,
) -> layout::AllocBlockCount {
debug_assert!(
extent_accounted_target_effective_payload_len.wrapping_sub(self.allocated_excess_effective_payload_len)
<= self.allocated_effective_payload_len
);
let min_extent_alignment_allocation_blocks_log2 = min_extent_alignment_allocation_blocks_log2
.max(self.request.layout.extent_alignment_allocation_blocks_log2 as u32);
// If there's already some allocated excess, stemming from previous upwards
// alignment of this or a different extent, then deduct that from the
// current target payload length and update the accounting accordingly.
let x = extent_accounted_target_effective_payload_len.min(self.allocated_excess_effective_payload_len);
extent_accounted_target_effective_payload_len -= x;
self.allocated_excess_effective_payload_len -= x;
debug_assert!(
extent_accounted_target_effective_payload_len == 0 || self.allocated_excess_effective_payload_len == 0
);
// Determine the minimum amount of effective payload len this extent must
// provide. The actual payload length capacity might get larger, due to
// the upwards alignment (assuming no truncation to the
// max_extent_allocation_blocks limit below).
let remaining_effective_payload_len = self.remaining_effective_payload_len();
let extent_min_allocated_effective_payload_len = extent_accounted_target_effective_payload_len
.saturating_sub(max_final_remaining_effective_payload_len.saturating_sub(remaining_effective_payload_len));
if !extent_stores_extents_hdr && extent_min_allocated_effective_payload_len == 0 {
// Remove the extent from the accounting. See above, if
// extent_accounted_target_effective_payload_len != 0, then
// allocated_excess_effective_payload_len is 0, hence no need to deduct from
// there.
self.allocated_effective_payload_len -= extent_accounted_target_effective_payload_len;
return layout::AllocBlockCount::from(0);
}
let extent_allocation_blocks = layout::AllocBlockCount::from(
u64::from(
self.request
.layout
.extent_payload_len_to_allocation_blocks(
extent_min_allocated_effective_payload_len,
extent_stores_extents_hdr,
)
.0,
)
// This should be a no-op, because the input
// extent_accounted_target_effective_payload_len comes from an already allocated extent,
// but better make it explicit.
.min(
u64::from(self.request.layout.max_extent_allocation_blocks)
.round_down_pow2(min_extent_alignment_allocation_blocks_log2),
)
.round_up_pow2_unchecked(min_extent_alignment_allocation_blocks_log2),
);
debug_assert_ne!(extent_allocation_blocks, layout::AllocBlockCount::from(0u64));
// Finally compute the actual effective payload length capacity and update the
// internal bookkeeping in order to account for any differences from
// original input extent_accounted_target_effective_payload_len.
let extent_allocated_effective_len = self
.request
.layout
.extent_effective_payload_len(extent_allocation_blocks, extent_stores_extents_hdr);
debug_assert!(extent_stores_extents_hdr || extent_allocated_effective_len != 0);
if extent_allocated_effective_len >= extent_accounted_target_effective_payload_len {
let x = extent_allocated_effective_len - extent_accounted_target_effective_payload_len;
let y = x.min(remaining_effective_payload_len);
self.allocated_effective_payload_len += y;
self.allocated_excess_effective_payload_len += x - y;
} else {
debug_assert_ne!(extent_accounted_target_effective_payload_len, 0);
debug_assert_eq!(self.allocated_excess_effective_payload_len, 0);
let x = extent_accounted_target_effective_payload_len - extent_allocated_effective_len;
self.allocated_effective_payload_len -= x;
}
extent_allocation_blocks
}
/// Find the shortest among a sequence of extents.
///
/// If `extents` is non-empty, return the index of some extent of shortest
/// length wrapped in a `Some`, `None` otherwise.
fn find_shortest_extent(extents: &extents::PhysicalExtents) -> Option<usize> {
if !extents.is_empty() {
let mut shortest_extent = (0usize, extents.get_extent_range(0usize));
for (cur_extent_index, cur_extent) in extents.iter().enumerate().skip(1) {
let cur_extent_allocation_blocks = cur_extent.block_count();
let shortest_extent_allocation_blocks = shortest_extent.1.block_count();
// Prefer (in this order):
// a.) shorter extents,
// b.) extents at increasing positions.
if cur_extent_allocation_blocks < shortest_extent_allocation_blocks
|| (cur_extent_allocation_blocks == shortest_extent_allocation_blocks
&& cur_extent.begin() > shortest_extent.1.begin())
{
shortest_extent = (cur_extent_index, cur_extent);
}
}
Some(shortest_extent.0)
} else {
None
}
}
/// Minimize padding waste by optimizing placement of the [common extents
/// header](ExtentsLayout::extents_hdr_len).
///
/// On entry, it is assumed that the common extents header is currently
/// stored within the first extent of `extents` and that the internal
/// bookkeeping has been computed to reflect that fact.
///
/// In case a better extent to store the common extents header is found, it
/// will be swapped for the first and any gained additional effective
/// payload length recorded at the internal bookkeeping. In case the
/// gained effective payload length would render some portion of the
/// already allocated effective payload length superfluous, some extent will
/// get shrunken accordingly. To be more specific, it will be attempted
/// to shrink the existing overall allocation by the extent possible
/// while still keeping the amount of total unsatisfied effective length
/// in the bounds specified by `max_final_remaining_effective_payload_len`.
/// As for
/// [`fit_allocated_extent_to_effective_payload_len()`](Self::fit_allocated_extent_to_effective_payload_len),
/// the intended usecase is to not overallocate too much by upwards
/// alignment when doing full [`BitmapWord`] allocations in case the
/// remainder, including headers, could fit into a subword allocation
/// already
///
/// The return value is a pair of values, with the first entry equalling the
/// number of extents removed from `extents` in the course of the
/// shrinking process just described.
///
/// As a by-product, the index of the shortest among all extents from
/// `extents` will also be returned in the second slot, in case that had
/// been needed in the course of the optimization search.
///
/// # Arguments:
///
/// * `extents` - The allocated extents to optimize the [common extents
/// header](ExtentsLayout::extents_hdr_len) over.
/// * `shortest_extent_hint` - Index of the shortest extent if `Some`.
/// * `head_extent_min_allocation_blocks` - Minimum length of an extent
/// storing the [common extents header](ExtentsLayout::extents_hdr_len),
/// as determined by [`ExtentsLayout::min_extents_allocation_blocks()`].
/// * `max_final_remaining_effective_payload_len` - Upper bound on the value
/// [`remaining_effective_payload_len()`](Self::remaining_effective_payload_len)
/// may have after any potential extent shrinking.
/// * `min_extent_alignment_allocation_blocks_log2` - Alignment constrains
/// on the individual extents each.
fn optimize_extents_hdr_placement(
&mut self,
extents: &mut extents::PhysicalExtents,
shortest_extent_hint: Option<usize>,
head_extent_min_allocation_blocks: layout::AllocBlockCount,
max_final_remaining_effective_payload_len: u64,
min_extent_alignment_allocation_blocks_log2: u32,
) -> (u32, Option<usize>) {
// Return the position of an extent of minimal length as a byproduct, if
// possible.
if extents.len() <= 1 || self.request.layout.extents_hdr_len == 0 {
if extents.len() == 1 {
return (0, Some(0));
} else {
return (0, None);
}
}
if self.extents_hdr_placement_cost_is_invariant {
// The extents header placement cost does not depend on the extent length,
// use whatever extent is currently at the first position in extents.
(0, shortest_extent_hint)
} else {
// The extents header placement cost does depend on the extent length,
// store it in the best one. While traversing the extents anyway, also
// determine and return the (first) shortest extent.
let (
mut prev_containing_extent_index,
original_prev_containing_extent_allocation_blocks,
original_extents_hdr_placement_cost,
mut best_extents_hdr_placement_cost,
mut shortest_extent,
) = {
let first_extent = extents.get_extent_range(0);
let original_prev_containing_extent_allocation_blocks = first_extent.block_count();
let original_extents_hdr_placement_cost = self
.request
.layout
.extents_hdr_placement_cost(original_prev_containing_extent_allocation_blocks);
let mut best_placement_extent = (0, first_extent, original_extents_hdr_placement_cost);
let mut shortest_extent = (0, first_extent);
for (cur_extent_index, cur_extent) in extents.iter().enumerate().skip(1) {
let cur_extent_allocation_blocks = cur_extent.block_count();
// For the shortest extent search, prefer (in this order):
// a.) shorter extents,
// b.) extents at increasing positions,
// compare to Self::find_shortest_extent().
let shortest_extent_allocation_blocks = shortest_extent.1.block_count();
if cur_extent_allocation_blocks < shortest_extent_allocation_blocks
|| (cur_extent_allocation_blocks == shortest_extent_allocation_blocks
&& cur_extent.begin() > shortest_extent.1.begin())
{
shortest_extent = (cur_extent_index, cur_extent);
}
if cur_extent_allocation_blocks < head_extent_min_allocation_blocks {
// The extent does not qualify for storing the extents header.
continue;
}
let cur_extent_extents_hdr_placement_cost = self
.request
.layout
.extents_hdr_placement_cost(cur_extent_allocation_blocks);
// For the extents header placement, prefer (in this oder):
// a.) extents with a smaller extents header placement cost
// b.) shorter extents (so that a potential future truncation
// would free up larger ones),
// c.) extents located at smaller positions.
let best_placement_extent_allocation_blocks = best_placement_extent.1.block_count();
if cur_extent_extents_hdr_placement_cost < best_placement_extent.2
|| (cur_extent_extents_hdr_placement_cost == best_placement_extent.2
&& (cur_extent_allocation_blocks < best_placement_extent_allocation_blocks
|| (cur_extent_allocation_blocks == best_placement_extent_allocation_blocks
&& cur_extent.begin() < best_placement_extent.1.begin())))
{
best_placement_extent = (cur_extent_index, cur_extent, cur_extent_extents_hdr_placement_cost);
}
}
extents.swap_extents(0, best_placement_extent.0);
// The extent previously storing the extents header now has been swapped into
// the former position of the better extent.
let prev_containing_extent_index = best_placement_extent.0;
// Conditionally update shortest_extent to account for the swap.
if shortest_extent.0 == 0 {
shortest_extent.0 = best_placement_extent.0;
} else if shortest_extent.0 == best_placement_extent.0 {
shortest_extent.0 = 0;
}
(
prev_containing_extent_index,
original_prev_containing_extent_allocation_blocks,
original_extents_hdr_placement_cost,
best_placement_extent.2,
shortest_extent,
)
};
// The additional effective payload length gained due to the headers transfer,
// but not accounted for at allocated_effective_payload_len and
// allocated_excess_effective_payload_len respectively.
let mut gained_effective_payload_len =
original_extents_hdr_placement_cost - best_extents_hdr_placement_cost;
debug_assert!(gained_effective_payload_len < self.request.layout.extent_payload_len_alignment as u64);
// If there's more effective payload length to allocate, use the gained length
// up to the point we'd get into the max_final_remaining_effective_payload_len
// realm.
let remaining_effective_payload_len = self.remaining_effective_payload_len();
if remaining_effective_payload_len > max_final_remaining_effective_payload_len {
let x = gained_effective_payload_len
.min(remaining_effective_payload_len - max_final_remaining_effective_payload_len);
self.allocated_effective_payload_len += x;
gained_effective_payload_len -= x;
}
// Any non-zero gained_effective_payload_len at this point is not reflected
// in the allocated_effective_payload_len and
// allocated_excess_effective_payload_len respectively, but is
// effectively present in the allocated extents. Try to shrink
// existing extents and update the accounting alongside. Start out with the
// shortest extent in the hope it can get dropped alltogether.
let mut n_extents_removed = 0u32;
let shortest_extent = if gained_effective_payload_len != 0
&& shortest_extent.0 != 0
&& shortest_extent.0 != prev_containing_extent_index
{
let original_shortest_extent_block_count = shortest_extent.1.block_count();
let mut shortest_extent_used_effective_payload_len = self
.request
.layout
.extent_effective_payload_len(original_shortest_extent_block_count, false);
// Subtract the gained_effective_payload_len to reflect the actual accounting.
let x = shortest_extent_used_effective_payload_len.min(gained_effective_payload_len);
shortest_extent_used_effective_payload_len -= x;
gained_effective_payload_len -= x;
debug_assert!(shortest_extent_used_effective_payload_len == 0 || gained_effective_payload_len == 0);
let updated_shortest_extent_allocation_blocks = self.fit_allocated_extent_to_effective_payload_len(
shortest_extent_used_effective_payload_len,
false,
max_final_remaining_effective_payload_len,
min_extent_alignment_allocation_blocks_log2,
);
if u64::from(updated_shortest_extent_allocation_blocks) != 0 {
debug_assert_ne!(shortest_extent_used_effective_payload_len, 0);
debug_assert_eq!(gained_effective_payload_len, 0);
if updated_shortest_extent_allocation_blocks < original_shortest_extent_block_count {
let removed = extents.shrink_extent_by(
shortest_extent.0,
original_shortest_extent_block_count - updated_shortest_extent_allocation_blocks,
);
debug_assert!(!removed);
// In principle, a shrinking of the extent might have turned it into an even
// better choice for the extents header placement. However, note that the
// total sum of multiple successive header placement gains is still bounded
// (strictly from above) by the payload alignment unit, because all those
// stem from iteratively and monotonically decreasing the required alignment
// padding. When shortening (not removing in the general, but unrealistic
// case!) an existing extent (like just done here), the allocated effective
// payload "lost" is at least the size of a (two to the power of)
// min_extent_alignment_allocation_blocks_log2 sized block, aligned
// downwards to the requested payload alignment, which is at least one unit
// of that payload alignment in size, c.f. ExtentsLayout::new(). Thus in,
// summary, the net gain of the single extent shortening operation from
// above and any sequence of extents header placement optimization gains,
// including the initial placement optimization from above, is always
// negative in terms of allocated effective payload.
//
// Now assume there is some other extent, which can get truncated,
// i.e. shrunken by at least one (two to the power of)
// min_extent_alignment_allocation_blocks_log2 unit. Note that the current
// extent cannot have one more such unit removed, as per the fitting above.
// As the difference between that hypothetical and the current extent can only
// be due to different alignment paddings, it follows that
// we're less than one payload alignment unit
// into the max_final_remaining_effective_payload_len realm.
//
// Overall, in conclusion, it follows that another extent shrinking or
// removal would not be affordable without getting outside the
// max_final_remaining_effective_payload_len realm again.
if updated_shortest_extent_allocation_blocks >= head_extent_min_allocation_blocks {
let shortest_extent_extents_hdr_placement_cost = self
.request
.layout
.extents_hdr_placement_cost(updated_shortest_extent_allocation_blocks);
// On ties, prefer shorter extents, as in the initial search above.
if shortest_extent_extents_hdr_placement_cost <= best_extents_hdr_placement_cost {
extents.swap_extents(0, shortest_extent.0);
shortest_extent.0 = 0;
let gained_effective_payload_len =
best_extents_hdr_placement_cost - shortest_extent_extents_hdr_placement_cost;
// The following assignment is dead, but for good measure do it anyway.
best_extents_hdr_placement_cost = shortest_extent_extents_hdr_placement_cost;
let x = gained_effective_payload_len.min(self.remaining_effective_payload_len());
self.allocated_effective_payload_len += x;
self.allocated_excess_effective_payload_len += gained_effective_payload_len - x;
}
}
Some((shortest_extent.0, extents.get_extent_range(shortest_extent.0)))
} else {
Some(shortest_extent)
}
} else {
extents.swap_extents(shortest_extent.0, extents.len() - 1);
// Conditionally update prev_containing_extent_index to account for the swap.
if prev_containing_extent_index == extents.len() - 1 {
prev_containing_extent_index = shortest_extent.0
}
extents.pop_extent();
n_extents_removed += 1;
None
}
} else {
Some(shortest_extent)
};
// If gained_effective_payload_len is still non-zero, proceed with the extent
// the extents headers got moved away from.
let shortest_extent_index = if gained_effective_payload_len != 0 {
let prev_containing_extent_max_effective_payload_len = self
.request
.layout
.extent_effective_payload_len(original_prev_containing_extent_allocation_blocks, false);
// As all the all the gained effective payload length stems from
// headers removal from this extent, it is guaranteed that the
// gain fits into it.
debug_assert!(gained_effective_payload_len <= prev_containing_extent_max_effective_payload_len);
// Subtract the gained_effective_payload_len to reflect the actual accounting.
let prev_containing_extent_used_effective_payload_len =
prev_containing_extent_max_effective_payload_len - gained_effective_payload_len;
let updated_prev_containing_extent_allocation_blocks = self
.fit_allocated_extent_to_effective_payload_len(
prev_containing_extent_used_effective_payload_len,
false,
max_final_remaining_effective_payload_len,
min_extent_alignment_allocation_blocks_log2,
);
if u64::from(updated_prev_containing_extent_allocation_blocks) != 0 {
if updated_prev_containing_extent_allocation_blocks
< original_prev_containing_extent_allocation_blocks
{
let removed = extents.shrink_extent_by(
prev_containing_extent_index,
original_prev_containing_extent_allocation_blocks
- updated_prev_containing_extent_allocation_blocks,
);
debug_assert!(!removed);
let shortest_extent =
if updated_prev_containing_extent_allocation_blocks >= head_extent_min_allocation_blocks {
// In principle, a shrinking of the extent might have turned it into an even
// better choice for the extents header placement, just as in the case of
// when attempting to shrink the shortest extent
// above. The same comment re the impossiblity of
// multiple successive extent shrinkings apply.
let updated_prev_containing_extent_extents_hdr_placement_cost = self
.request
.layout
.extents_hdr_placement_cost(updated_prev_containing_extent_allocation_blocks);
let cur_best_placement_extent = extents.get_extent_range(0);
// Prefer as in in the initial search above (in this order):
// a.) extents with a smaller extents header placement cost
// b.) shorter extents (so that a potential future truncation
// would free up larger ones),
// c.) extents located at smaller positions.
if updated_prev_containing_extent_extents_hdr_placement_cost
< best_extents_hdr_placement_cost
|| (updated_prev_containing_extent_extents_hdr_placement_cost
== best_extents_hdr_placement_cost
&& (updated_prev_containing_extent_allocation_blocks
< cur_best_placement_extent.block_count()
|| (updated_prev_containing_extent_allocation_blocks
== cur_best_placement_extent.block_count()
&& extents.get_extent_range(prev_containing_extent_index).begin()
< cur_best_placement_extent.begin())))
{
extents.swap_extents(0, prev_containing_extent_index);
let shortest_extent = shortest_extent.map(|shortest_extent| {
let shortest_extent_index = if shortest_extent.0 == 0 {
prev_containing_extent_index
} else {
// No need to handle shortest_extent.0 ==
// prev_containing_extent_index separately, it will be
// caught by the conditional update of shortest_extent
// further below, because the previously containing extent,
// which would then coincide with the shortest extent, did
// get shortened.
debug_assert!(
shortest_extent.0 != prev_containing_extent_index
|| updated_prev_containing_extent_allocation_blocks
< shortest_extent.1.block_count()
);
shortest_extent.0
};
(shortest_extent_index, shortest_extent.1)
});
prev_containing_extent_index = 0;
let gained_effective_payload_len = best_extents_hdr_placement_cost
- updated_prev_containing_extent_extents_hdr_placement_cost;
let x = gained_effective_payload_len.min(self.remaining_effective_payload_len());
self.allocated_effective_payload_len += x;
self.allocated_excess_effective_payload_len += gained_effective_payload_len - x;
shortest_extent
} else {
shortest_extent
}
} else {
shortest_extent
};
shortest_extent.map(|(shortest_extent_index, shortest_extent)| {
// The shrunken extent might now have become the shortest one.
let shortest_extent_allocation_blocks = shortest_extent.block_count();
if shortest_extent_allocation_blocks > updated_prev_containing_extent_allocation_blocks
|| (shortest_extent_allocation_blocks
== updated_prev_containing_extent_allocation_blocks
&& shortest_extent.begin()
> extents.get_extent_range(prev_containing_extent_index).begin())
{
prev_containing_extent_index
} else {
shortest_extent_index
}
})
} else {
shortest_extent.map(|(shortest_extent_index, _)| shortest_extent_index)
}
} else {
extents.swap_extents(prev_containing_extent_index, extents.len() - 1);
extents.pop_extent();
n_extents_removed += 1;
shortest_extent
.map(|(shortest_extent_index, _)| shortest_extent_index)
.filter(|shortest_extent_index| *shortest_extent_index != prev_containing_extent_index)
}
} else {
shortest_extent.map(|(shortest_extent_index, _)| shortest_extent_index)
};
(n_extents_removed, shortest_extent_index)
}
}
}
/// Extent candidate filter queried by
/// [`AllocBitmap::_find_free_fullword_chunks()`] for deciding whether or not
/// some found free extent is eligible to be included in the allocation.
///
/// The intended usecase is to reject too short extent candidates from inclusion
/// in order to limit the total number of extents allocated for serving a
/// given [`ExtentsAllocationRequest`].
trait FindFreeFullwordChunksExtentCandiateFilter {
/// Invoked whenever some new extent has been added to the allocation.
fn account_extent_added(&mut self);
/// Invoked whenever one or more extents have been from the allocation
/// again.
///
/// For each extent represented in `n_extents_dropped`,
/// [`account_extent_added()`](Self::account_extent_added) had previously
/// been invoked.
fn account_extents_dropped(&mut self, n_extents_dropped: u32);
/// Update internal accounting state.
///
/// Run potentially costly internal state updates. `update_filter_state()`
/// is guaranteed to get invoked at least once inbetween a subsequent
/// [`extent_candidate_acceptable()`](Self::extent_candidate_acceptable) and
/// any non-zero number of preceding
/// [`account_extent_added()`](Self::account_extent_added) or
/// [`account_extents_dropped()`](Self::account_extents_dropped)
/// invocations.
///
/// # Arguments:
///
/// * `progress` - The current allocation operation's associated
/// [`ExtentsAllocationRequestProgress`] tracking.
/// * `max_subword_extent_effective_payload_len` - The maximum effective
/// payload capacity of any extent less than [`BitmapWord::BITS`]
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) in
/// length.
fn update_filter_state(
&mut self,
progress: &ExtentsAllocationRequestProgress,
max_subword_extent_effective_payload_len: u64,
);
/// Decide whether or not some found free extent is eligible for inclusion
/// in the allocation.
///
/// # Arguments:
///
/// * `candidate_extent_allocation_blocks` - The length of the found free
/// extent candidate.
fn extent_candidate_acceptable(&self, candidate_extent_allocation_blocks: layout::AllocBlockCount) -> bool;
}
/// Unconstrained [extent allocation candidate
/// filter](FindFreeFullwordChunksExtentCandiateFilter).
///
/// Any free extent candidates are accepted for inclusion in the allocation.
struct FindFreeFullwordChunksExtentCandidateFilterUnconstrained {}
impl FindFreeFullwordChunksExtentCandiateFilter for FindFreeFullwordChunksExtentCandidateFilterUnconstrained {
fn account_extent_added(&mut self) {}
fn account_extents_dropped(&mut self, _n_extents_dropped: u32) {}
fn update_filter_state(
&mut self,
_progress: &ExtentsAllocationRequestProgress,
_max_subword_extent_effective_payload_len: u64,
) {
}
fn extent_candidate_acceptable(&self, _candidate_extent_allocation_blocks: layout::AllocBlockCount) -> bool {
true
}
}
/// Adaptive [extent allocation candidate
/// filter](FindFreeFullwordChunksExtentCandiateFilter) bounding the total
/// number of extents in an allocation.
///
/// The overall number of extents in an allocation is bounded roughly by the
/// base-2 logarithm (of a fixed fraction) of the request's total effective
/// payload length.
///
/// The filter is adaptive, i.e. candiate extents are accepted or rejected based
/// on whether or not the goal would still be achievable by some assumed
/// worst case extent length distribution after adding the current candidate
/// extent to the already accepted ones.
struct FindFreeFullwordChunksExtentCandidateFilterConstrainExtentsCount {
budget: u32,
min_accepted_extent_fullword_blocks: u64,
}
impl FindFreeFullwordChunksExtentCandidateFilterConstrainExtentsCount {
/// Instantiate.
///
/// # Arguments:
///
/// * `allocation_request` - The [`ExtentsAllocationRequest`] to filter
/// candidate extents for.
fn new(allocation_request: &ExtentsAllocationRequest) -> Self {
// Try to limit the total number of extents by (adaptively) rejecting too short
// runs of contiguous free fullword blocks. Many schemes are possible,
// for example one could fix the upper limit on the number of extents
// and require that each run is at least the total allocation request
// size divided by that number in length. However, that would put
// unreasonably strict constraints on possible chunk length distributions: if
// the scan happened to find one significantly longer run by chance,
// then one or more smaller ones could still be accepted while staying
// within the bounds of the total extent number limit. On the other
// hand, if the rejection scheme was to accept anything for all but the
// last extent, no matter how short, then chances are that no suitable free run
// of sufficient length covering the remaining allocation request size
// can be found in the last iteration.
//
// Thus, a more adaptive approach is chosen here. First of all, the total number
// of extents is limited to the log2 (rounded up) of the allocation
// request size, in units of fullword blocks. Over the course of the
// allocation, a budget of remaining extents is maintained. At any point
// in time, the minimum acceptable free fullword block run length
// is determined such that the overall remaining allocation request goal would
// still be achievable with a worst case configuration of exponentially
// growing chunks. To be more specific, those exponentially growing
// chunk lengths in the assumed worst case scenario would correspond
// (roughly) to the set bits in the remaining allocation request size
// each. In particular the maximum imposed lower bound would be less or equal to
// roughly half the remaining allocation request size.
let mut budget = if allocation_request.total_effective_payload_len != 1 {
(allocation_request.total_effective_payload_len - 1).ilog2() + 1
} else {
0
} + 1;
let fullword_block_len_log2 =
allocation_request.layout.allocation_block_size_128b_log2 as u32 + 7 + BITMAP_WORD_BITS_LOG2;
budget = budget.max(fullword_block_len_log2 + 1) - fullword_block_len_log2;
Self {
budget,
min_accepted_extent_fullword_blocks: 1,
}
}
}
impl FindFreeFullwordChunksExtentCandiateFilter for FindFreeFullwordChunksExtentCandidateFilterConstrainExtentsCount {
fn account_extent_added(&mut self) {
// In case the maximum extent length is constrained by the request, we might
// run out of budget early.
self.budget = self.budget.wrapping_sub(1)
}
fn account_extents_dropped(&mut self, n_extents_dropped: u32) {
// One or more extents got removed from the allocation again (because
// they got e.g. absorbed into subsequently found larger ones),
// revive the associated budget tickets, but only if the budget
// had not dropped to zero in the meanwhile.
if self.budget == 0 {
return;
}
self.budget += n_extents_dropped;
}
fn update_filter_state(
&mut self,
progress: &ExtentsAllocationRequestProgress,
max_subword_extent_effective_payload_len: u64,
) {
debug_assert!(
max_subword_extent_effective_payload_len
< (BitmapWord::BITS as u64) << (progress.request.layout.allocation_block_size_128b_log2 + 7)
);
let remaining_effective_payload_len = progress.remaining_effective_payload_len();
debug_assert!(remaining_effective_payload_len > max_subword_extent_effective_payload_len);
// Split off the maximum effective payload len amount which would fit a
// sub-bitmap word allocation for the accounting that follows below.
// If, after aligning upwards, the fullwords allocation extends into that,
// good; if not, a subsequent subword allocation search will take care of it.
let remaining_effective_payload_len =
remaining_effective_payload_len - max_subword_extent_effective_payload_len;
// In case of an user-specified upper bound on the allocation_request's
// max_extent_allocation_blocks, the budget might get exhausted prematurely, due
// to the cap. Accept only extents of maximum length then.
if self.budget == 0 {
// If the budget has been exhausted, then this cannot be the first allocation.
debug_assert_ne!(progress.allocated_effective_payload_len, 0);
let remaining_request_max_extent_allocation_blocks = progress
.request
.layout
.extent_payload_len_to_allocation_blocks(remaining_effective_payload_len, false)
.0;
let remaining_request_max_extent_allocation_blocks = remaining_request_max_extent_allocation_blocks.min(
progress
.request
.layout
.max_extent_allocation_blocks
.align_down(BITMAP_WORD_BITS_LOG2),
);
// The imposed upper bound of max_extent_allocation_blocks is aligned, so
// rounding up would not overflow.
let remaining_request_max_extent_allocation_blocks =
u64::from(remaining_request_max_extent_allocation_blocks)
.round_up_pow2_unchecked(BITMAP_WORD_BITS_LOG2);
self.min_accepted_extent_fullword_blocks =
remaining_request_max_extent_allocation_blocks >> BITMAP_WORD_BITS_LOG2;
return;
}
let is_first = progress.allocated_effective_payload_len == 0;
// First step: allocate the remaining "budget tickets" to the individual
// positions in remaining_fullword_blocks.
// - First allocate to all set bits in remaining_fullword_blocks, from most to
// least significant bits.
// - Then fill up the unset bits in remaining_fullword_blocks, from most to
// least significant, until the budget is exhausted.
// Note that the only thing that matters is the least signifcant bit position
// with a "budget ticket" allocated to it, c.f. the second step below. So handle
// three different cases, of increasing computational cost:
// 1. The distance between the most and the least significant bits in
// remaining_fullword_blocks is less than the budget: in this case all set
// bits as well as the unset ones interspersed inbetween will receive a
// "budget ticket". The least significant bit receiving a "budget ticket"
// allocation will be located at or to the right of the least significant bit
// in remaining_fullword_blocks and can be computed directly.
// 2. The number of set bits in remaining_fullword_blocks is less or equal to
// the budget: in this case, all set bits will have a "budget ticket"
// allocated to them, but not all their separating unset bits. The least
// significant bit receiving a "budget ticket" allocation will be indentical
// to the least significant set bit in remaining_fullword_blocks.
// 3. In the remaining case, there are fewer budget tickets than set bits in
// remaining_fullword_blocks. Allocate them from most to least significant
// bits.
let remaining_fullword_blocks = (remaining_effective_payload_len
>> (progress.request.layout.allocation_block_size_128b_log2 as u32 + 7)
>> BITMAP_WORD_BITS_LOG2)
.max(1);
let remaining_fullword_blocks_lsb = remaining_fullword_blocks & remaining_fullword_blocks.wrapping_neg();
let remaining_fullword_blocks_lsb_log2 = remaining_fullword_blocks_lsb.ilog2();
let budget_allocation_mask = if remaining_fullword_blocks_lsb_log2 + self.budget >= u64::BITS
|| remaining_fullword_blocks_lsb << self.budget > remaining_fullword_blocks
{
// Case 1.)
1u64 << (remaining_fullword_blocks.ilog2().saturating_sub(self.budget - 1))
} else if self.budget >= remaining_fullword_blocks.count_ones() {
// Case 2.)
remaining_fullword_blocks_lsb
} else {
// Case 3.)
// Create a left aligned contiguous chunk of budget set bits and
// scatter that over the set bits in remaining_fullword_blocks, starting
// from the left.
let budget_pool = !(u64::trailing_bits_mask(u64::BITS - self.budget));
budget_pool.expand_from_left(remaining_fullword_blocks)
};
// Second step: determine the minimum acceptable fullword block run length from
// the least significant bit that received a "budget ticket"
// allocation to it. Require that the minimum fullword block run
// will have sufficient length to accomodate for at least the modulo
// of the remaining allocation request length by twice that least significant
// bit. That is, if a run length of exactly that minimum length
// would have been accepted, all the bits at and to the right of
// this least significant bit position in
// remaining_effective_payload_len would become clear thereafter.
// Convert the mask from units of fullword blocks to bytes:
let budget_allocation_mask = budget_allocation_mask
<< (progress.request.layout.allocation_block_size_128b_log2 + 7)
<< BITMAP_WORD_BITS_LOG2;
let budget_allocation_mask = if budget_allocation_mask != 0 {
// Set all bits at and to the right of the least significant bit, clear anything
// above:
budget_allocation_mask ^ (budget_allocation_mask - 1)
} else {
!0
};
let min_accepted_extent_effective_payload_len = remaining_effective_payload_len & budget_allocation_mask;
// As an additional constraint to avoid an excessive number of small extents for
// certain values of remaining_effective_payload_len with a longer
// sequence of zeros towards the tail, require that the minimum is
// at least equal to the least significant bit with a
// "budget ticket" allocated to it. Isolate the LSB:
let budget_allocation_mask_lsb = budget_allocation_mask & budget_allocation_mask.wrapping_neg();
let min_accepted_extent_effective_payload_len = min_accepted_extent_effective_payload_len
.max(budget_allocation_mask_lsb)
.min(remaining_effective_payload_len);
let min_accepted_extent_allocation_blocks = progress
.request
.layout
.extent_payload_len_to_allocation_blocks(min_accepted_extent_effective_payload_len, is_first)
.0;
let min_accepted_extent_allocation_blocks = u64::from(min_accepted_extent_allocation_blocks);
let min_accepted_extent_allocation_blocks = min_accepted_extent_allocation_blocks.min(
u64::from(progress.request.layout.max_extent_allocation_blocks).round_down_pow2(BITMAP_WORD_BITS_LOG2),
);
// The imposed upper bound of max_extent_allocation_blocks is aligned, so
// rounding up would not overflow.
let min_accepted_extent_allocation_blocks =
min_accepted_extent_allocation_blocks.round_up_pow2_unchecked(BITMAP_WORD_BITS_LOG2);
self.min_accepted_extent_fullword_blocks = min_accepted_extent_allocation_blocks >> BITMAP_WORD_BITS_LOG2;
}
fn extent_candidate_acceptable(&self, candidate_extent_allocation_blocks: layout::AllocBlockCount) -> bool {
u64::from(candidate_extent_allocation_blocks) >> BITMAP_WORD_BITS_LOG2
>= self.min_accepted_extent_fullword_blocks
}
}
/// The maximum search distance in units of bitmap words when attempting
/// allocation placement optimization in order to reduce overall fragmentation.
const PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS: u64 = 16;
/// In-memory representation of the filesystem instance's allocation bitmap.
pub struct AllocBitmap {
pub(super) bitmap: Vec<BitmapWord>,
}
impl AllocBitmap {
/// Instantiate an [`AllocBitmap`] initialized to all `false`.
///
/// # Arguments:
///
/// `image_allocation_blocks` - Total number of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) to track.
pub fn new(image_allocation_blocks: layout::AllocBlockCount) -> Result<Self, NvFsError> {
if u64::from(image_allocation_blocks) == 0 {
return Ok(Self { bitmap: Vec::new() });
}
let bitmap_words = ((u64::from(image_allocation_blocks) - 1) >> BITMAP_WORD_BITS_LOG2) + 1;
let bitmap_words = usize::try_from(bitmap_words).map_err(|_| NvFsError::DimensionsNotSupported)?;
let bitmap = try_alloc_vec(bitmap_words)?;
Ok(Self { bitmap })
}
/// Resize an [`AllocBitmap`].
///
/// Any new entries added will get initialized with `false`.
///
/// # Arguments:
///
/// `image_allocation_blocks` - Updated total number of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) to track.
pub fn resize(&mut self, image_allocation_blocks: layout::AllocBlockCount) -> Result<(), NvFsError> {
if u64::from(image_allocation_blocks) == 0 {
self.bitmap = Vec::new();
return Ok(());
}
let bitmap_words = ((u64::from(image_allocation_blocks) - 1) >> BITMAP_WORD_BITS_LOG2) + 1;
let bitmap_words = usize::try_from(bitmap_words).map_err(|_| NvFsError::DimensionsNotSupported)?;
if bitmap_words < self.bitmap.len() {
self.bitmap.truncate(bitmap_words);
let bits_in_last_bitmap_word = u64::BITS
- ((u64::from(image_allocation_blocks).wrapping_neg() & u64::trailing_bits_mask(BITMAP_WORD_BITS_LOG2))
as u32);
self.bitmap[bitmap_words - 1] &= u64::trailing_bits_mask(bits_in_last_bitmap_word);
} else {
let prev_bitmap_words = self.bitmap.len();
self.bitmap.try_reserve_exact(bitmap_words - prev_bitmap_words)?;
self.bitmap.resize(bitmap_words, 0 as BitmapWord);
}
Ok(())
}
/// Iterate over individual [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), starting
/// at a specified position.
///
/// # Arguments:
///
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`] for the purpose
/// of the iteration.
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`] for the purpose of
/// the iteration.
/// * `first_physical_allocation_block_index` - Starting position of the
/// returned iterator.
pub fn iter_at_allocation_block<'a, const AN: usize, const FN: usize>(
&'a self,
pending_allocs: &'a SparseAllocBitmapUnion<'_, AN>,
pending_frees: &'a SparseAllocBitmapUnion<'_, FN>,
first_physical_allocation_block_index: layout::PhysicalAllocBlockIndex,
) -> AllocBitmapIterator<'a, AN, FN> {
AllocBitmapIterator::new_at(
self,
pending_allocs,
pending_frees,
first_physical_allocation_block_index,
)
}
/// Iterate over chunks of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), starting
/// at a specified position.
///
/// Iterate over the [`AllocBitmap`] in fixed chunks of a specified number
/// of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) each, as
/// specified by `chunk_allocation_blocks`.
/// `chunk_allocation_blocks` must not exceed [`BitmapWord::BITS`] and the
/// iterator returns [`BitmapWord`]s, one at a time, with the lower
/// `chunk_allocation_blocks` bits representing the allocation status of
/// each [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) in the
/// current chunk.
///
/// # Arguments:
///
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`] for the purpose
/// of the iteration.
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`] for the purpose of
/// the iteration.
/// * `first_physical_allocation_block_index` - Starting position of the
/// returned iterator.
/// * `chunk_allocation_blocks` - Iteration chunk size in units of
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
pub fn iter_chunked_at_allocation_block<'a, const AN: usize, const FN: usize>(
&'a self,
pending_allocs: &'a SparseAllocBitmapUnion<'_, AN>,
pending_frees: &'a SparseAllocBitmapUnion<'_, FN>,
first_physical_allocation_block_index: layout::PhysicalAllocBlockIndex,
chunk_allocation_blocks: u32,
) -> AllocBitmapChunkedIterator<'a, AN, FN> {
AllocBitmapChunkedIterator::new_at(
self,
pending_allocs,
pending_frees,
first_physical_allocation_block_index,
chunk_allocation_blocks,
)
}
/// Apply pending allocations or deallocations to the [`AllocBitmap`].
///
/// # Arguments:
///
/// * `pending` - The set of pending allocations or deallocations, with a
/// bit set for each member in the set.
/// * `is_free` - `true` if `pending` is to get applied as a deallocation,
/// `false` if as an allocation.
pub fn apply_pending(&mut self, pending: &SparseAllocBitmap, is_free: bool) -> Result<(), NvFsError> {
if !is_free {
for (allocation_blocks_begin, bitmap_word) in pending.iter() {
let bitmap_word_index = usize::try_from(u64::from(allocation_blocks_begin) >> BITMAP_WORD_BITS_LOG2)
.map_err(|_| nvfs_err_internal!())?;
self.bitmap[bitmap_word_index] |= bitmap_word;
}
} else {
for (allocation_blocks_begin, bitmap_word) in pending.iter() {
let bitmap_word_index = usize::try_from(u64::from(allocation_blocks_begin) >> BITMAP_WORD_BITS_LOG2)
.map_err(|_| nvfs_err_internal!())?;
self.bitmap[bitmap_word_index] &= !bitmap_word;
}
}
Ok(())
}
/// Set the bits in a range to a specified value.
///
/// # Arguments:
///
/// * `range` - The range to set the correspinding [`AllocBitmap`] bits for.
/// * `value` - The value to set the specified `range`'s corresponding bits
/// to.
pub fn set_in_range(&mut self, range: &layout::PhysicalAllocBlockRange, value: bool) -> Result<(), NvFsError> {
let mut physical_allocation_block_count = u64::from(range.block_count());
let first_physical_allocation_block_index = u64::from(range.begin());
let bitmap_word_index_begin = first_physical_allocation_block_index >> BITMAP_WORD_BITS_LOG2;
let mut offset_in_bitmap_word =
(first_physical_allocation_block_index & u64::trailing_bits_mask(BITMAP_WORD_BITS_LOG2)) as u32;
let bitmap_word_index_end = ((u64::from(range.end()) - 1) >> BITMAP_WORD_BITS_LOG2) + 1;
let mut bitmap_word_index_end =
usize::try_from(bitmap_word_index_end).map_err(|_| NvFsError::IoError(NvFsIoError::RegionOutOfRange))?;
if bitmap_word_index_end > self.bitmap.len() {
if value {
return Err(NvFsError::IoError(NvFsIoError::RegionOutOfRange));
} else {
// The BitmapWord's beyond the end of the Allocation Bitmap are all implicitly
// considered unset already. Modify only what's allocated.
bitmap_word_index_end = self.bitmap.len();
if bitmap_word_index_begin >= bitmap_word_index_end as u64 {
return Ok(());
}
}
}
let bitmap_word_index_begin = bitmap_word_index_begin as usize;
let set_mask = if value { !0 } else { 0 };
for bitmap_word_index in bitmap_word_index_begin..bitmap_word_index_end {
let bits_in_word =
physical_allocation_block_count.min((BitmapWord::BITS - offset_in_bitmap_word) as u64) as u32;
let bits_in_word_mask = BitmapWord::trailing_bits_mask(bits_in_word) << offset_in_bitmap_word;
self.bitmap[bitmap_word_index] &= !bits_in_word_mask;
self.bitmap[bitmap_word_index] |= set_mask & bits_in_word_mask;
physical_allocation_block_count -= bits_in_word as u64;
offset_in_bitmap_word = 0;
}
Ok(())
}
/// Find a free aligned block of specified size.
///
/// Find a free block of size as specified by
/// `block_allocation_blocks_log2`, and aligned to that size.
///
/// If needed, the search might return some subblock of a larger containing
/// free block, but it tries to avoid that as much as possible in order
/// to not unnecessarily split up such larger free blocks and to reduce
/// fragmentation.
///
/// # Arguments:
///
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the free block's
/// size and alignment in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must
/// be less than or equal to [`BitmapWord::BITS`].
/// * `allocated_fullword_chunks` - List of extents to consider virtually
/// as having been allocated, independent of the current state in the
/// [`AllocBitmap`].
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
/// * `search_allocation_blocks_begin` - Hint about where to start the
/// search, if any. Should usually be set to the location of the most
/// recently allocated block in a series of such allocations.
/// * `optimize_placement` - Whether to attempt to optimize block placement
/// in order to reduce fragmentation. Doing that is relatively costly, so
/// it may be set to false when allocating blocks with a limited
/// lifetime, such as for the journal staging copies.
#[allow(clippy::too_many_arguments)]
pub fn find_free_block<const AN: usize, const FN: usize>(
&self,
block_allocation_blocks_log2: u32,
mut allocated_fullword_chunks: Option<&extents::PhysicalExtents>,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
search_allocation_blocks_begin: Option<layout::PhysicalAllocBlockIndex>,
optimize_placement: bool,
) -> Option<layout::PhysicalAllocBlockIndex> {
let block_allocations_blocks = 1u32 << block_allocation_blocks_log2;
debug_assert!(block_allocations_blocks <= BitmapWord::BITS);
if block_allocations_blocks == BitmapWord::BITS {
return self.find_free_fullword_block(allocated_fullword_chunks, pending_allocs, pending_frees, image_size);
}
// The addition does not overflow, image_size is in units of Allocation Blocks,
// and has at least the upper 7 Bits clear.
let image_bitmap_words = (u64::from(image_size) + (u64::BITS as u64 - 1)) >> BITMAP_WORD_BITS_LOG2;
let search_begin_bitmap_word_index = match search_allocation_blocks_begin {
Some(search_allocation_blocks_begin) => {
let search_begin_bitmap_word_index = u64::from(search_allocation_blocks_begin) >> BITMAP_WORD_BITS_LOG2;
if optimize_placement {
// Go back by the optimization search distance, the second best fit might have
// been skipped over and we want to favor allocations towards the beginning of
// the storage in general.
search_begin_bitmap_word_index
.saturating_sub(PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS - 1)
} else {
// Assume the previous block had too been searched for with placement
// optimization disabled, i.e. it's been the first free
// block found.
search_begin_bitmap_word_index
}
}
None => 0,
};
debug_assert!(search_begin_bitmap_word_index < image_bitmap_words);
let word_blocks_lsbs_mask_table = BitmapWordBlocksLsbsMaskTable::new();
let word_blocks_lsbs_mask = word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(block_allocation_blocks_log2);
let mut next_allocated_fullword_chunk: Option<layout::PhysicalAllocBlockRange> = None;
struct FoundCandidate {
bitmap_word_index: u64,
bitmap_word: BitmapWord,
split_block_allocation_blocks_log2: u32, // Minimize.
}
let mut best: Option<FoundCandidate> = None;
// While nothing has been found, keep going. The increment cannot overflow,
// image_bitmap_words has the upper BITMAP_WORD_BITS_LOG2 clear.
let mut remaining_optimization_search_distance = image_bitmap_words + 1;
let mut bitmaps_words_iter = AllocBitmapWordIterator::new_at_bitmap_word_index(
self,
pending_allocs,
pending_frees,
search_begin_bitmap_word_index,
)
.take(usize::try_from(image_bitmap_words - search_begin_bitmap_word_index).unwrap_or(usize::MAX));
while let Some((bitmap_word_index, mut bitmap_word)) = bitmaps_words_iter.next() {
remaining_optimization_search_distance -= 1;
if remaining_optimization_search_distance == 0 {
// Placement optimization search distance exhausted. Return what we have.
debug_assert!(optimize_placement && best.is_some());
break;
}
if bitmap_word_index + 1 == image_bitmap_words {
// Set the excess high bits not backed by any actual storage.
bitmap_word |= !BitmapWord::trailing_bits_mask(
BitmapWord::BITS - ((u64::from(image_size).wrapping_neg() & (BitmapWord::BITS as u64 - 1)) as u32),
);
// Prepare the bitmap_words_iter for wrap-around in the next iteration, if any.
if search_begin_bitmap_word_index != 0 {
bitmaps_words_iter =
AllocBitmapWordIterator::new_at_bitmap_word_index(self, pending_allocs, pending_frees, 0)
.take(usize::try_from(search_begin_bitmap_word_index).unwrap_or(usize::MAX));
}
}
// Don't bother examining any further if all allocation blocks tracked by this
// word are allocated already anyway.
if bitmap_word == !0 {
continue;
}
if bitmap_word == 0 {
if best.is_none() {
// Check if the bitmap word is really free or has perhaps been previously
// allocated as part of a preceeding fullword chunks allocation
// round for processing the very same request.
next_allocated_fullword_chunk = next_allocated_fullword_chunk
.filter(|next_allocated_fullword_chunk| {
u64::from(next_allocated_fullword_chunk.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index
})
.or_else(|| {
allocated_fullword_chunks
.map(|e| e.iter())
.into_iter()
.flatten()
.filter(|e| u64::from(e.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index)
.min_by_key(|e| e.end())
});
if let Some(next_allocated_fullword_chunk) = next_allocated_fullword_chunk {
if u64::from(next_allocated_fullword_chunk.begin()) >> BITMAP_WORD_BITS_LOG2
<= bitmap_word_index
{
continue;
}
} else {
// No more extents at or after the current position, avoid another search..
allocated_fullword_chunks = None;
}
if !optimize_placement {
// Found something and no placement optimization requested, bail out.
return Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index << BITMAP_WORD_BITS_LOG2,
));
}
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance = PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
best = Some(FoundCandidate {
bitmap_word_index,
bitmap_word,
split_block_allocation_blocks_log2: BITMAP_WORD_BITS_LOG2,
});
}
continue;
}
let free_blocks_lsbs =
Self::bitmap_word_free_blocks_lsbs(bitmap_word, block_allocation_blocks_log2, word_blocks_lsbs_mask);
if free_blocks_lsbs == 0 {
continue;
}
if !optimize_placement {
// Not interested in placement optimizations and there is at least one free
// block in the current bitmap word. Take that.
return Some(layout::PhysicalAllocBlockIndex::from(
(bitmap_word_index << BITMAP_WORD_BITS_LOG2) + free_blocks_lsbs.trailing_zeros() as u64,
));
}
// It is possible to allocate the block from the range tracked by the current
// word. See if it is the best candidate: determine the minimum
// power-of-two sized block the allocation would split, if any, and
// minimize that.
let split_block_allocation_blocks_log2 = Self::bitmap_word_block_alloc_split_block_size_log2(
free_blocks_lsbs,
best.as_ref().map(
|FoundCandidate {
split_block_allocation_blocks_log2,
..
}| {
debug_assert!(*split_block_allocation_blocks_log2 > block_allocation_blocks_log2);
*split_block_allocation_blocks_log2
},
),
block_allocation_blocks_log2,
word_blocks_lsbs_mask,
&word_blocks_lsbs_mask_table,
);
if split_block_allocation_blocks_log2 == block_allocation_blocks_log2 {
// It's a perfect fit.
return Some(layout::PhysicalAllocBlockIndex::from(
(bitmap_word_index << BITMAP_WORD_BITS_LOG2)
+ Self::bitmap_word_block_alloc_select_block(
free_blocks_lsbs,
block_allocation_blocks_log2,
word_blocks_lsbs_mask,
&word_blocks_lsbs_mask_table,
) as u64,
));
} else if best
.as_ref()
.map(
|FoundCandidate {
split_block_allocation_blocks_log2: best_split_block_allocations_block_log2,
..
}| {
*best_split_block_allocations_block_log2 > split_block_allocation_blocks_log2
},
)
.unwrap_or(true)
{
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance = PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
}
best = Some(FoundCandidate {
bitmap_word_index,
bitmap_word,
split_block_allocation_blocks_log2,
});
}
}
if let Some(FoundCandidate {
bitmap_word_index,
bitmap_word,
split_block_allocation_blocks_log2,
}) = best
{
let word_split_blocks_lsbs_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(split_block_allocation_blocks_log2);
let mut free_split_blocks_lsbs = Self::bitmap_word_free_blocks_lsbs(
bitmap_word,
split_block_allocation_blocks_log2,
word_split_blocks_lsbs_mask,
);
if split_block_allocation_blocks_log2 < BITMAP_WORD_BITS_LOG2 - 1 {
let double_split_block_allocations_block_log2 = split_block_allocation_blocks_log2 + 1;
let word_double_split_blocks_lsbs_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(double_split_block_allocations_block_log2);
free_split_blocks_lsbs = Self::bitmap_word_filter_blocks_with_free_buddy_lsbs(
free_split_blocks_lsbs,
free_split_blocks_lsbs,
split_block_allocation_blocks_log2,
word_double_split_blocks_lsbs_mask,
);
}
debug_assert_ne!(free_split_blocks_lsbs, 0);
Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * BitmapWord::BITS as u64
+ Self::bitmap_word_block_alloc_select_block(
free_split_blocks_lsbs,
split_block_allocation_blocks_log2,
word_split_blocks_lsbs_mask,
&word_blocks_lsbs_mask_table,
) as u64,
))
} else {
None
}
}
/// Find free [extents](extents::PhysicalExtents).
///
/// Find free [extents](extents::PhysicalExtents) for serving the
/// `allocation_request`.
///
/// On success, a pair of the found extents and the amount of excess payload
/// space is being returned.
///
/// The [common extents header](ExtentsLayout::extents_hdr_len), if any, is
/// expected to get placed into the first extent of the returned
/// sequence.
///
/// # Arguments:
///
/// * `allocation_request` - The allocation request parameters, the desired
/// total effective payload length as well as the [`ExtentsLayout`] to be
/// more specific.
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
/// * `optimize_placement` - Whether to attempt to optimize extent
/// placement in order to reduce fragmentation. Doing that is relatively
/// costly, so it may be set to false when allocating blocks with a
/// limited lifetime, such as for the journal staging copies.
pub fn find_free_extents<const AN: usize, const FN: usize>(
&self,
allocation_request: &ExtentsAllocationRequest,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
optimize_placement: bool,
) -> Result<Option<(extents::PhysicalExtents, u64)>, NvFsError> {
debug_assert!(
allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32 <= BITMAP_WORD_BITS_LOG2
);
if allocation_request.total_effective_payload_len == 0 {
return Ok(Some((extents::PhysicalExtents::new(), 0)));
}
let (max_first_extent_allocation_blocks, max_first_extent_is_exhaustive) =
allocation_request.remaining_max_extent_allocation_blocks(0);
debug_assert_ne!(u64::from(max_first_extent_allocation_blocks), 0);
if max_first_extent_is_exhaustive && u64::from(max_first_extent_allocation_blocks) < 2 * BitmapWord::BITS as u64
{
let total_allocated_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(max_first_extent_allocation_blocks, true);
let allocated_excess_effective_payload_len =
total_allocated_effective_payload_len - allocation_request.total_effective_payload_len;
if u64::from(max_first_extent_allocation_blocks) <= BitmapWord::BITS as u64 {
return match self.find_free_subword_chunk(
u64::from(max_first_extent_allocation_blocks) as u32,
allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32,
None,
pending_allocs,
pending_frees,
image_size,
optimize_placement,
) {
Some(extent_allocation_blocks_begin) => {
let extent = layout::PhysicalAllocBlockRange::from((
extent_allocation_blocks_begin,
max_first_extent_allocation_blocks,
));
let mut extents = extents::PhysicalExtents::new();
extents.push_extent(&extent, true)?;
Ok(Some((extents, allocated_excess_effective_payload_len)))
}
None => Ok(None),
};
} else if let Some(extent_allocation_blocks_begin) = self.find_free_sub_doubleword_chunk(
u64::from(max_first_extent_allocation_blocks) as u32,
pending_allocs,
pending_frees,
image_size,
optimize_placement,
) {
let extent = layout::PhysicalAllocBlockRange::from((
extent_allocation_blocks_begin,
max_first_extent_allocation_blocks,
));
let mut extents = extents::PhysicalExtents::new();
extents.push_extent(&extent, true)?;
return Ok(Some((extents, allocated_excess_effective_payload_len)));
}
}
let (head_extent_min_allocation_blocks, tail_extent_min_allocation_blocks) =
allocation_request.layout.min_extents_allocation_blocks();
if u64::from(allocation_request.layout.max_extent_allocation_blocks) >= BitmapWord::BITS as u64 {
let (mut progress, mut extents) = match self.find_free_fullword_chunks(
allocation_request,
pending_allocs,
pending_frees,
image_size,
head_extent_min_allocation_blocks,
tail_extent_min_allocation_blocks,
)? {
Some(result) => result,
None => return Ok(None),
};
let remaining_effective_payload_len = progress.remaining_effective_payload_len();
if remaining_effective_payload_len != 0 {
let remainder_extent_allocation_blocks = progress
.request
.layout
.extent_payload_len_to_allocation_blocks(remaining_effective_payload_len, false)
.0;
debug_assert!(u64::from(remainder_extent_allocation_blocks) < BitmapWord::BITS as u64);
let remainder_extent = match self.find_free_subword_chunk(
u64::from(remainder_extent_allocation_blocks) as u32,
allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32,
Some(&extents),
pending_allocs,
pending_frees,
image_size,
optimize_placement,
) {
Some(remainder_extent_allocation_blocks_begin) => layout::PhysicalAllocBlockRange::from((
remainder_extent_allocation_blocks_begin,
remainder_extent_allocation_blocks,
)),
None => return Ok(None),
};
extents.push_extent(&remainder_extent, true)?;
let remainder_extent_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(remainder_extent_allocation_blocks, false);
debug_assert_eq!(progress.allocated_excess_effective_payload_len, 0);
progress.allocated_effective_payload_len += remaining_effective_payload_len;
debug_assert_eq!(
progress.allocated_effective_payload_len,
allocation_request.total_effective_payload_len
);
progress.allocated_excess_effective_payload_len =
remainder_extent_effective_payload_len - remaining_effective_payload_len;
}
Ok(Some((extents, progress.allocated_excess_effective_payload_len)))
} else {
// The request's maxium allowed extent size is less than what's covered by a
// fullword block. The overall strategy is to allocate (mostly)
// fullword block extents and split all extents up as is necessary
// afterwards.
debug_assert!(u64::from(max_first_extent_allocation_blocks) < u64::BITS as u64);
debug_assert!(!max_first_extent_is_exhaustive);
// First, try to make the maximum extent length a power of two so that it
// would evenly divide a fullword block extent.
let max_extent_allocation_blocks = layout::AllocBlockCount::from(
u64::from(allocation_request.layout.max_extent_allocation_blocks).round_down_next_pow2(),
);
if max_extent_allocation_blocks >= head_extent_min_allocation_blocks
&& max_extent_allocation_blocks >= tail_extent_min_allocation_blocks
{
// Determine how many of such extents of maximum size are needed
// for satisfying the request. The remainder, if any, will get handled
// below.
let max_first_extent_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(max_extent_allocation_blocks, true);
debug_assert!(max_first_extent_effective_payload_len < allocation_request.total_effective_payload_len);
let max_tail_extent_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(max_extent_allocation_blocks, false);
let n_max_extents = 1
+ ((allocation_request.total_effective_payload_len - max_first_extent_effective_payload_len)
/ max_tail_extent_effective_payload_len);
// Compute the remainder to be allocated in an extent strictly smaller than the
// maximum.
let remainder_extent_min_effective_payload_len = allocation_request.total_effective_payload_len
- max_first_extent_effective_payload_len
- (n_max_extents - 1) * max_tail_extent_effective_payload_len;
let remainder_extent_allocation_blocks = allocation_request
.layout
.extent_payload_len_to_allocation_blocks(remainder_extent_min_effective_payload_len, false)
.0;
// All the allocated excess comes from this remainder extent, compute it now.
let remainder_extent_allocated_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(remainder_extent_allocation_blocks, false);
let allocated_excess_effective_payload_len =
remainder_extent_allocated_effective_payload_len - remainder_extent_min_effective_payload_len;
// Now determine how many Allocation Blocks need to get allocated in total.
let total_allocation_blocks = n_max_extents
.checked_mul(u64::from(max_extent_allocation_blocks))
.ok_or(NvFsError::DimensionsNotSupported)?;
let total_allocation_blocks = total_allocation_blocks
.checked_add(u64::from(remainder_extent_allocation_blocks))
.ok_or(NvFsError::DimensionsNotSupported)?;
if total_allocation_blocks
>> (u64::BITS - allocation_request.layout.allocation_block_size_128b_log2 as u32 - 7)
!= 0
{
return Err(NvFsError::DimensionsNotSupported);
}
// Allocate as many of the total Allocation Blocks to allocate in chunks of
// (multiples of) fullword blocks, the remainder will get handled in
// a single subword chunk allocation below.
let fullwords_extents_allocation_blocks =
total_allocation_blocks.round_down_pow2(BITMAP_WORD_BITS_LOG2);
let subword_extent_allocation_blocks = total_allocation_blocks - fullwords_extents_allocation_blocks;
debug_assert!(
(allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32) < BITMAP_WORD_BITS_LOG2
);
// Create a trivial dummy layout for the fullword allocations -- the original
// request layout's header lengths, payload alignment and such had
// already been accounted for when calculating the needed number of
// extents of maximum size (n_max_extents), as well as the remainder
// (tail_extent_allocation_blocks) above.
let trivial_fullwords_extents_layout = ExtentsLayout::new(
None,
BITMAP_WORD_BITS_LOG2 as u8,
0,
0,
0,
1,
allocation_request.layout.allocation_block_size_128b_log2,
)?;
let fullwords_allocation_request = ExtentsAllocationRequest::new(
fullwords_extents_allocation_blocks << (allocation_request.layout.allocation_block_size_128b_log2),
&trivial_fullwords_extents_layout,
);
let fullwords_allocation_result = self.find_free_fullword_chunks(
&fullwords_allocation_request,
pending_allocs,
pending_frees,
image_size,
layout::AllocBlockCount::from(BitmapWord::BITS as u64),
layout::AllocBlockCount::from(BitmapWord::BITS as u64),
)?;
let mut unconstrained_extents = match fullwords_allocation_result {
Some((fullwords_allocation_progress, unconstrained_extents)) => {
debug_assert_eq!(fullwords_allocation_progress.remaining_effective_payload_len(), 0);
debug_assert_eq!(fullwords_allocation_progress.allocated_excess_effective_payload_len, 0);
unconstrained_extents
}
None => return Ok(None),
};
if subword_extent_allocation_blocks != 0 {
let subword_extent = match self.find_free_subword_chunk(
subword_extent_allocation_blocks as u32,
allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32,
Some(&unconstrained_extents),
pending_allocs,
pending_frees,
image_size,
optimize_placement,
) {
Some(subword_extent_allocation_blocks_begin) => layout::PhysicalAllocBlockRange::from((
subword_extent_allocation_blocks_begin,
layout::AllocBlockCount::from(subword_extent_allocation_blocks),
)),
None => return Ok(None),
};
unconstrained_extents.push_extent(&subword_extent, true)?;
}
let mut extents = extents::PhysicalExtents::new();
for unconstrained_extent in unconstrained_extents.iter() {
let mut extent_begin = unconstrained_extent.begin();
let mut remaining_unconstrained_allocation_blocks = unconstrained_extent.block_count();
while u64::from(remaining_unconstrained_allocation_blocks) != 0 {
let constrained_allocation_blocks =
remaining_unconstrained_allocation_blocks.min(max_extent_allocation_blocks);
extents.push_extent(
&layout::PhysicalAllocBlockRange::from((extent_begin, constrained_allocation_blocks)),
true,
)?;
extent_begin += constrained_allocation_blocks;
remaining_unconstrained_allocation_blocks =
remaining_unconstrained_allocation_blocks - constrained_allocation_blocks;
}
}
Ok(Some((extents, allocated_excess_effective_payload_len)))
} else {
// The max_extent_allocation_blocks cannot get rounded down to power of two,
// because that would drop to below the minimum extent length.
// At this point in time, nothing would issue such requests, so
// don't even bother supporting it.
Err(NvFsError::from(FormatError::UnsupportedImageLayoutConfig))
}
}
}
/// Find a free block of size and alignment equal to [`BitmapWord::BITS`]
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
///
/// # Arguments:
///
/// * `allocated_fullword_chunks` - List of extents to consider virtually
/// as having been allocated, independent of the current state in the
/// [`AllocBitmap`].
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
fn find_free_fullword_block<const AN: usize, const FN: usize>(
&self,
allocated_fullword_chunks: Option<&extents::PhysicalExtents>,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
) -> Option<layout::PhysicalAllocBlockIndex> {
let image_bitmap_words = u64::from(image_size) >> BITMAP_WORD_BITS_LOG2;
let bitmaps_words_iter =
AllocBitmapWordIterator::new_at_bitmap_word_index(self, pending_allocs, pending_frees, 0);
let mut next_allocated_fullword_chunk: Option<layout::PhysicalAllocBlockRange> = None;
for (bitmap_word_index, bitmap_word) in
bitmaps_words_iter.take(usize::try_from(image_bitmap_words).unwrap_or(usize::MAX))
{
if bitmap_word == 0 {
// Check if the bitmap word is really free or has perhaps been previously
// allocated as part of a preceeding fullword chunks allocation
// round for processing the very same request.
next_allocated_fullword_chunk = next_allocated_fullword_chunk
.filter(|next_allocated_fullword_chunk| {
u64::from(next_allocated_fullword_chunk.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index
})
.or_else(|| {
allocated_fullword_chunks
.map(|e| e.iter())
.into_iter()
.flatten()
.filter(|e| u64::from(e.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index)
.min_by_key(|e| e.end())
});
if let Some(next_allocated_fullword_chunk) = next_allocated_fullword_chunk {
if u64::from(next_allocated_fullword_chunk.begin()) >> BITMAP_WORD_BITS_LOG2 <= bitmap_word_index {
continue;
}
}
return Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * (BitmapWord::BITS as u64),
));
}
}
None
}
/// Find a free extent of size and alignment less than[`BitmapWord::BITS`]
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
///
/// Find a free extent of size and alignment as specified by
/// `chunk_allocation_blocks` and
/// `chunk_alignment_allocation_blocks_log2` respectively.
///
/// Note that the search procedure attempts to reduce fragmentation by
/// avoiding to split up free blocks, i.e. free extents of length a power of
/// two, if possible, and prefers to split up smaller blocks over larger
/// ones.
///
/// # Arguments:
///
/// * `chunk_allocation_blocks` - The desired extent length in units of
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must
/// be strictly less than [`BitmapWord::BITS`] and a multiple of the
/// alignment specified via `chunk_alignment_allocation_blocks_log2`.
/// * `chunk_alignment_allocation_blocks_log2` - Base-2 logarithm of the
/// desired extent alignment in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2).
/// * `allocated_fullword_chunks` - List of extents to consider virtually
/// as having been allocated, independent of the current state in the
/// [`AllocBitmap`].
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
/// * `optimize_placement` - Whether to attempt to optimize extent
/// placement in order to reduce fragmentation. Doing that is relatively
/// costly, so it may be set to false when allocating blocks with a
/// limited lifetime, such as for the journal staging copies.
#[allow(clippy::too_many_arguments)]
fn find_free_subword_chunk<const AN: usize, const FN: usize>(
&self,
chunk_allocation_blocks: u32,
chunk_alignment_allocation_blocks_log2: u32,
mut allocated_fullword_chunks: Option<&extents::PhysicalExtents>,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
optimize_placement: bool,
) -> Option<layout::PhysicalAllocBlockIndex> {
debug_assert_ne!(chunk_allocation_blocks, 0);
debug_assert_eq!(
chunk_allocation_blocks & u32::trailing_bits_mask(chunk_alignment_allocation_blocks_log2),
0
);
debug_assert!(chunk_alignment_allocation_blocks_log2 <= BITMAP_WORD_BITS_LOG2);
let containing_block_allocation_blocks = chunk_allocation_blocks.round_up_next_pow2().unwrap();
let containing_block_allocation_blocks_log2 = containing_block_allocation_blocks.ilog2();
if containing_block_allocation_blocks == chunk_allocation_blocks {
return self.find_free_block(
containing_block_allocation_blocks_log2,
allocated_fullword_chunks,
pending_allocs,
pending_frees,
image_size,
None,
optimize_placement,
);
}
debug_assert!(chunk_allocation_blocks >= 3);
// The addition does not overflow, image_size is in units of Allocation Blocks,
// and has at least the upper 7 Bits clear.
let image_bitmap_words = (u64::from(image_size) + (u64::BITS as u64 - 1)) >> BITMAP_WORD_BITS_LOG2;
let word_blocks_lsbs_mask_table = BitmapWordBlocksLsbsMaskTable::new();
let word_containing_blocks_lsbs_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(containing_block_allocation_blocks_log2);
let word_chunk_alignment_anchors_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(chunk_alignment_allocation_blocks_log2);
let bitmaps_words_iter =
AllocBitmapWordIterator::new_at_bitmap_word_index(self, pending_allocs, pending_frees, 0);
let mut next_allocated_fullword_chunk: Option<layout::PhysicalAllocBlockRange> = None;
enum FoundCandidate {
FreeContainingBlock {
bitmap_word_index: u64,
bitmap_word: u64,
split_block_allocation_blocks_log2: u32, // Minimize.
},
ChunkInPartialContainingBlock {
bitmap_word_index: u64,
chunk_begin: u32,
excess_aligned_blocks_set: u32, // Maximize.
},
}
// To be minimized first, start out with the worst case value.
let mut best_excess_allocation_blocks = containing_block_allocation_blocks - chunk_allocation_blocks;
// The best_excess_allocation_blocks value found so far, scattered across all
// individual block fields in a bitmap word.
let mut containing_blocks_max_excess_len =
word_containing_blocks_lsbs_mask * best_excess_allocation_blocks as BitmapWord;
// Similarly, the requested chunk_allocation_blocks distributed uniformly across
// all block fields in a bitmap word.
let containing_blocks_min_maxstr_len = word_containing_blocks_lsbs_mask * chunk_allocation_blocks as BitmapWord;
let mut best: Option<FoundCandidate> = None;
// While nothing has been found, keep going. The increment cannot overflow,
// image_bitmap_words has the upper BITMAP_WORD_BITS_LOG2 clear.
let mut remaining_optimization_search_distance = image_bitmap_words + 1;
for (bitmap_word_index, mut bitmap_word) in
bitmaps_words_iter.take(usize::try_from(image_bitmap_words).unwrap_or(usize::MAX))
{
remaining_optimization_search_distance -= 1;
if remaining_optimization_search_distance == 0 {
// Placement optimization search distance exhausted. Return what we have.
debug_assert!(optimize_placement && best.is_some());
break;
}
if bitmap_word_index + 1 == image_bitmap_words {
// Set the excess high bits not backed by any actual storage.
bitmap_word |= !BitmapWord::trailing_bits_mask(
BitmapWord::BITS - ((u64::from(image_size).wrapping_neg() & (BitmapWord::BITS as u64 - 1)) as u32),
);
}
// Don't bother examining any further if all allocation blocks tracked by this
// word are allocated already anyway.
if bitmap_word == !0 {
continue;
}
if bitmap_word == 0 {
if best.is_none() {
// Check if the bitmap word is really free or has perhaps been previously
// allocated as part of a preceeding fullword chunks allocation
// round for processing the very same request.
next_allocated_fullword_chunk = next_allocated_fullword_chunk
.filter(|next_allocated_fullword_chunk| {
u64::from(next_allocated_fullword_chunk.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index
})
.or_else(|| {
allocated_fullword_chunks
.map(|e| e.iter())
.into_iter()
.flatten()
.filter(|e| u64::from(e.end()) >> BITMAP_WORD_BITS_LOG2 > bitmap_word_index)
.min_by_key(|e| e.end())
});
if let Some(next_allocated_fullword_chunk) = next_allocated_fullword_chunk {
if u64::from(next_allocated_fullword_chunk.begin()) >> BITMAP_WORD_BITS_LOG2
<= bitmap_word_index
{
continue;
}
} else {
// No more extents at or after the current position, avoid another search..
allocated_fullword_chunks = None;
}
if !optimize_placement {
// Found something and no placement optimization requested, bail out.
return Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index << BITMAP_WORD_BITS_LOG2,
));
}
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance = PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
best = Some(FoundCandidate::FreeContainingBlock {
bitmap_word_index,
bitmap_word,
// If containing_block_allocation_blocks_log2 == BITMAP_WORD_BITS_LOG2, the
// correct value would in fact be zero, but it won't be of any relevance in
// this particular case anyway.
split_block_allocation_blocks_log2: BITMAP_WORD_BITS_LOG2,
});
}
continue;
}
if !optimize_placement {
// Not interested in placement optimizations. Go the easy, not so costly route
// and just check whether there's any properly aligned 0-string
// of sufficient length.
match Self::bitmap_word_find_str_with_min_len(
!bitmap_word,
chunk_allocation_blocks,
chunk_alignment_allocation_blocks_log2,
word_chunk_alignment_anchors_mask,
) {
Some(begin_in_bitmap_word) => {
return Some(layout::PhysicalAllocBlockIndex::from(
(bitmap_word_index << BITMAP_WORD_BITS_LOG2) + begin_in_bitmap_word as u64,
));
}
None => continue,
}
}
let (containing_blocks_max_aligned_maxstr_len, containing_blocks_aligned_maxstr_lens) =
Self::bitmap_word_blocks_maxstr_lens(
!bitmap_word,
containing_block_allocation_blocks_log2,
word_containing_blocks_lsbs_mask,
chunk_alignment_allocation_blocks_log2,
word_chunk_alignment_anchors_mask,
);
if containing_blocks_max_aligned_maxstr_len < chunk_allocation_blocks {
continue;
}
// Determine the set of candidate containing blocks by comparing the found
// maxstr lengths against the lower bound as well as against the
// upper bound as given by the best (minimum) match found so far.
// Note that the upper bounding part is only a pre-filtering, as the
// maxstr excesses are getting computed in terms of the maxstr
// boundaries with alignment constraints imposed at this point, the actual
// maxstr lengths might extend beyond that. A more thorough check
// considering the actual, unconstrained excess lengths will be
// conducted below for the remaining candidates.
//
// This does wrap for those blocks that don't have a string of consecutive free
// blocks of sufficient length left. However, the single (unsigned)
// comparison below would effectively test (the individual block
// fields) for
// containing_blocks_min_maxstr_len
// <= containing_blocks_aligned_maxstr_lens
// <= containing_blocks_min_maxstr_len + containing_blocks_max_excess_len,
// c.f. Hacker's Delight, 2nd edition, 4-1 ("Checking Bounds of Integers"),
// which is what is needed.
let mut containing_blocks_candidates_lsbs = {
let containing_blocks_excess_lens = Self::bitmap_word_blocks_fields_sub(
containing_blocks_aligned_maxstr_lens,
containing_blocks_min_maxstr_len,
containing_block_allocation_blocks_log2,
word_containing_blocks_lsbs_mask,
);
Self::bitmap_word_blocks_fields_geq_lsbs(
containing_blocks_max_excess_len,
containing_blocks_excess_lens,
containing_block_allocation_blocks_log2,
word_containing_blocks_lsbs_mask,
)
};
if containing_blocks_candidates_lsbs == 0 {
continue;
}
if containing_blocks_max_aligned_maxstr_len == containing_block_allocation_blocks {
// There is at least one fully free containing block.
let free_containing_blocks_lsbs = Self::bitmap_word_free_blocks_lsbs(
bitmap_word,
containing_block_allocation_blocks_log2,
word_containing_blocks_lsbs_mask,
);
debug_assert_ne!(free_containing_blocks_lsbs, 0);
// The fully free blocks are a subset of all candidates.
debug_assert_eq!(
containing_blocks_candidates_lsbs & free_containing_blocks_lsbs,
free_containing_blocks_lsbs
);
if free_containing_blocks_lsbs == containing_blocks_candidates_lsbs {
// All candidates are fully free blocks.
// The case that the full range covered by the bitmap_word is free has been
// handled separately above already.
debug_assert!(containing_block_allocation_blocks_log2 < BITMAP_WORD_BITS_LOG2);
let best_split_block_allocations_block_log2 = match best {
Some(FoundCandidate::FreeContainingBlock {
split_block_allocation_blocks_log2,
..
}) => Some(split_block_allocation_blocks_log2),
Some(FoundCandidate::ChunkInPartialContainingBlock { .. }) => {
// At this point, the best (minimum) excess value found so far is known
// to not be smaller than that of a fully
// free containing block.
unreachable!();
}
None => None,
};
let split_block_allocation_blocks_log2 = Self::bitmap_word_block_alloc_split_block_size_log2(
free_containing_blocks_lsbs,
best_split_block_allocations_block_log2,
containing_block_allocation_blocks_log2,
word_containing_blocks_lsbs_mask,
&word_blocks_lsbs_mask_table,
);
if best_split_block_allocations_block_log2
.map(|best_split_block_allocations_block_log2| {
split_block_allocation_blocks_log2 < best_split_block_allocations_block_log2
})
.unwrap_or(true)
{
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance =
PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
}
best = Some(FoundCandidate::FreeContainingBlock {
bitmap_word_index,
bitmap_word,
split_block_allocation_blocks_log2,
});
// No need to update the best_excess_allocation_blocks, it is still at
// its worst case value.
debug_assert_eq!(
best_excess_allocation_blocks,
containing_block_allocation_blocks - chunk_allocation_blocks
);
}
continue;
}
// There is at least one partially allocated containing block and it will get
// rated better than the fully free ones. Mask off the latter as they won't win
// anyway.
containing_blocks_candidates_lsbs ^= free_containing_blocks_lsbs;
debug_assert_ne!(containing_blocks_candidates_lsbs, 0);
}
// At this point, all (remaining) containing candidate blocks are known to
// already have some other allocations in them. The
// containing_block_allocation_blocks has been chosen such that it's
// less than twice the chunk_allocation_blocks. It follows that
// there is at most one maxstr of consecutive unallocated allocation blocks
// whose length is >= chunk_allocation_blocks in each containing
// block. Thus, when searching for the ends of such a maxstr (known
// to exist), it suffices to find the tail of a 1-str of
// length at least containing_block_allocation_blocks / 2. Note that as we do
// know the length of each block's (aligned) maxstr already, the
// respective (aligned) maxstrs' start can be computed from the
// (aligned) end right away.
let mut containing_blocks_maxstr_end_bits = !bitmap_word;
let mut s = 1;
while s < containing_block_allocation_blocks / 2 {
// Retain those bits which are at the end (from least to most significant order)
// of a string of consecutive ones at least 2 * s in length.
containing_blocks_maxstr_end_bits =
containing_blocks_maxstr_end_bits & (containing_blocks_maxstr_end_bits << s);
s *= 2;
}
let word_containing_block_field_mask = BitmapWord::trailing_bits_mask(containing_block_allocation_blocks);
let mut containing_block_begin = 0;
while containing_blocks_candidates_lsbs != 0 {
let next_containing_block_candidate_offset = containing_blocks_candidates_lsbs.trailing_zeros();
containing_block_begin += next_containing_block_candidate_offset;
if next_containing_block_candidate_offset + containing_block_allocation_blocks != BitmapWord::BITS {
containing_blocks_candidates_lsbs >>=
next_containing_block_candidate_offset + containing_block_allocation_blocks;
} else {
containing_blocks_candidates_lsbs = 0;
}
let containing_block_candidate_aligned_maxstr_len = (containing_blocks_aligned_maxstr_lens
>> containing_block_begin)
& word_containing_block_field_mask;
debug_assert!(
containing_block_candidate_aligned_maxstr_len < containing_block_allocation_blocks as BitmapWord
);
let containing_block_candidate_aligned_maxstr_len =
containing_block_candidate_aligned_maxstr_len as u32;
debug_assert!(containing_block_candidate_aligned_maxstr_len >= chunk_allocation_blocks);
let containing_block_candidate_maxstr_end_bits =
(containing_blocks_maxstr_end_bits >> containing_block_begin) & word_containing_block_field_mask;
debug_assert_ne!(containing_block_candidate_maxstr_end_bits, 0);
let containing_block_candidate_maxstr_end = containing_block_candidate_maxstr_end_bits.ilog2() + 1;
debug_assert!(containing_block_candidate_maxstr_end >= containing_block_candidate_aligned_maxstr_len);
let containing_block_candidate_aligned_maxstr_end =
containing_block_candidate_maxstr_end.round_down_pow2(chunk_alignment_allocation_blocks_log2);
let containing_block_candidate_aligned_maxstr_begin =
containing_block_candidate_aligned_maxstr_end - containing_block_candidate_aligned_maxstr_len;
let containing_block_candidate_maxstr_begin = if chunk_alignment_allocation_blocks_log2 == 0 {
containing_block_candidate_aligned_maxstr_begin
} else {
let containing_block_candidate_bitmap_subword_head = (bitmap_word >> containing_block_begin)
& u64::trailing_bits_mask(containing_block_candidate_aligned_maxstr_begin);
if containing_block_candidate_bitmap_subword_head != 0 {
containing_block_candidate_bitmap_subword_head.ilog2() + 1
} else {
debug_assert_eq!(containing_block_candidate_aligned_maxstr_begin, 0);
containing_block_candidate_aligned_maxstr_begin
}
};
let containing_block_candidate_maxstr_len =
containing_block_candidate_maxstr_end - containing_block_candidate_maxstr_begin;
// Recompute the actual excess length, this time w/o any alignment constraints
// imposed on the maxstr boundaries.
let excess_allocation_blocks = containing_block_candidate_maxstr_len - chunk_allocation_blocks;
if excess_allocation_blocks > best_excess_allocation_blocks {
// The actual, unconstrained maxstr length exceeds the best fit found so far,
// either because considering the alignment padding made its length to increase
// beyond the best fit found in some previous bitmap word or because a previous
// block from this very same word has been a better fit already.
containing_block_begin += containing_block_allocation_blocks;
continue;
}
if excess_allocation_blocks == 0 {
// It's a perfect fit, no need to look any further.
return Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * BitmapWord::BITS as u64
+ containing_block_begin as u64
+ containing_block_candidate_aligned_maxstr_begin as u64,
));
}
// Determine the maximum possible alignment for the leftover excess space, the
// more it is aligned, with the meaning to be specified in what
// follows, the better. Logically, the excess space can be
// viewed as a collection of differently sized blocks, one for
// each bit set in excess_allocation_blocks, with a size
// corresponding to that bit position. Example: for excess_allocation_blocks =
// 0x15, the excess space would consist of three blocks: one of
// size 1, another one of size 2^2 = 4 and a third one of size
// 2^4 = 16. Now, depending on where the string of free blocks
// starts and on the alignment of the chunk_allocation_blocks, it
// might or might not be possible to place the allocation within the string of
// free blocks to keep the remaining excess blocks aligned. In
// general, the larger the maximum excess space block which is
// aligned, the better the configuration.
//
// Neglecting the user specified alignment constraints for the moment, there are
// two possibilities to place the new allocation relative to the
// excess space: either in front or after it. Note that in
// principle, there is more degree of freedom, as the allocation
// could be placed somewhere "in the middle" of the excess space, but
// none of these additional options would improve the best possible overall
// excess blocks alignment. With the user specified alignment
// constraints taken into account, the situation is basically
// the same, except that there are fixed padding areas at the
// head and tail before and after the aligned maxstr boundaries
// respectively, with the "movable" leftover excess space also happening to be
// aligned to the user specified alignment.
//
// For each of the possible excess starting points, either at
// containing_block_candidate_aligned_maxstr_begin or at
// containing_block_candidate_aligned_maxstr_begin + chunk_allocation_blocks,
// determine the point of maximum alignment within the excess range (which
// happens to be aligned to the maximum excess block, at least)
// and split the excess space into two parts at this point. All
// the blocks now found in the two individual parts can be
// considered aligned: in the first part the sequence of blocks would
// be ordered from from smallest to largest, and in the second part after the
// point of maximum alignment from largest down to smallest.
let excess_fixed_alignment_padding_head =
containing_block_candidate_aligned_maxstr_begin - containing_block_candidate_maxstr_begin;
let excess_fixed_alignment_padding_tail =
containing_block_candidate_maxstr_end - containing_block_candidate_aligned_maxstr_end;
let excess_fixed_alignment_padding_aligned_blocks_set =
excess_fixed_alignment_padding_head | excess_fixed_alignment_padding_tail;
// As mentioned above, the "movable" part of the excess space is aligned to to
// the user specified alignment.
let movable_excess_allocation_blocks =
containing_block_candidate_aligned_maxstr_len - chunk_allocation_blocks;
debug_assert_eq!(
excess_fixed_alignment_padding_head
+ movable_excess_allocation_blocks
+ excess_fixed_alignment_padding_tail,
excess_allocation_blocks
);
let max_movable_excess_block = u32::next_power_of_two(movable_excess_allocation_blocks + 1) >> 1;
// First option: the allocation is placed after the (movable part of the) excess
// space.
// This computes the amount of excess space after the point point of maximum
// alignment within the excess space, c.f. Hacker's Delight, 2nd edition, 3-3
// ("Detecting a Power-of-2 Boundary Crossing").
let movable_excess_aligned_blocks_set_after_1 = (containing_block_candidate_aligned_maxstr_begin
| max_movable_excess_block.wrapping_neg())
.wrapping_add(movable_excess_allocation_blocks);
let movable_excess_aligned_blocks_set_after_0 =
movable_excess_allocation_blocks - movable_excess_aligned_blocks_set_after_1;
let movable_excess_aligned_blocks_set_after =
movable_excess_aligned_blocks_set_after_0 | movable_excess_aligned_blocks_set_after_1;
// Second option: the allocation is placed before the (movable part of the)
// excess space.
let movable_excess_aligned_blocks_set_before_1 = ((containing_block_candidate_aligned_maxstr_begin
+ chunk_allocation_blocks)
| max_movable_excess_block.wrapping_neg())
.wrapping_add(movable_excess_allocation_blocks);
let movable_excess_aligned_blocks_set_before_0 =
movable_excess_allocation_blocks - movable_excess_aligned_blocks_set_before_1;
let movable_excess_aligned_blocks_set_before =
movable_excess_aligned_blocks_set_before_0 | movable_excess_aligned_blocks_set_before_1;
let (chunk_begin, excess_aligned_blocks_set) = if movable_excess_aligned_blocks_set_after
> movable_excess_aligned_blocks_set_before
{
(
containing_block_candidate_aligned_maxstr_begin + movable_excess_allocation_blocks,
movable_excess_aligned_blocks_set_after | excess_fixed_alignment_padding_aligned_blocks_set,
)
} else {
(
containing_block_candidate_aligned_maxstr_begin,
movable_excess_aligned_blocks_set_before | excess_fixed_alignment_padding_aligned_blocks_set,
)
};
let chunk_begin = chunk_begin + containing_block_begin;
debug_assert!(excess_allocation_blocks < containing_block_allocation_blocks);
debug_assert!(best_excess_allocation_blocks == containing_block_allocation_blocks || best.is_some());
if excess_allocation_blocks < best_excess_allocation_blocks {
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance = PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
}
best = Some(FoundCandidate::ChunkInPartialContainingBlock {
bitmap_word_index,
chunk_begin,
excess_aligned_blocks_set,
});
best_excess_allocation_blocks = excess_allocation_blocks;
// Update the containing_blocks_max_excess_len block fields accordingly:
// uniformly set all fields to the new effective upper bound for the subsequent
// search.
containing_blocks_max_excess_len =
word_containing_blocks_lsbs_mask * best_excess_allocation_blocks as BitmapWord;
} else {
debug_assert_eq!(best_excess_allocation_blocks, excess_allocation_blocks);
debug_assert!(best_excess_allocation_blocks < containing_block_allocation_blocks);
debug_assert!(best.is_some());
let best_excess_aligned_blocks_set = match best.as_ref().unwrap() {
FoundCandidate::ChunkInPartialContainingBlock {
excess_aligned_blocks_set,
..
} => *excess_aligned_blocks_set,
FoundCandidate::FreeContainingBlock { .. } => unreachable!(),
};
if excess_aligned_blocks_set > best_excess_aligned_blocks_set {
best = Some(FoundCandidate::ChunkInPartialContainingBlock {
bitmap_word_index,
chunk_begin,
excess_aligned_blocks_set,
});
}
}
containing_block_begin += containing_block_allocation_blocks;
}
}
match best {
Some(FoundCandidate::ChunkInPartialContainingBlock {
bitmap_word_index,
chunk_begin,
excess_aligned_blocks_set: _,
}) => Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * BitmapWord::BITS as u64 + chunk_begin as u64,
)),
Some(FoundCandidate::FreeContainingBlock {
bitmap_word_index,
bitmap_word,
split_block_allocation_blocks_log2,
}) => {
let word_split_blocks_lsbs_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(split_block_allocation_blocks_log2);
let mut free_split_blocks_lsbs = Self::bitmap_word_free_blocks_lsbs(
bitmap_word,
split_block_allocation_blocks_log2,
word_split_blocks_lsbs_mask,
);
if split_block_allocation_blocks_log2 < BITMAP_WORD_BITS_LOG2 - 1 {
let double_split_block_allocations_block_log2 = split_block_allocation_blocks_log2 + 1;
let word_double_split_blocks_lsbs_mask =
word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(double_split_block_allocations_block_log2);
free_split_blocks_lsbs = Self::bitmap_word_filter_blocks_with_free_buddy_lsbs(
free_split_blocks_lsbs,
free_split_blocks_lsbs,
split_block_allocation_blocks_log2,
word_double_split_blocks_lsbs_mask,
);
}
debug_assert_ne!(free_split_blocks_lsbs, 0);
Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * BitmapWord::BITS as u64
+ Self::bitmap_word_block_alloc_select_block(
free_split_blocks_lsbs,
split_block_allocation_blocks_log2,
word_split_blocks_lsbs_mask,
&word_blocks_lsbs_mask_table,
) as u64,
))
}
None => None,
}
}
/// Find a free extent of length in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) between
/// [`BitmapWord::BITS`] and two times [`BitmapWord::BITS`].
///
/// Find a free extent of size as specified by `chunk_allocation_blocks`.
///
/// The resulting extent, if any, will be aligned to the maximum alignment
/// `chunk_allocation_blocks` has.
///
/// Note that the search procedure attempts to reduce fragmentation by
/// avoiding to split up free blocks, i.e. free extents of length a power of
/// two, if possible, and prefers to split up smaller blocks over larger
/// ones.
///
/// # Arguments:
///
/// * `chunk_allocation_blocks` - The desired extent length in units of
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2). Must
/// be between [`BitmapWord::BITS`] and two times [`BitmapWord::BITS`]
/// (both exlusive).
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
fn find_free_sub_doubleword_chunk<const AN: usize, const FN: usize>(
&self,
chunk_allocation_blocks: u32,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
optimize_placement: bool,
) -> Option<layout::PhysicalAllocBlockIndex> {
debug_assert!(chunk_allocation_blocks > BitmapWord::BITS);
debug_assert!(chunk_allocation_blocks < 2 * BitmapWord::BITS);
let subword_rem_allocation_blocks = chunk_allocation_blocks - BitmapWord::BITS;
let subword_rem_free_head_word_mask = BitmapWord::trailing_bits_mask(subword_rem_allocation_blocks);
let subword_rem_free_tail_word_mask =
subword_rem_free_head_word_mask << (BitmapWord::BITS - subword_rem_allocation_blocks);
// The addition does not overflow, image_size is in units of Allocation Blocks,
// and has at least the upper 7 Bits clear.
let image_bitmap_words = (u64::from(image_size) + (u64::BITS as u64 - 1)) >> BITMAP_WORD_BITS_LOG2;
let mut previous_bitmap_word: Option<BitmapWord> = None;
let bitmaps_words_iter =
AllocBitmapWordIterator::new_at_bitmap_word_index(self, pending_allocs, pending_frees, 0);
struct FoundCandidate {
bitmap_word_index: u64,
first_bitmap_word: u64,
excess_allocation_blocks: u32, // Minimize.
}
let mut best: Option<FoundCandidate> = None;
// While nothing has been found, keep going. The increment cannot overflow,
// image_bitmap_words has the upper BITMAP_WORD_BITS_LOG2 clear.
let mut remaining_optimization_search_distance = image_bitmap_words + 1;
for (bitmap_word_index, mut bitmap_word) in
bitmaps_words_iter.take(usize::try_from(image_bitmap_words).unwrap_or(usize::MAX))
{
remaining_optimization_search_distance -= 1;
if remaining_optimization_search_distance == 0 {
// Placement optimization search distance exhausted. Return what we have.
debug_assert!(optimize_placement && best.is_some());
break;
}
if bitmap_word_index + 1 == image_bitmap_words {
// Set the excess high bits not backed by any actual storage.
bitmap_word |= !BitmapWord::trailing_bits_mask(
BitmapWord::BITS - ((u64::from(image_size).wrapping_neg() & (BitmapWord::BITS as u64 - 1)) as u32),
);
}
// Don't bother examining any further if all allocation blocks tracked by this
// word are allocated already anyway.
if bitmap_word == !0 {
previous_bitmap_word = None;
continue;
}
if bitmap_word == 0 {
match previous_bitmap_word {
Some(0) => {
if !optimize_placement {
// Found something and no placement optimization requested, bail out.
return Some(layout::PhysicalAllocBlockIndex::from(
(bitmap_word_index - 1) << BITMAP_WORD_BITS_LOG2,
));
}
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance =
PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
best = Some(FoundCandidate {
bitmap_word_index: bitmap_word_index - 1,
first_bitmap_word: 0,
excess_allocation_blocks: BitmapWord::BITS - subword_rem_allocation_blocks,
});
}
}
Some(previous_bitmap_word) => {
debug_assert_eq!(previous_bitmap_word & subword_rem_free_tail_word_mask, 0);
if !optimize_placement {
// Found something and no placement optimization requested, bail out.
return Some(layout::PhysicalAllocBlockIndex::from(
((bitmap_word_index - 1) << BITMAP_WORD_BITS_LOG2)
+ (previous_bitmap_word & subword_rem_free_tail_word_mask).trailing_zeros() as u64,
));
}
let excess_allocation_blocks =
previous_bitmap_word.leading_zeros() - subword_rem_allocation_blocks;
if best
.as_ref()
.map(|best| best.excess_allocation_blocks > excess_allocation_blocks)
.unwrap_or(true)
{
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance =
PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
}
best = Some(FoundCandidate {
bitmap_word_index: bitmap_word_index - 1,
first_bitmap_word: previous_bitmap_word,
excess_allocation_blocks,
});
if excess_allocation_blocks == 0 {
// It's a perfect fit, no need to look any further.
break;
}
}
}
None => (),
}
previous_bitmap_word = Some(0);
continue;
} else if bitmap_word & subword_rem_free_head_word_mask == 0 {
if let Some(0) = previous_bitmap_word {
if !optimize_placement {
// Found something and no placement optimization requested, bail out.
return Some(layout::PhysicalAllocBlockIndex::from(
(bitmap_word_index - 1) << BITMAP_WORD_BITS_LOG2,
));
}
let excess_allocation_blocks = bitmap_word.trailing_zeros() - subword_rem_allocation_blocks;
if best
.as_ref()
.map(|best| best.excess_allocation_blocks > excess_allocation_blocks)
.unwrap_or(true)
{
if best.is_none() {
// Something's been found, arm the placement optimization search distance limit.
remaining_optimization_search_distance =
PLACEMENT_OPTIMIZATION_SEARCH_DISTANCE_BITMAP_WORDS;
}
best = Some(FoundCandidate {
bitmap_word_index: bitmap_word_index - 1,
first_bitmap_word: 0,
excess_allocation_blocks,
});
if excess_allocation_blocks == 0 {
// It's a perfect fit, no need to look any further.
break;
}
}
}
}
if bitmap_word & subword_rem_free_tail_word_mask == 0 {
previous_bitmap_word = Some(bitmap_word);
} else {
previous_bitmap_word = None;
}
}
match best {
Some(FoundCandidate {
bitmap_word_index,
first_bitmap_word,
..
}) => {
let chunk_begin_in_fullword_block = if first_bitmap_word == 0 {
0
} else {
BitmapWord::BITS - subword_rem_allocation_blocks
};
Some(layout::PhysicalAllocBlockIndex::from(
bitmap_word_index * BitmapWord::BITS as u64 + chunk_begin_in_fullword_block as u64,
))
}
None => None,
}
}
/// Find free extents of lengths a multiple of [`BitmapWord::BITS`]
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) each
/// for serving an [`ExtentsAllocationRequest`].
///
/// Attempt to find a sequence of free extents with lengths a multiple of
/// [`BitmapWord::BITS`] [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) each for
/// serving an [`ExtentsAllocationRequest`] and collectively providing
/// enough payload storage capacity so that the unsatisfied requested
/// payload length remainder, if any, can be provided by one additional
/// extent less than [`BitmapWord::BITS`] in length.
///
/// In general, the search procedure always attempts to minimize the total
/// number of extents allocated.
///
/// In order to bound peak memory usage for tracking the set of extents
/// allocated at any given point in time, if possible, the search
/// proceeds in two phases: in a first search, an [extent candidate
/// filter](FindFreeFullwordChunksExtentCandiateFilter) effectively limiting
/// the number of extents in the list at any point is applied to each
/// found free extent candidate. If that fails, another unconstrained
/// search is conducted.
///
/// # Arguments:
///
/// * `allocation_request` - The [`ExtentsAllocationRequest`] to find free
/// extents for.
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
/// * `head_extent_min_allocation_blocks` - The head extent's minimum
/// length in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), as
/// returned by [`ExtentsLayout::min_extents_allocation_blocks()`].
/// * `tail_extent_min_allocation_blocks` - Minimum length of any tail
/// "continuation" extent in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), as
/// returned by [`ExtentsLayout::min_extents_allocation_blocks()`].
fn find_free_fullword_chunks<'a, const AN: usize, const FN: usize>(
&self,
allocation_request: &'a ExtentsAllocationRequest,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
head_extent_min_allocation_blocks: layout::AllocBlockCount,
tail_extent_min_allocation_blocks: layout::AllocBlockCount,
) -> Result<Option<(ExtentsAllocationRequestProgress<'a>, extents::PhysicalExtents)>, NvFsError> {
let extent_candidate_filter =
FindFreeFullwordChunksExtentCandidateFilterConstrainExtentsCount::new(allocation_request);
Ok(
match self._find_free_fullword_chunks(
allocation_request,
extent_candidate_filter,
pending_allocs,
pending_frees,
image_size,
head_extent_min_allocation_blocks,
tail_extent_min_allocation_blocks,
)? {
Some(result) => Some(result),
None => {
let extent_candidate_filter = FindFreeFullwordChunksExtentCandidateFilterUnconstrained {};
self._find_free_fullword_chunks(
allocation_request,
extent_candidate_filter,
pending_allocs,
pending_frees,
image_size,
head_extent_min_allocation_blocks,
tail_extent_min_allocation_blocks,
)?
}
},
)
}
/// Find free extents of lengths a multiple of [`BitmapWord::BITS`]
/// [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) and
/// accepted by a specified [candidate
/// filter](FindFreeFullwordChunksExtentCandiateFilter) each for serving an
/// [`ExtentsAllocationRequest`].
///
/// Attempt to find a sequence of free extents with lengths a multiple of
/// [`BitmapWord::BITS`] [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2) each for
/// serving an [`ExtentsAllocationRequest`] and collectively providing
/// enough payload storage capacity so that the unsatisfied requested
/// payload length remainder, if any, can be provided by one additional
/// extent less than [`BitmapWord::BITS`] in length.
///
/// Only free extents passing the `extent_candidate_filter` will be
/// considered.
///
/// In general, the search procedure always attempts to minimize the total
/// number of extents allocated, even for an
/// [unconstrained](FindFreeFullwordChunksExtentCandidateFilterUnconstrained)
/// `extent_candidate_filter`.
///
/// # Arguments:
/// * `allocation_request` - The [`ExtentsAllocationRequest`] to find free
/// extents for.
/// * `extent_candidate_filter` - The [extent candidate
/// filter](FindFreeFullwordChunksExtentCandiateFilter) to query about
/// any free candidate extent's eligibility for inclusion in the
/// resulting allocation.
/// * `pending_allocs` - Pending allocations to apply virtually on top of
/// the state as currently found in the [`AllocBitmap`].
/// * `pending_frees` - Pending frees to apply virtually on top of the
/// state as currently found in the [`AllocBitmap`].
/// * `image_size` - The filesystem image size. No [Allocation
/// Block](layout::ImageLayout::allocation_block_size_128b_log2) beyond
/// it will be considered for the allocation.
/// * `head_extent_min_allocation_blocks` - The head extent's minimum
/// length in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), as
/// returned by [`ExtentsLayout::min_extents_allocation_blocks()`].
/// * `tail_extent_min_allocation_blocks` - Minimum length of any tail
/// "continuation" extent in units of [Allocation
/// Blocks](layout::ImageLayout::allocation_block_size_128b_log2), as
/// returned by [`ExtentsLayout::min_extents_allocation_blocks()`].
#[allow(clippy::too_many_arguments)]
fn _find_free_fullword_chunks<
'a,
const AN: usize,
const FN: usize,
ECF: FindFreeFullwordChunksExtentCandiateFilter,
>(
&self,
allocation_request: &'a ExtentsAllocationRequest,
mut extent_candidate_filter: ECF,
pending_allocs: &SparseAllocBitmapUnion<'_, AN>,
pending_frees: &SparseAllocBitmapUnion<'_, FN>,
image_size: layout::AllocBlockCount,
head_extent_min_allocation_blocks: layout::AllocBlockCount,
tail_extent_min_allocation_blocks: layout::AllocBlockCount,
) -> Result<Option<(ExtentsAllocationRequestProgress<'a>, extents::PhysicalExtents)>, NvFsError> {
debug_assert!(
allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32 <= BITMAP_WORD_BITS_LOG2
);
let mut progress = ExtentsAllocationRequestProgress::new(allocation_request);
let mut extents = extents::PhysicalExtents::new();
if u64::from(allocation_request.remaining_max_extent_allocation_blocks(0).0) < BitmapWord::BITS as u64 {
return Ok(Some((progress, extents)));
}
debug_assert!(u64::from(allocation_request.layout.max_extent_allocation_blocks) >= BitmapWord::BITS as u64);
// Stop condition for allocating further fullword blocks: once the remainder
// (including additional extent + payload headers, as well as padding),
// would fit into the next possible extent size smaller than a fullword
// block, stop. Note that for remaining lengths larger than that, a
// fullword allocation could have some excess space (due to saved
// headers + padding) of up to, but less than, twice the size of a minimum
// extent satisfying the alignment constraints
// (extent_alignment_allocation_blocks_log2). The minimum extent
// length is bounded by 2^63 from above, c.f. ExtentsAllocationRequest::new().
// Hence the excess space will fit an u64, even in such unrealistic scenarios.
debug_assert_eq!(
tail_extent_min_allocation_blocks
.align_down(allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32),
tail_extent_min_allocation_blocks
);
let (max_subword_extent_effective_payload_len, max_allocated_effective_payload_excess_len) =
if u64::from(tail_extent_min_allocation_blocks) < BitmapWord::BITS as u64 {
// Does not overflow, as per max_extent_allocation_blocks being >= the
// BitmapWord::BITS when here.
let max_subword_extent_effective_payload_len = allocation_request.layout.extent_effective_payload_len(
layout::AllocBlockCount::from(
(u64::BITS as u64)
- (1u64 << allocation_request.layout.extent_alignment_allocation_blocks_log2),
),
false,
);
let fullword_block_len =
(BitmapWord::BITS as u64) << (allocation_request.layout.allocation_block_size_128b_log2 + 7);
debug_assert_eq!(
fullword_block_len >> (allocation_request.layout.allocation_block_size_128b_log2 + 7),
BitmapWord::BITS as u64
);
let max_allocated_effective_payload_excess_len =
fullword_block_len - max_subword_extent_effective_payload_len - 1;
(
max_subword_extent_effective_payload_len,
max_allocated_effective_payload_excess_len,
)
} else {
// The minimum extent length is > than what's covered by a single
// BitmapWord, and sub-BitmapWord extents are not possible. The maximum allowed
// excess is one less than what's provided by an extent of
// minimum possible length.
let max_allocated_effective_payload_excess_len = allocation_request
.layout
.extent_effective_payload_len(tail_extent_min_allocation_blocks, false)
- 1;
(0, max_allocated_effective_payload_excess_len)
};
// Cached shortest extent found (and used) so far: pair of index and length in
// units of allocation blocks.
let mut shortest_extent: Option<(usize, layout::AllocBlockCount)> = None;
let image_bitmap_words = u64::from(image_size) >> BITMAP_WORD_BITS_LOG2;
let mut bitmaps_words_iter =
AllocBitmapWordIterator::new_at_bitmap_word_index(self, pending_allocs, pending_frees, 0)
.take(usize::try_from(image_bitmap_words).unwrap_or(usize::MAX));
let max_extent_fullword_blocks =
usize::try_from(u64::from(allocation_request.layout.max_extent_allocation_blocks) >> BITMAP_WORD_BITS_LOG2)
.unwrap_or(usize::MAX >> BITMAP_WORD_BITS_LOG2);
while let Some((cur_free_run_begin_bitmap_word_index, _)) =
bitmaps_words_iter.find(|(_, bitmap_word)| *bitmap_word == 0)
{
let cur_free_run_end_bitmap_word_index = (&mut bitmaps_words_iter)
.map_while(|(bitmap_word_index, bitmap_word)| {
if bitmap_word == 0 {
Some(bitmap_word_index)
} else {
None
}
})
.take(max_extent_fullword_blocks - 1)
.last()
.unwrap_or(cur_free_run_begin_bitmap_word_index)
+ 1;
// If the current free run is acceptable in terms of the current lower bound on
// its length, consume all that is needed to satisfy the remaining
// allocation request size.
let cur_extent_max_fullword_blocks =
cur_free_run_end_bitmap_word_index - cur_free_run_begin_bitmap_word_index;
// No need to align, the alignment in units of Allocation Blocks is <=
// BitmapWord::BITS.
let cur_extent_max_allocation_blocks =
layout::AllocBlockCount::from(cur_extent_max_fullword_blocks << BITMAP_WORD_BITS_LOG2);
debug_assert_eq!(
cur_extent_max_allocation_blocks
.align_down(allocation_request.layout.extent_alignment_allocation_blocks_log2 as u32),
cur_extent_max_allocation_blocks
);
if cur_extent_max_allocation_blocks
< if extents.is_empty() {
head_extent_min_allocation_blocks
} else {
tail_extent_min_allocation_blocks
}
{
continue;
}
let mut cur_extent_max_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(cur_extent_max_allocation_blocks, extents.is_empty());
let mut cur_extent_used_effective_payload_len = 0;
{
let remaining_effective_payload_len = progress.remaining_effective_payload_len();
if (extents.is_empty() || remaining_effective_payload_len > max_subword_extent_effective_payload_len)
&& extent_candidate_filter.extent_candidate_acceptable(cur_extent_max_allocation_blocks)
{
cur_extent_used_effective_payload_len =
cur_extent_max_effective_payload_len.min(remaining_effective_payload_len);
progress.allocated_effective_payload_len += cur_extent_used_effective_payload_len;
}
}
// Use the remaining space in the current run, if any, to perhaps
// replace one or more shorter extents already found before.
let mut extents_hdr_transferred = false;
while !extents.is_empty() && cur_extent_max_effective_payload_len > cur_extent_used_effective_payload_len {
// Loop invariants:
debug_assert!(
progress.allocated_excess_effective_payload_len == 0
|| progress.remaining_effective_payload_len() == 0
);
debug_assert!(
progress.allocated_excess_effective_payload_len <= max_allocated_effective_payload_excess_len
);
shortest_extent = shortest_extent.or_else(|| {
ExtentsAllocationRequestProgress::find_shortest_extent(&extents).map(|shortest_extent_index| {
(
shortest_extent_index,
extents.get_extent_range(shortest_extent_index).block_count(),
)
})
});
let (shortest_extent_index, original_shortest_extent_allocation_blocks) = match shortest_extent {
Some(shortest_extent) => shortest_extent,
None => break,
};
// If the current run's total length is <= the shortest previously found one,
// it would be counter-productive to shovel blocks from the latter over to the
// former.
// Also, it doesn't make any sense to replace a single small extent by a larger
// one, truncating the latter in the course -- that would only
// increase fragmentation.
if original_shortest_extent_allocation_blocks >= cur_extent_max_allocation_blocks
|| (cur_extent_used_effective_payload_len == 0
&& extents
.iter()
.map(|e| e.block_count())
.filter(|c| *c < cur_extent_max_allocation_blocks)
.count()
< 2)
{
break;
}
let shortest_extent_stores_extents_hdr = !extents_hdr_transferred
&& allocation_request.layout.extents_hdr_len != 0
&& shortest_extent_index == 0;
if shortest_extent_stores_extents_hdr
&& cur_extent_max_allocation_blocks >= head_extent_min_allocation_blocks
{
// Recompute the current extent's maximum payload length if the extents header
// was transferred to it.
cur_extent_max_effective_payload_len = allocation_request
.layout
.extent_effective_payload_len(cur_extent_max_allocation_blocks, true);
if cur_extent_used_effective_payload_len > cur_extent_max_effective_payload_len {
break;
}
extents_hdr_transferred = true;
}
let mut shortest_extent_used_effective_payload_len =
allocation_request.layout.extent_effective_payload_len(
original_shortest_extent_allocation_blocks,
shortest_extent_stores_extents_hdr,
);
if shortest_extent_used_effective_payload_len > progress.allocated_excess_effective_payload_len {
shortest_extent_used_effective_payload_len -= progress.allocated_excess_effective_payload_len;
progress.allocated_excess_effective_payload_len = 0;
let x = shortest_extent_used_effective_payload_len
.min(cur_extent_max_effective_payload_len - cur_extent_used_effective_payload_len);
cur_extent_used_effective_payload_len += x;
shortest_extent_used_effective_payload_len -= x;
} else {
progress.allocated_excess_effective_payload_len -= shortest_extent_used_effective_payload_len;
shortest_extent_used_effective_payload_len = 0;
}
let updated_shortest_extent_allocation_blocks = progress.fit_allocated_extent_to_effective_payload_len(
shortest_extent_used_effective_payload_len,
false,
max_subword_extent_effective_payload_len,
BITMAP_WORD_BITS_LOG2,
);
if updated_shortest_extent_allocation_blocks < original_shortest_extent_allocation_blocks {
if u64::from(updated_shortest_extent_allocation_blocks) == 0 {
extents.swap_extents(shortest_extent_index, extents.len() - 1);
extents.pop_extent();
shortest_extent = None;
extent_candidate_filter.account_extents_dropped(1);
} else {
let removed = extents.shrink_extent_by(
shortest_extent_index,
original_shortest_extent_allocation_blocks - updated_shortest_extent_allocation_blocks,
);
debug_assert!(!removed);
shortest_extent = Some((shortest_extent_index, updated_shortest_extent_allocation_blocks));
}
}
// The loop invariants are still being upheld.
debug_assert!(
progress.allocated_excess_effective_payload_len == 0
|| progress.remaining_effective_payload_len() == 0
);
debug_assert!(
progress.allocated_excess_effective_payload_len <= max_allocated_effective_payload_excess_len
);
}
let cur_extent_allocated_allocation_blocks = if cur_extent_used_effective_payload_len
== cur_extent_max_effective_payload_len
{
// If all of the current extent is to be used, then there won't be any padding
// and thus, no excess allocation from this current extent (a shorter one merged
// partially into the current one can have padding/excess allocation though).
cur_extent_max_allocation_blocks
} else {
// When here, not all available space of the current extent has been allocated.
// If some has been allocated, then either all smaller extents got absorbed,
// or the next one to merge would contain the extents header and the current
// extent does not have enough space left to transfer these headers over to it.
//
// Regarding allocated excess space, note that excess space can emerge whenever
// inserting or adjusting an extent's length (due to rounding the extent's
// length upwards to make it align to
// extent_alignment_allocation_blocks_log2). Allocations from
// the current extent (cur_extent_used_effective_payload_len) will
// have been made only above after the allocated_excess_effective_payload_len
// dropped to zero (that is, when absorbing shorter extents, only the actual
// part of payload needed will be accounted for). So the new
// extent insertion below would be the only one potentially
// contributing to excess, and thus, the amount of excess
// will stay within the allowed bounds. There is one subtle corner case though:
// if the extents headers got transferred, and
// cur_extent_used_effective_payload_len is zero, because the
// allocated excess at that time had been larger than the sum of
// all shorter extents' payload lengths, the current extent still needs to get
// inserted to have the headers placed somewhere. When adding such extent
// below, note that it will be of minimum possible length, and
// the overall excess will not be worse than what it had been
// before removing the extent previously storing the
// extent headers.
debug_assert!(
cur_extent_used_effective_payload_len == 0 || progress.allocated_excess_effective_payload_len == 0
);
progress.fit_allocated_extent_to_effective_payload_len(
cur_extent_used_effective_payload_len,
extents.is_empty() || extents_hdr_transferred,
max_subword_extent_effective_payload_len,
BITMAP_WORD_BITS_LOG2,
)
};
// The loop invariants are still being upheld.
debug_assert!(
progress.allocated_excess_effective_payload_len == 0 || progress.remaining_effective_payload_len() == 0
);
debug_assert!(
progress.allocated_excess_effective_payload_len <= max_allocated_effective_payload_excess_len
);
if u64::from(cur_extent_allocated_allocation_blocks) != 0 {
let cur_extent_begin = layout::PhysicalAllocBlockIndex::from(
cur_free_run_begin_bitmap_word_index << BITMAP_WORD_BITS_LOG2,
);
extents.push_extent(
&layout::PhysicalAllocBlockRange::from((cur_extent_begin, cur_extent_allocated_allocation_blocks)),
true,
)?;
let mut cur_extent_index = extents.len() - 1;
// If the common extents headers have been transferred from a shorter to
// the current extent, then swap the latter into the first position.
if extents_hdr_transferred {
extents.swap_extents(0, cur_extent_index);
if let Some((shortest_extent_index, shortest_extent_allocation_blocks)) = shortest_extent {
if shortest_extent_index == 0 {
shortest_extent = Some((cur_extent_index, shortest_extent_allocation_blocks));
}
}
cur_extent_index = 0;
}
// Try to update the cached shortest_extent if possible.
if let Some((shortest_extent_index, shortest_extent_allocation_blocks)) = shortest_extent {
// Prefer (in this order):
// a.) shorter extents,
// b.) extents at increasing positions.
// compare to ExtentsAllocationRequestProgress::find_shortest_extent().
// Note that b.) is always true for the current extent, as the extent
// allocation search is in order of increasing positions..
if shortest_extent_allocation_blocks >= cur_extent_allocated_allocation_blocks {
debug_assert!(extents.get_extent_range(shortest_extent_index).begin() < cur_extent_begin);
shortest_extent = Some((cur_extent_index, cur_extent_allocated_allocation_blocks));
}
}
let (n_extents_removed, shortest_extent_index) = progress.optimize_extents_hdr_placement(
&mut extents,
shortest_extent.map(|(shortest_extent_index, _)| shortest_extent_index),
head_extent_min_allocation_blocks,
max_subword_extent_effective_payload_len,
BITMAP_WORD_BITS_LOG2,
);
shortest_extent = shortest_extent_index.map(|shortest_extent_index| {
(
shortest_extent_index,
extents.get_extent_range(shortest_extent_index).block_count(),
)
});
extent_candidate_filter.account_extents_dropped(n_extents_removed);
// And now account for the allocation of the current extent above.
extent_candidate_filter.account_extent_added();
// Update the candidate filter, but do it only if needed as it can be costly. It
// will be needed if we're still trying to satisfy the allocation request, i.e.
// not yet merely trying to absorb smaller into larger extents.
if progress.remaining_effective_payload_len() > max_subword_extent_effective_payload_len {
extent_candidate_filter.update_filter_state(&progress, max_subword_extent_effective_payload_len);
} else {
// If all the requested effective payload length has been allocated and
// all extents (but one) have maximum length, then no further improvement
// is possible. Break out then.
if extents
.iter()
.map(|e| u64::from(e.block_count()) >> BITMAP_WORD_BITS_LOG2)
.filter(|c| {
*c < u64::from(allocation_request.layout.max_extent_allocation_blocks)
>> BITMAP_WORD_BITS_LOG2
})
.count()
== 1
{
break;
}
}
} else {
debug_assert!(!extents_hdr_transferred);
}
if extents.len() == 1
&& progress.remaining_effective_payload_len() <= max_subword_extent_effective_payload_len
{
// No further progress possible.
break;
}
}
let result = if progress.remaining_effective_payload_len() <= max_subword_extent_effective_payload_len {
// The request could be satisfied within the budget.
// Sort the extents by (in this order)
// a.) extent lengths (so that a potential future truncation
// would free up larger ones),
// b.) their position.
let sort_start_index = if progress.extents_hdr_placement_cost_is_invariant {
0
} else {
1
};
let sort_end_index = extents.len();
extents.sort_extents_by(
sort_start_index..sort_end_index,
|e0, e1| match e0.block_count().cmp(&e1.block_count()) {
cmp::Ordering::Less => cmp::Ordering::Less,
cmp::Ordering::Equal => e0.begin().cmp(&e1.begin()),
cmp::Ordering::Greater => cmp::Ordering::Greater,
},
true,
);
Some((progress, extents))
} else {
None
};
Ok(result)
}
/// Determine the minimum free block size greater than or equal to a given
/// one within a [`BitmapWord`].
///
/// When attempting to allocate some block of size and alignment as
/// specified by `block_allocation_blocks_log2` in a given
/// [`BitmapWord`], it is desirable to not unnecessarily split up some
/// larger aligned free containing block, or, if not avoidable, to prefer
/// splitting up a smaller containing free block over the larger ones.
///
/// Determine the minimum size among all aligned free blocks within a
/// [`BitmapWord`] greater than or equal to
/// `block_allocation_blocks_log2` in length.
///
/// # Arguments:
///
/// * `bitmap_word_free_blocks_lsbs` - Proper, non-empty subset of the
/// `bitmap_word_blocks_lsbs_mask` corresponding to the free blocks of
/// size and alignment equal to two to the power of
/// `block_allocation_blocks_log2` each, as computed by
/// [`bitmap_word_free_blocks_lsbs()`](Self::bitmap_word_free_blocks_lsbs).
/// * `max_split_block_allocation_blocks_log2` - Optional upper bound on the
/// split block size to return back. May be used as an optimization to
/// terminate the search early.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size to
/// examine containing free blocks for. Must be strictly less than
/// [`BitmapWord::BITS`].
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
/// * `bitmap_word_blocks_lsbs_mask_table` - Reference to a
/// [`BitmapWordBlocksLsbsMaskTable`] instance.
fn bitmap_word_block_alloc_split_block_size_log2(
bitmap_word_free_blocks_lsbs: BitmapWord,
max_split_block_allocation_blocks_log2: Option<u32>,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
bitmap_word_blocks_lsbs_mask_table: &BitmapWordBlocksLsbsMaskTable,
) -> u32 {
debug_assert_ne!(block_allocation_blocks_log2, BITMAP_WORD_BITS_LOG2);
debug_assert_eq!(
bitmap_word_blocks_lsbs_mask,
bitmap_word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(block_allocation_blocks_log2)
);
debug_assert_eq!(bitmap_word_free_blocks_lsbs & !bitmap_word_blocks_lsbs_mask, 0);
debug_assert_ne!(bitmap_word_free_blocks_lsbs, 0);
// At this point it is known that not all blocks are free, so the split block
// size will be half the range covered by the word at most.
debug_assert_ne!(bitmap_word_free_blocks_lsbs, bitmap_word_blocks_lsbs_mask);
let max_split_block_allocation_blocks_log2 = max_split_block_allocation_blocks_log2
.map(|m| m.min(BITMAP_WORD_BITS_LOG2 - 1))
.unwrap_or(BITMAP_WORD_BITS_LOG2 - 1);
let mut split_block_allocation_blocks_log2 = block_allocation_blocks_log2;
let mut free_split_blocks_lsbs = bitmap_word_free_blocks_lsbs;
while split_block_allocation_blocks_log2 < max_split_block_allocation_blocks_log2 {
let double_split_block_allocations_block_log2 = split_block_allocation_blocks_log2 + 1;
let word_double_split_blocks_lsbs_mask =
bitmap_word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(double_split_block_allocations_block_log2);
if Self::bitmap_word_filter_blocks_with_free_buddy_lsbs(
free_split_blocks_lsbs,
free_split_blocks_lsbs,
split_block_allocation_blocks_log2,
word_double_split_blocks_lsbs_mask,
) != 0
{
break;
}
// All remaining candidate blocks of the current
// split_block_allocation_blocks_log2 size have a free buddy,
// meaning a block at least double that size must get split up.
let split_block_allocation_blocks = 1u32 << split_block_allocation_blocks_log2;
free_split_blocks_lsbs = (free_split_blocks_lsbs
& (free_split_blocks_lsbs >> split_block_allocation_blocks))
& word_double_split_blocks_lsbs_mask;
debug_assert_ne!(free_split_blocks_lsbs, 0);
split_block_allocation_blocks_log2 += 1;
}
split_block_allocation_blocks_log2
}
/// Select a free block of size and alignment a specified power of two
/// within a [`BitmapWord`].
///
/// Select a free block of size and alignment equal to two to the power of
/// `block_allocation_blocks_log2`, under the assumption that there exists
/// at least one such in the [`BitmapWord`] not in turn contained in some
/// larger free aligned block.
///
/// The position of the free block within the [`BitmapWord`] will get
/// returned.
///
/// # Arguments:
///
/// * `bitmap_word_free_blocks_lsbs` - Subset of
/// `bitmap_word_blocks_lsbs_mask` corresponding to the free blocks of
/// size and alignment equal to two to the power of
/// `block_allocation_blocks_log2` each, as computed by
/// [`bitmap_word_free_blocks_lsbs()`](Self::bitmap_word_free_blocks_lsbs).
/// There must be at least one bit set with its buddy clear -- for
/// otherwise every free aligned block of size as specified by
/// `block_allocation_blocks_log2` would be contained in some larger
/// aligned block, in violation of the assumption.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size to
/// to select a free block of.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
/// * `bitmap_word_blocks_lsbs_mask_table` - Reference to a
/// [`BitmapWordBlocksLsbsMaskTable`] instance.
fn bitmap_word_block_alloc_select_block(
mut bitmap_word_free_blocks_lsbs: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
bitmap_word_blocks_lsbs_mask_table: &BitmapWordBlocksLsbsMaskTable,
) -> u32 {
debug_assert_eq!(
bitmap_word_blocks_lsbs_mask,
bitmap_word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(block_allocation_blocks_log2)
);
debug_assert_eq!(bitmap_word_free_blocks_lsbs & !bitmap_word_blocks_lsbs_mask, 0);
// It is assumed that the input block_allocation_blocks_log2 has been increased
// to the minimum required split block size already.
debug_assert!(
block_allocation_blocks_log2 == BITMAP_WORD_BITS_LOG2
|| block_allocation_blocks_log2
== Self::bitmap_word_block_alloc_split_block_size_log2(
bitmap_word_free_blocks_lsbs,
None,
block_allocation_blocks_log2,
bitmap_word_blocks_lsbs_mask,
bitmap_word_blocks_lsbs_mask_table
)
);
if block_allocation_blocks_log2 < BITMAP_WORD_BITS_LOG2 - 1 {
let double_block_allocations_block_log2 = block_allocation_blocks_log2 + 1;
let word_double_blocks_lsbs_mask =
bitmap_word_blocks_lsbs_mask_table.get_blocks_lsbs_mask(double_block_allocations_block_log2);
bitmap_word_free_blocks_lsbs = Self::bitmap_word_filter_blocks_with_free_buddy_lsbs(
bitmap_word_free_blocks_lsbs,
bitmap_word_free_blocks_lsbs,
block_allocation_blocks_log2,
word_double_blocks_lsbs_mask,
);
}
debug_assert_ne!(bitmap_word_free_blocks_lsbs, 0);
bitmap_word_free_blocks_lsbs.trailing_zeros()
}
/// Find free blocks of size and alignment a specified power of two in a
/// [`BitmapWord`].
///
/// Examine `bitmap_word` and set the least significant bit in each group of
/// bits corresponding to a free block of size and alignment equal to two to
/// the power of `block_allocation_blocks_log2` each.
///
/// # Arguments:
///
/// * `bitmap_word` - The [`BitmapWord`] to examine for free aligned blocks.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the desired block
/// size and alignment.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
fn bitmap_word_free_blocks_lsbs(
bitmap_word: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
) -> BitmapWord {
Self::bitmap_word_nonzero_blocks_lsbs(bitmap_word, block_allocation_blocks_log2, bitmap_word_blocks_lsbs_mask)
^ bitmap_word_blocks_lsbs_mask
}
/// Filter free blocks within a [`BitmapWord`] with an associated free
/// buddy.
///
/// Partition the [`BitmapWord`]'s bits into "buddy" pairs of blocks, with
/// each such block's size and alignment equal to two to the power of
/// `block_allocation_blocks_log2`. Retain from the
/// `bitmap_word_free_blocks_lsbs` only those least significant set bits
/// in each group of bits corresponding to an aligned block of specified
/// size for which the one from the associated buddy is clear.
/// Finally, mask the result by `bitmap_word_candidate_blocks_lsbs`.
///
/// # Arguments:
///
/// * `bitmap_word_candidate_blocks_lsbs` - Mask to apply to the result
/// before returning.
/// * `bitmap_word_free_blocks_lsbs` - Bitmask with its bits logically
/// partitioned into groups of the block size each, and the least
/// significant bit in each such group set if and only if the
/// corresponding block is considered free.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `bitmap_word_double_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by twice the value of two to the power of
/// `block_allocation_blocks_log2` each.
fn bitmap_word_filter_blocks_with_free_buddy_lsbs(
bitmap_word_candidate_blocks_lsbs: BitmapWord,
bitmap_word_free_blocks_lsbs: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_double_blocks_lsbs_masks: BitmapWord,
) -> BitmapWord {
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
debug_assert!(block_allocation_blocks < BitmapWord::BITS);
// Interchange the buddy block pairs in bitmap_word_free_blocks_lsbs.
let t1 = (bitmap_word_free_blocks_lsbs ^ (bitmap_word_free_blocks_lsbs >> block_allocation_blocks))
& bitmap_word_double_blocks_lsbs_masks;
let t2 = t1 << block_allocation_blocks;
let swapped_bitmap_word_free_blocks_lsbs = bitmap_word_free_blocks_lsbs ^ t1 ^ t2;
// Invert to go from "free buddy" mask to "allocated buddy" mask.
let mask = !swapped_bitmap_word_free_blocks_lsbs;
bitmap_word_candidate_blocks_lsbs & mask
}
/// Find non-zero blocks of size and alignment equal to a specified power of
/// two in a [`BitmapWord`].
///
/// Partition a [`BitmapWord`]'s bit into blocks of size equal to two to the
/// power of `block_allocation_blocks_log2` each and return the result of
/// setting the least significant bit in each group if and only if the
/// corresponding group in the input `bitmap_word` has at least one of its
/// bits set.
///
/// # Arguments:
///
/// * `bitmap_word` - The [`BitmapWord`] value to examine.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
fn bitmap_word_nonzero_blocks_lsbs(
bitmap_word: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
) -> BitmapWord {
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
(((((bitmap_word & !bitmap_word_blocks_lsbs_mask) >> 1) + ((!bitmap_word_blocks_lsbs_mask) >> 1))
>> (block_allocation_blocks - 1))
| bitmap_word)
& bitmap_word_blocks_lsbs_mask
}
/// Create a block field selection [`BitmapWord`] mask.
///
/// Partition a [`BitmapWord`]'s bits into groups of length equal to two to
/// the power of `block_allocation_blocks_log2` each. Return a value
/// with all bits set in each such group if and only if the corresponding
/// group's least significant bit in the input `selected_blocks_lsbs` is
/// set.
///
/// # Arguments:
///
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `selected_blocks_lsbs` - Block selection mask. A block is selected if
/// and only if its least significant bit in this mask is set.
fn bitmap_word_blocks_select_mask(
block_allocation_blocks_log2: u32,
selected_blocks_lsbs: BitmapWord,
) -> BitmapWord {
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
let selected_blocks_msbs = selected_blocks_lsbs << (block_allocation_blocks - 1);
(selected_blocks_msbs - selected_blocks_lsbs) | selected_blocks_msbs
}
/// Determine the lengths of longest 1-strings within each of a
/// [`BitmapWord`]'s blocks of specified width.
///
/// Partition the input `bitmap_word`'s bits into blocks of size equal to
/// two to the power of `block_allocation_blocks_log2` each. Find
/// the longest string of consecutive ones subject to the
/// alignment contraints specified via
/// `str_alignment_allocation_blocks_log2` within each such block.
/// Return a pair of the longest string found among all of the blocks, as
/// well as a packed integer specifying the per-block results in fields
/// of the block width each.
///
/// # Arguments:
///
/// * `bitmap_word` - The [`BitmapWord`] value to examine.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
/// * `str_alignment_allocation_blocks_log2` - Alignment constrained on the
/// 1-strings.
/// * `bitmap_word_str_alignment_anchors_mask` - Mask of equidistant set
/// bits, separated by two to the power of
/// `str_alignment_allocation_blocks_log2` each.
fn bitmap_word_blocks_maxstr_lens(
mut bitmap_word: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
str_alignment_allocation_blocks_log2: u32,
bitmap_word_str_alignment_anchors_mask: BitmapWord,
) -> (u32, BitmapWord) {
debug_assert!(str_alignment_allocation_blocks_log2 < block_allocation_blocks_log2);
// This is a modified variant of the algorithm from Hacker's Delight, 2nd
// edition, 6-3 ("Find longest string of 1-Bits") working on individual
// fields of block_allocation_blocks bit width each and, moreover,
// considering only 1-strings of specified alignment, if desired.
if bitmap_word == 0 {
return (0, 0);
}
// "Zeroth", preprocessing part: enforce the specified alignment on the found
// 1-strings.
let mut s_log2 = 0u32;
let mut mask = bitmap_word_blocks_lsbs_mask;
while s_log2 < str_alignment_allocation_blocks_log2 {
bitmap_word &= bitmap_word << (1 << s_log2);
// Applying the mask prevents strings in neighbouring blocks from combining.
bitmap_word &= !mask;
mask |= mask << (1 << s_log2);
s_log2 += 1;
}
// Only retain those bits at the head (most significant first order) of
// *aligned* strings. This will henceforth ensure that all considered
// 1-strings are aligned with respect to both, the starting position as
// well as their respective lengths.
bitmap_word &= bitmap_word_str_alignment_anchors_mask << ((1 << str_alignment_allocation_blocks_log2) - 1);
if bitmap_word == 0 {
return (0, 0);
}
// First part: determine the maximum power of two less or equal than the length
// of the longest string of consecutive ones, it will be refined by the
// backtracking part below.
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
let mut s_per_block = Self::bitmap_word_nonzero_blocks_lsbs(
bitmap_word,
block_allocation_blocks_log2,
bitmap_word_blocks_lsbs_mask,
) << str_alignment_allocation_blocks_log2;
while s_log2 < block_allocation_blocks_log2 {
// Test for bitstrings of lengths at least 2 * (1 << s_log2).
// The heading bit(s) of any such string in the original input will remain set.
let mut y = bitmap_word & (bitmap_word << (1 << s_log2));
// Applying the mask prevents strings in neighbouring blocks from combining.
y &= !mask;
mask |= mask << (1 << s_log2);
debug_assert!(y & bitmap_word_blocks_lsbs_mask == 0);
let mut nonzero_blocks_lsbs = (((y >> 1) + (!bitmap_word_blocks_lsbs_mask >> 1))
>> (block_allocation_blocks - 1))
& bitmap_word_blocks_lsbs_mask;
// Only consider blocks which are still in the game -- otherwise disconnected
// chunks within a block might eventually combine, which would be
// invalid.
nonzero_blocks_lsbs &= s_per_block >> s_log2;
if nonzero_blocks_lsbs == 0 {
break;
}
// The addition actually doubles the value in each block field where
// nonzero_blocks_lsbs is (still) set.
s_per_block += nonzero_blocks_lsbs << s_log2;
// Update the bitmap_word block fields with the non-zero ones from y.
let nonzero_blocks_select_mask =
Self::bitmap_word_blocks_select_mask(block_allocation_blocks_log2, nonzero_blocks_lsbs);
bitmap_word = bitmap_word ^ ((bitmap_word ^ y) & nonzero_blocks_select_mask);
s_log2 += 1;
}
// Second part: backtracking to refine the found s-value. From part 1 above, the
// most significant set bit of each block's maximum 1-bit string length
// is known. Process the remaining less significant bits of the
// respective length values from most to least significant and set them
// as appropriate.
let mut s_max = 1u32 << s_log2;
// For reporting an accurate s_max back, keep track of the set of blocks
// (potentially still) having a maximum 1-bit string of the maximum
// length across all blocks. To be more specific, this set comprises all
// blocks with a common prefix in their respective maxstr length values
// up to the current position, equal to the prefix of the of the maximum
// across all blocks as determined up the current position.
let mut blocks_with_max_str_lsbs = (s_per_block >> s_log2) & bitmap_word_blocks_lsbs_mask;
// In each iteration, consider only those blocks which have an initial s-value
// (as determined in the previous loop) greater than the current s_delta
// below. Note that this set grows with decreasing s_log2.
let mut blocks_with_s_str_lsbs: BitmapWord = blocks_with_max_str_lsbs;
while s_log2 > 0 {
s_log2 -= 1;
let s_delta = 1u32 << s_log2;
let y = bitmap_word & (bitmap_word << s_delta);
if y != 0 {
debug_assert!(y & blocks_with_s_str_lsbs == 0);
// Instead of and'ing with bitmap_word_blocks_lsbs_mask, and with
// blocks_with_s_str_lsbs directly to save an additional and
// operation.
let nonzero_blocks_lsbs = (((y >> 1) + (!bitmap_word_blocks_lsbs_mask >> 1))
>> (block_allocation_blocks - 1))
& blocks_with_s_str_lsbs;
s_per_block |= nonzero_blocks_lsbs << s_log2;
// Update the bitmap_word block fields with the non-zero ones from y.
let nonzero_blocks_select_mask =
Self::bitmap_word_blocks_select_mask(block_allocation_blocks_log2, nonzero_blocks_lsbs);
bitmap_word = bitmap_word ^ ((bitmap_word ^ y) & nonzero_blocks_select_mask);
if blocks_with_max_str_lsbs & nonzero_blocks_lsbs != 0 {
// All blocks which do not have the current bit set in their resp. maxstr
// lengths, cannot attain the maximum across all blocks anymore.
blocks_with_max_str_lsbs &= nonzero_blocks_lsbs;
s_max |= s_delta;
}
}
blocks_with_s_str_lsbs |= (s_per_block >> s_log2) & bitmap_word_blocks_lsbs_mask;
}
(s_max, s_per_block)
}
/// Find a 1-string of specified minimum length in a [`BitmapWord`], if any.
///
/// Return the position, counted from the least significant bit, of the
/// first string of `min_str_len` consecutive 1-bits in `bitmap_word`
/// starting at an alignment boundary as determined by
/// `str_alignment_allocation_blocks_log2`. If no such 1-string is found,
/// `None` is returned.
///
/// # Arguments:
///
/// * `bitmap_word` - The [`BitmapWord`] value to examine.
/// * `min_str_len` - The 1-string length to search for. Must be a multiple
/// of the alignment as determined by
/// `str_alignment_allocation_blocks_log2` and strictly less than
/// [`BitmapWord::BITS`].
/// * `str_alignment_allocation_blocks_log2` - Alignment constrained on the
/// 1-string.
/// * `bitmap_word_str_alignment_anchors_mask` - Mask of equidistant set
/// bits, separated by two to the power of
/// `str_alignment_allocation_blocks_log2` each.
fn bitmap_word_find_str_with_min_len(
mut bitmap_word: BitmapWord,
min_str_len: u32,
str_alignment_allocation_blocks_log2: u32,
bitmap_word_str_alignment_anchors_mask: BitmapWord,
) -> Option<u32> {
debug_assert!(str_alignment_allocation_blocks_log2 < BITMAP_WORD_BITS_LOG2);
debug_assert!(min_str_len.is_aligned_pow2(str_alignment_allocation_blocks_log2));
debug_assert!(min_str_len < 64);
// This is the the algorithm from Hacker's Delight, 2nd
// edition, 6-2 ("Find first string of 1-Bits of a Given Length").
let mut n = min_str_len;
while n > 1 {
let s = n >> 1;
bitmap_word &= bitmap_word << s;
n -= s;
}
bitmap_word &= bitmap_word_str_alignment_anchors_mask << ((1 << str_alignment_allocation_blocks_log2) - 1);
if bitmap_word != 0 {
Some(bitmap_word.trailing_zeros() - (min_str_len - 1))
} else {
None
}
}
/// Packed integer `<=` comparison.
///
/// Interpret `x` and `y` as sequences of packed integers with a width of
/// two to the power of `block_allocation_blocks_log2` stored in a
/// [`BitmapWord`] each. Compare the corresponding packed integer fields
/// of `x` and `y` each and return the result as a sequence of packed
/// integers of matching format with their least significant bits set if
/// and only if the the corresponding packed integer field from `x` compares
/// as less than or equal to the one from `y`.
///
/// # Arguments:
/// `x` - Packed first operand integers.
/// `y` - Packed second operand integers.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
fn bitmap_word_blocks_fields_geq_lsbs(
x: BitmapWord,
y: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
) -> BitmapWord {
// Adapted from the subtraction algorithm in Hacker's Delight, 2nd edition,
// 2-18 ("Multibyte Add, Subtract, Absolute Value")
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
let bitmap_word_blocks_msbs_mask = bitmap_word_blocks_lsbs_mask << (block_allocation_blocks - 1);
// The MSBs in each block of d are set iff there's no borrow carried into that
// position.
let d = (x | bitmap_word_blocks_msbs_mask) - (y & !bitmap_word_blocks_msbs_mask);
// Set each block's MSB iff no borrow would be carried out of that position.
let no_borrow_msbs = (x | (!y & d)) & (!y | d);
(no_borrow_msbs >> (block_allocation_blocks - 1)) & bitmap_word_blocks_lsbs_mask
}
/// Packed integer subtraction.
///
/// Interpret `x` and `y` as sequences of packed integers with a width of
/// two to the power of `block_allocation_blocks_log2` stored in a
/// [`BitmapWord`] each. Subtract the corresponding packed integer fields
/// of `x` and `y` modulo (two to the power of) the block size each and
/// return the result as a sequence of packed integers of matching
/// format.
///
/// # Arguments:
/// `x` - Packed first operand integers.
/// `y` - Packed second operand integers.
/// * `block_allocation_blocks_log2` - Base-2 logarithm of the block size
/// and alignment.
/// * `bitmap_word_blocks_lsbs_mask` - Mask of equidistant set bits,
/// separated by two to the power of `block_allocation_blocks_log2` each.
fn bitmap_word_blocks_fields_sub(
x: BitmapWord,
y: BitmapWord,
block_allocation_blocks_log2: u32,
bitmap_word_blocks_lsbs_mask: BitmapWord,
) -> BitmapWord {
// C.f. Hacker's Delight, 2nd edition, 2-18 ("Multibyte Add, Subtract, Absolute
// Value")
let block_allocation_blocks = 1u32 << block_allocation_blocks_log2;
let bitmap_word_blocks_msbs_mask = bitmap_word_blocks_lsbs_mask << (block_allocation_blocks - 1);
let d = (x | bitmap_word_blocks_msbs_mask) - (y & !bitmap_word_blocks_msbs_mask);
!(((x ^ y) | !bitmap_word_blocks_msbs_mask) ^ d)
}
}
/// [`Iterator`] over an [`AllocBitmap`]'s [`BitmapWord`]s.
pub(super) struct AllocBitmapWordIterator<'a, const AN: usize, const FN: usize> {
bitmap: &'a AllocBitmap,
pending_allocs_iter: SparseAllocBitmapUnionWordIterator<'a, AN>,
next_pending_alloc: Option<(u64, BitmapWord)>,
pending_frees_iter: SparseAllocBitmapUnionWordIterator<'a, FN>,
next_pending_free: Option<(u64, BitmapWord)>,
next_bitmap_word_index: u64,
}
impl<'a, const AN: usize, const FN: usize> AllocBitmapWordIterator<'a, AN, FN> {
pub(super) fn new_at_bitmap_word_index(
bitmap: &'a AllocBitmap,
pending_allocs: &'a SparseAllocBitmapUnion<'a, AN>,
pending_frees: &'a SparseAllocBitmapUnion<'a, FN>,
bitmap_word_index_begin: u64,
) -> Self {
let (mut pending_allocs_iter, mut pending_frees_iter) = if bitmap_word_index_begin == 0 {
(pending_allocs.iter(), pending_frees.iter())
} else {
(
pending_allocs.iter_at_bitmap_word_index(bitmap_word_index_begin),
pending_frees.iter_at_bitmap_word_index(bitmap_word_index_begin),
)
};
let next_pending_alloc = pending_allocs_iter.next();
let next_pending_free = pending_frees_iter.next();
Self {
bitmap,
pending_allocs_iter,
next_pending_alloc,
pending_frees_iter,
next_pending_free,
next_bitmap_word_index: bitmap_word_index_begin,
}
}
fn goto_bitmap_word_index(&mut self, bitmap_word_index: u64) {
if self.next_bitmap_word_index == bitmap_word_index {
return;
}
self.pending_allocs_iter.goto_bitmap_word_index(bitmap_word_index);
self.next_pending_alloc = self.pending_allocs_iter.next();
self.pending_frees_iter.goto_bitmap_word_index(bitmap_word_index);
self.next_pending_free = self.pending_frees_iter.next();
self.next_bitmap_word_index = bitmap_word_index;
}
}
impl<'a, const AN: usize, const FN: usize> Iterator for AllocBitmapWordIterator<'a, AN, FN> {
type Item = (u64, BitmapWord);
fn next(&mut self) -> Option<Self::Item> {
if self.next_bitmap_word_index >= self.bitmap.bitmap.len() as u64 {
return None;
}
let bitmap_word_index = self.next_bitmap_word_index;
self.next_bitmap_word_index += 1;
let mut bitmap_word = self.bitmap.bitmap[bitmap_word_index as usize];
if let Some(pending_alloc) = &self.next_pending_alloc {
if pending_alloc.0 == bitmap_word_index {
bitmap_word |= pending_alloc.1;
self.next_pending_alloc = self.pending_allocs_iter.next();
}
}
if let Some(pending_free) = &self.next_pending_free {
if pending_free.0 == bitmap_word_index {
bitmap_word &= !pending_free.1;
self.next_pending_free = self.pending_frees_iter.next();
}
}
Some((bitmap_word_index, bitmap_word))
}
}
/// [`Iterator`] returned by [`AllocBitmap::iter_at_allocation_block()`].
pub struct AllocBitmapIterator<'a, const AN: usize, const FN: usize> {
bitmap_word_iter: AllocBitmapWordIterator<'a, AN, FN>,
cur_bitmap_word: Option<BitmapWord>,
next_pos_in_cur_bitmap_word: u32,
}
impl<'a, const AN: usize, const FN: usize> AllocBitmapIterator<'a, AN, FN> {
fn new_at(
bitmap: &'a AllocBitmap,
pending_allocs: &'a SparseAllocBitmapUnion<'a, AN>,
pending_frees: &'a SparseAllocBitmapUnion<'a, FN>,
first_physical_allocation_block_index: layout::PhysicalAllocBlockIndex,
) -> Self {
let first_physical_allocation_block_index = u64::from(first_physical_allocation_block_index);
let bitmap_word_index_begin = first_physical_allocation_block_index >> BITMAP_WORD_BITS_LOG2;
let next_pos_in_cur_bitmap_word =
(first_physical_allocation_block_index & BitmapWord::trailing_bits_mask(BITMAP_WORD_BITS_LOG2)) as u32;
let mut bitmap_word_iter = AllocBitmapWordIterator::new_at_bitmap_word_index(
bitmap,
pending_allocs,
pending_frees,
bitmap_word_index_begin,
);
let cur_bitmap_word = bitmap_word_iter.next().map(|v| v.1);
Self {
bitmap_word_iter,
cur_bitmap_word,
next_pos_in_cur_bitmap_word,
}
}
}
impl<'a, const AN: usize, const FN: usize> Iterator for AllocBitmapIterator<'a, AN, FN> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
let cur_bitmap_word = self.cur_bitmap_word?;
let bit = (cur_bitmap_word >> self.next_pos_in_cur_bitmap_word) & 1 != 0;
self.next_pos_in_cur_bitmap_word += 1;
if self.next_pos_in_cur_bitmap_word == BitmapWord::BITS {
self.next_pos_in_cur_bitmap_word = 0;
self.cur_bitmap_word = self.bitmap_word_iter.next().map(|v| v.1);
}
Some(bit)
}
}
/// [`Iterator`] returned by
/// [`AllocBitmap::iter_chunked_at_allocation_block()`].
pub struct AllocBitmapChunkedIterator<'a, const AN: usize, const FN: usize> {
bitmap_word_iter: AllocBitmapWordIterator<'a, AN, FN>,
cur_bitmap_word: Option<BitmapWord>,
next_pos_in_cur_bitmap_word: u32,
chunk_allocation_blocks: u32,
}
impl<'a, const AN: usize, const FN: usize> AllocBitmapChunkedIterator<'a, AN, FN> {
fn new_at(
bitmap: &'a AllocBitmap,
pending_allocs: &'a SparseAllocBitmapUnion<'a, AN>,
pending_frees: &'a SparseAllocBitmapUnion<'a, FN>,
first_physical_allocation_block_index: layout::PhysicalAllocBlockIndex,
chunk_allocation_blocks: u32,
) -> Self {
debug_assert!(chunk_allocation_blocks <= BitmapWord::BITS);
let first_physical_allocation_block_index = u64::from(first_physical_allocation_block_index);
let bitmap_word_index_begin = first_physical_allocation_block_index >> BITMAP_WORD_BITS_LOG2;
let next_pos_in_cur_bitmap_word =
(first_physical_allocation_block_index & BitmapWord::trailing_bits_mask(BITMAP_WORD_BITS_LOG2)) as u32;
let mut bitmap_word_iter = AllocBitmapWordIterator::new_at_bitmap_word_index(
bitmap,
pending_allocs,
pending_frees,
bitmap_word_index_begin,
);
let cur_bitmap_word = bitmap_word_iter.next().map(|v| v.1);
Self {
bitmap_word_iter,
cur_bitmap_word,
next_pos_in_cur_bitmap_word,
chunk_allocation_blocks,
}
}
pub fn goto(&mut self, physical_allocation_block_index: layout::PhysicalAllocBlockIndex) {
let physical_allocation_block_index = u64::from(physical_allocation_block_index);
let bitmap_word_index = physical_allocation_block_index >> BITMAP_WORD_BITS_LOG2;
self.next_pos_in_cur_bitmap_word =
(physical_allocation_block_index & BitmapWord::trailing_bits_mask(BITMAP_WORD_BITS_LOG2)) as u32;
self.bitmap_word_iter.goto_bitmap_word_index(bitmap_word_index);
self.cur_bitmap_word = self.bitmap_word_iter.next().map(|v| v.1);
}
}
impl<'a, const AN: usize, const FN: usize> Iterator for AllocBitmapChunkedIterator<'a, AN, FN> {
type Item = BitmapWord;
fn next(&mut self) -> Option<Self::Item> {
let cur_bitmap_word = self.cur_bitmap_word?;
let mut cur_chunk = cur_bitmap_word >> self.next_pos_in_cur_bitmap_word;
self.next_pos_in_cur_bitmap_word += self.chunk_allocation_blocks;
if self.next_pos_in_cur_bitmap_word >= BitmapWord::BITS {
self.next_pos_in_cur_bitmap_word -= BitmapWord::BITS;
self.cur_bitmap_word = self.bitmap_word_iter.next().map(|v| v.1);
if self.next_pos_in_cur_bitmap_word > 0 {
let cur_bitmap_word = self.cur_bitmap_word.unwrap_or(0);
cur_chunk |= cur_bitmap_word << (self.chunk_allocation_blocks - self.next_pos_in_cur_bitmap_word)
}
}
Some(cur_chunk & BitmapWord::trailing_bits_mask(self.chunk_allocation_blocks))
}
}