btrfs-transaction 0.13.0

Userspace transaction infrastructure for modifying btrfs filesystems
Documentation
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
//! # Free space tree range tracking and apply
//!
//! Helpers for tracking per-block-group byte range deltas during a
//! transaction and applying them to the free space tree (FST).
//!
//! Three layers, all wired together by `Transaction::commit`'s
//! convergence loop:
//!
//! 1. Pure data types and coalescing/cancellation helpers
//!    ([`Range`], [`BlockGroupRangeDeltas`]) accumulate the
//!    allocated and freed ranges produced during a transaction.
//!    Add / drop pairs to the same range cancel.
//! 2. The pure delta-apply step folds those deltas into a
//!    per-block-group free-range list.
//! 3. The on-disk FST update step (driven from
//!    `Transaction::update_free_space_tree`) deletes and
//!    re-inserts the matching `FREE_SPACE_EXTENT` items and
//!    bumps `FREE_SPACE_INFO.extent_count`. Bitmap-layout block
//!    groups are refused with a clear error.
//!
//! The shape of a "range" in this module is `(start, length)` in
//! bytes, both `u64`. Ranges always live within a single block
//! group and `start` is the absolute logical address (not
//! block-group relative).

use std::collections::BTreeMap;

/// A half-open byte range `[start, start + length)` within a block group.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Range {
    pub start: u64,
    pub length: u64,
}

impl Range {
    #[must_use]
    pub fn new(start: u64, length: u64) -> Self {
        Self { start, length }
    }

    #[must_use]
    pub fn end(self) -> u64 {
        self.start + self.length
    }

    #[must_use]
    pub fn is_empty(self) -> bool {
        self.length == 0
    }
}

/// A sorted, coalesced list of disjoint byte ranges.
///
/// Invariants maintained after every mutating operation:
///
/// - Ranges are sorted by `start`.
/// - No two ranges overlap or touch (adjacent ranges are merged).
/// - No zero-length ranges.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RangeList {
    ranges: Vec<Range>,
}

impl RangeList {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn from_sorted_unchecked(ranges: Vec<Range>) -> Self {
        Self { ranges }
    }

    #[must_use]
    pub fn as_slice(&self) -> &[Range] {
        &self.ranges
    }

    #[must_use]
    pub fn into_vec(self) -> Vec<Range> {
        self.ranges
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.ranges.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.ranges.is_empty()
    }

    /// Insert a range, merging with any existing range it overlaps or
    /// touches.
    pub fn insert(&mut self, range: Range) {
        if range.is_empty() {
            return;
        }
        // Find the first existing range whose end is >= range.start.
        // That is the first range that could possibly merge with ours
        // (touching counts as merge: end == range.start).
        let mut start = range.start;
        let mut end = range.end();

        // Index of the first range that could merge.
        let first = self
            .ranges
            .iter()
            .position(|r| r.end() >= start)
            .unwrap_or(self.ranges.len());

        // Index one past the last range that merges. A range merges if
        // its start <= end.
        let mut last = first;
        while last < self.ranges.len() && self.ranges[last].start <= end {
            start = start.min(self.ranges[last].start);
            end = end.max(self.ranges[last].end());
            last += 1;
        }

        // Replace [first..last) with the merged range.
        let merged = Range::new(start, end - start);
        self.ranges.splice(first..last, std::iter::once(merged));
    }

    /// Subtract a range from the list. The result still satisfies the
    /// invariants.
    ///
    /// If the subtracted range is not fully covered by existing ranges,
    /// the uncovered portions are silently ignored — callers that want
    /// to detect this should use the F2 apply function which performs
    /// extent-tree consistency checks.
    pub fn subtract(&mut self, range: Range) {
        if range.is_empty() {
            return;
        }
        let sub_start = range.start;
        let sub_end = range.end();

        let mut i = 0;
        while i < self.ranges.len() {
            let r = self.ranges[i];
            if r.end() <= sub_start {
                i += 1;
                continue;
            }
            if r.start >= sub_end {
                break;
            }
            // r overlaps [sub_start, sub_end). Compute remainder pieces.
            let left = if r.start < sub_start {
                Some(Range::new(r.start, sub_start - r.start))
            } else {
                None
            };
            let right = if r.end() > sub_end {
                Some(Range::new(sub_end, r.end() - sub_end))
            } else {
                None
            };
            // Replace r with left/right pieces.
            match (left, right) {
                (None, None) => {
                    self.ranges.remove(i);
                }
                (Some(l), None) => {
                    self.ranges[i] = l;
                    i += 1;
                }
                (None, Some(rt)) => {
                    self.ranges[i] = rt;
                    i += 1;
                }
                (Some(l), Some(rt)) => {
                    self.ranges[i] = l;
                    self.ranges.insert(i + 1, rt);
                    i += 2;
                }
            }
        }
    }

    /// Total number of bytes covered by the list.
    #[must_use]
    pub fn total_bytes(&self) -> u64 {
        self.ranges.iter().map(|r| r.length).sum()
    }
}

/// Per-block-group accumulator of allocated and freed ranges produced
/// during a single transaction commit.
///
/// Each block group is keyed by its start address (the same key used
/// for `BLOCK_GROUP_ITEM`). Within each block group, allocated and
/// freed ranges are kept in sorted, coalesced lists. A range that
/// appears in *both* lists is removed from both: it was allocated and
/// freed within the same transaction and the FST should see neither.
#[derive(Debug, Default, Clone)]
pub struct BlockGroupRangeDeltas {
    bgs: BTreeMap<u64, BlockGroupDelta>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct BlockGroupDelta {
    pub allocated: RangeList,
    pub freed: RangeList,
}

impl BlockGroupRangeDeltas {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn record_allocated(&mut self, bg_start: u64, range: Range) {
        let entry = self.bgs.entry(bg_start).or_default();
        entry.allocated.insert(range);
    }

    pub fn record_freed(&mut self, bg_start: u64, range: Range) {
        let entry = self.bgs.entry(bg_start).or_default();
        entry.freed.insert(range);
    }

    /// Cancel any range that appears in both `allocated` and `freed`
    /// for the same block group. Operates on each block group
    /// independently. Idempotent.
    pub fn cancel_within_transaction(&mut self) {
        for delta in self.bgs.values_mut() {
            // Walk the freed list and subtract any portion that is also
            // in the allocated list, then do the symmetric subtraction.
            // This handles partial overlap correctly: only the
            // overlapping bytes are removed from each side.
            let to_cancel: Vec<Range> = delta.allocated.as_slice().to_vec();
            for r in to_cancel {
                // Compute the intersection between r and the freed
                // list, then subtract that intersection from both.
                let intersected = intersect(&delta.freed, r);
                for piece in intersected {
                    delta.allocated.subtract(piece);
                    delta.freed.subtract(piece);
                }
            }
        }
        // Drop empty block groups.
        self.bgs
            .retain(|_, d| !(d.allocated.is_empty() && d.freed.is_empty()));
    }

    pub fn iter(&self) -> impl Iterator<Item = (&u64, &BlockGroupDelta)> {
        self.bgs.iter()
    }

    #[must_use]
    pub fn get(&self, bg_start: u64) -> Option<&BlockGroupDelta> {
        self.bgs.get(&bg_start)
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.bgs.is_empty()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.bgs.len()
    }

    pub fn clear(&mut self) {
        self.bgs.clear();
    }
}

/// Errors produced by [`apply_delta`] when the proposed delta is
/// inconsistent with the existing free-range list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplyError {
    /// The block group's `FREE_SPACE_INFO` item has the BITMAPS flag
    /// set. Bitmap layout is out of scope for v1; the caller must
    /// detect this before computing the delta.
    BitmapLayout { bg_start: u64 },
    /// An allocated range is not fully contained inside an existing
    /// free range. The FST and the extent tree disagree about who owns
    /// these bytes.
    AllocatedNotFree { bg_start: u64, range: Range },
    /// A freed range overlaps a range that the FST already considers
    /// free. The same byte was freed twice.
    FreedAlreadyFree { bg_start: u64, range: Range },
    /// A range in the resulting free list lies outside the block group
    /// span.
    OutOfBlockGroup { bg_start: u64, range: Range },
}

impl std::fmt::Display for ApplyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BitmapLayout { bg_start } => write!(
                f,
                "free space tree block group {bg_start} uses bitmap layout (unsupported in v1)"
            ),
            Self::AllocatedNotFree { bg_start, range } => write!(
                f,
                "allocated range {}..{} in block group {bg_start} is not contained in any free extent",
                range.start,
                range.end()
            ),
            Self::FreedAlreadyFree { bg_start, range } => write!(
                f,
                "freed range {}..{} in block group {bg_start} overlaps an existing free extent",
                range.start,
                range.end()
            ),
            Self::OutOfBlockGroup { bg_start, range } => write!(
                f,
                "resulting free range {}..{} lies outside block group {bg_start}",
                range.start,
                range.end()
            ),
        }
    }
}

impl std::error::Error for ApplyError {}

/// Apply a per-block-group delta to an existing free-range list,
/// producing the new free-range list.
///
/// `existing` is the current set of `FREE_SPACE_EXTENT` ranges for the
/// block group, sorted and coalesced. `delta` is the set of byte
/// ranges allocated and freed during the current transaction (already
/// passed through `cancel_within_transaction`). `bg` is the block
/// group's full extent (start + length) and is used to validate the
/// result.
///
/// Returns the new free-range list or an [`ApplyError`] if the delta
/// is inconsistent with the existing state.
///
/// This function is pure: it does not read or write the on-disk FST.
/// Stage F3 calls it from the commit path with input read out of the
/// FST and writes the output back.
pub fn apply_delta(
    bg_start: u64,
    bg: Range,
    existing: &RangeList,
    delta: &BlockGroupDelta,
) -> Result<RangeList, ApplyError> {
    let mut out = existing.clone();

    // Allocated ranges: each must be fully contained in some existing
    // free range. Subtract from the running list.
    for &alloc in delta.allocated.as_slice() {
        if !out.contains(alloc) {
            return Err(ApplyError::AllocatedNotFree {
                bg_start,
                range: alloc,
            });
        }
        out.subtract(alloc);
    }

    // Freed ranges: each must NOT overlap any existing free range
    // (after allocations have been subtracted). Insert into the
    // running list.
    for &freed in delta.freed.as_slice() {
        if out.overlaps(freed) {
            return Err(ApplyError::FreedAlreadyFree {
                bg_start,
                range: freed,
            });
        }
        out.insert(freed);
    }

    // Bound check: every range must lie inside the block group.
    let bg_end = bg.end();
    for &r in out.as_slice() {
        if r.start < bg.start || r.end() > bg_end {
            return Err(ApplyError::OutOfBlockGroup { bg_start, range: r });
        }
    }

    Ok(out)
}

impl RangeList {
    /// True if `range` is fully contained within a single existing
    /// range in this list.
    #[must_use]
    pub fn contains(&self, range: Range) -> bool {
        if range.is_empty() {
            return true;
        }
        self.ranges
            .iter()
            .any(|r| r.start <= range.start && r.end() >= range.end())
    }

    /// True if `range` overlaps any existing range. Touching does not
    /// count as overlap (`[100, 110)` does not overlap `[110, 120)`).
    #[must_use]
    pub fn overlaps(&self, range: Range) -> bool {
        if range.is_empty() {
            return false;
        }
        self.ranges
            .iter()
            .any(|r| r.start < range.end() && range.start < r.end())
    }
}

/// Intersect a range with a range list, returning the per-piece
/// intersections (sorted, disjoint).
fn intersect(list: &RangeList, range: Range) -> Vec<Range> {
    let mut out = Vec::new();
    if range.is_empty() {
        return out;
    }
    let r_start = range.start;
    let r_end = range.end();
    for piece in list.as_slice() {
        if piece.end() <= r_start {
            continue;
        }
        if piece.start >= r_end {
            break;
        }
        let s = piece.start.max(r_start);
        let e = piece.end().min(r_end);
        if e > s {
            out.push(Range::new(s, e - s));
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rl(items: &[(u64, u64)]) -> RangeList {
        let mut l = RangeList::new();
        for &(s, n) in items {
            l.insert(Range::new(s, n));
        }
        l
    }

    fn collect(l: &RangeList) -> Vec<(u64, u64)> {
        l.as_slice().iter().map(|r| (r.start, r.length)).collect()
    }

    #[test]
    fn insert_into_empty() {
        let l = rl(&[(100, 10)]);
        assert_eq!(collect(&l), &[(100, 10)]);
    }

    #[test]
    fn insert_disjoint_sorted() {
        let l = rl(&[(100, 10), (200, 10), (50, 5)]);
        assert_eq!(collect(&l), &[(50, 5), (100, 10), (200, 10)]);
    }

    #[test]
    fn insert_merges_touching() {
        // 100..110 and 110..120 touch -> merge to 100..120
        let l = rl(&[(100, 10), (110, 10)]);
        assert_eq!(collect(&l), &[(100, 20)]);
    }

    #[test]
    fn insert_merges_overlapping() {
        let l = rl(&[(100, 20), (110, 20)]);
        assert_eq!(collect(&l), &[(100, 30)]);
    }

    #[test]
    fn insert_merges_chain() {
        // Insert spanning two existing ranges so they all merge.
        let mut l = rl(&[(100, 10), (200, 10)]);
        l.insert(Range::new(105, 100)); // 105..205
        assert_eq!(collect(&l), &[(100, 110)]); // 100..210
    }

    #[test]
    fn insert_zero_length_noop() {
        let mut l = rl(&[(100, 10)]);
        l.insert(Range::new(150, 0));
        assert_eq!(collect(&l), &[(100, 10)]);
    }

    #[test]
    fn subtract_whole_range() {
        let mut l = rl(&[(100, 10)]);
        l.subtract(Range::new(100, 10));
        assert!(l.is_empty());
    }

    #[test]
    fn subtract_left_edge() {
        let mut l = rl(&[(100, 10)]);
        l.subtract(Range::new(100, 4));
        assert_eq!(collect(&l), &[(104, 6)]);
    }

    #[test]
    fn subtract_right_edge() {
        let mut l = rl(&[(100, 10)]);
        l.subtract(Range::new(106, 4));
        assert_eq!(collect(&l), &[(100, 6)]);
    }

    #[test]
    fn subtract_middle_splits() {
        let mut l = rl(&[(100, 10)]);
        l.subtract(Range::new(103, 4));
        assert_eq!(collect(&l), &[(100, 3), (107, 3)]);
    }

    #[test]
    fn subtract_spanning_multiple_ranges() {
        let mut l = rl(&[(100, 10), (200, 10), (300, 10)]);
        l.subtract(Range::new(105, 200));
        // 100..105 remains; 200..210 fully removed; 300..305 removed,
        // 305..310 remains.
        assert_eq!(collect(&l), &[(100, 5), (305, 5)]);
    }

    #[test]
    fn subtract_outside_noop() {
        let mut l = rl(&[(100, 10)]);
        l.subtract(Range::new(50, 10));
        l.subtract(Range::new(120, 10));
        assert_eq!(collect(&l), &[(100, 10)]);
    }

    #[test]
    fn total_bytes() {
        let l = rl(&[(100, 10), (200, 30)]);
        assert_eq!(l.total_bytes(), 40);
    }

    #[test]
    fn cancel_exact_match_removes_both() {
        let mut d = BlockGroupRangeDeltas::new();
        d.record_allocated(0, Range::new(1000, 16384));
        d.record_freed(0, Range::new(1000, 16384));
        d.cancel_within_transaction();
        assert!(d.is_empty());
    }

    #[test]
    fn cancel_disjoint_keeps_both() {
        let mut d = BlockGroupRangeDeltas::new();
        d.record_allocated(0, Range::new(1000, 16384));
        d.record_freed(0, Range::new(50000, 16384));
        d.cancel_within_transaction();
        let bg = d.get(0).unwrap();
        assert_eq!(bg.allocated.total_bytes(), 16384);
        assert_eq!(bg.freed.total_bytes(), 16384);
    }

    #[test]
    fn cancel_partial_overlap() {
        // Allocated 100..200, freed 150..250. Cancel 150..200 from
        // both. Allocated remains 100..150, freed remains 200..250.
        let mut d = BlockGroupRangeDeltas::new();
        d.record_allocated(0, Range::new(100, 100));
        d.record_freed(0, Range::new(150, 100));
        d.cancel_within_transaction();
        let bg = d.get(0).unwrap();
        assert_eq!(collect(&bg.allocated), &[(100, 50)]);
        assert_eq!(collect(&bg.freed), &[(200, 50)]);
    }

    fn delta(alloc: &[(u64, u64)], freed: &[(u64, u64)]) -> BlockGroupDelta {
        BlockGroupDelta {
            allocated: rl(alloc),
            freed: rl(freed),
        }
    }

    #[test]
    fn apply_subtract_middle_splits() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(0, 1024)]);
        let d = delta(&[(400, 100)], &[]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(0, 400), (500, 524)]);
    }

    #[test]
    fn apply_subtract_whole_range_removes() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[(100, 100)], &[]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert!(out.is_empty());
    }

    #[test]
    fn apply_subtract_left_edge() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[(100, 30)], &[]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(130, 70)]);
    }

    #[test]
    fn apply_add_merges_both_sides() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(0, 100), (200, 100)]);
        let d = delta(&[], &[(100, 100)]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(0, 300)]);
    }

    #[test]
    fn apply_add_inserts_in_middle() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(0, 100), (500, 100)]);
        let d = delta(&[], &[(200, 100)]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(0, 100), (200, 100), (500, 100)]);
    }

    #[test]
    fn apply_alloc_outside_free_errors() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[(400, 50)], &[]);
        let err = apply_delta(0, bg, &existing, &d).unwrap_err();
        assert!(matches!(err, ApplyError::AllocatedNotFree { .. }));
    }

    #[test]
    fn apply_alloc_partial_outside_free_errors() {
        // Allocation straddles the edge of an existing free range.
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[(180, 50)], &[]);
        let err = apply_delta(0, bg, &existing, &d).unwrap_err();
        assert!(matches!(err, ApplyError::AllocatedNotFree { .. }));
    }

    #[test]
    fn apply_freed_overlaps_free_errors() {
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[], &[(150, 50)]);
        let err = apply_delta(0, bg, &existing, &d).unwrap_err();
        assert!(matches!(err, ApplyError::FreedAlreadyFree { .. }));
    }

    #[test]
    fn apply_freed_touching_is_ok_and_merges() {
        // Touching but not overlapping is fine and produces a merge.
        let bg = Range::new(0, 1024);
        let existing = rl(&[(100, 100)]);
        let d = delta(&[], &[(200, 50)]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(100, 150)]);
    }

    #[test]
    fn apply_result_outside_bg_errors() {
        let bg = Range::new(1000, 1024);
        // Existing range straddles the bg end. The result keeps it,
        // which the bound check rejects.
        let existing =
            RangeList::from_sorted_unchecked(vec![Range::new(2000, 100)]);
        let d = delta(&[], &[]);
        let err = apply_delta(1000, bg, &existing, &d).unwrap_err();
        assert!(matches!(err, ApplyError::OutOfBlockGroup { .. }));
    }

    #[test]
    fn apply_alloc_then_free_into_just_freed_slot() {
        // Subtractions are applied before additions, so freeing into
        // a slot that was just allocated out of an existing free
        // range is valid.
        let bg = Range::new(0, 1024);
        // Existing free: 0..400. Allocate 100..200. Then free 100..200
        // back. Result should be 0..400 again.
        let existing = rl(&[(0, 400)]);
        let d = delta(&[(100, 100)], &[(100, 100)]);
        let out = apply_delta(0, bg, &existing, &d).unwrap();
        assert_eq!(collect(&out), &[(0, 400)]);
    }

    #[test]
    fn range_list_contains() {
        let l = rl(&[(100, 100), (300, 100)]);
        assert!(l.contains(Range::new(120, 50)));
        assert!(l.contains(Range::new(100, 100)));
        assert!(!l.contains(Range::new(150, 100)));
        assert!(!l.contains(Range::new(50, 10)));
    }

    #[test]
    fn range_list_overlaps() {
        let l = rl(&[(100, 100)]);
        assert!(l.overlaps(Range::new(150, 10)));
        assert!(l.overlaps(Range::new(50, 100)));
        assert!(!l.overlaps(Range::new(200, 50))); // touches
        assert!(!l.overlaps(Range::new(50, 50))); // touches
        assert!(!l.overlaps(Range::new(0, 10)));
    }

    #[test]
    fn cancel_isolates_per_block_group() {
        let mut d = BlockGroupRangeDeltas::new();
        d.record_allocated(0, Range::new(100, 10));
        d.record_freed(1000, Range::new(100, 10));
        d.cancel_within_transaction();
        // Different block groups; nothing cancels.
        assert_eq!(d.len(), 2);
    }
}