Use static mut over UnsafeCell

This commit is contained in:
jD91mZM2
2018-06-22 22:12:29 +02:00
parent ea24699798
commit 12ce441f5c
+9 -22
View File
@@ -1,25 +1,12 @@
use core::cell::UnsafeCell;
use core::sync::atomic::AtomicBool;
use core::ptr;
use super::{constants, internal, BUFSIZ, FILE, UNGET};
struct GlobalFile(UnsafeCell<FILE>);
impl GlobalFile {
const fn new(file: FILE) -> Self {
GlobalFile(UnsafeCell::new(file))
}
fn get(&self) -> *mut FILE {
self.0.get()
}
}
// statics need to be Sync
unsafe impl Sync for GlobalFile {}
#[allow(non_upper_case_globals)]
static mut default_stdin_buf: [u8; BUFSIZ as usize + UNGET] = [0; BUFSIZ as usize + UNGET];
#[allow(non_upper_case_globals)]
static mut default_stdin: GlobalFile = GlobalFile::new(FILE {
static mut default_stdin: FILE = FILE {
flags: constants::F_PERM | constants::F_NOWR | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
@@ -32,13 +19,13 @@ static mut default_stdin: GlobalFile = GlobalFile::new(FILE {
buf_char: -1,
unget: UNGET,
lock: AtomicBool::new(false),
});
};
#[allow(non_upper_case_globals)]
static mut default_stdout_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stdout: GlobalFile = GlobalFile::new(FILE {
static mut default_stdout: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
@@ -51,13 +38,13 @@ static mut default_stdout: GlobalFile = GlobalFile::new(FILE {
buf_char: b'\n' as i8,
unget: 0,
lock: AtomicBool::new(false),
});
};
#[allow(non_upper_case_globals)]
static mut default_stderr_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stderr: GlobalFile = GlobalFile::new(FILE {
static mut default_stderr: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
@@ -70,19 +57,19 @@ static mut default_stderr: GlobalFile = GlobalFile::new(FILE {
buf_char: -1,
unget: 0,
lock: AtomicBool::new(false),
});
};
#[no_mangle]
pub extern "C" fn __stdin() -> *mut FILE {
unsafe { default_stdin.get() }
unsafe { &mut default_stdin }
}
#[no_mangle]
pub extern "C" fn __stdout() -> *mut FILE {
unsafe { default_stdout.get() }
unsafe { &mut default_stdout }
}
#[no_mangle]
pub extern "C" fn __stderr() -> *mut FILE {
unsafe { default_stderr.get() }
unsafe { &mut default_stderr }
}