Skip to main content

structured_asn1/
memory.rs

1//! Shared storage accounting for independently owned results.
2//!
3//! The caller creates the trusted account before processing input. Containers
4//! retain that account independently of any parser and release their charge
5//! only when the backing allocation is deallocated. Input buffers supplied by
6//! the caller are outside these allocations; borrowed views do not copy them.
7
8use alloc::sync::Arc;
9use core::fmt;
10use core::ops::{Deref, DerefMut};
11use core::sync::atomic::{AtomicUsize, Ordering};
12
13use allocator_api2::collections::TryReserveErrorKind;
14use allocator_api2::vec::Vec;
15
16mod allocator;
17mod map;
18mod sort;
19mod string;
20mod work;
21pub use map::{HashWork, OwnedMap};
22pub use sort::SortError;
23pub use string::OwnedString;
24pub use work::{Execution, Work};
25
26/// A failed storage operation, distinct from malformed input.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum MemoryError {
29    /// The shared capacity allowance cannot cover this allocation's peak.
30    LimitExceeded,
31    /// The underlying allocator could not supply storage.
32    OutOfMemory,
33    /// The requested capacity cannot be represented by an allocation layout.
34    CapacityOverflow,
35    /// An aggregate or borrowed parser-session work allowance is exhausted.
36    WorkExceeded,
37}
38
39impl fmt::Display for MemoryError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.write_str(match self {
42            Self::LimitExceeded => "memory budget spent",
43            Self::OutOfMemory => "allocator could not supply storage",
44            Self::CapacityOverflow => "storage capacity overflow",
45            Self::WorkExceeded => "work budget spent",
46        })
47    }
48}
49
50#[cfg(feature = "std")]
51impl std::error::Error for MemoryError {}
52
53/// Caller-owned accounting state, constructed without allocating memory.
54///
55/// The caller supplies its shared ownership separately, during trusted setup.
56/// This keeps account creation out of document-driven allocation paths.
57#[derive(Debug)]
58pub struct MemoryAccount {
59    limit: usize,
60    used: AtomicUsize,
61    work_limit: usize,
62    work: AtomicUsize,
63}
64
65impl MemoryAccount {
66    /// An immutable capacity allowance, in bytes of allocator-visible storage.
67    #[must_use]
68    pub const fn new(limit: usize, work_limit: usize) -> Self {
69        Self {
70            limit,
71            used: AtomicUsize::new(0),
72            work_limit,
73            work: AtomicUsize::new(0),
74        }
75    }
76
77    fn reserve(&self, bytes: usize) -> core::result::Result<Reservation<'_>, MemoryError> {
78        let mut used = self.used.load(Ordering::Relaxed);
79        loop {
80            debug_assert!(used <= self.limit);
81            if bytes > self.limit - used {
82                return Err(MemoryError::LimitExceeded);
83            }
84            match self.used.compare_exchange_weak(
85                used,
86                used + bytes,
87                Ordering::Relaxed,
88                Ordering::Relaxed,
89            ) {
90                Ok(_) => {
91                    return Ok(Reservation {
92                        account: self,
93                        bytes,
94                    });
95                }
96                Err(current) => used = current,
97            }
98        }
99    }
100
101    fn release(&self, bytes: usize) {
102        let previous = self.used.fetch_sub(bytes, Ordering::Relaxed);
103        debug_assert!(previous >= bytes);
104    }
105}
106
107struct Reservation<'a> {
108    account: &'a MemoryAccount,
109    bytes: usize,
110}
111
112impl Reservation<'_> {
113    fn commit(mut self) {
114        self.bytes = 0;
115    }
116}
117
118impl Drop for Reservation<'_> {
119    fn drop(&mut self) {
120        self.account.release(self.bytes);
121    }
122}
123
124/// A shared storage domain retained by owned results.
125///
126/// Cloning this handle shares existing state and allocates no new block.
127#[derive(Debug, Clone)]
128pub struct Memory {
129    account: Arc<MemoryAccount>,
130}
131
132impl Memory {
133    /// Uses caller-supplied shared state; this constructor allocates nothing.
134    #[must_use]
135    pub fn new(account: Arc<MemoryAccount>) -> Self {
136        Self { account }
137    }
138
139    /// The immutable allowance in bytes.
140    #[must_use]
141    pub fn limit(&self) -> usize {
142        self.account.limit
143    }
144
145    /// Currently reserved bytes, including pending allocations and growth peaks.
146    #[must_use]
147    pub fn used(&self) -> usize {
148        self.account.used.load(Ordering::Relaxed)
149    }
150
151    /// Whether two handles share exactly the same account, without allocation.
152    #[must_use]
153    pub fn same_account(&self, other: &Self) -> bool {
154        Arc::ptr_eq(&self.account, &other.account)
155    }
156
157    /// Cumulative work already admitted, including operations that later failed.
158    #[must_use]
159    pub fn work_used(&self) -> usize {
160        self.account.work.load(Ordering::Relaxed)
161    }
162
163    /// Currently available aggregate work units, without reserving them.
164    /// Concurrent consumption can reduce this observation; every operation
165    /// must still use [`Self::charge_work`] before execution.
166    #[must_use]
167    pub fn work_remaining(&self) -> usize {
168        let spent = self.account.work.load(Ordering::Relaxed);
169        debug_assert!(spent <= self.account.work_limit);
170        self.account.work_limit - spent
171    }
172
173    /// Charges work before performing it; storage release never refunds work.
174    ///
175    /// An amount unrepresentable on this target is refused before any work,
176    /// preserving the existing u64 scanning API on 32-bit targets.
177    pub fn charge_work(&self, amount: u64) -> core::result::Result<(), MemoryError> {
178        let amount = usize::try_from(amount).map_err(|_| MemoryError::WorkExceeded)?;
179        let mut spent = self.account.work.load(Ordering::Relaxed);
180        loop {
181            debug_assert!(spent <= self.account.work_limit);
182            if amount > self.account.work_limit - spent {
183                return Err(MemoryError::WorkExceeded);
184            }
185            match self.account.work.compare_exchange_weak(
186                spent,
187                spent + amount,
188                Ordering::Relaxed,
189                Ordering::Relaxed,
190            ) {
191                Ok(_) => return Ok(()),
192                Err(current) => spent = current,
193            }
194        }
195    }
196
197    /// Reserves storage for an adapter-owned allocation before allocating it.
198    ///
199    /// Keep the returned guard until after deallocation, including rollback on
200    /// failure. Reserve the complete actual capacity; a growth must retain the
201    /// old guard while admitting the replacement. This bridge does not allocate.
202    pub fn reserve(&self, bytes: usize) -> core::result::Result<MemoryReservation, MemoryError> {
203        let reservation = self.account.reserve(bytes)?;
204        reservation.commit();
205        Ok(MemoryReservation {
206            memory: self.clone(),
207            bytes,
208        })
209    }
210
211    /// An empty owned collection sharing this domain, without allocating storage.
212    #[must_use]
213    pub fn vector<T>(&self) -> OwnedVec<T> {
214        Work::vector(self)
215    }
216
217    /// An empty accounted hash map, without allocating storage.
218    #[must_use]
219    pub fn map<K: HashWork, V>(&self) -> OwnedMap<K, V> {
220        OwnedMap::new(self.clone())
221    }
222
223    /// Empty accounted UTF-8 text, without allocating storage.
224    #[must_use]
225    pub fn string(&self) -> OwnedString {
226        OwnedString::new(self)
227    }
228}
229
230/// A reservation retained alongside an adapter-owned allocation.
231///
232/// Its drop releases the reserved charge. The adapter must deallocate its block
233/// first; dropping this guard earlier would misreport the allocation's lifetime.
234#[derive(Debug)]
235#[must_use = "retain the reservation until its allocation has been deallocated"]
236pub struct MemoryReservation {
237    memory: Memory,
238    bytes: usize,
239}
240
241impl Drop for MemoryReservation {
242    fn drop(&mut self) {
243        self.memory.account.release(self.bytes);
244    }
245}
246
247/// An owned collection whose capacity remains charged until deallocation.
248///
249/// It exposes slices rather than the underlying growable container, so callers
250/// cannot bypass fallible growth. Moving it retains accounting; copying values
251/// requires a fresh reservation. Clearing values does not release capacity.
252/// The default [`Memory`] execution owner is independent of parser lifetime;
253/// a collection using [`Execution`] borrows its parser's work ceiling too.
254pub struct OwnedVec<T, W: Work = Memory> {
255    values: Vec<T, allocator::AccountAllocator<W>>,
256}
257
258impl<T, W: Work> OwnedVec<T, W> {
259    /// Finishes bounded construction as an independently owned result.
260    ///
261    /// Transfers the existing allocation and its original account without
262    /// copying elements, reallocating, changing capacity or reserving again.
263    /// Subsequent operations use that aggregate account; the parser-local
264    /// ceiling governed construction and is no longer attached. Borrows in
265    /// `T` retain their own lifetimes.
266    ///
267    /// # Examples
268    /// ```
269    /// use std::sync::Arc;
270    /// use structured_asn1::{Parser, memory::{Memory, MemoryAccount, Work}};
271    /// let memory = Memory::new(Arc::new(MemoryAccount::new(1024, 1024)));
272    /// let result;
273    /// {
274    ///     let parser = Parser::ber(memory.clone());
275    ///     let reader = parser.read(&[], 0)?;
276    ///     let mut scratch = reader.execution().vector();
277    ///     scratch.try_push(7_u8).unwrap();
278    ///     result = scratch.into_owned();
279    /// }
280    /// assert_eq!(&*result, &[7]);
281    /// assert!(memory.used() > 0);
282    /// drop(result);
283    /// assert_eq!(memory.used(), 0);
284    /// # Ok::<(), structured_asn1::Error>(())
285    /// ```
286    #[must_use]
287    pub fn into_owned(self) -> OwnedVec<T> {
288        OwnedVec {
289            values: allocator::retain(self.values),
290        }
291    }
292}
293
294impl<T: fmt::Debug, W: Work> fmt::Debug for OwnedVec<T, W> {
295    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296        self.values.fmt(f)
297    }
298}
299
300impl<T: PartialEq, W: Work> PartialEq for OwnedVec<T, W> {
301    fn eq(&self, other: &Self) -> bool {
302        **self == **other
303    }
304}
305
306impl<T: Eq, W: Work> Eq for OwnedVec<T, W> {}
307
308impl<T: PartialEq, W: Work, const N: usize> PartialEq<[T; N]> for OwnedVec<T, W> {
309    fn eq(&self, other: &[T; N]) -> bool {
310        &**self == other
311    }
312}
313
314impl<T, W: Work> OwnedVec<T, W> {
315    /// The account retained by this collection, for related adapter operations.
316    #[must_use]
317    pub fn memory(&self) -> &Memory {
318        self.values.allocator().memory()
319    }
320    /// The allocated capacity in elements (zero-sized types allocate no bytes).
321    #[must_use]
322    pub fn capacity(&self) -> usize {
323        self.values.capacity()
324    }
325
326    /// Reserves capacity before any allocation, accounting for growth's peak.
327    pub fn try_reserve_exact(
328        &mut self,
329        additional: usize,
330    ) -> core::result::Result<(), MemoryError> {
331        self.values.allocator().clear_failure();
332        self.values
333            .try_reserve_exact(additional)
334            .map_err(|error| match error.kind() {
335                TryReserveErrorKind::CapacityOverflow => MemoryError::CapacityOverflow,
336                TryReserveErrorKind::AllocError { .. } => self.values.allocator().failure(),
337            })
338    }
339
340    /// Reserves amortized growth, charging the actual capacity and growth peak.
341    pub fn try_reserve(&mut self, additional: usize) -> core::result::Result<(), MemoryError> {
342        self.values.allocator().clear_failure();
343        self.values
344            .try_reserve(additional)
345            .map_err(|error| match error.kind() {
346                TryReserveErrorKind::CapacityOverflow => MemoryError::CapacityOverflow,
347                TryReserveErrorKind::AllocError { .. } => self.values.allocator().failure(),
348            })
349    }
350
351    /// Appends one value, acquiring any required storage before moving it.
352    pub fn try_push(&mut self, value: T) -> core::result::Result<(), MemoryError> {
353        self.values.allocator().work().charge_work(1)?;
354        if self.values.len() == self.values.capacity() {
355            self.try_reserve(1)?;
356        }
357        // Vec guarantees push cannot allocate while capacity is available.
358        self.values.push(value);
359        Ok(())
360    }
361
362    /// Drops the last value, retaining the allocated capacity.
363    pub fn pop(&mut self) -> Option<T> {
364        self.values.pop()
365    }
366
367    /// Drops every value, retaining the allocated capacity and its charge.
368    pub fn clear(&mut self) {
369        self.values.clear();
370    }
371
372    /// Sorts in place without allocating or recursion.
373    ///
374    /// Comparisons and moves are admitted before execution. The comparator
375    /// must admit its own data-dependent work before examining values. On
376    /// refusal, values remain a permutation of the input, partially ordered.
377    pub fn try_sort_unstable_by<E>(
378        &mut self,
379        compare: impl FnMut(&T, &T) -> core::result::Result<core::cmp::Ordering, E>,
380    ) -> core::result::Result<(), SortError<E>> {
381        let work = self.values.allocator().work().clone();
382        sort::sort(&mut self.values, &work, compare)
383    }
384}
385
386impl<T: Copy, W: Work> OwnedVec<T, W> {
387    /// Copies a slice after reserving storage and cumulative work in this domain.
388    pub fn try_extend_from_slice(&mut self, values: &[T]) -> core::result::Result<(), MemoryError> {
389        let work = values.len().max(core::mem::size_of_val(values));
390        self.values.allocator().work().charge_work(work as u64)?;
391        if self.values.capacity() == 0 {
392            self.try_reserve_exact(values.len())?;
393        } else {
394            self.try_reserve(values.len())?;
395        }
396        self.values.extend_from_slice(values);
397        Ok(())
398    }
399    /// Copies values into independently charged storage in the same domain.
400    pub fn try_copy(&self) -> core::result::Result<Self, MemoryError> {
401        let mut copy = self.values.allocator().work().vector();
402        let work = self.values.len().max(core::mem::size_of_val(&**self));
403        self.values.allocator().work().charge_work(work as u64)?;
404        copy.try_reserve_exact(self.values.len())?;
405        // Reservation above guarantees the Copy specialization cannot allocate.
406        copy.values.extend_from_slice(&self.values);
407        Ok(copy)
408    }
409}
410
411impl<T, W: Work> Deref for OwnedVec<T, W> {
412    type Target = [T];
413    fn deref(&self) -> &[T] {
414        &self.values
415    }
416}
417
418impl<T, W: Work> DerefMut for OwnedVec<T, W> {
419    fn deref_mut(&mut self) -> &mut [T] {
420        &mut self.values
421    }
422}
423
424impl<'a, T, W: Work> IntoIterator for &'a OwnedVec<T, W> {
425    type Item = &'a T;
426    type IntoIter = core::slice::Iter<'a, T>;
427    fn into_iter(self) -> Self::IntoIter {
428        self.iter()
429    }
430}
431
432impl<'a, T, W: Work> IntoIterator for &'a mut OwnedVec<T, W> {
433    type Item = &'a mut T;
434    type IntoIter = core::slice::IterMut<'a, T>;
435    fn into_iter(self) -> Self::IntoIter {
436        self.iter_mut()
437    }
438}
439
440/// An owning iterator that retains capacity accounting until storage is freed.
441pub struct OwnedIter<T, W: Work = Memory> {
442    values: allocator_api2::vec::IntoIter<T, allocator::AccountAllocator<W>>,
443}
444
445impl<T, W: Work> Iterator for OwnedIter<T, W> {
446    type Item = T;
447    fn next(&mut self) -> Option<T> {
448        self.values.next()
449    }
450    fn size_hint(&self) -> (usize, Option<usize>) {
451        self.values.size_hint()
452    }
453}
454
455impl<T, W: Work> DoubleEndedIterator for OwnedIter<T, W> {
456    fn next_back(&mut self) -> Option<T> {
457        self.values.next_back()
458    }
459}
460
461impl<T, W: Work> ExactSizeIterator for OwnedIter<T, W> {}
462impl<T, W: Work> core::iter::FusedIterator for OwnedIter<T, W> {}
463
464impl<T, W: Work> IntoIterator for OwnedVec<T, W> {
465    type Item = T;
466    type IntoIter = OwnedIter<T, W>;
467    fn into_iter(self) -> Self::IntoIter {
468        OwnedIter {
469            values: self.values.into_iter(),
470        }
471    }
472}
473
474#[cfg(test)]
475mod tests;