| 1 | // Copyright 2014 The Servo Project Developers. See the COPYRIGHT |
| 2 | // file at the top-level directory of this distribution. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 | // option. This file may not be copied, modified, or distributed |
| 8 | // except according to those terms. |
| 9 | |
| 10 | use parking_lotrust-analyzer cargo parking_lot 0.12.5 crate/::Mutexrust-analyzer cargo parking_lot 0.12.5 mutex/Mutex#; |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#; |
| 14 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#; |
| 15 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#; |
| 16 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#; |
| 17 | |
| 18 | const NB_BUCKETSrust-analyzer cargo string_cache 0.9.0 dynamic_set/NB_BUCKETS.: usize = 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl().12; // 4096 |
| 19 | const BUCKET_MASKrust-analyzer cargo string_cache 0.9.0 dynamic_set/BUCKET_MASK.: u32 = (1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().12) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().1; |
| 20 | |
| 21 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) struct Setrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set# { |
| 22 | bucketsrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set#buckets.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[Mutexrust-analyzer cargo parking_lot 0.12.5 mutex/Mutex#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#>>>]>, |
| 23 | } |
| 24 | |
| 25 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) struct Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry# { |
| 26 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) stringrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#string.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<str>, |
| 27 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) hashrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#hash.: u32, |
| 28 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) ref_countrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#ref_count.: AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#, |
| 29 | next_in_bucketrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#next_in_bucket.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#>>, |
| 30 | } |
| 31 | |
| 32 | // Addresses are a multiples of this, |
| 33 | // and therefore have have TAG_MASK bits unset, available for tagging. |
| 34 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) const ENTRY_ALIGNMENTrust-analyzer cargo string_cache 0.9.0 dynamic_set/ENTRY_ALIGNMENT.: usize = 4; |
| 35 | |
| 36 | #[test] |
| 37 | fn entry_alignment_is_sufficient() { |
| 38 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(mem::align_of::<Entry>() >= ENTRY_ALIGNMENT); |
| 39 | } |
| 40 | |
| 41 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) fn dynamic_setrust-analyzer cargo string_cache 0.9.0 dynamic_set/dynamic_set().() -> &'static Setrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set# { |
| 42 | // NOTE: Using const initialization for buckets breaks the small-stack test. |
| 43 | // ``` |
| 44 | // // buckets: [Mutex<Option<Box<Entry>>>; NB_BUCKETS], |
| 45 | // const MUTEX: Mutex<Option<Box<Entry>>> = Mutex::new(None); |
| 46 | // let buckets = Box::new([MUTEX; NB_BUCKETS]); |
| 47 | // ``` |
| 48 | static DYNAMIC_SETrust-analyzer cargo string_cache 0.9.0 dynamic_set/DYNAMIC_SET.: OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#<Setrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set#> = OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/impl#[`OnceLock<T>`]new().(); |
| 49 | |
| 50 | DYNAMIC_SETrust-analyzer cargo string_cache 0.9.0 dynamic_set/DYNAMIC_SET..get_or_initrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/impl#[`OnceLock<T>`]get_or_init().(|| { |
| 51 | let buckets = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#NB_BUCKETSrust-analyzer cargo string_cache 0.9.0 dynamic_set/NB_BUCKETS.).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|_| Mutexrust-analyzer cargo parking_lot 0.12.5 mutex/Mutex#::newrust-analyzer cargo lock_api 0.4.14 mutex/impl#[`Mutex<R, T>`]new().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 52 | Setrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set# { bucketsrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set#buckets. } |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | impl Setrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set# { |
| 57 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) fn insertrust-analyzer cargo string_cache 0.9.0 dynamic_set/impl#[Set]insert().(&self, string: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>, hash: u32) -> NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#> { |
| 58 | let bucket_index = (hash rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().BUCKET_MASKrust-analyzer cargo string_cache 0.9.0 dynamic_set/BUCKET_MASK.) as usize; |
| 59 | let mut linked_list = self.bucketsrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set#buckets.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().bucket_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lockrust-analyzer cargo lock_api 0.4.14 mutex/impl#[`Mutex<R, T>`]lock().(); |
| 60 | |
| 61 | { |
| 62 | let mut ptr: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&mut Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#>> = linked_list.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().(); |
| 63 | |
| 64 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(entry) = ptr.takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() { |
| 65 | if entry.hashrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#hash. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().hash && *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().entry.stringrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#string. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/impl#[str][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/impl#[str][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/impl#[str][`PartialEq<Self>`]eq().*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().string { |
| 66 | if entry.ref_countrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#ref_count..fetch_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]fetch_add().(1, SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().0 { |
| 67 | return NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`][`From<&mut T>`]from().(&mut *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().entry); |
| 68 | } |
| 69 | // Uh-oh. The pointer's reference count was zero, which means someone may try |
| 70 | // to free it. (Naive attempts to defend against this, for example having the |
| 71 | // destructor check to see whether the reference count is indeed zero, don't |
| 72 | // work due to ABA.) Thus we need to temporarily add a duplicate string to the |
| 73 | // list. |
| 74 | entry.ref_countrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#ref_count..fetch_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]fetch_sub().(1, SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#); |
| 75 | break; |
| 76 | } |
| 77 | ptr = entry.next_in_bucketrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#next_in_bucket..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().(); |
| 78 | } |
| 79 | } |
| 80 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#>() >= ENTRY_ALIGNMENTrust-analyzer cargo string_cache 0.9.0 dynamic_set/ENTRY_ALIGNMENT.); |
| 81 | let string = string.into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().(); |
| 82 | let mut entry = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry# { |
| 83 | next_in_bucketrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#next_in_bucket.: linked_list.takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().(), |
| 84 | hashrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#hash., |
| 85 | ref_countrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#ref_count.: AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]new().(1), |
| 86 | stringrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#string.: string.into_boxed_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_boxed_str().(), |
| 87 | }); |
| 88 | let ptr = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`][`From<&mut T>`]from().(&mut *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().entry); |
| 89 | *rust-analyzer cargo lock_api 0.4.14 mutex/impl#[`MutexGuard<'a, R, T>`][DerefMut]deref_mut().linked_list = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(entry); |
| 90 | ptr |
| 91 | } |
| 92 | |
| 93 | pub(craterust-analyzer cargo string_cache 0.9.0 crate/) fn removerust-analyzer cargo string_cache 0.9.0 dynamic_set/impl#[Set]remove().(&self, ptr: *mut Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#) { |
| 94 | let value: &Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry# = unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().ptr }; |
| 95 | let bucket_index = (value.hashrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#hash. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().BUCKET_MASKrust-analyzer cargo string_cache 0.9.0 dynamic_set/BUCKET_MASK.) as usize; |
| 96 | |
| 97 | let mut linked_list = self.bucketsrust-analyzer cargo string_cache 0.9.0 dynamic_set/Set#buckets.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().bucket_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lockrust-analyzer cargo lock_api 0.4.14 mutex/impl#[`Mutex<R, T>`]lock().(); |
| 98 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(value.ref_countrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#ref_count..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]load().(SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#) == 0); |
| 99 | let mut current: &mut Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#>> = &mut linked_list; |
| 100 | |
| 101 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(entry_ptr) = current.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() { |
| 102 | let entry_ptr: *mut Entryrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry# = &mut *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().entry_ptr; |
| 103 | if entry_ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`][`PartialEq<Self>`]eq().ptr { |
| 104 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(current, unsafe { |
| 105 | (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().entry_ptr).next_in_bucketrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#next_in_bucket..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() |
| 106 | })); |
| 107 | break; |
| 108 | } |
| 109 | current = unsafe { &mut (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().entry_ptr).next_in_bucketrust-analyzer cargo string_cache 0.9.0 dynamic_set/Entry#next_in_bucket. }; |
| 110 | } |
| 111 | } |
| 112 | } |