Skip to content

Commit ba24ecd

Browse files
authored
Put program::at_exit and thread::at_exit behind feature flags. (#141)
This allow them to be excluded when not needed, reducing code size.
1 parent 4b29cb9 commit ba24ecd

File tree

12 files changed

+43
-32
lines changed

12 files changed

+43
-32
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ features = ["param"]
5858
assert_cmd = "2.0.12"
5959

6060
[features]
61-
default = ["std", "log", "libc", "errno", "thread", "init-fini-arrays"]
61+
default = ["std", "log", "libc", "errno", "thread", "init-fini-arrays", "program-at-exit", "thread-at-exit"]
6262
std = ["rustix/std", "bitflags/std", "alloc"]
6363
rustc-dep-of-std = [
6464
"dep:core",
@@ -89,8 +89,8 @@ external-start = ["take-charge"]
8989
# The loggers depend on a `.init_array` entry to initialize themselves, and
9090
# `env_logger` needs it so that `c-scape` can initialize environment variables
9191
# and make `RUST_LOG` available.
92-
atomic-dbg-logger = ["atomic-dbg/log", "init-fini-arrays"]
93-
env_logger = ["dep:env_logger", "init-fini-arrays"]
92+
atomic-dbg-logger = ["atomic-dbg/log", "init-array"]
93+
env_logger = ["dep:env_logger", "init-array"]
9494

9595
# Disable logging.
9696
max_level_off = ["log/max_level_off"]
@@ -105,7 +105,7 @@ thread = ["rustix/thread", "rustix/mm", "param", "rustix/process", "rustix/runti
105105
# Enable support for signal handlers.
106106
signal = ["rustix/runtime"]
107107

108-
# Enable support for `rustix::param`.
108+
# Have origin call `rustix::param::init`.
109109
param = ["rustix/param"]
110110

111111
# Enable support for ELF `.init_array` and `.fini_array`.
@@ -117,6 +117,12 @@ init-array = []
117117
# Enable support for ELF `.fini_array`.
118118
fini-array = []
119119

120+
# Enable support for `origin::program::at_exit`.
121+
program-at-exit = ["alloc"]
122+
123+
# Enable support for `origin::thread::at_exit`.
124+
thread-at-exit = ["alloc", "thread"]
125+
120126
# Enable highly experimental support for performing startup-time relocations,
121127
# needed to support statically-linked PIE executables.
122128
experimental-relocate = ["rustix/mm", "rustix/runtime"]
@@ -171,4 +177,4 @@ panic-handler-abort = ["unwinding?/panic-handler-dummy"]
171177
getauxval = ["rustix/param"]
172178

173179
[package.metadata.docs.rs]
174-
features = ["take-charge", "origin-start", "thread", "signal", "nightly"]
180+
features = ["origin-start", "thread", "signal", "program-at-exit", "thread-at-exit", "nightly"]

example-crates/external-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["take-charge", "external-start", "thread", "alloc", "eh-personality-continue", "panic-handler-abort", "nightly"] }
10+
origin = { path = "../..", default-features = false, features = ["take-charge", "external-start", "thread", "alloc", "program-at-exit", "thread-at-exit", "eh-personality-continue", "panic-handler-abort", "nightly"] }
1111

1212
# Ensure that libc gets linked.
1313
libc = { version = "0.2", default-features = false }

example-crates/no-std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features. And enable "libc" to enable the libc implementations.
10-
origin = { path = "../..", default-features = false, features = ["libc", "thread", "alloc", "eh-personality-continue", "panic-handler-abort", "nightly"] }
10+
origin = { path = "../..", default-features = false, features = ["libc", "thread", "alloc", "program-at-exit", "thread-at-exit", "eh-personality-continue", "panic-handler-abort", "nightly"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start-dynamic-linker/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
# Origin can be depended on just like any other crate. For no_std, disable
1212
# the default features, and the desired features.
13-
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc", "experimental-relocate", "eh-personality-continue", "panic-handler-abort", "nightly"] }
13+
origin = { path = "../..", default-features = false, features = ["origin-start", "thread", "alloc", "program-at-exit", "thread-at-exit", "experimental-relocate", "eh-personality-continue", "panic-handler-abort", "nightly"] }
1414

1515
# Crates to help writing no_std code.
1616
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start-lto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc", "eh-personality-continue", "panic-handler-abort", "nightly"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-start", "thread", "alloc", "program-at-exit", "thread-at-exit", "eh-personality-continue", "panic-handler-abort", "nightly"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start-stable/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc", "eh-personality-continue", "panic-handler-abort"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-start", "thread", "alloc", "program-at-exit", "thread-at-exit", "eh-personality-continue", "panic-handler-abort"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc", "eh-personality-continue", "panic-handler-abort", "nightly"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-start", "thread", "alloc", "program-at-exit", "thread-at-exit", "eh-personality-continue", "panic-handler-abort", "nightly"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

src/program/libc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
//! with origin's goal of providing Rust-idiomatic interfaces, however it does
2929
//! mean that origin can avoid doing any work that users might not need.
3030
31-
#[cfg(feature = "alloc")]
31+
#[cfg(feature = "program-at-exit")]
3232
use alloc::boxed::Box;
33+
#[cfg(feature = "program-at-exit")]
3334
use core::ptr::null_mut;
3435
use linux_raw_sys::ctypes::c_int;
3536

3637
/// Register a function to be called when [`exit`] is called.
37-
#[cfg(feature = "alloc")]
38-
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
38+
#[cfg(feature = "program-at-exit")]
39+
#[cfg_attr(docsrs, doc(cfg(feature = "program-at-exit")))]
3940
pub fn at_exit(func: Box<dyn FnOnce() + Send>) {
4041
use core::ffi::c_void;
4142

src/program/linux_raw.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
3131
#[cfg(feature = "thread")]
3232
use crate::thread;
33-
#[cfg(feature = "alloc")]
33+
#[cfg(feature = "program-at-exit")]
3434
use alloc::boxed::Box;
35-
#[cfg(all(feature = "alloc", not(feature = "thread")))]
35+
#[cfg(all(feature = "program-at-exit", not(feature = "thread")))]
3636
use core::cell::UnsafeCell;
3737
use linux_raw_sys::ctypes::c_int;
38-
#[cfg(all(feature = "alloc", feature = "thread"))]
38+
#[cfg(all(feature = "program-at-exit", feature = "thread"))]
3939
use rustix_futex_sync::Mutex;
4040

4141
#[cfg(not(any(feature = "origin-start", feature = "external-start")))]
@@ -227,28 +227,28 @@ unsafe fn init_runtime(mem: *mut usize, envp: *mut *mut u8) {
227227
/// `SmallVec` to ensure we can register that many without allocating.
228228
///
229229
/// [POSIX guarantees]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/atexit.html
230-
#[cfg(all(feature = "alloc", feature = "thread"))]
230+
#[cfg(all(feature = "program-at-exit", feature = "thread"))]
231231
static DTORS: Mutex<smallvec::SmallVec<[Box<dyn FnOnce() + Send>; 32]>> =
232232
Mutex::new(smallvec::SmallVec::new_const());
233233

234234
/// A type for `DTORS` in the single-threaded case that we can mark as `Sync`.
235-
#[cfg(all(feature = "alloc", not(feature = "thread")))]
235+
#[cfg(all(feature = "program-at-exit", not(feature = "thread")))]
236236
struct Dtors(UnsafeCell<smallvec::SmallVec<[Box<dyn FnOnce() + Send>; 32]>>);
237237

238238
/// SAFETY: With `feature = "take-charge"`, we can assume that Origin is
239239
/// responsible for creating all threads in the program, and with
240240
/// `not(feature = "thread")` mode, Origin can't create any new threads, so we
241241
/// don't need to synchronize.
242-
#[cfg(all(feature = "alloc", not(feature = "thread")))]
242+
#[cfg(all(feature = "program-at-exit", not(feature = "thread")))]
243243
unsafe impl Sync for Dtors {}
244244

245245
/// The single-threaded version of `DTORS`.
246-
#[cfg(all(feature = "alloc", not(feature = "thread")))]
246+
#[cfg(all(feature = "program-at-exit", not(feature = "thread")))]
247247
static DTORS: Dtors = Dtors(UnsafeCell::new(smallvec::SmallVec::new_const()));
248248

249249
/// Register a function to be called when [`exit`] is called.
250-
#[cfg(feature = "alloc")]
251-
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
250+
#[cfg(feature = "program-at-exit")]
251+
#[cfg_attr(docsrs, doc(cfg(feature = "program-at-exit")))]
252252
pub fn at_exit(func: Box<dyn FnOnce() + Send>) {
253253
#[cfg(feature = "thread")]
254254
let mut dtors = DTORS.lock();
@@ -263,13 +263,13 @@ pub fn at_exit(func: Box<dyn FnOnce() + Send>) {
263263
/// `.fini_array` section, and exit the program.
264264
pub fn exit(status: c_int) -> ! {
265265
// Call functions registered with `at_thread_exit`.
266-
#[cfg(all(feature = "alloc", feature = "thread"))]
266+
#[cfg(feature = "thread-at-exit")]
267267
crate::thread::call_dtors(crate::thread::current());
268268

269269
// Call all the registered functions, in reverse order. Leave `DTORS`
270270
// unlocked while making the call so that functions can add more functions
271271
// to the end of the list.
272-
#[cfg(feature = "alloc")]
272+
#[cfg(feature = "program-at-exit")]
273273
loop {
274274
#[cfg(feature = "thread")]
275275
let mut dtors = DTORS.lock();

src/thread/libc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#[cfg(not(feature = "nightly"))]
44
use crate::ptr::{with_exposed_provenance_mut, without_provenance_mut, Polyfill as _};
5+
#[cfg(feature = "thread-at-exit")]
56
use alloc::boxed::Box;
67
use core::ffi::{c_int, c_void};
78
use core::mem::{size_of, transmute, zeroed};
@@ -23,6 +24,7 @@ extern "C" {
2324
fn __errno_location() -> *mut c_int;
2425
fn __tls_get_addr(p: &[usize; 2]) -> *mut c_void;
2526

27+
#[cfg(feature = "thread-at-exit")]
2628
static __dso_handle: *const c_void;
2729
}
2830

@@ -154,6 +156,7 @@ pub unsafe fn create(
154156
}
155157

156158
/// Registers a function to call when the current thread exits.
159+
#[cfg(feature = "thread-at-exit")]
157160
pub fn at_exit(func: Box<dyn FnOnce()>) {
158161
extern "C" fn call(arg: *mut c_void) {
159162
unsafe {
@@ -322,6 +325,7 @@ pub fn yield_current() {
322325
}
323326

324327
/// Return the address of `__dso_handle`, appropriately casted.
328+
#[cfg(feature = "thread-at-exit")]
325329
unsafe fn dso_handle() -> *mut c_void {
326330
let dso_handle: *const *const c_void = &__dso_handle;
327331
dso_handle.cast::<c_void>().cast_mut()

0 commit comments

Comments
 (0)