bstack 0.4.0

A persistent, fsync-durable binary stack backed by a single file
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
use super::{
    BStackAllocError, BStackAllocator, BStackBulkAllocError, BStackBulkAllocator, BStackOwnedSlice,
};
use crate::BStack;
#[cfg(not(feature = "atomic"))]
use std::cell::Cell;
#[cfg(not(feature = "atomic"))]
use std::marker::PhantomData;
use std::{fmt, io};

/// A simple bump allocator that owns a [`BStack`] and allocates regions
/// sequentially by appending to the tail.
///
/// # Realloc policy
///
/// `realloc` is O(1) for the tail allocation.  `realloc` of a non-tail
/// allocation returns [`io::ErrorKind::Unsupported`].
///
/// # Dealloc policy
///
/// `dealloc` reclaims the tail allocation via [`BStack::discard`] (or
/// [`BStack::try_discard`] with the `atomic` feature).  For non-tail
/// allocations it is a no-op — the bytes remain on disk but are logically
/// unreachable through this allocator.
///
/// # Crash consistency
///
/// Every operation maps to exactly one [`BStack`] call and is therefore
/// crash-safe by inheritance:
///
/// | Operation            | Without `atomic`    | With `atomic`           |
/// |----------------------|---------------------|-------------------------|
/// | `alloc`              | [`BStack::extend`]  | [`BStack::extend`]      |
/// | `realloc` grow       | [`BStack::extend`]  | [`BStack::try_extend`]  |
/// | `realloc` shrink     | [`BStack::discard`] | [`BStack::try_discard`] |
/// | `dealloc` (tail)     | [`BStack::discard`] | [`BStack::try_discard`] |
/// | `dealloc` (non-tail) | no-op               | no-op                   |
///
/// # Thread safety
///
/// Without the `atomic` feature, `LinearBStackAllocator` is **`Send`** but
/// **not `Sync`**: `realloc` and `dealloc` read the tail length and then
/// modify it in two separate steps, so concurrent `&self` calls would race.
///
/// With the `atomic` feature, `LinearBStackAllocator` is **`Send + Sync`**.
/// The tail-modifying operations are replaced with their `try_*` counterparts,
/// which check-and-modify the tail in a single locked step:
///
/// * **`alloc`** / **`alloc_bulk`**: a single [`BStack::extend`] returns a
///   distinct region to every caller regardless of concurrency.
/// * **`realloc`**: uses [`BStack::try_extend`] / [`BStack::try_discard`]
///   with `slice.end()` as the sentinel.  If the tail has moved (another
///   thread raced), the call returns [`io::ErrorKind::Unsupported`] — the
///   same error as a non-tail realloc on a single thread.
/// * **`dealloc`** / **`dealloc_bulk`**: uses [`BStack::try_discard`]; if the
///   tail has moved the discard is silently skipped, matching the existing
///   non-tail no-op semantics.
///
/// ```
/// fn assert_send<T: Send>() {}
/// assert_send::<bstack::LinearBStackAllocator>();
/// ```
///
/// # Example
///
/// ```no_run
/// use bstack::{BStack, BStackAllocator, LinearBStackAllocator};
///
/// # fn main() -> std::io::Result<()> {
/// let alloc = LinearBStackAllocator::new(BStack::open("data.bstack")?);
/// let mut slice = alloc.alloc(128)?;
/// let data = slice.read()?;
/// // On failure `dealloc` returns the handle inside the error; surface just
/// // the underlying error with `.map_err(|e| e.source)`.
/// alloc.dealloc(slice).map_err(|e| e.source)?;
/// let stack = alloc.into_stack();
/// # Ok(())
/// # }
/// ```
pub struct LinearBStackAllocator {
    stack: BStack,
    #[cfg(not(feature = "atomic"))]
    _not_sync: PhantomData<Cell<()>>,
}

impl LinearBStackAllocator {
    /// Create a new `LinearBStackAllocator` that takes ownership of `stack`.
    pub fn new(stack: BStack) -> Self {
        Self {
            stack,
            #[cfg(not(feature = "atomic"))]
            _not_sync: PhantomData,
        }
    }
}

impl fmt::Debug for LinearBStackAllocator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LinearBStackAllocator")
            .finish_non_exhaustive()
    }
}

impl From<BStack> for LinearBStackAllocator {
    fn from(stack: BStack) -> Self {
        Self::new(stack)
    }
}

impl From<LinearBStackAllocator> for BStack {
    fn from(alloc: LinearBStackAllocator) -> Self {
        alloc.into_stack()
    }
}

impl BStackAllocator for LinearBStackAllocator {
    type Error = io::Error;
    type Allocated<'a> = BStackOwnedSlice<'a, Self>;

    fn stack(&self) -> &BStack {
        &self.stack
    }

    fn into_stack(self) -> BStack {
        self.stack
    }

    fn alloc(&self, len: u64) -> io::Result<BStackOwnedSlice<'_, Self>> {
        let offset = self.stack.extend(len)?;
        // SAFETY: offset and len come from a fresh allocation via self.stack.extend
        Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, offset, len) })
    }

    #[cfg(not(feature = "atomic"))]
    fn realloc<'a>(
        &'a self,
        handle: BStackOwnedSlice<'a, Self>,
        new_len: u64,
    ) -> Result<BStackOwnedSlice<'a, Self>, BStackAllocError<'a, Self>> {
        let start = handle.start();
        let old_len = handle.len();
        let end = handle.end();
        // handle is dropped here (no-op Drop); we reconstruct below on success
        (|| -> io::Result<BStackOwnedSlice<'a, Self>> {
            let current_tail = self.stack.len()?;
            if end != current_tail {
                return Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "LinearBStackAllocator does not support reallocation of non-tail slices; \
                     callers must alloc a new region and copy manually. \
                     Note: dealloc of the old (non-tail) slice is also a no-op and leaks the region.",
                ));
            }
            match new_len.cmp(&old_len) {
                std::cmp::Ordering::Equal => {
                    // SAFETY: same region, reconstructed handle
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, old_len) })
                }
                std::cmp::Ordering::Greater => {
                    self.stack.extend(new_len - old_len)?;
                    // SAFETY: tail was extended in place
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, new_len) })
                }
                std::cmp::Ordering::Less => {
                    self.stack.discard(old_len - new_len)?;
                    // SAFETY: tail was shrunk in place
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, new_len) })
                }
            }
        })()
        .map_err(|source| BStackAllocError {
            source,
            // Every failure path above leaves the original region intact.
            // SAFETY: (start, old_len) still describes the live, unmodified allocation.
            handle: Some(unsafe { BStackOwnedSlice::from_raw_parts(self, start, old_len) }),
        })
    }

    // With the `atomic` feature the tail check and the modification are a
    // single locked step (`try_extend`/`try_discard`), eliminating the TOCTOU
    // race that exists in the non-atomic version.  A `false` return means
    // another thread moved the tail first; we surface this as `Unsupported`.
    #[cfg(feature = "atomic")]
    fn realloc<'a>(
        &'a self,
        handle: BStackOwnedSlice<'a, Self>,
        new_len: u64,
    ) -> Result<BStackOwnedSlice<'a, Self>, BStackAllocError<'a, Self>> {
        let start = handle.start();
        let old_len = handle.len();
        let end = handle.end();
        // handle dropped (no-op); reconstruct below on success
        (|| -> io::Result<BStackOwnedSlice<'a, Self>> {
            match new_len.cmp(&old_len) {
                std::cmp::Ordering::Equal => {
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, old_len) })
                }
                std::cmp::Ordering::Greater => {
                    let delta = new_len - old_len;
                    let zeros = vec![0u8; delta as usize];
                    if !self.stack.try_extend(end, zeros)? {
                        return Err(io::Error::new(
                            io::ErrorKind::Unsupported,
                            "LinearBStackAllocator does not support reallocation of non-tail slices; \
                             callers must alloc a new region and copy manually. \
                             Note: dealloc of the old (non-tail) slice is also a no-op and leaks the region.",
                        ));
                    }
                    // SAFETY: tail atomically extended in place
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, new_len) })
                }
                std::cmp::Ordering::Less => {
                    if !self.stack.try_discard(end, old_len - new_len)? {
                        return Err(io::Error::new(
                            io::ErrorKind::Unsupported,
                            "LinearBStackAllocator does not support reallocation of non-tail slices; \
                             callers must alloc a new region and copy manually. \
                             Note: dealloc of the old (non-tail) slice is also a no-op and leaks the region.",
                        ));
                    }
                    // SAFETY: tail atomically shrunk in place
                    Ok(unsafe { BStackOwnedSlice::from_raw_parts(self, start, new_len) })
                }
            }
        })()
        .map_err(|source| BStackAllocError {
            source,
            // Every failure path above leaves the original region intact.
            // SAFETY: (start, old_len) still describes the live, unmodified allocation.
            handle: Some(unsafe { BStackOwnedSlice::from_raw_parts(self, start, old_len) }),
        })
    }

    #[cfg(not(feature = "atomic"))]
    fn dealloc<'a>(
        &'a self,
        handle: BStackOwnedSlice<'a, Self>,
    ) -> Result<(), BStackAllocError<'a, Self>> {
        let start = handle.start();
        let end = handle.end();
        let len = handle.len();
        (|| -> io::Result<()> {
            let current_tail = self.stack.len()?;
            if end == current_tail {
                self.stack.discard(len)?;
            }
            Ok(())
        })()
        .map_err(|source| BStackAllocError {
            source,
            // A failed discard leaves the region still allocated.
            // SAFETY: (start, len) still describes the live allocation.
            handle: Some(unsafe { BStackOwnedSlice::from_raw_parts(self, start, len) }),
        })
    }

    #[cfg(feature = "atomic")]
    fn dealloc<'a>(
        &'a self,
        handle: BStackOwnedSlice<'a, Self>,
    ) -> Result<(), BStackAllocError<'a, Self>> {
        let start = handle.start();
        let end = handle.end();
        let len = handle.len();
        // try_discard is a no-op when the tail has moved, matching non-tail dealloc semantics.
        self.stack
            .try_discard(end, len)
            .map(|_| ())
            .map_err(|source| BStackAllocError {
                source,
                // A failed discard leaves the region still allocated.
                // SAFETY: (start, len) still describes the live allocation.
                handle: Some(unsafe { BStackOwnedSlice::from_raw_parts(self, start, len) }),
            })
    }
}

impl BStackBulkAllocator for LinearBStackAllocator {
    /// Allocate all slices with a single [`BStack::extend`] call.
    ///
    /// The total byte count is computed first; if it overflows `u64` the call
    /// returns [`io::ErrorKind::InvalidInput`] without modifying the file.
    /// Otherwise one `extend` (and one durable sync) covers all allocations.
    ///
    /// # Zero-length entries
    ///
    /// Zero-length requests are permitted and produce zero-length slices. Their
    /// offset is the same as the immediately following non-zero slice because
    /// adding zero to the running offset leaves it unchanged. Multiple
    /// consecutive zero-length entries therefore produce slices that compare
    /// equal by `(offset, len)`. This is harmless for I/O but callers that use
    /// slice identity to distinguish allocations should avoid zero-length entries
    /// or filter them out.
    fn alloc_bulk(
        &self,
        lengths: impl AsRef<[u64]>,
    ) -> Result<Vec<Self::Allocated<'_>>, Self::Error> {
        let lengths = lengths.as_ref();
        if lengths.is_empty() {
            return Ok(Vec::new());
        }
        let total = lengths
            .iter()
            .try_fold(0u64, |acc, &len| acc.checked_add(len))
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "alloc_bulk: total length overflows u64",
                )
            })?;
        let base = self.stack.extend(total)?;
        let mut result = Vec::with_capacity(lengths.len());
        let mut offset = base;
        for &len in lengths {
            // SAFETY: offset and len are within the bulk allocation starting at base
            result.push(unsafe { BStackOwnedSlice::from_raw_parts(self, offset, len) });
            offset += len;
        }
        Ok(result)
    }

    /// Reclaim the largest contiguous region at the tail with a single
    /// [`BStack::discard`] call.
    ///
    /// Handles that form a contiguous sequence ending at the current tail are
    /// all removed in one operation.  Handles that do not touch the tail (or
    /// that are separated from the tail by handles not included in `handles`)
    /// are silently ignored, matching the single-item
    /// [`dealloc`](BStackAllocator::dealloc) semantics.
    ///
    /// # Duplicates and overlapping handles
    ///
    /// Duplicate or overlapping handles are silently coalesced — no error is
    /// returned, and only the bytes they collectively cover are discarded once.
    #[cfg(not(feature = "atomic"))]
    fn dealloc_bulk<'a>(
        &'a self,
        handles: impl IntoIterator<Item = Self::Allocated<'a>>,
    ) -> Result<(), BStackBulkAllocError<'a, Self>> {
        let mut sorted: Vec<BStackOwnedSlice<'a, Self>> = handles.into_iter().collect();
        if sorted.is_empty() {
            return Ok(());
        }
        sorted.sort_by_key(|s| std::cmp::Reverse(s.end()));
        let result = (|| -> io::Result<()> {
            let current_tail = self.stack.len()?;
            let mut discard_start = current_tail;
            for handle in &sorted {
                if handle.end() == discard_start {
                    discard_start = handle.start();
                }
            }
            let to_discard = current_tail - discard_start;
            if to_discard > 0 {
                self.stack.discard(to_discard)?;
            }
            Ok(())
        })();
        // The discard is a single call: on failure nothing was freed, so every
        // handle is still owned by the caller.
        result.map_err(|source| BStackBulkAllocError {
            source,
            handles: sorted,
        })
    }

    // With `atomic`, we snapshot the tail once to determine what to discard,
    // then use `try_discard` to make the actual removal atomic.  If another
    // thread has moved the tail since the snapshot, `try_discard` returns
    // `false` and the discard is silently skipped.
    #[cfg(feature = "atomic")]
    fn dealloc_bulk<'a>(
        &'a self,
        handles: impl IntoIterator<Item = Self::Allocated<'a>>,
    ) -> Result<(), BStackBulkAllocError<'a, Self>> {
        let mut sorted: Vec<BStackOwnedSlice<'a, Self>> = handles.into_iter().collect();
        if sorted.is_empty() {
            return Ok(());
        }
        sorted.sort_by_key(|s| std::cmp::Reverse(s.end()));
        let result = (|| -> io::Result<()> {
            let current_tail = self.stack.len()?;
            let mut discard_start = current_tail;
            for handle in &sorted {
                if handle.end() == discard_start {
                    discard_start = handle.start();
                }
            }
            let to_discard = current_tail - discard_start;
            if to_discard > 0 {
                self.stack.try_discard(current_tail, to_discard)?;
            }
            Ok(())
        })();
        // The try_discard is a single call: on failure nothing was freed, so
        // every handle is still owned by the caller.
        result.map_err(|source| BStackBulkAllocError {
            source,
            handles: sorted,
        })
    }
}

#[cfg(test)]
mod _assertions {
    use super::LinearBStackAllocator;
    // LinearBStackAllocator is always Send.
    fn _send()
    where
        LinearBStackAllocator: Send,
    {
    }
    // LinearBStackAllocator is Sync only when the `atomic` feature is enabled.
    #[cfg(feature = "atomic")]
    fn _sync()
    where
        LinearBStackAllocator: Sync,
    {
    }
}