1
Fork 0

refactor[std]: do not use box syntax

This commit is contained in:
joboet 2023-01-17 14:08:35 +01:00
parent 159ba8a92c
commit 7f2cf19191
No known key found for this signature in database
GPG key ID: 704E0149B0194B3C
9 changed files with 35 additions and 59 deletions

View file

@ -57,39 +57,34 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
#[cfg(target_os = "macos")]
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
use crate::cell::Cell;
use crate::mem;
use crate::ptr;
#[thread_local]
static REGISTERED: Cell<bool> = Cell::new(false);
#[thread_local]
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
if !REGISTERED.get() {
_tlv_atexit(run_dtors, ptr::null_mut());
REGISTERED.set(true);
}
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
#[thread_local]
static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
if DTORS.get().is_null() {
let v: Box<List> = box Vec::new();
DTORS.set(Box::into_raw(v));
}
extern "C" {
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
}
let list: &mut List = &mut *DTORS.get();
let list = &mut DTORS;
list.push((t, dtor));
unsafe extern "C" fn run_dtors(_: *mut u8) {
let mut ptr = DTORS.replace(ptr::null_mut());
while !ptr.is_null() {
let list = Box::from_raw(ptr);
for (ptr, dtor) in list.into_iter() {
let mut list = mem::take(&mut DTORS);
while !list.is_empty() {
for (ptr, dtor) in list {
dtor(ptr);
}
ptr = DTORS.replace(ptr::null_mut());
list = mem::take(&mut DTORS);
}
}
}