id stringlengths 22 133 | text stringlengths 40 40.2k | arch stringclasses 1
value | syntax stringclasses 1
value | kind stringclasses 4
values | repo stringclasses 27
values | path stringlengths 5 116 | license stringclasses 6
values | commit stringlengths 40 40 | source_host stringclasses 1
value | category stringclasses 16
values | source_url stringlengths 85 196 | line_start int64 1 4.28k | line_end int64 4 4.31k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tokio-rs/tokio:tokio/src/util/slab.rs:13 | /// Release a slot into the page's free list.
fn release(&self, value: *const Value<T>) {
let mut locked = self.slots.lock();
let idx = locked.index_for(value);
locked.slots[idx].next = locked.head as u32;
locked.head = idx;
locked.used -= 1;
self.used.store(locked.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:14 | }
}
impl<T> Default for CachedPage<T> {
fn default() -> CachedPage<T> {
CachedPage {
slots: ptr::null(),
init: 0,
}
}
}
impl<T> Slots<T> {
/// Maps a slot pointer to an offset within the current page.
///
/// The pointer math removes the `usize` index from t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/util/slab.rs:15 | }
impl<T: Entry> Slot<T> {
/// Generates a `Ref` for the slot. This involves bumping the page's ref count.
fn gen_ref(&self, page: &Arc<Page<T>>) -> Ref<T> {
// The ref holds a ref on the page. The `Arc` is forgotten here and is
// resurrected in `release` when the `Ref` is dropped. By avoiding... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/util/slab.rs:16 | }
pub(crate) const fn as_usize(self) -> usize {
self.0
}
pub(crate) fn from_usize(src: usize) -> Address {
Address(src)
}
}
fn debug<T>(fmt: &mut fmt::Formatter<'_>, name: &str, pages: &[Arc<Page<T>>]) -> fmt::Result {
let mut capacity = 0;
let mut len = 0;
for page in pa... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/util/slab.rs:17 | fn default() -> Foo {
Foo {
cnt: AtomicUsize::new(0),
id: AtomicUsize::new(0),
}
}
}
impl Entry for Foo {
fn reset(&self) {
self.cnt.fetch_add(1, SeqCst);
}
}
#[test]
fn insert_remove() {
let mut sl... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/util/slab.rs:18 | drop(foo2);
drop(foo3);
slab.compact();
// The first page is never released
assert!(slab.get(addr1).is_some());
assert!(slab.get(addr2).is_some());
assert!(slab.get(addr3).is_some());
}
#[test]
fn insert_many() {
let mut slab = Slab::<Foo>::new();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/util/slab.rs:19 | }
#[test]
fn insert_drop_reverse() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.store(i, SeqCst);
entries.push((addr, val... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/util/slab.rs:20 | entries1.push((addr, val, i));
} else {
entries2.push(val);
}
}
drop(entries2);
for (addr, _, i) in &entries1 {
assert_eq!(*i, slab.get(*addr).unwrap().id.load(SeqCst));
}
}
#[test]
fn compact_all() {
let mut slab... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/util/slab.rs:21 | }
}
}
#[test]
fn issue_3014() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for _ in 0..5 {
entries.clear();
// Allocate a few pages + 1
for i in 0..(32 + 64 + 128 + 1) {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 03969cdae7674681d1b10926e6a56fbb8908dbb8 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/03969cdae7674681d1b10926e6a56fbb8908dbb8/tokio/src/util/slab.rs | 801 | 841 |
tokio-rs/tokio:tokio/src/util/slab.rs:2 | /// for using the slab with the I/O driver. Addresses are registered with the
/// OS's selector and I/O resources can be released without synchronizing with
/// the OS.
///
/// # Compaction
///
/// `Slab::compact` will release pages that have been allocated but are no
/// longer used. This is done by scanning the pages... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab.rs:3 | /// the slot, the generations are compared. If they match, the value matches the
/// address.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) struct Address(usize);
/// An entry in the slab.
pub(crate) trait Entry: Default {
/// Reset the entry's value and track the generation.
fn reset(&self);
}
/// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/util/slab.rs:4 | }
struct CachedPage<T> {
/// Pointer to the page's slots.
slots: *const Slot<T>,
/// Number of initialized slots.
init: usize,
}
/// Page state
struct Slots<T> {
/// Slots
slots: Vec<Slot<T>>,
head: usize,
/// Number of slots currently in use.
used: usize,
}
unsafe impl<T: Sync... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/util/slab.rs:5 | /// Value paired with a reference to the page
struct Value<T> {
/// Value stored in the value
value: T,
/// Pointer to the page containing the slot.
///
/// A raw pointer is used as this creates a ref cycle.
page: *const Page<T>,
}
impl<T> Slab<T> {
/// Create a new, empty, slab
pub(cr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/util/slab.rs:11 | // Update the free stack head to point to the next slot.
locked.head = slot.next as usize;
// Increment the number of used slots
locked.used += 1;
me.used.store(locked.used, Relaxed);
// Reset the slot
slot.value.with(|ptr| unsafe { (*ptr).value.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/util/slab.rs:12 | me.used.store(locked.used, Relaxed);
me.allocated.store(true, Relaxed);
debug_assert_eq!(locked.slots.len(), locked.head);
Some((me.addr(idx), locked.slots[idx].gen_ref(me)))
}
}
}
impl<T> Page<T> {
/// Returns the slot index within the current page referenced by t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/util/slab.rs:13 | /// Release a slot into the page's free list
fn release(&self, value: *const Value<T>) {
let mut locked = self.slots.lock();
let idx = locked.index_for(value);
locked.slots[idx].next = locked.head as u32;
locked.head = idx;
locked.used -= 1;
self.used.store(locked.u... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:14 | }
}
impl<T> Default for CachedPage<T> {
fn default() -> CachedPage<T> {
CachedPage {
slots: ptr::null(),
init: 0,
}
}
}
impl<T> Slots<T> {
/// Maps a slot pointer to an offset within the current page.
///
/// The pointer math removes the `usize` index from t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/util/slab.rs:15 | }
impl<T: Entry> Slot<T> {
/// Generates a `Ref` for the slot. This involves bumping the page's ref count.
fn gen_ref(&self, page: &Arc<Page<T>>) -> Ref<T> {
// The ref holds a ref on the page. The `Arc` is forgotten here and is
// resurrected in `release` when the `Ref` is dropped. By avoiding... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 1e2f893da2f33deef7fb7c6dc62b265f454c4456 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1e2f893da2f33deef7fb7c6dc62b265f454c4456/tokio/src/util/slab.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/util/slab.rs:7 | // used and the data is `Sync`.
Some(self.cached[page_idx].get(slot_idx))
}
/// Calls the given function with a reference to each slot in the slab. The
/// slot may not be in-use.
///
/// This is used by the I/O driver during the shutdown process to notify
/// each pending task.
pub... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | b48fec96551ac95768b76102703c4039a64c1168 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b48fec96551ac95768b76102703c4039a64c1168/tokio/src/util/slab.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/util/slab.rs:8 | let mut slots = match page.slots.try_lock() {
Some(slots) => slots,
// If the lock cannot be acquired due to being held by another
// thread, don't try to compact the page.
_ => continue,
};
if slots.used > 0 || slots.slots.capacit... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | b48fec96551ac95768b76102703c4039a64c1168 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b48fec96551ac95768b76102703c4039a64c1168/tokio/src/util/slab.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/util/slab.rs:7 | // used and the data is `Sync`.
Some(self.cached[page_idx].get(slot_idx))
}
/// Calls the given function with a reference to each slot in the slab. The
/// slot may not be in-use.
///
/// This is used by the I/O driver during the shutdown process to notify
/// each pending task.
pub... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/util/slab.rs:8 | let mut slots = match page.slots.try_lock() {
Some(slots) => slots,
// If the lock cannot be acquired due to being held by another
// thread, don't try to compact the page.
_ => continue,
};
if slots.used > 0 || slots.slots.capacit... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/util/slab.rs:9 | /// Allocate a new entry and return a handle to the entry.
///
/// Scans pages from smallest to biggest, stopping when a slot is found.
/// Pages are allocated if necessary.
///
/// Returns `None` if the slab is full.
pub(crate) fn allocate(&self) -> Option<(Address, Ref<T>)> {
// Find t... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/util/slab.rs:10 | }
impl<T: fmt::Debug> fmt::Debug for Ref<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(fmt)
}
}
impl<T: Entry> Page<T> {
// Allocates an object, returns the ref and address.
//
// `self: &Arc<Page<T>>` is avoided here as this would not work with the
// ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/util/slab.rs:11 | // Reset the slot
slot.value.with(|ptr| unsafe { (*ptr).value.reset() });
// Return a reference to the slot
Some((me.addr(idx), slot.gen_ref(me)))
} else if me.len == locked.slots.len() {
// The page is full
None
} else {
// No ini... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/util/slab.rs:12 | }
}
impl<T> Page<T> {
/// Returns the slot index within the current page referenced by the given
/// address.
fn slot(&self, addr: Address) -> usize {
addr.0 - self.prev_len
}
/// Returns the address for the given slot
fn addr(&self, slot: usize) -> Address {
Address(slot + sel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/util/slab.rs:13 | locked.used -= 1;
self.used.store(locked.used, Relaxed);
}
}
impl<T> CachedPage<T> {
/// Refresh the cache
fn refresh(&mut self, page: &Page<T>) {
let slots = page.slots.lock();
self.slots = slots.slots.as_ptr();
self.init = slots.slots.len();
}
// Get a value by i... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:14 | }
impl<T> Slots<T> {
/// Maps a slot pointer to an offset within the current page.
///
/// The pointer math removes the `usize` index from the `Ref` struct,
/// shrinking the struct to a single pointer size. The contents of the
/// function is safe, the resulting `usize` is bounds checked before be... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/util/slab.rs:15 | let slot = self as *const Slot<T>;
let value = slot as *const Value<T>;
Ref { value }
}
}
impl<T> Value<T> {
// Release the slot, returning the `Arc<Page<T>>` logically owned by the ref.
fn release(&self) -> Arc<Page<T>> {
// Safety: called by `Ref`, which owns an `Arc<Page<T>>` in... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/util/slab.rs:16 | fn debug<T>(fmt: &mut fmt::Formatter<'_>, name: &str, pages: &[Arc<Page<T>>]) -> fmt::Result {
let mut capacity = 0;
let mut len = 0;
for page in pages {
if page.allocated.load(Relaxed) {
capacity += page.len;
len += page.used.load(Relaxed);
}
}
fmt.debug_st... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/util/slab.rs:17 | self.cnt.fetch_add(1, SeqCst);
}
}
#[test]
fn insert_remove() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let (addr1, foo1) = alloc.allocate().unwrap();
foo1.id.store(1, SeqCst);
assert_eq!(0, foo1.cnt.load(SeqCst));
let (addr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/util/slab.rs:18 | #[test]
fn insert_many() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.store(i, SeqCst);
entries.push((addr, val));
}
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/util/slab.rs:19 | val.id.store(i, SeqCst);
entries.push((addr, val));
}
for _ in 0..10 {
// Drop 1000 in reverse
for _ in 0..1_000 {
entries.pop();
}
// Check remaining
for (i, (addr, v)) in entries.iter().enumerate() {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/util/slab.rs:20 | }
}
#[test]
fn compact_all() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for _ in 0..2 {
entries.clear();
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | c90681bd8e629b5fde988b9f5be7b915e5cf8ae5 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/c90681bd8e629b5fde988b9f5be7b915e5cf8ae5/tokio/src/util/slab.rs | 761 | 794 |
tokio-rs/tokio:tokio/src/util/slab.rs:2 | /// the OS.
///
/// # Compaction
///
/// `Slab::compact` will release pages that have been allocated but are no
/// longer used. This is done by scanning the pages and finding pages with no
/// allocated objects. These pages are then freed.
///
/// # Synchronization
///
/// The `Slab` structure is able to provide (most... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab.rs:3 | #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) struct Address(usize);
/// An entry in the slab.
pub(crate) trait Entry: Default {
/// Reset the entry's value and track the generation.
fn reset(&self);
}
/// A reference to a value stored in the slab
pub(crate) struct Ref<T> {
value: *const Value<T... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/util/slab.rs:4 | struct CachedPage<T> {
/// Pointer to the page's slots.
slots: *const Slot<T>,
/// Number of initialized slots.
init: usize,
}
/// Page state
struct Slots<T> {
/// Slots
slots: Vec<Slot<T>>,
head: usize,
/// Number of slots currently in use.
used: usize,
}
unsafe impl<T: Sync> S... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/util/slab.rs:5 | struct Value<T> {
/// Value stored in the value
value: T,
/// Pointer to the page containing the slot.
///
/// A raw pointer is used as this creates a ref cycle.
page: *const Page<T>,
}
impl<T> Slab<T> {
/// Create a new, empty, slab
pub(crate) fn new() -> Slab<T> {
// Initiali... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/util/slab.rs:6 | slab
}
/// Returns a new `Allocator`.
///
/// The `Allocator` supports concurrent allocation of objects.
pub(crate) fn allocator(&self) -> Allocator<T> {
Allocator {
pages: self.pages.clone(),
}
}
/// Returns a reference to the value stored at the given address.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/util/slab.rs:7 | }
/// Calls the given function with a reference to each slot in the slab. The
/// slot may not be in-use.
///
/// This is used by the I/O driver during the shutdown process to notify
/// each pending task.
pub(crate) fn for_each(&mut self, mut f: impl FnMut(&T)) {
for page_idx in 0..sel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/util/slab.rs:8 | Some(slots) => slots,
// If the lock cannot be acquired due to being held by another
// thread, don't try to compact the page.
_ => continue,
};
if slots.used > 0 || slots.slots.capacity() == 0 {
// The page is in use or it has not... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/util/slab.rs:9 | /// Scans pages from smallest to biggest, stopping when a slot is found.
/// Pages are allocated if necessary.
///
/// Returns `None` if the slab is full.
pub(crate) fn allocate(&self) -> Option<(Address, Ref<T>)> {
// Find the first available slot.
for page in &self.pages[..] {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/util/slab.rs:10 | impl<T: fmt::Debug> fmt::Debug for Ref<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(fmt)
}
}
impl<T: Entry> Page<T> {
// Allocates an object, returns the ref and address.
//
// `self: &Arc<Page<T>>` is avoided here as this would not work with the
// loo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/util/slab.rs:11 | // Return a reference to the slot
Some((me.addr(idx), slot.gen_ref(me)))
} else if me.len == locked.slots.len() {
// The page is full
None
} else {
// No initialized slots are available, but the page has more
// capacity. Initialize a new slot.... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/util/slab.rs:12 | impl<T> Page<T> {
/// Returns the slot index within the current page referenced by the given
/// address.
fn slot(&self, addr: Address) -> usize {
addr.0 - self.prev_len
}
/// Returns the address for the given slot
fn addr(&self, slot: usize) -> Address {
Address(slot + self.pre... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/util/slab.rs:13 | self.used.store(locked.used, Relaxed);
}
}
impl<T> CachedPage<T> {
/// Refresh the cache
fn refresh(&mut self, page: &Page<T>) {
let slots = page.slots.lock();
self.slots = slots.slots.as_ptr();
self.init = slots.slots.len();
}
// Get a value by index
fn get(&self, idx:... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:14 | impl<T> Slots<T> {
/// Maps a slot pointer to an offset within the current page.
///
/// The pointer math removes the `usize` index from the `Ref` struct,
/// shrinking the struct to a single pointer size. The contents of the
/// function is safe, the resulting `usize` is bounds checked before being... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/util/slab.rs:15 | Ref { value }
}
}
impl<T> Value<T> {
// Release the slot, returning the `Arc<Page<T>>` logically owned by the ref.
fn release(&self) -> Arc<Page<T>> {
// Safety: called by `Ref`, which owns an `Arc<Page<T>>` instance.
let page = unsafe { Arc::from_raw(self.page) };
page.release(self... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/util/slab.rs:16 | let mut capacity = 0;
let mut len = 0;
for page in pages {
if page.allocated.load(Relaxed) {
capacity += page.len;
len += page.used.load(Relaxed);
}
}
fmt.debug_struct(name)
.field("len", &len)
.field("capacity", &capacity)
.finish()
}
#... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/util/slab.rs:17 | }
#[test]
fn insert_remove() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let (addr1, foo1) = alloc.allocate().unwrap();
foo1.id.store(1, SeqCst);
assert_eq!(0, foo1.cnt.load(SeqCst));
let (addr2, foo2) = alloc.allocate().unwrap();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/util/slab.rs:18 | fn insert_many() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.store(i, SeqCst);
entries.push((addr, val));
}
for ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/util/slab.rs:19 | }
for _ in 0..10 {
// Drop 1000 in reverse
for _ in 0..1_000 {
entries.pop();
}
// Check remaining
for (i, (addr, v)) in entries.iter().enumerate() {
assert_eq!(i, v.id.load(SeqCst));
assert_eq!(i, slab... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/util/slab.rs:20 | #[test]
fn compact_all() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for _ in 0..2 {
entries.clear();
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.st... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 444660664b96f758610a0e7201a6a1a31a0f2405 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/444660664b96f758610a0e7201a6a1a31a0f2405/tokio/src/util/slab.rs | 761 | 792 |
tokio-rs/tokio:tokio/src/util/slab.rs:7 | }
/// Calls the given function with a reference to each slot in the slab. The
/// slot may not be in-use.
///
/// This is used by the I/O driver during the shutdown process to notify
/// each pending task.
pub(crate) fn for_each(&mut self, mut f: impl FnMut(&T)) {
for page_idx in 0..sel... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/util/slab.rs:8 | Ok(slots) => slots,
// If the lock cannot be acquired due to being held by another
// thread, don't try to compact the page.
_ => continue,
};
if slots.used > 0 || slots.slots.capacity() == 0 {
// The page is in use or it has not y... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/util/slab.rs:9 | /// Scans pages from smallest to biggest, stopping when a slot is found.
/// Pages are allocated if necessary.
///
/// Returns `None` if the slab is full.
pub(crate) fn allocate(&self) -> Option<(Address, Ref<T>)> {
// Find the first available slot.
for page in &self.pages[..] {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/util/slab.rs:10 | impl<T: fmt::Debug> fmt::Debug for Ref<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(fmt)
}
}
impl<T: Entry> Page<T> {
// Allocates an object, returns the ref and address.
//
// `self: &Arc<Page<T>>` is avoided here as this would not work with the
// loo... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/util/slab.rs:12 | impl<T> Page<T> {
/// Returns the slot index within the current page referenced by the given
/// address.
fn slot(&self, addr: Address) -> usize {
addr.0 - self.prev_len
}
/// Returns the address for the given slot
fn addr(&self, slot: usize) -> Address {
Address(slot + self.pre... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/util/slab.rs:13 | self.used.store(locked.used, Relaxed);
}
}
impl<T> CachedPage<T> {
/// Refresh the cache
fn refresh(&mut self, page: &Page<T>) {
let slots = page.slots.lock().unwrap();
self.slots = slots.slots.as_ptr();
self.init = slots.slots.len();
}
// Get a value by index
fn get(&s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | a0557840eb424e174bf81a0175c40f9e176a2cc2 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a0557840eb424e174bf81a0175c40f9e176a2cc2/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:4 | struct CachedPage<T> {
/// Pointer to the page's slots.
slots: *const Slot<T>,
/// Number of initialized slots.
init: usize,
}
/// Page state
struct Slots<T> {
/// Slots
slots: Vec<Slot<T>>,
head: usize,
/// Number of slots currently in use.
used: usize,
}
unsafe impl<T: Sync> S... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/util/slab.rs:5 | value: T,
/// Pointer to the page containing the slot.
///
/// A raw pointer is used as this creates a ref cycle.
page: *const Page<T>,
}
impl<T> Slab<T> {
/// Create a new, empty, slab
pub(crate) fn new() -> Slab<T> {
// Initializing arrays is a bit annoying. Instead of manually writi... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/util/slab.rs:6 | /// Returns a new `Allocator`.
///
/// The `Allocator` supports concurrent allocation of objects.
pub(crate) fn allocator(&self) -> Allocator<T> {
Allocator {
pages: self.pages.clone(),
}
}
/// Returns a reference to the value stored at the given address.
///
///... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/util/slab.rs:7 | /// Calls the given function with a reference to each slot in the slab. The
/// slot may not be in-use.
///
/// This is used by the I/O driver during the shutdown process to notify
/// each pending task.
pub(crate) fn for_each(&mut self, mut f: impl FnMut(&T)) {
for page_idx in 0..self.pages... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/util/slab.rs:8 | // thread, don't try to compact the page.
_ => continue,
};
if slots.used > 0 || slots.slots.capacity() == 0 {
// The page is in use or it has not yet been allocated. Either
// way, there is no more work to do.
continue;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/util/slab.rs:9 | ///
/// Returns `None` if the slab is full.
pub(crate) fn allocate(&self) -> Option<(Address, Ref<T>)> {
// Find the first available slot.
for page in &self.pages[..] {
if let Some((addr, val)) = Page::allocate(page) {
return Some((addr, val));
}
}... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/util/slab.rs:10 | (**self).fmt(fmt)
}
}
impl<T: Entry> Page<T> {
// Allocates an object, returns the ref and address.
//
// `self: &Arc<Page<T>>` is avoided here as this would not work with the
// loom `Arc`.
fn allocate(me: &Arc<Page<T>>) -> Option<(Address, Ref<T>)> {
// Before acquiring the lock, use ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/util/slab.rs:11 | Some((me.addr(idx), slot.gen_ref(me)))
} else if me.len == locked.slots.len() {
// The page is full
None
} else {
// No initialized slots are available, but the page has more
// capacity. Initialize a new slot.
let idx = locked.slots.len();
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/util/slab.rs:12 | /// Returns the slot index within the current page referenced by the given
/// address.
fn slot(&self, addr: Address) -> usize {
addr.0 - self.prev_len
}
/// Returns the address for the given slot
fn addr(&self, slot: usize) -> Address {
Address(slot + self.prev_len)
}
}
impl<T... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/util/slab.rs:13 | }
impl<T> CachedPage<T> {
/// Refresh the cache
fn refresh(&mut self, page: &Page<T>) {
let slots = page.slots.lock().unwrap();
self.slots = slots.slots.as_ptr();
self.init = slots.slots.len();
}
// Get a value by index
fn get(&self, idx: usize) -> &T {
assert!(idx ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/util/slab.rs:14 | ///
/// The pointer math removes the `usize` index from the `Ref` struct,
/// shrinking the struct to a single pointer size. The contents of the
/// function is safe, the resulting `usize` is bounds checked before being
/// used.
///
/// # Panics
///
/// panics if the provided slot point... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/util/slab.rs:15 | }
}
impl<T> Value<T> {
// Release the slot, returning the `Arc<Page<T>>` logically owned by the ref.
fn release(&self) -> Arc<Page<T>> {
// Safety: called by `Ref`, which owns an `Arc<Page<T>>` instance.
let page = unsafe { Arc::from_raw(self.page) };
page.release(self as *const _);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/util/slab.rs:16 | for page in pages {
if page.allocated.load(Relaxed) {
capacity += page.len;
len += page.used.load(Relaxed);
}
}
fmt.debug_struct(name)
.field("len", &len)
.field("capacity", &capacity)
.finish()
}
#[cfg(all(test, not(loom)))]
mod test {
use s... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/util/slab.rs:17 | #[test]
fn insert_remove() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let (addr1, foo1) = alloc.allocate().unwrap();
foo1.id.store(1, SeqCst);
assert_eq!(0, foo1.cnt.load(SeqCst));
let (addr2, foo2) = alloc.allocate().unwrap();
foo2.i... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/util/slab.rs:18 | let alloc = slab.allocator();
let mut entries = vec![];
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.store(i, SeqCst);
entries.push((addr, val));
}
for (i, (addr, v)) in entries.iter().enumerate() {
assert_eq!(... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/util/slab.rs:19 | for _ in 0..10 {
// Drop 1000 in reverse
for _ in 0..1_000 {
entries.pop();
}
// Check remaining
for (i, (addr, v)) in entries.iter().enumerate() {
assert_eq!(i, v.id.load(SeqCst));
assert_eq!(i, slab.get(*addr)... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/util/slab.rs:20 | fn compact_all() {
let mut slab = Slab::<Foo>::new();
let alloc = slab.allocator();
let mut entries = vec![];
for _ in 0..2 {
entries.clear();
for i in 0..10_000 {
let (addr, val) = alloc.allocate().unwrap();
val.id.store(i, SeqCs... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab.rs | MIT | 8feebab7cdef2fbeb810d18509b1443b6b9f60b1 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8feebab7cdef2fbeb810d18509b1443b6b9f60b1/tokio/src/util/slab.rs | 761 | 790 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:2 | //!
//! ```text
//! ┌──────────┬───────────────┬──────────────────┬──────────────────────────┐
//! │ reserved │ generation │ thread ID │ address │
//! └▲─────────┴▲──────────────┴▲─────────────────┴▲────────────────────────▲┘
//! │ │ │ ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/addr.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:3 | /// Re-exported by `Generation`.
pub(super) const GENERATION_WIDTH: u32 = GENERATION.width();
pub(super) fn new(shard_index: usize, generation: Generation) -> Address {
let mut repr = 0;
repr = SLOT.pack(shard_index, repr);
repr = GENERATION.pack(generation.to_usize(), repr);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/addr.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:4 | (bit::pointer_width() - slot_shifted.leading_zeros()) as usize
}
/// Returns the slot index
pub(super) fn slot(self) -> usize {
SLOT.unpack(self.0)
}
}
#[cfg(test)]
cfg_not_loom! {
use proptest::proptest;
#[test]
fn test_pack_format() {
assert_eq!(5, RESERVED.width());
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/addr.rs | 121 | 154 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:2 | //!
//! ```text
//! ┌──────────┬───────────────┬──────────────────┬──────────────────────────┐
//! │ reserved │ generation │ thread ID │ address │
//! └▲─────────┴▲──────────────┴▲─────────────────┴▲────────────────────────▲┘
//! │ │ │ ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/addr.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:3 | /// Re-exported by `Generation`.
pub(super) const GENERATION_WIDTH: u32 = GENERATION.width();
pub(super) fn new(shard_index: usize, generation: Generation) -> Address {
let mut repr = 0;
repr = SLOT.pack(shard_index, repr);
repr = GENERATION.pack(generation.to_usize(), repr);
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/addr.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/util/slab/addr.rs:4 | let slot_shifted = (self.slot() + INITIAL_PAGE_SIZE) >> PAGE_INDEX_SHIFT;
(bit::pointer_width() - slot_shifted.leading_zeros()) as usize
}
/// Returns the slot index
pub(super) fn slot(self) -> usize {
SLOT.unpack(self.0)
}
}
#[cfg(test)]
cfg_not_loom! {
use proptest::proptest;
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/addr.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/addr.rs | 121 | 155 |
tokio-rs/tokio:tokio/src/util/slab/entry.rs:1 | use crate::util::slab::Generation;
pub(crate) trait Entry: Default {
fn generation(&self) -> Generation;
fn reset(&self, generation: Generation) -> bool;
} | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/entry.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/entry.rs | 1 | 7 |
tokio-rs/tokio:tokio/src/util/slab/generation.rs:1 | use crate::util::bit;
use crate::util::slab::Address;
/// An mutation identifier for a slot in the slab. The generation helps prevent
/// accessing an entry with an outdated token.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub(crate) struct Generation(usize);
impl Generation {
pub(crate) const... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/generation.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/generation.rs | 1 | 32 |
tokio-rs/tokio:tokio/src/util/slab/mod.rs:1 | //! A lock-free concurrent slab.
mod addr;
pub(crate) use addr::Address;
mod entry;
pub(crate) use entry::Entry;
mod generation;
pub(crate) use generation::Generation;
mod page;
mod shard;
use shard::Shard;
mod slot;
use slot::Slot;
mod stack;
use stack::TransferStack;
#[cfg(all(loom, test))]
mod tests;
use cr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/mod.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/mod.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/util/slab/mod.rs:2 | /// Size of first page
const INITIAL_PAGE_SIZE: usize = 32;
}
cfg_loom! {
const INITIAL_PAGE_SIZE: usize = 2;
}
/// A sharded slab.
pub(crate) struct Slab<T> {
// Signal shard for now. Eventually there will be more.
shard: Shard<T>,
local: Mutex<()>,
}
unsafe impl<T: Send> Send for Slab<T> {}
uns... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/mod.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/mod.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/mod.rs:3 | pub(crate) fn remove(&self, idx: Address) {
// try to lock the slab so that we can use `remove_local`.
let lock = self.local.try_lock();
// if we were able to lock the slab, we are "local" and can use the fast
// path; otherwise, we will use `remove_remote`.
if lock.is_ok() {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/mod.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/mod.rs | 81 | 107 |
tokio-rs/tokio:tokio/src/util/slab/mod.rs:3 | pub(crate) fn remove(&self, idx: Address) {
// try to lock the slab so that we can use `remove_local`.
let lock = self.local.try_lock();
// if we were able to lock the slab, we are "local" and can use the fast
// path; otherwise, we will use `remove_remote`.
if lock.is_ok() {
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/mod.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/mod.rs | 81 | 109 |
tokio-rs/tokio:tokio/src/util/slab/page.rs:1 | use crate::loom::cell::UnsafeCell;
use crate::util::slab::{Address, Entry, Slot, TransferStack, INITIAL_PAGE_SIZE};
use std::fmt;
/// Data accessed only by the thread that owns the shard.
pub(crate) struct Local {
head: UnsafeCell<usize>,
}
/// Data accessed by any thread.
pub(crate) struct Shared<T> {
remot... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/page.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/page.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/util/slab/page.rs:2 | impl<T: Entry> Shared<T> {
pub(crate) fn new(size: usize, prev_sz: usize) -> Shared<T> {
Self {
prev_sz,
size,
remote: TransferStack::new(),
slab: UnsafeCell::new(None),
}
}
/// Allocates storage for this page if it does not allready exist.
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/page.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/page.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/page.rs:3 | // are there any items on the local free list? (fast path)
let head = if head < self.size {
head
} else {
// if the local free list is empty, pop all the items on the remote
// free list onto the local free list.
self.remote.pop_all()?
};
... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/page.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/page.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/util/slab/page.rs:4 | self.slab
.with(|slab| unsafe { &*slab }.as_ref()?.get(page_offset))
.map(|slot| slot.get())
}
pub(crate) fn remove_local(&self, local: &Local, addr: Address) {
let offset = addr.slot() - self.prev_sz;
self.slab.with(|slab| {
let slab = unsafe { &*slab }.as_... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/page.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/page.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/util/slab/page.rs:5 | self.remote.push(offset, |next| slot.set_next(next));
})
}
}
impl fmt::Debug for Local {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.head.with(|head| {
let head = unsafe { *head };
f.debug_struct("Local")
.field("head", &format_args!("... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/page.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/page.rs | 161 | 187 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:1 | use crate::util::slab::{page, Address, Entry, MAX_PAGES};
use std::fmt;
// ┌─────────────┐ ┌────────┐
// │ page 1 │ │ │
// ├─────────────┤ ┌───▶│ next──┼─┐
// │ page 2 │ │ ├────────┤ │
// │ │ │ │XXXXXXXX│ │
// │ local_free──┼─┘ ├────────┤ │
// │ global_free─┼─┐ │ ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/shard.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:2 | let shared = (0..MAX_PAGES)
.map(|page_num| {
let sz = page::size(page_num);
let prev_sz = total_sz;
total_sz += sz;
page::Shared::new(sz, prev_sz)
})
.collect();
let local = (0..MAX_PAGES).map(|_| page::Local::... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/shard.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:3 | if let Some(page) = self.shared.get(page_idx) {
page.remove_local(self.local(page_idx), addr);
}
}
/// Remove an item, while on a different thread from the shard's local thread.
pub(super) fn remove_remote(&self, addr: Address) {
if let Some(page) = self.shared.get(addr.page()) ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 8656b7b8eb6f3635ec40694eb71f14fb84211e05 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/8656b7b8eb6f3635ec40694eb71f14fb84211e05/tokio/src/util/slab/shard.rs | 81 | 105 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:1 | use crate::util::slab::{Address, Entry, page, MAX_PAGES};
use std::fmt;
// ┌─────────────┐ ┌────────┐
// │ page 1 │ │ │
// ├─────────────┤ ┌───▶│ next──┼─┐
// │ page 2 │ │ ├────────┤ │
// │ │ │ │XXXXXXXX│ │
// │ local_free──┼─┘ ├────────┤ │
// │ global_free─┼─┐ │ ... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/shard.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:2 | let shared = (0..MAX_PAGES)
.map(|page_num| {
let sz = page::size(page_num);
let prev_sz = total_sz;
total_sz += sz;
page::Shared::new(sz, prev_sz)
})
.collect();
let local = (0..MAX_PAGES).map(|_| page::Local::... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/shard.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/util/slab/shard.rs:3 | /// Remove an item on the shard's local thread.
pub(super) fn remove_local(&self, addr: Address) {
let page_idx = addr.page();
if let Some(page) = self.shared.get(page_idx) {
page.remove_local(self.local(page_idx), addr);
}
}
/// Remove an item, while on a different thr... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/shard.rs | MIT | 69975fb9601bbb21659db283d888470733bae660 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/69975fb9601bbb21659db283d888470733bae660/tokio/src/util/slab/shard.rs | 81 | 108 |
tokio-rs/tokio:tokio/src/util/slab/slot.rs:1 | use crate::loom::cell::UnsafeCell;
use crate::util::slab::{Entry, Generation};
/// Stores an entry in the slab.
pub(super) struct Slot<T> {
next: UnsafeCell<usize>,
entry: T,
}
impl<T: Entry> Slot<T> {
/// Initialize a new `Slot` linked to `next`.
///
/// The entry is initialized to a default valu... | rust | rust | rust-source | tokio-rs/tokio | tokio/src/util/slab/slot.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/util/slab/slot.rs | 1 | 42 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.