1use 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 shared_string;
19mod sort;
20mod string;
21mod text;
22mod work;
23pub use map::{HashWork, MapExecution, OwnedMap, RetainError};
24pub use shared_string::SharedString;
25pub use sort::SortError;
26pub use string::OwnedString;
27pub use text::{OwnedText, TextError, TextErrorKind};
28pub use work::{Execution, Work};
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum MemoryError {
33 LimitExceeded,
35 OutOfMemory,
37 CapacityOverflow,
39 WorkExceeded,
41}
42
43impl fmt::Display for MemoryError {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 f.write_str(match self {
46 Self::LimitExceeded => "memory budget spent",
47 Self::OutOfMemory => "allocator could not supply storage",
48 Self::CapacityOverflow => "storage capacity overflow",
49 Self::WorkExceeded => "work budget spent",
50 })
51 }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for MemoryError {}
56
57#[derive(Debug)]
62pub struct MemoryAccount {
63 limit: usize,
64 used: AtomicUsize,
65 work_limit: usize,
66 work: AtomicUsize,
67}
68
69impl MemoryAccount {
70 #[must_use]
72 pub const fn new(limit: usize, work_limit: usize) -> Self {
73 Self {
74 limit,
75 used: AtomicUsize::new(0),
76 work_limit,
77 work: AtomicUsize::new(0),
78 }
79 }
80
81 fn reserve(&self, bytes: usize) -> core::result::Result<Reservation<'_>, MemoryError> {
82 let mut used = self.used.load(Ordering::Relaxed);
83 loop {
84 debug_assert!(used <= self.limit);
85 if bytes > self.limit - used {
86 return Err(MemoryError::LimitExceeded);
87 }
88 match self.used.compare_exchange_weak(
89 used,
90 used + bytes,
91 Ordering::Relaxed,
92 Ordering::Relaxed,
93 ) {
94 Ok(_) => {
95 return Ok(Reservation {
96 account: self,
97 bytes,
98 });
99 }
100 Err(current) => used = current,
101 }
102 }
103 }
104
105 fn release(&self, bytes: usize) {
106 let previous = self.used.fetch_sub(bytes, Ordering::Relaxed);
107 debug_assert!(previous >= bytes);
108 }
109}
110
111struct Reservation<'a> {
112 account: &'a MemoryAccount,
113 bytes: usize,
114}
115
116impl Reservation<'_> {
117 fn commit(mut self) {
118 self.bytes = 0;
119 }
120}
121
122impl Drop for Reservation<'_> {
123 fn drop(&mut self) {
124 self.account.release(self.bytes);
125 }
126}
127
128#[derive(Debug, Clone)]
132pub struct Memory {
133 account: Arc<MemoryAccount>,
134}
135
136impl Memory {
137 #[must_use]
139 pub fn new(account: Arc<MemoryAccount>) -> Self {
140 Self { account }
141 }
142
143 #[must_use]
145 pub fn limit(&self) -> usize {
146 self.account.limit
147 }
148
149 #[must_use]
151 pub fn used(&self) -> usize {
152 self.account.used.load(Ordering::Relaxed)
153 }
154
155 #[must_use]
157 pub fn same_account(&self, other: &Self) -> bool {
158 Arc::ptr_eq(&self.account, &other.account)
159 }
160
161 #[must_use]
163 pub fn work_used(&self) -> usize {
164 self.account.work.load(Ordering::Relaxed)
165 }
166
167 #[must_use]
171 pub fn work_remaining(&self) -> usize {
172 let spent = self.account.work.load(Ordering::Relaxed);
173 debug_assert!(spent <= self.account.work_limit);
174 self.account.work_limit - spent
175 }
176
177 pub fn charge_work(&self, amount: u64) -> core::result::Result<(), MemoryError> {
182 let amount = usize::try_from(amount).map_err(|_| MemoryError::WorkExceeded)?;
183 let mut spent = self.account.work.load(Ordering::Relaxed);
184 loop {
185 debug_assert!(spent <= self.account.work_limit);
186 if amount > self.account.work_limit - spent {
187 return Err(MemoryError::WorkExceeded);
188 }
189 match self.account.work.compare_exchange_weak(
190 spent,
191 spent + amount,
192 Ordering::Relaxed,
193 Ordering::Relaxed,
194 ) {
195 Ok(_) => return Ok(()),
196 Err(current) => spent = current,
197 }
198 }
199 }
200
201 pub fn reserve(&self, bytes: usize) -> core::result::Result<MemoryReservation, MemoryError> {
207 let reservation = self.account.reserve(bytes)?;
208 reservation.commit();
209 Ok(MemoryReservation {
210 memory: self.clone(),
211 bytes,
212 })
213 }
214
215 #[must_use]
217 pub fn vector<T>(&self) -> OwnedVec<T> {
218 Work::vector(self)
219 }
220
221 #[must_use]
223 pub fn map<K: HashWork, V>(&self) -> OwnedMap<K, V> {
224 OwnedMap::new(self.clone())
225 }
226
227 #[must_use]
229 pub fn string(&self) -> OwnedString {
230 OwnedString::new(self)
231 }
232}
233
234#[derive(Debug)]
239#[must_use = "retain the reservation until its allocation has been deallocated"]
240pub struct MemoryReservation {
241 memory: Memory,
242 bytes: usize,
243}
244
245impl Drop for MemoryReservation {
246 fn drop(&mut self) {
247 self.memory.account.release(self.bytes);
248 }
249}
250
251pub struct OwnedVec<T, W: Work = Memory> {
259 values: Vec<T, allocator::AccountAllocator<W>>,
260}
261
262#[must_use]
265pub enum VecExecution<T, Old: Work, New: Work> {
266 Changed {
268 values: OwnedVec<T, New>,
270 previous: Old,
272 },
273 Foreign {
275 values: OwnedVec<T, Old>,
277 requested: New,
279 },
280}
281
282impl<T> OwnedVec<T> {
283 pub fn with_execution<W: Work>(self, execution: &W) -> Result<OwnedVec<T, W>, Self> {
292 if !self.memory().same_account(execution.memory()) {
293 return Err(self);
294 }
295 Ok(OwnedVec {
296 values: allocator::scope(self.values, execution),
297 })
298 }
299}
300
301impl<T, W: Work> OwnedVec<T, W> {
302 pub fn rebind<New: Work>(self, execution: New) -> VecExecution<T, W, New> {
306 if !self.memory().same_account(execution.memory()) {
307 return VecExecution::Foreign {
308 values: self,
309 requested: execution,
310 };
311 }
312 let (values, previous) = allocator::rebind(self.values, &execution);
313 VecExecution::Changed {
314 values: OwnedVec { values },
315 previous,
316 }
317 }
318 #[must_use]
346 pub fn into_owned(self) -> OwnedVec<T> {
347 OwnedVec {
348 values: allocator::retain(self.values),
349 }
350 }
351}
352
353impl<T: fmt::Debug, W: Work> fmt::Debug for OwnedVec<T, W> {
354 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355 self.values.fmt(f)
356 }
357}
358
359impl<T: PartialEq, W: Work> PartialEq for OwnedVec<T, W> {
360 fn eq(&self, other: &Self) -> bool {
361 **self == **other
362 }
363}
364
365impl<T: Eq, W: Work> Eq for OwnedVec<T, W> {}
366
367impl<T: PartialEq, W: Work, const N: usize> PartialEq<[T; N]> for OwnedVec<T, W> {
368 fn eq(&self, other: &[T; N]) -> bool {
369 &**self == other
370 }
371}
372
373impl<T, W: Work> OwnedVec<T, W> {
374 #[must_use]
376 pub fn memory(&self) -> &Memory {
377 self.values.allocator().memory()
378 }
379 #[must_use]
381 pub fn capacity(&self) -> usize {
382 self.values.capacity()
383 }
384
385 pub fn try_reserve_exact(
387 &mut self,
388 additional: usize,
389 ) -> core::result::Result<(), MemoryError> {
390 self.values.allocator().clear_failure();
391 self.values
392 .try_reserve_exact(additional)
393 .map_err(|error| match error.kind() {
394 TryReserveErrorKind::CapacityOverflow => MemoryError::CapacityOverflow,
395 TryReserveErrorKind::AllocError { .. } => self.values.allocator().failure(),
396 })
397 }
398
399 pub fn try_reserve(&mut self, additional: usize) -> core::result::Result<(), MemoryError> {
401 self.values.allocator().clear_failure();
402 self.values
403 .try_reserve(additional)
404 .map_err(|error| match error.kind() {
405 TryReserveErrorKind::CapacityOverflow => MemoryError::CapacityOverflow,
406 TryReserveErrorKind::AllocError { .. } => self.values.allocator().failure(),
407 })
408 }
409
410 pub fn try_push(&mut self, value: T) -> core::result::Result<(), MemoryError> {
412 self.values.allocator().work().charge_work(1)?;
413 if self.values.len() == self.values.capacity() {
414 self.try_reserve(1)?;
415 }
416 self.values.push(value);
418 Ok(())
419 }
420
421 pub fn pop(&mut self) -> Option<T> {
423 self.values.pop()
424 }
425
426 pub fn clear(&mut self) {
428 self.values.clear();
429 }
430
431 pub fn try_drain_all(
445 &mut self,
446 ) -> Result<
447 impl DoubleEndedIterator<Item = T> + ExactSizeIterator + core::iter::FusedIterator + '_,
448 MemoryError,
449 > {
450 let work = self.values.len().max(core::mem::size_of_val(&**self));
451 self.values.allocator().work().charge_work(work as u64)?;
452 Ok(self.values.drain(..))
453 }
454
455 pub fn try_sort_unstable_by<E>(
461 &mut self,
462 compare: impl FnMut(&T, &T) -> core::result::Result<core::cmp::Ordering, E>,
463 ) -> core::result::Result<(), SortError<E>> {
464 let work = self.values.allocator().work().clone();
465 sort::sort(&mut self.values, &work, compare)
466 }
467}
468
469impl<T: Copy, W: Work> OwnedVec<T, W> {
470 pub fn try_extend_from_slice(&mut self, values: &[T]) -> core::result::Result<(), MemoryError> {
472 let work = values.len().max(core::mem::size_of_val(values));
473 self.values.allocator().work().charge_work(work as u64)?;
474 if self.values.capacity() == 0 {
475 self.try_reserve_exact(values.len())?;
476 } else {
477 self.try_reserve(values.len())?;
478 }
479 self.values.extend_from_slice(values);
480 Ok(())
481 }
482 pub fn try_copy(&self) -> core::result::Result<Self, MemoryError> {
484 let mut copy = self.values.allocator().work().vector();
485 let work = self.values.len().max(core::mem::size_of_val(&**self));
486 self.values.allocator().work().charge_work(work as u64)?;
487 copy.try_reserve_exact(self.values.len())?;
488 copy.values.extend_from_slice(&self.values);
490 Ok(copy)
491 }
492}
493
494impl<T, W: Work> Deref for OwnedVec<T, W> {
495 type Target = [T];
496 fn deref(&self) -> &[T] {
497 &self.values
498 }
499}
500
501impl<T, W: Work> DerefMut for OwnedVec<T, W> {
502 fn deref_mut(&mut self) -> &mut [T] {
503 &mut self.values
504 }
505}
506
507impl<'a, T, W: Work> IntoIterator for &'a OwnedVec<T, W> {
508 type Item = &'a T;
509 type IntoIter = core::slice::Iter<'a, T>;
510 fn into_iter(self) -> Self::IntoIter {
511 self.iter()
512 }
513}
514
515impl<'a, T, W: Work> IntoIterator for &'a mut OwnedVec<T, W> {
516 type Item = &'a mut T;
517 type IntoIter = core::slice::IterMut<'a, T>;
518 fn into_iter(self) -> Self::IntoIter {
519 self.iter_mut()
520 }
521}
522
523pub struct OwnedIter<T, W: Work = Memory> {
525 values: allocator_api2::vec::IntoIter<T, allocator::AccountAllocator<W>>,
526}
527
528impl<T, W: Work> Iterator for OwnedIter<T, W> {
529 type Item = T;
530 fn next(&mut self) -> Option<T> {
531 self.values.next()
532 }
533 fn size_hint(&self) -> (usize, Option<usize>) {
534 self.values.size_hint()
535 }
536}
537
538impl<T, W: Work> DoubleEndedIterator for OwnedIter<T, W> {
539 fn next_back(&mut self) -> Option<T> {
540 self.values.next_back()
541 }
542}
543
544impl<T, W: Work> ExactSizeIterator for OwnedIter<T, W> {}
545impl<T, W: Work> core::iter::FusedIterator for OwnedIter<T, W> {}
546
547impl<T, W: Work> IntoIterator for OwnedVec<T, W> {
548 type Item = T;
549 type IntoIter = OwnedIter<T, W>;
550 fn into_iter(self) -> Self::IntoIter {
551 OwnedIter {
552 values: self.values.into_iter(),
553 }
554 }
555}
556
557#[cfg(test)]
558mod tests;