rusticl: work around reference-to-mutable-static warnings

Creating mutable or shared references to mutable static variables is
discouraged, but still possible by taking a detour to a raw pointer
via the addr_of! / addr_of_mut! macros.

For details, see: https://github.com/rust-lang/rust/issues/114447

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/10887
Cc: mesa-stable
Reviewed-by: Karol Herbst <kherbst@redhat.com>
Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28415>
This commit is contained in:
Philipp Zabel
2024-03-27 10:57:36 +01:00
committed by Marge Bot
parent 437e0cb3cb
commit f4199a9b21
2 changed files with 11 additions and 6 deletions
@@ -7,6 +7,8 @@ use mesa_rust_gen::*;
use rusticl_opencl_gen::*;
use std::env;
use std::ptr::addr_of;
use std::ptr::addr_of_mut;
use std::sync::Once;
#[repr(C)]
@@ -73,7 +75,8 @@ static mut PLATFORM_FEATURES: PlatformFeatures = PlatformFeatures {
};
fn load_env() {
let debug = unsafe { &mut PLATFORM_DBG };
// SAFETY: no other references exist at this point
let debug = unsafe { &mut *addr_of_mut!(PLATFORM_DBG) };
if let Ok(debug_flags) = env::var("RUSTICL_DEBUG") {
for flag in debug_flags.split(',') {
match flag {
@@ -88,7 +91,8 @@ fn load_env() {
}
}
let features = unsafe { &mut PLATFORM_FEATURES };
// SAFETY: no other references exist at this point
let features = unsafe { &mut *addr_of_mut!(PLATFORM_FEATURES) };
if let Ok(feature_flags) = env::var("RUSTICL_FEATURES") {
for flag in feature_flags.split(',') {
match flag {
@@ -109,17 +113,17 @@ impl Platform {
pub fn get() -> &'static Self {
debug_assert!(PLATFORM_ONCE.is_completed());
// SAFETY: no mut references exist at this point
unsafe { &PLATFORM }
unsafe { &*addr_of!(PLATFORM) }
}
pub fn dbg() -> &'static PlatformDebug {
debug_assert!(PLATFORM_ENV_ONCE.is_completed());
unsafe { &PLATFORM_DBG }
unsafe { &*addr_of!(PLATFORM_DBG) }
}
pub fn features() -> &'static PlatformFeatures {
debug_assert!(PLATFORM_ENV_ONCE.is_completed());
unsafe { &PLATFORM_FEATURES }
unsafe { &*addr_of!(PLATFORM_FEATURES) }
}
fn init(&mut self) {
@@ -20,6 +20,7 @@ use std::collections::HashSet;
use std::ffi::CString;
use std::mem::size_of;
use std::ptr;
use std::ptr::addr_of;
use std::slice;
use std::sync::Arc;
use std::sync::Mutex;
@@ -55,7 +56,7 @@ fn get_disk_cache() -> &'static Option<DiskCache> {
DISK_CACHE_ONCE.call_once(|| {
DISK_CACHE = DiskCache::new("rusticl", &func_ptrs, 0);
});
&DISK_CACHE
&*addr_of!(DISK_CACHE)
}
}