Skip to content

Commit 91c3eee

Browse files
committed
[unix] Don't clone command-line args on startup
1 parent d96cc6e commit 91c3eee

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/libstd/sys/unix/args.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -69,57 +69,50 @@ impl DoubleEndedIterator for Args {
6969
target_os = "fuchsia"))]
7070
mod imp {
7171
use os::unix::prelude::*;
72-
use mem;
72+
use ptr;
7373
use ffi::{CStr, OsString};
7474
use marker::PhantomData;
7575
use libc;
7676
use super::Args;
7777

7878
use sys_common::mutex::Mutex;
7979

80-
static mut GLOBAL_ARGS_PTR: usize = 0;
80+
static mut ARGC: isize = 0;
81+
static mut ARGV: *const *const u8 = ptr::null();
8182
static LOCK: Mutex = Mutex::new();
8283

8384
pub unsafe fn init(argc: isize, argv: *const *const u8) {
84-
let args = (0..argc).map(|i| {
85-
CStr::from_ptr(*argv.offset(i) as *const libc::c_char).to_bytes().to_vec()
86-
}).collect();
87-
8885
LOCK.lock();
89-
let ptr = get_global_ptr();
90-
assert!((*ptr).is_none());
91-
(*ptr) = Some(box args);
86+
ARGC = argc;
87+
ARGV = argv;
9288
LOCK.unlock();
9389
}
9490

9591
pub unsafe fn cleanup() {
9692
LOCK.lock();
97-
*get_global_ptr() = None;
93+
ARGC = 0;
94+
ARGV = ptr::null();
9895
LOCK.unlock();
9996
}
10097

10198
pub fn args() -> Args {
102-
let bytes = clone().unwrap_or(Vec::new());
103-
let v: Vec<OsString> = bytes.into_iter().map(|v| {
104-
OsStringExt::from_vec(v)
105-
}).collect();
106-
Args { iter: v.into_iter(), _dont_send_or_sync_me: PhantomData }
99+
Args {
100+
iter: clone().into_iter(),
101+
_dont_send_or_sync_me: PhantomData
102+
}
107103
}
108104

109-
fn clone() -> Option<Vec<Vec<u8>>> {
105+
fn clone() -> Vec<OsString> {
110106
unsafe {
111107
LOCK.lock();
112-
let ptr = get_global_ptr();
113-
let ret = (*ptr).as_ref().map(|s| (**s).clone());
108+
let ret = (0..ARGC).map(|i| {
109+
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const libc::c_char);
110+
OsStringExt::from_vec(cstr.to_bytes().to_vec())
111+
}).collect();
114112
LOCK.unlock();
115113
return ret
116114
}
117115
}
118-
119-
fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
120-
unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
121-
}
122-
123116
}
124117

125118
#[cfg(any(target_os = "macos",

0 commit comments

Comments
 (0)