]> git.lizzy.rs Git - rust.git/blob - src/librustrt/args.rs
return &mut T from the arenas, not &T
[rust.git] / src / librustrt / args.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Global storage for command line arguments
12 //!
13 //! The current incarnation of the Rust runtime expects for
14 //! the processes `argc` and `argv` arguments to be stored
15 //! in a globally-accessible location for use by the `os` module.
16 //!
17 //! Only valid to call on Linux. Mac and Windows use syscalls to
18 //! discover the command line arguments.
19 //!
20 //! FIXME #7756: Would be nice for this to not exist.
21
22 use core::prelude::*;
23 use collections::vec::Vec;
24
25 /// One-time global initialization.
26 pub unsafe fn init(argc: int, argv: *const *const u8) { imp::init(argc, argv) }
27
28 /// One-time global cleanup.
29 pub unsafe fn cleanup() { imp::cleanup() }
30
31 /// Take the global arguments from global storage.
32 pub fn take() -> Option<Vec<Vec<u8>>> { imp::take() }
33
34 /// Give the global arguments to global storage.
35 ///
36 /// It is an error if the arguments already exist.
37 pub fn put(args: Vec<Vec<u8>>) { imp::put(args) }
38
39 /// Make a clone of the global arguments.
40 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
41
42 #[cfg(any(target_os = "linux",
43           target_os = "android",
44           target_os = "freebsd",
45           target_os = "dragonfly"))]
46 mod imp {
47     use core::prelude::*;
48
49     use alloc::boxed::Box;
50     use collections::slice::CloneableVector;
51     use collections::vec::Vec;
52     use core::mem;
53     use core::slice;
54
55     use mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
56
57     static mut GLOBAL_ARGS_PTR: uint = 0;
58     static LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
59
60     pub unsafe fn init(argc: int, argv: *const *const u8) {
61         let args = load_argc_and_argv(argc, argv);
62         put(args);
63     }
64
65     pub unsafe fn cleanup() {
66         rtassert!(take().is_some());
67         LOCK.destroy();
68     }
69
70     pub fn take() -> Option<Vec<Vec<u8>>> {
71         with_lock(|| unsafe {
72             let ptr = get_global_ptr();
73             let val = mem::replace(&mut *ptr, None);
74             val.as_ref().map(|s: &Box<Vec<Vec<u8>>>| (**s).clone())
75         })
76     }
77
78     pub fn put(args: Vec<Vec<u8>>) {
79         with_lock(|| unsafe {
80             let ptr = get_global_ptr();
81             rtassert!((*ptr).is_none());
82             (*ptr) = Some(box args.clone());
83         })
84     }
85
86     pub fn clone() -> Option<Vec<Vec<u8>>> {
87         with_lock(|| unsafe {
88             let ptr = get_global_ptr();
89             (*ptr).as_ref().map(|s: &Box<Vec<Vec<u8>>>| (**s).clone())
90         })
91     }
92
93     fn with_lock<T>(f: || -> T) -> T {
94         unsafe {
95             let _guard = LOCK.lock();
96             f()
97         }
98     }
99
100     fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
101         unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
102     }
103
104     unsafe fn load_argc_and_argv(argc: int, argv: *const *const u8) -> Vec<Vec<u8>> {
105         Vec::from_fn(argc as uint, |i| {
106             let base = *argv.offset(i as int);
107             let mut len = 0;
108             while *base.offset(len) != 0 { len += 1; }
109             slice::raw::buf_as_slice(base, len as uint, |slice| {
110                 slice.to_vec()
111             })
112         })
113     }
114
115     #[cfg(test)]
116     mod tests {
117         use std::prelude::*;
118         use std::finally::Finally;
119
120         use super::*;
121
122         #[test]
123         fn smoke_test() {
124             // Preserve the actual global state.
125             let saved_value = take();
126
127             let expected = vec![
128                 b"happy".to_vec(),
129                 b"today?".to_vec(),
130             ];
131
132             put(expected.clone());
133             assert!(clone() == Some(expected.clone()));
134             assert!(take() == Some(expected.clone()));
135             assert!(take() == None);
136
137             (|| {
138             }).finally(|| {
139                 // Restore the actual global state.
140                 match saved_value {
141                     Some(ref args) => put(args.clone()),
142                     None => ()
143                 }
144             })
145         }
146     }
147 }
148
149 #[cfg(any(target_os = "macos",
150           target_os = "ios",
151           target_os = "windows"))]
152 mod imp {
153     use core::prelude::*;
154     use collections::vec::Vec;
155
156     pub unsafe fn init(_argc: int, _argv: *const *const u8) {
157     }
158
159     pub fn cleanup() {
160     }
161
162     pub fn take() -> Option<Vec<Vec<u8>>> {
163         fail!()
164     }
165
166     pub fn put(_args: Vec<Vec<u8>>) {
167         fail!()
168     }
169
170     pub fn clone() -> Option<Vec<Vec<u8>>> {
171         fail!()
172     }
173 }