-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Insert function into
.init_array
section to support Linux
- Loading branch information
1 parent
416a0db
commit 624a9d8
Showing
8 changed files
with
160 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::sync::{atomic::AtomicUsize, Mutex}; | ||
|
||
use constructor_array::*; | ||
|
||
static INIT_NUM: AtomicUsize = AtomicUsize::new(0); | ||
|
||
#[register_ctor] | ||
fn set_init_num() { | ||
INIT_NUM.fetch_add(20, std::sync::atomic::Ordering::Relaxed); | ||
} | ||
|
||
static INIT_VEC: Mutex<Vec<usize>> = Mutex::new(Vec::new()); | ||
|
||
#[register_ctor] | ||
fn init_vector() { | ||
let mut vec = INIT_VEC.lock().unwrap(); | ||
vec.push(1); | ||
vec.push(2); | ||
vec.push(3); | ||
} | ||
|
||
#[test] | ||
fn test_constructor_array() { | ||
// The constructor functions will be called before the main function. | ||
assert!(INIT_NUM.load(std::sync::atomic::Ordering::Relaxed) == 20); | ||
let vec = INIT_VEC.lock().unwrap(); | ||
assert!(vec.len() == 3); | ||
assert!(vec[0] == 1); | ||
assert!(vec[1] == 2); | ||
assert!(vec[2] == 3); | ||
drop(vec); | ||
|
||
// But we can invoke the constructor functions again manually. | ||
init_vector(); | ||
let vec = INIT_VEC.lock().unwrap(); | ||
assert!(vec.len() == 6); | ||
assert!(vec[3] == 1); | ||
assert!(vec[4] == 2); | ||
assert!(vec[5] == 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#[test] | ||
fn test_empty() { | ||
// Sometimes under certain conditions, we may not have any constructor functions. | ||
// But the `invoke_ctors` function should still work, and the `__init_array_start` and | ||
// `__init_array_end` symbols should be valid. | ||
constructor_array::invoke_ctors(); | ||
println!("It should exit successfully when we don't specify any constructor functions."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters