/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! A generic backing store for caches. //! //! `FreeList` is a simple vector-backed data structure where each entry in the //! vector contains an Option<T>. It maintains an index-based (rather than //! pointer-based) free list to efficiently locate the next unused entry. If all //! entries are occupied, insertion appends a new element to the vector. //! //! It also supports both strong and weak handle semantics. There is exactly one //! (non-Clonable) strong handle per occupied entry, which must be passed by //! value into `free()` to release an entry. Strong handles can produce an //! unlimited number of (Clonable) weak handles, which are used to perform //! lookups which may fail of the entry has been freed. A per-entry epoch ensures //! that weak handle lookups properly fail even if the entry has been freed and //! reused. //! //! TODO(gw): Add an occupied list head, for fast iteration of the occupied list //! to implement retain() style functionality.
use std::{fmt, u32}; use std::marker::PhantomData;
/// Returns true if this handle and the supplied weak handle reference /// the same underlying location in the freelist. pubfn matches(&self, weak_handle: &WeakFreeListHandle<M>) -> bool { self.index == weak_handle.index && self.epoch == weak_handle.epoch
}
}
impl<T, M> FreeList<T, M> { /// Mints a new `FreeList` with no entries. /// /// Triggers a 1-entry allocation. pubfn new() -> Self { // We guarantee that we never have zero entries by starting with one // free entry. This allows WeakFreeListHandle::invalid() to work // without adding any additional branches. let first_slot = Slot {
next: None,
epoch: Epoch::new(),
value: None,
};
FreeList {
slots: vec![first_slot],
free_list_head: Some(0),
active_count: 0,
_marker: PhantomData,
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.