usecrate::page::{
slot::{Generation, RefCount},
Addr,
}; usecrate::Pack; use std::{fmt, marker::PhantomData}; /// Configuration parameters which can be overridden to tune the behavior of a slab. pubtrait Config: Sized { /// The maximum number of threads which can access the slab. /// /// This value (rounded to a power of two) determines the number of shards /// in the slab. If a thread is created, accesses the slab, and then terminates, /// its shard may be reused and thus does not count against the maximum /// number of threads once the thread has terminated. const MAX_THREADS: usize = DefaultConfig::MAX_THREADS; /// The maximum number of pages in each shard in the slab. /// /// This value, in combination with `INITIAL_PAGE_SIZE`, determines how many /// bits of each index are used to represent page addresses. const MAX_PAGES: usize = DefaultConfig::MAX_PAGES; /// The size of the first page in each shard. /// /// When a page in a shard has been filled with values, a new page /// will be allocated that is twice as large as the previous page. Thus, the /// second page will be twice this size, and the third will be four times /// this size, and so on. /// /// Note that page sizes must be powers of two. If this value is not a power /// of two, it will be rounded to the next power of two. const INITIAL_PAGE_SIZE: usize = DefaultConfig::INITIAL_PAGE_SIZE; /// Sets a number of high-order bits in each index which are reserved from /// user code. /// /// Note that these bits are taken from the generation counter; if the page /// address and thread IDs are configured to use a large number of bits, /// reserving additional bits will decrease the period of the generation /// counter. These should thus be used relatively sparingly, to ensure that /// generation counters are able to effectively prevent the ABA problem. const RESERVED_BITS: usize = 0;
}
// Configure the slab with a very large number of bits for the generation // counter. This will only leave 1 bit to use for the slot reference // counter, which will fail to validate. impl Config for GiantGenConfig { const INITIAL_PAGE_SIZE: usize = 1; const MAX_THREADS: usize = 1; const MAX_PAGES: usize = 1;
}
let _slab = Slab::<usize>::new_with_config::<GiantGenConfig>();
}
#[test] #[cfg_attr(loom, ignore)] fn big() { let slab = Slab::new();
for i in0..10000 {
println!("{:?}", i); let k = slab.insert(i).expect("insert");
assert_eq!(slab.get(k).expect("get"), i);
}
}
#[test] #[cfg_attr(loom, ignore)] fn custom_page_sz() { let slab = Slab::new_with_config::<test_util::TinyConfig>();
for i in0..4096 {
println!("{}", i); let k = slab.insert(i).expect("insert");
assert_eq!(slab.get(k).expect("get"), i);
}
}
}
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.