refactor(pal/hermit): make ENV a non-mutable static

Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
This commit is contained in:
Martin Kröning 2024-07-31 13:14:29 +02:00
parent 127b469b7e
commit b21af629ef
Failed to extract signature

View File

@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
unsupported()
}
static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);
pub fn init_environment(env: *const *const i8) {
let mut guard = ENV.lock().unwrap();
let map = guard.insert(HashMap::new());
if env.is_null() {
return;
}
unsafe {
ENV = Some(Mutex::new(HashMap::new()));
if env.is_null() {
return;
}
let mut guard = ENV.as_ref().unwrap().lock().unwrap();
let mut environ = env;
while !(*environ).is_null() {
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
guard.insert(key, value);
map.insert(key, value);
}
environ = environ.add(1);
}
@ -154,30 +154,26 @@ fn size_hint(&self) -> (usize, Option<usize>) {
/// Returns a vector of (variable, value) byte-vector pairs for all the
/// environment variables of the current process.
pub fn env() -> Env {
unsafe {
let guard = ENV.as_ref().unwrap().lock().unwrap();
let mut result = Vec::new();
let guard = ENV.lock().unwrap();
let env = guard.as_ref().unwrap();
for (key, value) in guard.iter() {
result.push((key.clone(), value.clone()));
}
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();
return Env { iter: result.into_iter() };
}
Env { iter: result.into_iter() }
}
pub fn getenv(k: &OsStr) -> Option<OsString> {
unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
ENV.lock().unwrap().as_ref().unwrap().get(k).cloned()
}
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
let (k, v) = (k.to_owned(), v.to_owned());
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
ENV.lock().unwrap().as_mut().unwrap().insert(k, v);
Ok(())
}
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
ENV.as_ref().unwrap().lock().unwrap().remove(k);
ENV.lock().unwrap().as_mut().unwrap().remove(k);
Ok(())
}