std_detect/detect/
cache.rs1#![allow(dead_code)] use core::sync::atomic::{AtomicUsize, Ordering};
7
8#[inline]
10const fn set_bit(x: u128, bit: u32) -> u128 {
11 x | 1 << bit
12}
13
14#[inline]
16const fn test_bit(x: u128, bit: u32) -> bool {
17 x & (1 << bit) != 0
18}
19
20#[inline]
22const fn unset_bit(x: u128, bit: u32) -> u128 {
23 x & !(1 << bit)
24}
25
26const CACHE_CAPACITY: u32 = 93;
28
29#[derive(Copy, Clone, Default, PartialEq, Eq)]
33pub(crate) struct Initializer(u128);
34
35impl Initializer {
38 #[inline]
40 pub(crate) fn test(self, bit: u32) -> bool {
41 debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
42 test_bit(self.0, bit)
43 }
44
45 #[inline]
47 pub(crate) fn set(&mut self, bit: u32) {
48 debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
49 let v = self.0;
50 self.0 = set_bit(v, bit);
51 }
52
53 #[inline]
55 pub(crate) fn unset(&mut self, bit: u32) {
56 debug_assert!(bit < CACHE_CAPACITY, "too many features, time to increase the cache size!");
57 let v = self.0;
58 self.0 = unset_bit(v, bit);
59 }
60}
61
62static CACHE: [Cache; 3] = [Cache::uninitialized(), Cache::uninitialized(), Cache::uninitialized()];
66
67struct Cache(AtomicUsize);
76
77impl Cache {
78 const CAPACITY: u32 = (core::mem::size_of::<usize>() * 8 - 1) as u32;
79 const MASK: usize = (1 << Cache::CAPACITY) - 1;
80 const INITIALIZED_BIT: usize = 1usize << Cache::CAPACITY;
81
82 #[allow(clippy::declare_interior_mutable_const)]
84 const fn uninitialized() -> Self {
85 Cache(AtomicUsize::new(0))
86 }
87
88 #[inline]
90 pub(crate) fn test(&self, bit: u32) -> Option<bool> {
91 let cached = self.0.load(Ordering::Relaxed);
92 if cached == 0 { None } else { Some(test_bit(cached as u128, bit)) }
93 }
94
95 #[inline]
97 fn initialize(&self, value: usize) -> usize {
98 debug_assert_eq!((value & !Cache::MASK), 0);
99 self.0.store(value | Cache::INITIALIZED_BIT, Ordering::Relaxed);
100 value
101 }
102}
103
104cfg_select! {
105 feature = "std_detect_env_override" => {
106 #[inline]
107 fn disable_features(disable: &[u8], value: &mut Initializer) {
108 if let Ok(disable) = core::str::from_utf8(disable) {
109 for v in disable.split(" ") {
110 let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
111 }
112 }
113 }
114
115 #[inline]
116 fn initialize(mut value: Initializer) -> Initializer {
117 use core::ffi::CStr;
118 const RUST_STD_DETECT_UNSTABLE: &CStr = c"RUST_STD_DETECT_UNSTABLE";
119 cfg_select! {
120 windows => {
121 use alloc::vec;
122 #[link(name = "kernel32")]
123 unsafe extern "system" {
124 fn GetEnvironmentVariableA(
125 name: *const u8,
126 buffer: *mut u8,
127 size: u32,
128 ) -> u32;
129 }
130 let len = unsafe {
131 GetEnvironmentVariableA(
132 RUST_STD_DETECT_UNSTABLE.as_ptr().cast::<u8>(),
133 core::ptr::null_mut(),
134 0,
135 )
136 };
137 if len > 0 {
138 let mut env = vec![0; len as usize + 1];
140 let len = unsafe {
141 GetEnvironmentVariableA(
142 RUST_STD_DETECT_UNSTABLE.as_ptr().cast::<u8>(),
143 env.as_mut_ptr(),
144 len + 1,
145 )
146 };
147 if len > 0 {
148 disable_features(&env[..len as usize], &mut value);
149 }
150 }
151 }
152 _ => {
153 let env = unsafe { libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr()) };
154 if !env.is_null() {
155 let len = unsafe { libc::strlen(env) };
156 let env = unsafe { core::slice::from_raw_parts(env as *const u8, len) };
157 disable_features(env, &mut value);
158 }
159 }
160 }
161 do_initialize(value);
162 value
163 }
164 }
165 _ => {
166 #[inline]
167 fn initialize(value: Initializer) -> Initializer {
168 do_initialize(value);
169 value
170 }
171 }
172}
173
174#[inline]
175fn do_initialize(value: Initializer) {
176 CACHE[0].initialize((value.0) as usize & Cache::MASK);
177 CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
178 CACHE[2].initialize((value.0 >> (2 * Cache::CAPACITY)) as usize & Cache::MASK);
179}
180
181#[cold]
191fn detect_and_initialize() -> Initializer {
192 initialize(super::os::detect_features())
193}
194
195#[inline]
208pub(crate) fn test(bit: u32) -> bool {
209 let (relative_bit, idx) = if bit < Cache::CAPACITY {
210 (bit, 0)
211 } else if bit < 2 * Cache::CAPACITY {
212 (bit - Cache::CAPACITY, 1)
213 } else {
214 (bit - 2 * Cache::CAPACITY, 2)
215 };
216 CACHE[idx].test(relative_bit).unwrap_or_else(|| detect_and_initialize().test(bit))
217}