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
use core::cmp;
use core::intrinsics::assume;
use core::marker::{PhantomData, PhantomPinned};
use core::mem;
use core::ptr;
use core::sync::atomic::{self, AtomicPtr, AtomicUsize};
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
use core::usize;
use crate::block::{Block, Layout};
use crate::alloc::{Heap, HeapError};

/// Pointer bit flag indicating a temporarily frozen list node.
const FREEZE_FLAG: usize = 0x1;
/// Pointer bit flag indicating a logically deleted list node.
const REMOVE_FLAG: usize = 0x2;
/// Bit mask that clears the freeze and remove flags from a pointer.
const ADDR_MASK: usize = !0x3;

/// Bit flag indicating a list node that is logically merged with its successor.
const MERGE_FLAG: usize = 0x4;
/// Bit flag indicating a logically split list node; the high order bits of
/// the status field hold the extent aligned split offset.
const SPLIT_FLAG: usize = 0x8;
/// Bit mask that clears the freeze, remove, merge, and split flags from a status value.
const SIZE_MASK: usize = !0xF;

/// Power-of-two base address alignment of an extent.
const EXTENT_ALIGN: usize = mem::align_of::<ExtentNode>();
/// Bit mask that rounds an address within a free extent header down to the
/// base address of the extent.
const EXTENT_ADDR_MASK: usize = !(EXTENT_ALIGN - 1);

/// Maximum number of levels in the free extent skip list.
const MAX_LEVEL: usize = 32;

/// Reference counted smart pointer to an `ExtentList`.
pub struct AddrSpace<'a> {
    ptr: *mut ExtentList<'a>,
    size: usize,
}

/// Lock-free allocator of page-aligned memory extents from some address range.
#[repr(C, align(4096))]
pub struct ExtentList<'a> {
    /// Size in bytes of this extent; always zero for the head extent.
    zero: usize,
    /// Insertion status of this slip list.
    status: AtomicUsize,
    /// Number of references to this extent.
    refcount: AtomicUsize,
    /// Skip list of free extents, ordered by ascending address.
    addr_list: AddrList,
    /// Skip list of free extents, ordered by ascending size,
    /// then ascending address.
    size_list: SizeList,
    /// Number of bytes in the address space, including the head extent.
    size: AtomicUsize,
    /// Number of live allocations in the address space.
    live: AtomicUsize,
    /// Number of currently allocated bytes in the address space.
    used: AtomicUsize,
    /// Lifetime of the address space in which this extent list resides.
    lifetime: PhantomData<&'a ()>,
    /// Pin to the base address of the head extent.
    pinned: PhantomPinned,
}

/// Header embedded within a free memory extent, with skip list nodes for the
/// address-ordered free extent list, and the size-ordered free extent list.
/// Transmutable from an `ExtentList`.
#[repr(C, align(4096))]
struct ExtentNode {
    /// Size in bytes of the extent, including this header.
    size: usize,
    /// Insertion status of this slip list node.
    status: AtomicUsize,
    /// Number of references to this extent.
    refcount: AtomicUsize,
    /// Skip list node linking this extent to subsequent free extents,
    /// ordered by ascending address.
    addr_node: AddrNode,
    /// Skip list node linking this extent to subsequent free extents,
    /// ordered by ascending size, then ascending address.
    size_node: SizeNode,
    /// Pin to the base address of the free extent.
    pinned: PhantomPinned,
}

/// Base address of a free extent, used to order the address skip list.
type AddrKey = usize;

/// Lock-free sorted skip list of free extents, ordered by ascending address.
#[repr(C)]
struct AddrList {
    /// Pseudo skip list node whose address is less than all valid extent addresses.
    head: AddrNode,
}

/// Lock-free sorted skip list node for a skip list of free extents, ordered by
/// ascending address. The base address of the extent in which a node resides
/// is obtained by rounding down the address of the node to the extent alignment.
#[repr(C)]
struct AddrNode {
    /// State of the pseudo-random number generator used to select the heights
    /// of inserted skip list nodes; used only by `AddrList` head nodes.
    seed: AtomicUsize,
    /// Tower of forward-pointing list links, with `height` valid levels.
    levels: [AddrLink; MAX_LEVEL],
}

/// Reference counted smart pointer to an `AddrNode`; uses the `refcount` field
/// of the extent in which the pointed-to node resides.
struct AddrNodeRef {
    ptr: *mut AddrNode,
}

/// Lock-free sorted skip list link for given level of a skip list of free
/// extents, ordered by ascending address. The root level of the node in which
/// a link resides is obtained by decrementing the link pointer by its level in
/// the skip list. The base address of the extent in which a link resides is
/// obtained by rounding down the address of the link to the extent alignment.
#[repr(C)]
struct AddrLink {
    /// Pointer to the next link in the list, possibly tagged with a freeze or
    /// remove flag--but never both. Zero indicates the end of the list.
    succ: AtomicUsize,
    /// Back pointer to the predecessor link in the level list, whose successor
    /// points to this link, with its freeze bit set so that it cannot be marked
    /// while `back` points to it. Non-zero only during removal.
    back: AtomicPtr<AddrLink>,
    /// Pin to level in `AddrNode` tower.
    pinned: PhantomPinned,
}

/// Reference counted smart pointer to an `AddrLink`; uses the `refcount` field
/// of the extent in which the pointed-to link resides.
struct AddrLinkRef {
    ptr: *mut AddrLink,
}

/// Size and base address of a free extent, used to order the size skip list.
type SizeKey = (usize, usize);

/// Lock-free sorted skip list of free extents, ordered by ascending size, then
/// ascending address.
#[repr(C)]
struct SizeList {
    /// Pseudo skip list node whose size is less than all valid extent sizes.
    head: SizeNode,
}

/// Lock-free sorted skip list node for a skip list of free extents, ordered by
/// ascending size, then ascending address. The base address of the extent in
/// which a node resides is obtained by rounding down the address of the node
/// to the extent alignment.
#[repr(C)]
struct SizeNode {
    /// State of the pseudo-random number generator used to select the heights
    /// of inserted skip list nodes; used only by `SizeList` head nodes.
    seed: AtomicUsize,
    /// Tower of forward-pointing list links, with `height` valid levels.
    levels: [SizeLink; MAX_LEVEL],
}

/// Reference counted smart pointer to a `SizeNode`; uses the `refcount` field
/// of the extent in which the pointed-to node resides.
struct SizeNodeRef {
    ptr: *mut SizeNode,
}

/// Lock-free sorted skip list link for given level of a skip list of free
/// extents, ordered by ascending size, then ascending address. The root level
/// of the node in which a link resides is obtained by decrementing the link
/// pointer by its level in the skip list. The base address of the extent in
/// which a link resides is obtained by rounding down the address of the link
/// to the extent alignment.
#[repr(C)]
struct SizeLink {
    /// Pointer to the next link in the list, possibly tagged with a freeze or
    /// remove flag--but never both. Zero indicates the end of the list.
    succ: AtomicUsize,
    /// Back pointer to the predecessor link in the level list, whose successor
    /// points to this link, with its freeze bit set so that it cannot be marked
    /// while `back` points to it. Non-zero only during removal.
    back: AtomicPtr<SizeLink>,
    /// Pin to level in `AddrNode` tower.
    pinned: PhantomPinned,
}

/// Reference counted smart pointer to a `SizeLink`; uses the `refcount` field
/// of the extent in which the pointed-to link resides.
struct SizeLinkRef {
    ptr: *mut SizeLink,
}

#[macro_export]
macro_rules! addr_space {
    // pub heap $name = [$size];
    ($(#[$attr:meta])* pub heap $name:ident = [$size:expr];) => (
        #[repr(align(4096))]
        struct __Heap([u8; $size]);
        static mut __HEAP: __Heap = __Heap([0; $size]);
        $(#[$attr])* pub static $name: $crate::alloc::AddrSpace<'static> =
            unsafe { $crate::alloc::AddrSpace::from_raw(&__HEAP as *const __Heap as *mut u8, $size) };
    );
}

unsafe impl<'a> Send for AddrSpace<'a> {
}

unsafe impl<'a> Sync for AddrSpace<'a> {
}

impl<'a> AddrSpace<'a> {
    #[inline]
    pub const unsafe fn from_raw(ptr: *mut u8, size: usize) -> AddrSpace<'a> {
        AddrSpace {
            ptr: ptr as *mut ExtentList,
            size: size,
        }
    }

    #[inline]
    unsafe fn extent(&self) -> *mut ExtentNode {
        self.ptr as *mut ExtentNode
    }

    /// Returns the total number of bytes in this address space.
    #[inline]
    pub fn size(&self) -> usize {
        self.size
    }

    /// Returns the number of live allocations from this address space.
    #[inline]
    pub fn live(&self) -> usize {
        unsafe { (*self.ptr).live() }
    }

    /// Returns the number of bytes currently allocated from this address space.
    #[inline]
    pub fn used(&self) -> usize {
        unsafe { (*self.ptr).used() }
    }
}

impl<'a> Heap<'a> for AddrSpace<'a> {
    unsafe fn alloc(&self, layout: Layout) -> Result<Block<'a>, HeapError> {
        (*self.ptr).grow(self.size);
        (*self.ptr).alloc(layout)
    }

    unsafe fn dealloc(&self, block: Block<'a>) -> usize {
        (*self.ptr).grow(self.size);
        (*self.ptr).dealloc(block)
    }
}

impl<'a> Clone for AddrSpace<'a> {
    fn clone(&self) -> AddrSpace<'a> {
        unsafe {
            self.extent().retain();
            AddrSpace::from_raw(self.ptr as *mut u8, self.size)
        }
    }
}

impl<'a> Drop for AddrSpace<'a> {
    fn drop(&mut self) {
        unsafe {
            self.extent().release();
        }
    }
}

impl<'a> ExtentList<'a> {
    /// Returns the total number of bytes in this address space.
    #[inline]
    pub fn size(&self) -> usize {
        self.size.load(Relaxed)
    }

    /// Returns the number of live allocations from this address space.
    #[inline]
    pub fn live(&self) -> usize {
        self.live.load(Relaxed)
    }

    /// Returns the number of bytes currently allocated from this address space.
    #[inline]
    pub fn used(&self) -> usize {
        self.used.load(Relaxed)
    }

    /// Extends this address space to `new_size` bytes in length by inserting a
    /// free extent for the delta between the current size and the new size.
    pub unsafe fn grow(&self, new_size: usize) {
        // Get the current size of the address space; synchronized by subsequent CAS.
        let mut old_size = self.size.load(Relaxed);
        // The address space must be large enough to hold the head extent.
        let mut min_size;
        // Loop until the address space has been resized.
        loop {
            // Check if the address space is already the desired size.
            if old_size == new_size {
                return;
            }
            // Exclude the head extent from the growable range.
            min_size = cmp::max(EXTENT_ALIGN, old_size);
            // Ensure that the new address space size is extent aligned.
            assert!(new_size % EXTENT_ALIGN == 0);
            // Make sure there's room for a complete extent.
            assert!(min_size + EXTENT_ALIGN <= new_size);
            // Try to grow the address space, synchronizing with concurrent
            // grow operations.
            match self.size.compare_exchange(old_size, new_size, SeqCst, Relaxed) {
                // CAS succeeded; incorporate the new extent.
                Ok(_) => break,
                // CAS failed; update the current address space size and try again.
                Err(size) => old_size = size,
            }
        }
        // Get a pointer to the new extent.
        let extent = (self as *const ExtentList<'a> as usize).wrapping_add(min_size) as *mut ExtentNode;
        // Compute the size of the new extent as the delta between the old and
        // new address space sizes.
        let size = new_size.wrapping_sub(min_size);
        // Insert the extent into the free extent skip lists.
        self.insert(extent, size);
    }

    /// Inserts an extent into the free extent skip lists, without updating
    /// allocation accounting information.
    #[inline]
    unsafe fn insert(&self, extent: *mut ExtentNode, size: usize) {
        // Initialize the size of the free extent.
        ptr::write(&mut (*extent).size, size);
        // Initialize two references to the free extent, one for the address
        // skip list, and one for the size skip list.
        ptr::write(&mut (*extent).refcount, AtomicUsize::new(2));
        // Get a reference to the free extent's address skip list node.
        let addr_node = AddrNodeRef::from_raw(&mut (*extent).addr_node);
        // Get a reference to the free extent's size skip list node.
        let size_node = SizeNodeRef::from_raw(&mut (*extent).size_node);
        // Insert the free extent into the size-ordered skip list.
        self.size_list.insert(size_node);
        // Insert the free extent into the address-ordered skip list;
        // linearization point for extent insertion.
        self.addr_list.insert(addr_node);
    }
}

impl<'a> Heap<'a> for ExtentList<'a> {
    unsafe fn alloc(&self, layout: Layout) -> Result<Block<'a>, HeapError> {
        // Get the requested allocation size.
        let size = layout.size();
        // Check if this is a zero-sized allocation.
        if size == 0 {
            // Return the zero-sized block.
            return Ok(Block::empty());
        }
        // Align the allocation size to the extent alignment.
        let size = size.wrapping_add(EXTENT_ALIGN).wrapping_sub(1) & !EXTENT_ALIGN.wrapping_sub(1);
        // Compute the greaters lower bound of the desired allocation size.
        let size_key = (size.wrapping_sub(1), usize::MAX);
        // Loop until a suitable extent is allocated.
        loop {
            // Remove the first suitable extent from the size skip list;
            // linearization point for allocation attempt.
            let size_link = self.size_list.take_next(size_key);
            // Check if no suitable extent was found.
            if size_link.is_nil() {
                // Out of memory.
                return Err(HeapError::OutOfMemory);
            }
            // Get the address of the removed extent.
            let addr = size_link.addr();
            // Drop the reference to the removed link in the size list to
            // reduce latency with concurrent allocations.
            mem::drop(size_link);
            // Remove the extent from the address skip list; linearization
            // point for allocation completion.
            let addr_link = self.addr_list.remove(addr, addr.wrapping_sub(1));
            // Check if removal from the address skip list failed.
            if addr_link.is_nil() {
                // Extent concurrently merged with its predecessor; try again.
                continue;
            }
            // Drop the reference to the address skip link, converting it to an
            // uncounted pointer to the removed extent.
            let extent = addr_link.into_extent();
            // Wait for all remaining references to the extent drop.
            extent.await_release();

            // Compute the difference between the allocated extent size and the
            // requested allocation size.
            let excess_size = (*extent).size.wrapping_sub(size);
            // Check if the allocated extent can be split in two, and an extent
            // containing the excess bytes re-inserted into the free lists.
            if excess_size != 0 {
                // Ensure that the excess extent is properly aligned.
                assert!(excess_size % EXTENT_ALIGN == 0);
                // Get a pointer to the excess extent.
                let excess_extent = (extent as usize).wrapping_add(size) as *mut ExtentNode;
                // Insert the excess extent back into the free skip lists.
                self.insert(excess_extent, excess_size);
            }

            // Increment the number of live allocations in the address space.
            self.live.fetch_add(1, Relaxed);
            // Increment the number of allocated bytes in the address space.
            self.used.fetch_add(size, Relaxed);
            // Return the raw memory block of the newly allocated extent.
            return Ok(Block::from_raw_parts(extent as *mut u8, size));
        }
    }

    unsafe fn dealloc(&self, block: Block<'a>) -> usize {
        // Get the size of the memory block to deallocate.
        let size = block.size();
        // Check if this is a zero-sized deallocation.
        if size == 0 {
            // Nothing to deallocate.
            return 0;
        }
        // Transmute the memory block into a free extent.
        let extent = block.into_raw() as *mut ExtentNode;
        // Insert the extent into the free extent skip lists.
        self.insert(extent, size);
        // Return the size of the freed extent.
        size
    }
}

impl ExtentNode {
    /// Returns `true` if this is the `nil` extent.
    #[inline]
    fn is_nil(self: *mut ExtentNode) -> bool {
        self.is_null()
    }

    /// Increments the reference count of this extent.
    #[inline]
    unsafe fn retain(self: *mut ExtentNode) {
        // Check if this is the nil extent.
        if self.is_nil() {
            // The nil extent has no reference count.
            return;
        }
        // Increment the reference count of this extent, synchronizing with
        // reference releases.
        let old_refcount = (*self).refcount.fetch_add(1, Acquire);
        if old_refcount == 0 {
            panic!();
        }
        // Check for reference count overflow.
        if old_refcount == usize::MAX {
            panic!("refcount overflow");
        }
    }

    /// Decremements the reference count of this extent, returning `true` if
    /// this causes the reference count to drop to zero.
    #[inline]
    unsafe fn release(self: *mut ExtentNode) -> bool {
        // Check if this is the nil extent.
        if self.is_nil() {
            // The nil extent has no reference count.
            return false;
        }
        // Decrement the reference count of this extent, synchronizing with
        // reference count acquires.
        let old_refcount = (*self).refcount.fetch_sub(1, Release);
        if old_refcount == 0 {
            panic!();
        }
        // Return true if the reference count dropped to zero.
        old_refcount == 1
    }

    /// Busy waits until all references to this extent have been released.
    #[inline]
    unsafe fn await_release(self: *mut ExtentNode) {
        // Loop until all references to this extent have been released.
        loop {
            // Load this extent's reference count, synchronizing with reference
            // count releases.
            let refcount = (*self).refcount.load(Acquire);
            // Check if the reference count has dropped to zero.
            if refcount == 0 {
                // The extent is no longer aliased.
                break;
            }
            // Busy wait before trying again.
            atomic::spin_loop_hint();
        }
    }

    ///// Merges this extent with its successor.
    //#[inline]
    //unsafe fn merge(self: *mut ExtentNode) {
    //    // Load the status field of the extent in which this link resides;
    //    // synchronized by subsequent CAS.
    //    let mut old_status = (*self).status.load(Relaxed);
    //    // Loop until the merge flag is set.
    //    loop {
    //        // Check if the extent is flagged for removal, merging, or splitting.
    //        if old_status & REMOVE_FLAG != 0 {
    //            // Can't merge logically removed extent.
    //            return;
    //        } else if old_status & MERGE_FLAG != 0 {
    //            // Help merge the extent with its successor.
    //            self.help_merge();
    //        } else if old_status & SPLIT_FLAG != 0 {
    //            // Help split the extent at the size boundary encoded in the
    //            // high order bits of the status field.
    //            self.help_split(old_status & SIZE_MASK);
    //        } else {
    //            // Set the merge flag on the status field.
    //            let new_status = old_status | MERGE_FLAG;
    //            // Try to update the status field, synchronizing with other
    //            // list mutations; linearization point for extent merging.
    //            match (*self).status.compare_exchange(old_status, new_status, SeqCst, Relaxed) {
    //                // CAS succeeded.
    //                Ok(_) => break,
    //                // CAS failed; try again with the latest status.
    //                Err(status) => old_status = status,
    //            }
    //        }
    //    }
    //}

    ///// Splits this extent at the `size` byte boundary.
    //#[inline]
    //unsafe fn split(self: *mut ExtentNode, size: usize) {
    //    // TODO
    //}

    /// Helps merge this extent with its successor.
    #[inline]
    unsafe fn help_merge(self: *mut ExtentNode) {
        // TODO
    }

    /// Helps split this extent in two at the `size` byte boundary.
    #[inline]
    unsafe fn help_split(self: *mut ExtentNode, size: usize) {
        // Get a pointer to the split point of the extent.
        let _next = (self as usize).wrapping_add(size) as *mut ExtentNode;
        // Insert the excess extent back into the free skip lists.
        //(*self).size_list.insert(next, size);
    }
}

impl AddrList {
    /// Returns a pointer to the extent in which this list resides.
    #[inline]
    unsafe fn extent(&self) -> *mut ExtentNode {
        (self as *const AddrList as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Inserts a `node` into this skip list, returning a reference to the base
    /// link of the inserted node, or `nil` if the node was already found in
    /// the list.
    unsafe fn insert(&self, node: AddrNodeRef) -> AddrLinkRef {
        // Find a pair of references to consecutive base links whose extent
        // keys bound the key of the extent of the inserted node. The returned
        // next reference will be nil if the key of the extent of the inserted
        // node is greater than the keys of the extents of all links in the
        // base level list.
        let (mut prev, mut next) = self.search_to_level(node.key(), 0);
        // Check if this node is already in the list.
        if prev.key() == node.key() {
            panic!("duplicate node");
        }
        // Generate a random height for the inserted node.
        let height = self.random_height();
        // Get a pointer to the link at the base level of the inserted node,
        // stealing the given node's reference.
        let base = AddrLinkRef::from_raw((*node.ptr).levels.as_ptr() as *mut AddrLink);
        // Discard the node, whose reference we stole.
        mem::forget(node);
        // Start with the base link.
        let mut link = base.clone();
        // Of the base level list.
        let mut level = 0;
        // Loop until `height` link levels have been inserted into the skip list.
        loop {
            // Insert this node's link for the current level into the level list,
            // between the links that bound the address of the extent of the
            // inserted node..
            let (new_prev, result) = link.clone().insert(prev, next, level);
            // Update to the latest predecessor link.
            prev = new_prev;
            // Check if the insert failed due to a duplicate base link.
            if result.is_nil() && level == 0 {
                // Propagate the insert failure.
                return AddrLinkRef::nil();
            }
            // Check if the inserted base link has already become superfluous.
            if (*base.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                // Check if the current link was inserted, and isn't the base link.
                if result.ptr == link.ptr && link.ptr != base.ptr {
                    // If so, remove the superfluous link.
                    link.remove(prev, level);
                }
                // Return the superfluous base link.
                return base;
            }
            // Increment the link level.
            level = level.wrapping_add(1);
            // Check if the new link level exceeds the chosen height for the
            // inserted node.
            if level > height {
                // All desired levels have been inserted; return the base link.
                return base;
            }
            // Ascend to the next higher level link.
            link.ascend();
            // Find a pair of pointers to consecutive links in the current
            // level whose extent addresses bound the address of the extent of
            // the inserted node. The returned next pointer will be null if the
            // address of the extent of the inserted node is greater than the
            // addresses of the extents of all links in the current level list.
            let (new_prev, new_next) = self.search_to_level(link.key(), level);
            // Recurse into the lower bound link of the current level list.
            prev = new_prev;
            // And the upper bound link of the current level list.
            next = new_next;
        }
    }

    /// Removes the node with the given `key`, and greatest lower bound, from
    /// this skip list, returning a reference to the removed node's base link,
    /// or `nil` if no node with the given `key` was found in the list.
    unsafe fn remove(&self, key: AddrKey, glb: AddrKey) -> AddrLinkRef {
        // Find a pair consecutive base link references whose extent keys bound
        // the `key` of the node to remove, and its greatest lower bound key.
        let (prev, del) = self.search_to_level(glb, 0);
        // Check if the upper bound link doesn't match the node to remove,
        // indicating that the node is not present in the list.
        if del.key() != key {
            // Return nil to indicate that the node was not found.
            return AddrLinkRef::nil();
        }
        // Load the status field of the extent in which this link resides;
        // synchronized by subsequent CAS.
        let mut old_status = (*self.extent()).status.load(Relaxed);
        // Loop until the remove flag is set.
        loop {
            // Set the remove flag on the status field.
            let new_status = old_status | REMOVE_FLAG;
            // Try to update the status field of the extent in which this link resides,
            // synchronizing with other list mutations; linearization point for skip
            // list removal.
            match (*self.extent()).status.compare_exchange(old_status, new_status, SeqCst, Relaxed) {
                // CAS succeeded.
                Ok(_) => break,
                // CAS failed; try again with the latest status.
                Err(status) => old_status = status,
            }
        }
        // Remove the node from the base level list.
        let result = del.remove(prev, 0);
        // Check if the removal of the link from the base level list failed.
        if result.is_nil() {
            // Return nil to indicate that the node was already removed.
            return AddrLinkRef::nil();
        }
        // Delete the links at the higher levels of the node.
        self.search_to_level(key, 1);
        // Return a pointer to the base link of the successfully removed node.
        return result;
    }

    /// Returns two consecutive links in the `target_level` list, with the
    /// extent of the first link having a key less than or equal to the search
    /// `key`, and the extent of the second link having a key strictly greater
    /// than the search `key`. Returns `nil` for the second link if the search
    /// `key` is greater than or equal to the keys of all nodes in the list.
    unsafe fn search_to_level(&self, key: AddrKey, target_level: usize)
        -> (AddrLinkRef, AddrLinkRef)
    {
        // Get the head link and level of the highest level non-empty list
        // whose level is greater than or equal to the target level.
        let (mut link, mut level) = self.find_start(target_level);
        // Search the skip list down to the target level.
        loop {
            // Search for bounding links on the current level.
            let (prev, next) = link.search_right(key, level);
            // Step into the predecessor link on the current level.
            link = prev;
            // Check if we're still above the target level.
            if level > target_level {
                // Descend to the next lower level link.
                link.descend();
                // Decrement the level index.
                level = level.wrapping_sub(1);
            } else {
                // Return bounding links on the target level.
                return (link, next);
            }
        }
    }

    /// Returns the head link and level of the highest level non-empty list
    /// whose level is greater than or equal to the minimum start `level`.
    #[inline]
    unsafe fn find_start(&self, mut level: usize) -> (AddrLinkRef, usize) {
        // Get a pointer to the head link for the minimum start level.
        let mut link = self.head.levels.as_ptr().wrapping_add(level) as *mut AddrLink;
        // Loop until the next higher level list is empty, or the top level is reached.
        loop {
            // Get a pointer to the head link for the next highest level list.
            let up_link = link.wrapping_add(1);
            // Load the address of the next link in the next highest level list,
            // synchronizing with level list mutations.
            let up_next = ((*up_link).succ.load(SeqCst) & ADDR_MASK) as *mut AddrLink;
            // Check if the next highest level list is empty.
            if up_next.is_null() {
                // The current level is the highest non-empty level.
                break;
            }
            // Step to the head link of the next highest level list.
            link = up_link;
            // Increment the level index.
            level = level.wrapping_add(1);
            // Check if the new level is as high as the skip list goes.
            if level == MAX_LEVEL {
                // Can't go any higher.
                break;
            }
        }
        // Acquire a reference the head extent in which this list resides.
        (*self.extent()).refcount.fetch_add(1, Acquire);
        // Return the head link and level of the highest level non-empty list.
        (AddrLinkRef::from_raw(link), level)
    }

    /// Returns a pseudo-random skip node height, ranging from 0 to 31, inclusive.
    unsafe fn random_height(&self) -> usize {
        // Load the current PRNG state.
        let mut old_seed = self.head.seed.load(Relaxed);
        // Loop until a new pseudo-random number is generated.
        loop {
            // Compute the next pseudo-random number, using a simple xorshit PRNG.
            let mut new_seed = old_seed ^ old_seed << 13;
            new_seed = new_seed ^ new_seed >> 17;
            new_seed = new_seed ^ new_seed << 5;
            // Try to update the PRNG state.
            match self.head.seed.compare_exchange_weak(old_seed, new_seed, Relaxed, Relaxed) {
                // CAS succeeded.
                Ok(_) => {
                    // Truncate the pseudo-random number to 32 bits.
                    let seed = new_seed as u32;
                    // Check if the highest and/or lowest bits are unset.
                    if seed & 0x80000001 != 0x80000001 {
                        // Return the highest probability skip node height.
                        return 0;
                    }
                    // Return the number of consecutive trailing zeros, between 1 and 31.
                    return (seed & 0xFFFFFFFE).trailing_zeros() as usize
                },
                // CAS failed; update the PRNG state and try again.
                Err(seed) => old_seed = seed,
            }
        }
    }
}

impl AddrNodeRef {
    /// Returns a new reference to a skip list node from a raw pointer.
    #[inline]
    unsafe fn from_raw(ptr: *mut AddrNode) -> AddrNodeRef {
        AddrNodeRef { ptr: ptr }
    }

    /// Returns `true` if this node is the `nil` end of list sentinel.
    #[inline]
    fn is_nil(&self) -> bool {
        self.ptr.is_null()
    }

    /// Returns a pointer to the extent in which this node resides.
    #[inline]
    fn extent(&self) -> *mut ExtentNode {
        (self.ptr as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Returns the address of the extent in which this node resides.
    #[inline]
    fn addr(&self) -> usize {
        if !self.is_nil() {
            self.extent() as usize
        } else {
            usize::MAX
        }
    }

    /// Returns the sort key used to order this node.
    #[inline]
    fn key(&self) -> AddrKey {
        self.addr()
    }
}

impl Clone for AddrNodeRef {
    #[inline]
    fn clone(&self) -> AddrNodeRef {
        unsafe {
            self.extent().retain();
            AddrNodeRef::from_raw(self.ptr)
        }
    }
}

impl Drop for AddrNodeRef {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.extent().release();
        }
    }
}

impl AddrLinkRef {
    /// Returns the end of list sentinel.
    #[inline]
    fn nil() -> AddrLinkRef {
        AddrLinkRef { ptr: ptr::null_mut() }
    }

    /// Returns a new reference to a skip list link from a raw pointer.
    #[inline]
    unsafe fn from_raw(ptr: *mut AddrLink) -> AddrLinkRef {
        AddrLinkRef { ptr: ptr }
    }

    /// Returns `true` if this is the `nil` end of list sentinel.
    #[inline]
    fn is_nil(&self) -> bool {
        self.ptr.is_null()
    }

    /// Returns a pointer to the extent in which this link resides.
    #[inline]
    fn extent(&self) -> *mut ExtentNode {
        (self.ptr as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Returns the address of the extent in which this link resides.
    #[inline]
    fn addr(&self) -> usize {
        if !self.is_nil() {
            self.extent() as usize
        } else {
            usize::MAX
        }
    }

    /// Returns the sort key used to order this link.
    #[inline]
    fn key(&self) -> AddrKey {
        self.addr()
    }

    /// Returns the greatest key that precedes this link's key.
    #[inline]
    fn glb(&self) -> AddrKey {
        self.addr().wrapping_sub(1)
    }

    /// Updates this reference to point to the node's next higher level link.
    #[inline]
    unsafe fn ascend(&mut self) {
        self.ptr = self.ptr.wrapping_add(1);
    }

    /// Updates this reference to point to the node's next lower level link.
    #[inline]
    unsafe fn descend(&mut self) {
        self.ptr = self.ptr.wrapping_sub(1);
    }

    /// Returns `true` if the node in which this link resides is marked for removal.
    #[inline]
    unsafe fn is_superfluous(&self, level: usize) -> bool {
        !self.is_nil() && (*self.ptr.wrapping_sub(level)).succ.load(SeqCst) & REMOVE_FLAG != 0
    }

    /// Returns the insertion status of the node in which this link resides.
    #[inline]
    unsafe fn status(&self) -> usize {
        if !self.is_nil() {
            (*self.extent()).status.load(Acquire)
        } else {
            0
        }
    }

    /// Returns a reference to the successor of this link, incrementing its
    /// reference count.
    #[inline]
    unsafe fn acquire_next(&self) -> AddrLinkRef {
        // Loop until a reference is acquired to the successor of this link.
        loop {
            // Load the address of the successor to this link in the level list,
            // synchronizing with level list mutations.
            let next = AddrLinkRef::from_raw(((*self.ptr).succ.load(SeqCst) & ADDR_MASK) as *mut AddrLink);
            // Check if the next link is the end of list sentinel.
            if next.is_nil() {
                // Return a reference to the end of list sentinel.
                return next;
            }
            // Increment the reference count of the extent of the next link,
            // synchronizing with reference releases.
            (*next.extent()).refcount.fetch_add(1, Acquire);
            // Check if the successor to this link remains unchanged,
            // synchronizing with level list mutations.
            if next.ptr == ((*self.ptr).succ.load(SeqCst) & ADDR_MASK) as *mut AddrLink {
                // Return the acquired reference to the next link.
                return next;
            } else {
                // Successor changed; release the reference we acquired to the
                // previous successor, and try again.
                mem::drop(next);
            }
        }
    }

    /// Returns a reference to the predecessor of this marked link,
    /// incrementing its reference count.
    #[inline]
    unsafe fn acquire_back(&self) -> AddrLinkRef {
        // Loop until a reference is acquired to the predecessor of this link.
        loop {
            // Load the address of the predecessor to this link in the level list,
            // synchronizing with back reference releases.
            let prev = AddrLinkRef::from_raw((*self.ptr).back.load(Acquire));
            // The back pointer must not be null.
            debug_assert!(!prev.ptr.is_null());
            // The back pointer cannot be null.
            assume(!prev.ptr.is_null());
            // Increment the reference count of the extent of the prev link,
            // synchronizing with reference releases.
            (*prev.extent()).refcount.fetch_add(1, Acquire);
            // Check if the predecessor to this link remains unchanged,
            // synchronizing with back reference releases.
            if prev.ptr == (*self.ptr).back.load(Acquire) {
                // Return the acquired reference to the prev link.
                return prev;
            } else {
                // Successor changed; release the reference we acquired to the
                // previous predecessor, and try again.
                mem::drop(prev);
            }
        }
    }

    /// Returns two consecutive links in the list at `level`, with the extent
    /// of the first link having a key less than or equal to the search key,
    /// and the extent of the second link having a key strictly greater than
    /// the search `key`. Assumes that the address of the extent of this link
    /// precedes the search address.
    unsafe fn search_right(mut self, key: AddrKey, level: usize) -> (AddrLinkRef, AddrLinkRef) {
        // Acquire a reference to the next link in the level list.
        let mut next = self.acquire_next();
        // Loop while the key of the extent of the next link does not exceed the search key.
        while !next.is_nil() && next.key() <= key {
            // Load the insertion status of the extent in which the next link resides.
            let status = next.status();
            // Check if the extent is flagged for merging or splitting.
            if status & MERGE_FLAG != 0 {
                // Help merge the extent in which this link resides with its successor.
                next.extent().help_merge();
            } else if status & SPLIT_FLAG != 0 {
                // Help split the extent in which this link resides at the size boundary
                // enocded in the high order bits of the status field.
                next.extent().help_split(status & SIZE_MASK);
            }
            // Loop while the next node is marked for removal.
            while next.is_superfluous(level) {
                // The next node is marked for removal; try to freeze its predecessor.
                let (prev, result) = next.try_freeze(self, level);
                // Point the current link at the latest predecrssor of the next link.
                self = prev;
                // Check if the current link is frozen.
                if result & FREEZE_FLAG != 0 {
                    // If so, help remove the next link.
                    next.help_freeze(&self);
                }
                // Acquire a reference to the new next link in the level list.
                next = self.acquire_next();
            }
            // Check if the key of the extent of the next link still precedes
            // the search key.
            if next.key() <= key {
                // If so, step into the next link.
                self = next;
                // And acquire a reference to the new current link's successor.
                next = self.acquire_next();
            }
        }
        // The key of the extent of the next link now exceeds the search key;
        // return the consecutive links, which bound the search key.
        (self, next)
    }

    /// Inserts this link into the list at `level`, between the `prev` link and
    /// the `next` link; returns the predecessor to the inserted link, and the
    /// inserted link itself.
    unsafe fn insert(self, mut prev: AddrLinkRef, mut next: AddrLinkRef, level: usize)
        -> (AddrLinkRef, AddrLinkRef)
    {
        // Check if this link is already inserted into the list.
        if prev.key() == self.key() {
            // Duplicate link.
            return (prev, AddrLinkRef::nil());
        }
        // Loop until the link is inserted into the list.
        loop {
            // Load the successor to the predecessor link, synchronizing with
            // list mutations.
            let prev_succ = (*prev.ptr).succ.load(SeqCst);
            // Check if the successor to the predecessor link is frozen.
            if prev_succ & FREEZE_FLAG != 0 {
                // If so, borrow the predecessor's reference to its successor;
                // safe because the predecessor is frozen.
                let succ = AddrLinkRef::from_raw((prev_succ & ADDR_MASK) as *mut AddrLink);
                // Help remove the predecessor's successor.
                succ.help_freeze(&prev);
                // Discard the borrowed successor reference.
                mem::forget(succ);
            } else {
                // Set the successor of this link to the next link in this list;
                // no need to synchronize because this new link is not aliased.
                (*self.ptr).succ.store(next.ptr as usize, Relaxed);
                // Try to set the successor of the predecessor link to this link,
                // synchronizing with list mutations; linearization point for
                // link insertion.
                match (*prev.ptr).succ.compare_exchange(next.ptr as usize,
                                                        self.ptr as usize,
                                                        SeqCst, SeqCst) {
                    // Link successfully inserted; return the new link bounds.
                    Ok(_) => return (prev, self),
                    // Failed to insert link.
                    Err(result) => {
                        // Check if the predecessor link was frozen.
                        if result & FREEZE_FLAG != 0 {
                            // If so, borrow the predecessor's reference to its successor;
                            // safe because the predecessor is frozen.
                            let succ = AddrLinkRef::from_raw((result & ADDR_MASK) as *mut AddrLink);
                            // Help remove the predecessor's successor.
                            succ.help_freeze(&prev);
                            // Discard the borrowed successor reference.
                            mem::forget(succ);
                        }
                        // Loop while the sucessor to the predecessor link is
                        // marked for removal, synchronizing with list mutations.
                        while (*prev.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                            // Acquire a reference to the predecessor of the predecessor link.
                            prev = prev.acquire_back();
                        }
                    },
                }
            }
            // Get the links that bound the search address in the level list.
            let (new_prev, new_next) = prev.search_right(self.key(), level);
            // Update the predecessor link.
            prev = new_prev;
            // Update the successor link.
            next = new_next;
            // Check if this link is already inserted into the list.
            if prev.key() == self.key() {
                // Duplicate link.
                return (prev, AddrLinkRef::nil());
            }
        }
    }

    /// Removes this link from the `level` list, following the `prev` link.
    unsafe fn remove(self, mut prev: AddrLinkRef, level: usize) -> AddrLinkRef {
        // Try to freeze the predecessor of this link.
        let (new_prev, result) = self.try_freeze(prev, level);
        // Update to the latest predecessor of this link.
        prev = new_prev;
        // Check if the predecessor link is frozen.
        if result & FREEZE_FLAG != 0 {
            // If so, help remove this link.
            self.help_freeze(&prev);
        }
        // Check if this link was not in the list.
        if result & REMOVE_FLAG == 0 {
            // Return nil to indicate that no link was removed.
            return AddrLinkRef::nil();
        }
        // Return a pointer to the removed link.
        return self;
    }

    /// Attempts to set the freeze bit on the successor pointer of the predecessor
    /// of this link, freezing the predecessor from concurrent mutation while
    /// the node in which this link resides is removed. Returns the latest
    /// predecessor to this link, and a bit mask with the freeze bit set if the
    /// predecessor is frozen, and with the remove flag set if this operation
    /// is the one that transitioned the predecessor into the frozen state.
    #[inline]
    unsafe fn try_freeze(&self, mut prev: AddrLinkRef, level: usize) -> (AddrLinkRef, usize) {
        loop {
            // Check if the predecessor is already frozen.
            if (*prev.ptr).succ.load(SeqCst) == self.ptr as usize & FREEZE_FLAG {
                // Already frozen.
                return (prev, FREEZE_FLAG);
            }
            // Try to set the freeze bit on the successor pointer of the predecessor link;
            // linearization point for successor removal.
            match (*prev.ptr).succ.compare_exchange(self.ptr as usize,
                                                    self.ptr as usize | FREEZE_FLAG,
                                                    SeqCst, SeqCst) {
                // Successfully frozen.
                Ok(_) => return (prev, FREEZE_FLAG | REMOVE_FLAG),
                // Failed to set freeze bit.
                Err(result) => {
                    // Check if this link is still the successor to the precedessor,
                    // and if the successor pointer to the predecessor link has its
                    // freeze bit set.
                    if result == self.ptr as usize | FREEZE_FLAG {
                        // Predecessor was concurrently frozen; return the address
                        // of the predecessor link, with the freeze bit set.
                        return (prev, FREEZE_FLAG);
                    }
                    // Loop while the sucessor to the predecessor link is
                    // marked for removal, synchronizing with list mutations.
                    while (*prev.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                        // Acquire a reference to the predecessor of the predecessor link.
                        prev = prev.acquire_back();
                    }
                    // Successor to the predecessor concurrently changed;
                    // search for the links that bound the key
                    // "infinitessimally" smaller than this key.
                    let (new_prev, new_next) = prev.search_right(self.glb(), level);
                    // Update to the latest predecessor link.
                    prev = new_prev;
                    // Check if this link is no longer the successor to the
                    // predecessor of this key.
                    if new_next.ptr != self.ptr {
                        // This link was concurrently removed; return the address
                        // of the predecessor link.
                        return (prev, 0);
                    }
                },
            }
        }
    }

    /// Attempts to mark and remove this link, which is a successor of the `prev` link.
    #[inline]
    unsafe fn help_freeze(&self, prev: &AddrLinkRef) {
        // Store a weak reference to the predecessor link in the back pointer
        // of this link, synchronizing with back pointer reads.
        (*self.ptr).back.store(prev.ptr, Release);
        // Check if this link hasn't yet been marked for removal.
        if (*self.ptr).succ.load(SeqCst) & REMOVE_FLAG == 0 {
            // Try to mark this link for removal.
            self.try_remove();
        }
        // Help physically remove this link.
        self.help_remove(prev);
    }

    /// Attempts to mark this link for removal.
    #[inline]
    unsafe fn try_remove(&self) {
        // Loops until this link is marked for removal.
        loop {
            // Load the address of the successor to this link,
            // synchronized by subsequent CAS.
            let next = (*self.ptr).succ.load(Relaxed) & ADDR_MASK;
            // Try to set the remove flag of the successor pointer;
            // linearization point for logical link removal.
            match (*self.ptr).succ.compare_exchange(next, next | REMOVE_FLAG, SeqCst, SeqCst) {
                // Successfully marked.
                Ok(_) => break,
                // Failed to set remove flag.
                Err(result) => {
                    // Check if this link is frozen.
                    if result & FREEZE_FLAG != 0 {
                        // Borrow this link's reference to its successor;
                        // safe because this link is frozen.
                        let succ = AddrLinkRef::from_raw((result & ADDR_MASK) as *mut AddrLink);
                        // Help remove the successor of the successor of this link.
                        succ.acquire_next().help_freeze(self);
                        // Discard the borrowed successor reference.
                        mem::forget(succ);
                    }
                    // Check if this link is marked.
                    if result & REMOVE_FLAG != 0 {
                        // Link already marked for removal.
                        break;
                    }
                },
            }
        }
    }

    /// Attempts to physically remove this link from the list.
    #[inline]
    unsafe fn help_remove(&self, prev: &AddrLinkRef) {
        // Load the successor of this link, synchronizing with list mutation.
        let next = (*self.ptr).succ.load(SeqCst) & ADDR_MASK;
        // Try to set the successor of the predecessor link to the successor of
        // this link; linearization point for physical link removal.
        let _ = (*prev.ptr).succ.compare_exchange(self.ptr as usize | FREEZE_FLAG, next, SeqCst, Relaxed);
    }

    /// Consumes this link reference, returning a raw, uncounted pointer to the
    /// extent in which this link resides.
    unsafe fn into_extent(self) -> *mut ExtentNode {
        self.extent()
    }
}

impl Clone for AddrLinkRef {
    #[inline]
    fn clone(&self) -> AddrLinkRef {
        unsafe {
            self.extent().retain();
            AddrLinkRef::from_raw(self.ptr)
        }
    }
}

impl Drop for AddrLinkRef {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.extent().release();
        }
    }
}

impl SizeList {
    /// Returns a pointer to the extent in which this list resides.
    #[inline]
    unsafe fn extent(&self) -> *mut ExtentNode {
        (self as *const SizeList as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Inserts a `node` into this skip list, returning a reference to the base
    /// link of the inserted node, or `nil` if the node was already found in
    /// the list.
    unsafe fn insert(&self, node: SizeNodeRef) -> SizeLinkRef {
        // Find a pair of references to consecutive base links whose extent
        // keys bound the key of the extent of the inserted node. The returned
        // next reference will be nil if the key of the extent of the inserted
        // node is greater than the keys of the extents of all links in the
        // base level list.
        let (mut prev, mut next) = self.search_to_level(node.key(), 0);
        // Check if this node is already in the list.
        if prev.key() == node.key() {
            panic!("duplicate node");
        }
        // Generate a random height for the inserted node.
        let height = self.random_height();
        // Get a pointer to the link at the base level of the inserted node,
        // stealing the given node's reference.
        let base = SizeLinkRef::from_raw((*node.ptr).levels.as_ptr() as *mut SizeLink);
        // Discard the node, whose reference we stole.
        mem::forget(node);
        // Start with the base link.
        let mut link = base.clone();
        // Of the base level list.
        let mut level = 0;
        // Loop until `height` link levels have been inserted into the skip list.
        loop {
            // Insert this node's link for the current level into the level list,
            // between the links that bound the address of the extent of the
            // inserted node..
            let (new_prev, result) = link.clone().insert(prev, next, level);
            // Update to the latest predecessor link.
            prev = new_prev;
            // Check if the insert failed due to a duplicate base link.
            if result.is_nil() && level == 0 {
                // Propagate the insert failure.
                return SizeLinkRef::nil();
            }
            // Check if the inserted base link has already become superfluous.
            if (*base.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                // Check if the current link was inserted, and isn't the base link.
                if result.ptr == link.ptr && link.ptr != base.ptr {
                    // If so, remove the superfluous link.
                    link.remove(prev, level);
                }
                // Return the superfluous base link.
                return base;
            }
            // Increment the link level.
            level = level.wrapping_add(1);
            // Check if the new link level exceeds the chosen height for the
            // inserted node.
            if level > height {
                // All desired levels have been inserted; return the base link.
                return base;
            }
            // Ascend to the next higher level link.
            link.ascend();
            // Find a pair of pointers to consecutive links in the current
            // level whose extent addresses bound the address of the extent of
            // the inserted node. The returned next pointer will be null if the
            // address of the extent of the inserted node is greater than the
            // addresses of the extents of all links in the current level list.
            let (new_prev, new_next) = self.search_to_level(link.key(), level);
            // Recurse into the lower bound link of the current level list.
            prev = new_prev;
            // And the upper bound link of the current level list.
            next = new_next;
        }
    }

    /// Removes the first node from the least with a key greater than a given
    /// greatest lower bound (`glb`) key, returning a reference to the removed
    /// node's base link, or `nil` of no node with a key greater than `glb` was
    /// found in the list.
    unsafe fn take_next(&self, glb: SizeKey) -> SizeLinkRef {
        loop {
            // Find a pair consecutive base link references whose upper bound key
            // is greater than the given greatest lower bound key.
            let (prev, link) = self.search_to_level(glb, 0);
            // Check if no node was found whose key is greater than the given
            // greatest lower bound key.
            if link.is_nil() {
                // Return nil to indicate that no node was found.
                return link;
            }
            // Load the status field of the extent in which this link resides;
            // synchronized by subsequent CAS.
            let mut old_status = (*self.extent()).status.load(Relaxed);
            // Loop until the remove flag is set.
            loop {
                // Set the remove flag on the status field.
                let new_status = old_status | REMOVE_FLAG;
                // Try to update the status field of the extent in which this link resides,
                // synchronizing with other list mutations; linearization point for skip
                // list removal.
                match (*self.extent()).status.compare_exchange(old_status, new_status, SeqCst, Relaxed) {
                    // CAS succeeded.
                    Ok(_) => break,
                    // CAS failed; try again with the latest status.
                    Err(status) => old_status = status,
                }
            }
            // Remove the node from the base level list.
            let result = link.remove(prev, 0);
            // Check if the removal of the link from the base level list failed.
            if result.is_nil() {
                // The node was concurrently removed; try again.
                continue;
            }
            // Delete the links at the higher levels of the node.
            self.search_to_level(result.key(), 1);
            // Return a pointer to the base link of the successfully removed node.
            return result;
        }
    }

    /// Returns two consecutive links in the `target_level` list, with the
    /// extent of the first link having a key less than or equal to the search
    /// `key`, and the extent of the second link having a key strictly greater
    /// than the search `key`. Returns `nil` for the second link if the search
    /// `key` is greater than or equal to the keys of all nodes in the list.
    unsafe fn search_to_level(&self, key: SizeKey, target_level: usize)
        -> (SizeLinkRef, SizeLinkRef)
    {
        // Get the head link and level of the highest level non-empty list
        // whose level is greater than or equal to the target level.
        let (mut link, mut level) = self.find_start(target_level);
        // Search the skip list down to the target level.
        loop {
            // Search for bounding links on the current level.
            let (prev, next) = link.search_right(key, level);
            // Step into the predecessor link on the current level.
            link = prev;
            // Check if we're still above the target level.
            if level > target_level {
                // Descend to the next lower level link.
                link.descend();
                // Decrement the level index.
                level = level.wrapping_sub(1);
            } else {
                // Return bounding links on the target level.
                return (link, next);
            }
        }
    }

    /// Returns the head link and level of the highest level non-empty list
    /// whose level is greater than or equal to the minimum start `level`.
    #[inline]
    unsafe fn find_start(&self, mut level: usize) -> (SizeLinkRef, usize) {
        // Get a pointer to the head link for the minimum start level.
        let mut link = self.head.levels.as_ptr().wrapping_add(level) as *mut SizeLink;
        // Loop until the next higher level list is empty, or the top level is reached.
        loop {
            // Get a pointer to the head link for the next highest level list.
            let up_link = link.wrapping_add(1);
            // Load the address of the next link in the next highest level list,
            // synchronizing with level list mutations.
            let up_next = ((*up_link).succ.load(SeqCst) & ADDR_MASK) as *mut SizeLink;
            // Check if the next highest level list is empty.
            if up_next.is_null() {
                // The current level is the highest non-empty level.
                break;
            }
            // Step to the head link of the next highest level list.
            link = up_link;
            // Increment the level index.
            level = level.wrapping_add(1);
            // Check if the new level is as high as the skip list goes.
            if level == MAX_LEVEL {
                // Can't go any higher.
                break;
            }
        }
        // Acquire a reference the head extent in which this list resides.
        (*self.extent()).refcount.fetch_add(1, Acquire);
        // Return the head link and level of the highest level non-empty list.
        (SizeLinkRef::from_raw(link), level)
    }

    /// Returns a pseudo-random skip node height, ranging from 0 to 31, inclusive.
    unsafe fn random_height(&self) -> usize {
        // Load the current PRNG state.
        let mut old_seed = self.head.seed.load(Relaxed);
        // Loop until a new pseudo-random number is generated.
        loop {
            // Compute the next pseudo-random number, using a simple xorshit PRNG.
            let mut new_seed = old_seed ^ old_seed << 13;
            new_seed = new_seed ^ new_seed >> 17;
            new_seed = new_seed ^ new_seed << 5;
            // Try to update the PRNG state.
            match self.head.seed.compare_exchange_weak(old_seed, new_seed, Relaxed, Relaxed) {
                // CAS succeeded.
                Ok(_) => {
                    // Truncate the pseudo-random number to 32 bits.
                    let seed = new_seed as u32;
                    // Check if the highest and/or lowest bits are unset.
                    if seed & 0x80000001 != 0x80000001 {
                        // Return the highest probability skip node height.
                        return 0;
                    }
                    // Return the number of consecutive trailing zeros, between 1 and 31.
                    return (seed & 0xFFFFFFFE).trailing_zeros() as usize
                },
                // CAS failed; update the PRNG state and try again.
                Err(seed) => old_seed = seed,
            }
        }
    }
}

impl SizeNodeRef {
    /// Returns a new reference to a skip list node from a raw pointer.
    #[inline]
    unsafe fn from_raw(ptr: *mut SizeNode) -> SizeNodeRef {
        SizeNodeRef { ptr: ptr }
    }

    /// Returns a pointer to the extent in which this node resides.
    #[inline]
    fn extent(&self) -> *mut ExtentNode {
        (self.ptr as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Returns the sort key used to order this node.
    #[inline]
    fn key(&self) -> SizeKey {
        unsafe {
            let extent = self.extent();
            if !extent.is_null() {
                ((*extent).size, extent as usize)
            } else {
                (usize::MAX, usize::MAX)
            }
        }
    }
}

impl Clone for SizeNodeRef {
    #[inline]
    fn clone(&self) -> SizeNodeRef {
        unsafe {
            self.extent().retain();
            SizeNodeRef::from_raw(self.ptr)
        }
    }
}

impl Drop for SizeNodeRef {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.extent().release();
        }
    }
}

impl SizeLinkRef {
    /// Returns the end of list sentinel.
    #[inline]
    fn nil() -> SizeLinkRef {
        SizeLinkRef { ptr: ptr::null_mut() }
    }

    /// Returns a new reference to a skip list link from a raw pointer.
    #[inline]
    unsafe fn from_raw(ptr: *mut SizeLink) -> SizeLinkRef {
        SizeLinkRef { ptr: ptr }
    }

    /// Updates this reference to point to the node's next higher level link.
    #[inline]
    unsafe fn ascend(&mut self) {
        self.ptr = self.ptr.wrapping_add(1);
    }

    /// Updates this reference to point to the node's next lower level link.
    #[inline]
    unsafe fn descend(&mut self) {
        self.ptr = self.ptr.wrapping_sub(1);
    }

    /// Returns `true` if this is the `nil` end of list sentinel.
    #[inline]
    fn is_nil(&self) -> bool {
        self.ptr.is_null()
    }

    /// Returns a pointer to the extent in which this link resides.
    #[inline]
    fn extent(&self) -> *mut ExtentNode {
        (self.ptr as usize & EXTENT_ADDR_MASK) as *mut ExtentNode
    }

    /// Returns the address of the extent in which this link resides.
    #[inline]
    fn addr(&self) -> usize {
        if !self.is_nil() {
            self.extent() as usize
        } else {
            usize::MAX
        }
    }

    /// Returns the sort key used to order this link.
    #[inline]
    fn key(&self) -> (usize, usize) {
        unsafe {
            let extent = self.extent();
            if !extent.is_null() {
                ((*extent).size, extent as usize)
            } else {
                (usize::MAX, usize::MAX)
            }
        }
    }

    /// Returns the greatest key that precedes this link's key.
    #[inline]
    fn glb(&self) -> (usize, usize) {
        unsafe {
            let extent = self.extent();
            if !extent.is_null() {
                ((*extent).size.wrapping_sub(1), extent as usize)
            } else {
                (usize::MAX, usize::MAX)
            }
        }
    }

    /// Returns `true` if the node in which this link resides is marked for removal.
    #[inline]
    unsafe fn is_superfluous(&self, level: usize) -> bool {
        !self.is_nil() && (*self.ptr.wrapping_sub(level)).succ.load(SeqCst) & REMOVE_FLAG != 0
    }

    /// Returns the insertion status of the node in which this link resides.
    #[inline]
    unsafe fn status(&self) -> usize {
        if !self.is_nil() {
            (*self.extent()).status.load(Acquire)
        } else {
            0
        }
    }

    /// Returns a reference to the successor of this link, incrementing its
    /// reference count.
    #[inline]
    unsafe fn acquire_next(&self) -> SizeLinkRef {
        // Loop until a reference is acquired to the successor of this link.
        loop {
            // Load the address of the successor to this link in the level list,
            // synchronizing with level list mutations.
            let next = SizeLinkRef::from_raw(((*self.ptr).succ.load(SeqCst) & ADDR_MASK) as *mut SizeLink);
            // Check if the next link is the end of list sentinel.
            if next.is_nil() {
                // Return a reference to the end of list sentinel.
                return next;
            }
            // Increment the reference count of the extent of the next link,
            // synchronizing with reference releases.
            (*next.extent()).refcount.fetch_add(1, Acquire);
            // Check if the successor to this link remains unchanged,
            // synchronizing with level list mutations.
            if next.ptr == ((*self.ptr).succ.load(SeqCst) & ADDR_MASK) as *mut SizeLink {
                // Return the acquired reference to the next link.
                return next;
            } else {
                // Successor changed; release the reference we acquired to the
                // previous successor, and try again.
                mem::drop(next);
            }
        }
    }

    /// Returns a reference to the predecessor of this marked link,
    /// incrementing its reference count.
    #[inline]
    unsafe fn acquire_back(&self) -> SizeLinkRef {
        // Loop until a reference is acquired to the predecessor of this link.
        loop {
            // Load the address of the predecessor to this link in the level list,
            // synchronizing with back reference releases.
            let prev = SizeLinkRef::from_raw((*self.ptr).back.load(Acquire));
            // The back pointer must not be null.
            debug_assert!(!prev.ptr.is_null());
            // The back pointer cannot be null.
            assume(!prev.ptr.is_null());
            // Increment the reference count of the extent of the prev link,
            // synchronizing with reference releases.
            (*prev.extent()).refcount.fetch_add(1, Acquire);
            // Check if the predecessor to this link remains unchanged,
            // synchronizing with back reference releases.
            if prev.ptr == (*self.ptr).back.load(Acquire) {
                // Return the acquired reference to the prev link.
                return prev;
            } else {
                // Successor changed; release the reference we acquired to the
                // previous predecessor, and try again.
                mem::drop(prev);
            }
        }
    }

    /// Returns two consecutive links in the list at `level`, with the extent
    /// of the first link having a key less than or equal to the search key,
    /// and the extent of the second link having a key strictly greater than
    /// the search `key`. Assumes that the address of the extent of this link
    /// precedes the search address.
    unsafe fn search_right(mut self, key: SizeKey, level: usize) -> (SizeLinkRef, SizeLinkRef) {
        // Acquire a reference to the next link in the level list.
        let mut next = self.acquire_next();
        // Loop while the key of the extent of the next link does not exceed the search key.
        while !next.is_nil() && next.key() <= key {
            // Load the insertion status of the extent in which the next link resides.
            let status = next.status();
            // Check if the extent is flagged for merging or splitting.
            if status & MERGE_FLAG != 0 {
                // Help merge the extent in which this link resides with its successor.
                next.extent().help_merge();
            } else if status & SPLIT_FLAG != 0 {
                // Help split the extent in which this link resides at the size boundary
                // enocded in the high order bits of the status field.
                next.extent().help_split(status & SIZE_MASK);
            }
            // Loop while the next node is marked for removal.
            while next.is_superfluous(level) {
                // The next node is marked for removal; try to freeze its predecessor.
                let (prev, result) = next.try_freeze(self, level);
                // Point the current link at the latest predecrssor of the next link.
                self = prev;
                // Check if the current link is frozen.
                if result & FREEZE_FLAG != 0 {
                    // If so, help remove the next link.
                    next.help_freeze(&self);
                }
                // Acquire a reference to the new next link in the level list.
                next = self.acquire_next();
            }
            // Check if the key of the extent of the next link still precedes
            // the search key.
            if next.key() <= key {
                // If so, step into the next link.
                self = next;
                // And acquire a reference to the new current link's successor.
                next = self.acquire_next();
            }
        }
        // The key of the extent of the next link now exceeds the search key;
        // return the consecutive links, which bound the search key.
        (self, next)
    }

    /// Inserts this link into the list at `level`, between the `prev` link and
    /// the `next` link; returns the predecessor to the inserted link, and the
    /// inserted link itself.
    unsafe fn insert(self, mut prev: SizeLinkRef, mut next: SizeLinkRef, level: usize)
        -> (SizeLinkRef, SizeLinkRef)
    {
        // Check if this link is already inserted into the list.
        if prev.key() == self.key() {
            // Duplicate link.
            return (prev, SizeLinkRef::nil());
        }
        // Loop until the link is inserted into the list.
        loop {
            // Load the successor to the predecessor link, synchronizing with
            // list mutations.
            let prev_succ = (*prev.ptr).succ.load(SeqCst);
            // Check if the successor to the predecessor link is frozen.
            if prev_succ & FREEZE_FLAG != 0 {
                // If so, borrow the predecessor's reference to its successor;
                // safe because the predecessor is frozen.
                let succ = SizeLinkRef::from_raw((prev_succ & ADDR_MASK) as *mut SizeLink);
                // Help remove the predecessor's successor.
                succ.help_freeze(&prev);
                // Discard the borrowed successor reference.
                mem::forget(succ);
            } else {
                // Set the successor of this link to the next link in this list;
                // no need to synchronize because this new link is not aliased.
                (*self.ptr).succ.store(next.ptr as usize, Relaxed);
                // Try to set the successor of the predecessor link to this link,
                // synchronizing with list mutations; linearization point for
                // link insertion.
                match (*prev.ptr).succ.compare_exchange(next.ptr as usize,
                                                        self.ptr as usize,
                                                        SeqCst, SeqCst) {
                    // Link successfully inserted; return the new link bounds.
                    Ok(_) => return (prev, self),
                    // Failed to insert link.
                    Err(result) => {
                        // Check if the predecessor link was frozen.
                        if result & FREEZE_FLAG != 0 {
                            // If so, borrow the predecessor's reference to its successor;
                            // safe because the predecessor is frozen.
                            let succ = SizeLinkRef::from_raw((result & ADDR_MASK) as *mut SizeLink);
                            // Help remove the predecessor's successor.
                            succ.help_freeze(&prev);
                            // Discard the borrowed successor reference.
                            mem::forget(succ);
                        }
                        // Loop while the sucessor to the predecessor link is
                        // marked for removal, synchronizing with list mutations.
                        while (*prev.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                            // Acquire a reference to the predecessor of the predecessor link.
                            prev = prev.acquire_back();
                        }
                    },
                }
            }
            // Get the links that bound the search address in the level list.
            let (new_prev, new_next) = prev.search_right(self.key(), level);
            // Update the predecessor link.
            prev = new_prev;
            // Update the successor link.
            next = new_next;
            // Check if this link is already inserted into the list.
            if prev.key() == self.key() {
                // Duplicate link.
                return (prev, SizeLinkRef::nil());
            }
        }
    }

    /// Removes this link from the `level` list, following the `prev` link.
    unsafe fn remove(self, mut prev: SizeLinkRef, level: usize) -> SizeLinkRef {
        // Try to freeze the predecessor of this link.
        let (new_prev, result) = self.try_freeze(prev, level);
        // Update to the latest predecessor of this link.
        prev = new_prev;
        // Check if the predecessor link is frozen.
        if result & FREEZE_FLAG != 0 {
            // If so, help remove this link.
            self.help_freeze(&prev);
        }
        // Check if this link was not in the list.
        if result & REMOVE_FLAG == 0 {
            // Return nil to indicate that no link was removed.
            return SizeLinkRef::nil();
        }
        // Return a pointer to the removed link.
        return self;
    }

    /// Attempts to set the freeze bit on the successor pointer of the predecessor
    /// of this link, freezing the predecessor from concurrent mutation while
    /// the node in which this link resides is removed. Returns the latest
    /// predecessor to this link, and a bit mask with the freeze bit set if the
    /// predecessor is frozen, and with the remove flag set if this operation
    /// is the one that transitioned the predecessor into the frozen state.
    #[inline]
    unsafe fn try_freeze(&self, mut prev: SizeLinkRef, level: usize) -> (SizeLinkRef, usize) {
        loop {
            // Check if the predecessor is already frozen.
            if (*prev.ptr).succ.load(SeqCst) == self.ptr as usize & FREEZE_FLAG {
                // Already frozen.
                return (prev, FREEZE_FLAG);
            }
            // Try to set the freeze bit on the successor pointer of the predecessor link;
            // linearization point for successor removal.
            match (*prev.ptr).succ.compare_exchange(self.ptr as usize,
                                                    self.ptr as usize | FREEZE_FLAG,
                                                    SeqCst, SeqCst) {
                // Successfully frozen.
                Ok(_) => return (prev, FREEZE_FLAG | REMOVE_FLAG),
                // Failed to set freeze bit.
                Err(result) => {
                    // Check if this link is still the successor to the precedessor,
                    // and if the successor pointer to the predecessor link has its
                    // freeze bit set.
                    if result == self.ptr as usize | FREEZE_FLAG {
                        // Predecessor was concurrently frozen; return the address
                        // of the predecessor link, with the freeze bit set.
                        return (prev, FREEZE_FLAG);
                    }
                    // Loop while the sucessor to the predecessor link is
                    // marked for removal, synchronizing with list mutations.
                    while (*prev.ptr).succ.load(SeqCst) & REMOVE_FLAG != 0 {
                        // Acquire a reference to the predecessor of the predecessor link.
                        prev = prev.acquire_back();
                    }
                    // Successor to the predecessor concurrently changed;
                    // search for the links that bound the key
                    // "infinitessimally" smaller than this key.
                    let (new_prev, new_next) = prev.search_right(self.glb(), level);
                    // Update to the latest predecessor link.
                    prev = new_prev;
                    // Check if this link is no longer the successor to the
                    // predecessor of this key.
                    if new_next.ptr != self.ptr {
                        // This link was concurrently removed; return the address
                        // of the predecessor link.
                        return (prev, 0);
                    }
                },
            }
        }
    }

    /// Attempts to mark and remove this link, which is a successor of the `prev` link.
    #[inline]
    unsafe fn help_freeze(&self, prev: &SizeLinkRef) {
        // Store a weak reference to the predecessor link in the back pointer
        // of this link, synchronizing with back pointer reads.
        (*self.ptr).back.store(prev.ptr, Release);
        // Check if this link hasn't yet been marked for removal.
        if (*self.ptr).succ.load(SeqCst) & REMOVE_FLAG == 0 {
            // Try to mark this link for removal.
            self.try_remove();
        }
        // Help physically remove this link.
        self.help_remove(prev);
    }

    /// Attempts to mark this link for removal.
    #[inline]
    unsafe fn try_remove(&self) {
        // Loops until this link is marked for removal.
        loop {
            // Load the address of the successor to this link,
            // synchronized by subsequent CAS.
            let next = (*self.ptr).succ.load(Relaxed) & ADDR_MASK;
            // Try to set the remove flag of the successor pointer;
            // linearization point for logical link removal.
            match (*self.ptr).succ.compare_exchange(next, next | REMOVE_FLAG, SeqCst, SeqCst) {
                // Successfully marked.
                Ok(_) => break,
                // Failed to set remove flag.
                Err(result) => {
                    // Check if this link is frozen.
                    if result & FREEZE_FLAG != 0 {
                        // Borrow this link's reference to its successor;
                        // safe because this link is frozen.
                        let succ = SizeLinkRef::from_raw((result & ADDR_MASK) as *mut SizeLink);
                        // Help remove the successor of the successor of this link.
                        succ.acquire_next().help_freeze(self);
                        // Discard the borrowed successor reference.
                        mem::forget(succ);
                    }
                    // Check if this link is marked.
                    if result & REMOVE_FLAG != 0 {
                        // Link already marked for removal.
                        break;
                    }
                },
            }
        }
    }

    /// Attempts to physically remove this link from the list.
    #[inline]
    unsafe fn help_remove(&self, prev: &SizeLinkRef) {
        // Load the successor of this link, synchronizing with list mutation.
        let next = (*self.ptr).succ.load(SeqCst) & ADDR_MASK;
        // Try to set the successor of the predecessor link to the successor of
        // this link; linearization point for physical link removal.
        let _ = (*prev.ptr).succ.compare_exchange(self.ptr as usize | FREEZE_FLAG, next, SeqCst, Relaxed);
    }
}

impl Clone for SizeLinkRef {
    #[inline]
    fn clone(&self) -> SizeLinkRef {
        unsafe {
            self.extent().retain();
            SizeLinkRef::from_raw(self.ptr)
        }
    }
}

impl Drop for SizeLinkRef {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.extent().release();
        }
    }
}