summaryrefslogtreecommitdiffstats
path: root/vendor/psm/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/psm/examples
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/psm/examples')
-rw-r--r--vendor/psm/examples/info.rs20
-rw-r--r--vendor/psm/examples/on_stack_fibo.rs77
-rw-r--r--vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs53
-rw-r--r--vendor/psm/examples/panics.rs52
-rw-r--r--vendor/psm/examples/replace_stack_1.rs33
-rw-r--r--vendor/psm/examples/thread.rs60
6 files changed, 295 insertions, 0 deletions
diff --git a/vendor/psm/examples/info.rs b/vendor/psm/examples/info.rs
new file mode 100644
index 000000000..46a619b68
--- /dev/null
+++ b/vendor/psm/examples/info.rs
@@ -0,0 +1,20 @@
+extern crate psm;
+
+psm::psm_stack_information! {
+ yes {
+ fn main() {
+ println!("Stack is {:?} and is at {:p} currently",
+ psm::StackDirection::new(), psm::stack_pointer());
+ }
+ }
+ no {
+ fn main() {
+ eprintln!("Stack information not supported by this target");
+ }
+ }
+}
+
+#[test]
+fn run_example() {
+ main();
+}
diff --git a/vendor/psm/examples/on_stack_fibo.rs b/vendor/psm/examples/on_stack_fibo.rs
new file mode 100644
index 000000000..07e0338da
--- /dev/null
+++ b/vendor/psm/examples/on_stack_fibo.rs
@@ -0,0 +1,77 @@
+extern crate psm;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+
+ #[inline(never)]
+ fn fib(n: usize, stack_limit: *mut u8) -> Option<u64> {
+ // match psm::StackDirection::new() {
+ // psm::StackDirection::Ascending => if psm::stack_pointer() > stack_limit {
+ // return None;
+ // }
+ // psm::StackDirection::Descending => if psm::stack_pointer() < stack_limit {
+ // return None;
+ // }
+ // }
+
+ match n {
+ 0 => Some(0),
+ 1 => Some(1),
+ _ => fib(n - 1, stack_limit).and_then(|x| fib(n - 2, stack_limit).map(|y| x + y)),
+ }
+ }
+
+ const STACK_ALIGN: usize = 4096;
+ const STACK_REDLINE: usize = 512;
+ const FIB_COUNTS: [(usize, u64); 3] = [
+ (8, 21),
+ (16, 987),
+ (32, 2178309),
+ ];
+
+
+ fn main() {
+ let mut stack_size = 1024 * 128;
+ unsafe {
+ for &(n, expected) in FIB_COUNTS.iter() {
+ loop {
+ println!("fib({}) with {} bytes of stack", n, stack_size - STACK_REDLINE);
+ let layout = alloc::Layout::from_size_align(stack_size, STACK_ALIGN).unwrap();
+ let new_stack = alloc::alloc(layout);
+ assert!(!new_stack.is_null(), "allocations must succeed!");
+ let max_stack = match psm::StackDirection::new() {
+ psm::StackDirection::Ascending =>
+ new_stack.offset((stack_size - STACK_REDLINE) as isize),
+ psm::StackDirection::Descending =>
+ new_stack.offset(STACK_REDLINE as isize),
+ };
+ let result = psm::on_stack(new_stack, stack_size, || {
+ fib(n, max_stack)
+ });
+ alloc::dealloc(new_stack, layout);
+ if let Some(res) = result {
+ assert_eq!(res, expected);
+ println!("fib({}) = {}", n, res);
+ break;
+ } else {
+ println!("Stack not large enough!");
+ stack_size *= 2;
+ }
+ }
+ }
+ }
+ }
+
+ }
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}
+
+#[test]
+fn run_example() {
+ main()
+}
diff --git a/vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs b/vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs
new file mode 100644
index 000000000..9b9f834b3
--- /dev/null
+++ b/vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs
@@ -0,0 +1,53 @@
+extern crate psm;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+
+ const STACK_ALIGN: usize = 4096;
+ const FRAME_SIZE: usize = 4096;
+ const FIB_COUNTS: [(usize, u64); 3] = [
+ (8, 21),
+ (16, 987),
+ (24, 46368),
+ ];
+
+ #[inline(never)]
+ fn fib(n: usize) -> u64 {
+ unsafe {
+ let layout = alloc::Layout::from_size_align(FRAME_SIZE, STACK_ALIGN).unwrap();
+ let new_stack = alloc::alloc(layout);
+ assert!(!new_stack.is_null(), "allocations must succeed!");
+ let r = match n {
+ 0 => 0,
+ 1 => 1,
+ _ => {
+ psm::on_stack(new_stack, FRAME_SIZE, || {
+ fib(n - 1) + fib(n - 2)
+ })
+ }
+ };
+ alloc::dealloc(new_stack, layout);
+ r
+ }
+ }
+
+ fn main() {
+ for &(n, expected) in FIB_COUNTS.iter() {
+ let res = fib(n);
+ assert_eq!(res, expected);
+ println!("fib({}) = {}", n, res);
+ }
+ }
+ }
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}
+
+#[test]
+fn run_example() {
+ main()
+}
diff --git a/vendor/psm/examples/panics.rs b/vendor/psm/examples/panics.rs
new file mode 100644
index 000000000..ada658d80
--- /dev/null
+++ b/vendor/psm/examples/panics.rs
@@ -0,0 +1,52 @@
+extern crate psm;
+
+use std::panic;
+
+const CHAIN_DEPTH: usize = 16;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+ const STACK_ALIGN: usize = 4096;
+ // Generating backraces (because of RUST_BACKTRACE) create a few quite large frames, so it is
+ // important, that all frames have sufficient amount of available memory to not run over the
+ // stack...
+ const FRAME_SIZE: usize = 4096 * 10;
+
+ fn panic_chain(depth: usize) {
+ if depth == 0 {
+ panic!("full chain!");
+ } else {
+ unsafe {
+ let layout = alloc::Layout::from_size_align(FRAME_SIZE, STACK_ALIGN).unwrap();
+ let new_stack = alloc::alloc(layout);
+ assert!(!new_stack.is_null(), "allocations must succeed!");
+ let p = psm::on_stack(new_stack, FRAME_SIZE, || {
+ panic::catch_unwind(|| {
+ panic_chain(depth - 1);
+ })
+ });
+ alloc::dealloc(new_stack, layout);
+ p.map_err(panic::resume_unwind).unwrap()
+ }
+ }
+ }
+
+ fn main() {
+ panic_chain(CHAIN_DEPTH);
+ }
+
+ #[test]
+ fn run_example() {
+ assert!(panic::catch_unwind(|| {
+ panic_chain(CHAIN_DEPTH);
+ }).is_err(), "Panic did not propagate!");
+ }
+ }
+
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}
diff --git a/vendor/psm/examples/replace_stack_1.rs b/vendor/psm/examples/replace_stack_1.rs
new file mode 100644
index 000000000..af9e4b92c
--- /dev/null
+++ b/vendor/psm/examples/replace_stack_1.rs
@@ -0,0 +1,33 @@
+extern crate psm;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+
+ const STACK_SIZE: usize = 4096 * 64;
+ const STACK_ALIGN: usize = 4096;
+
+ fn main() {
+ println!("current stack pointer is {:p}", psm::stack_pointer());
+ unsafe {
+ let new_stack = alloc::alloc(alloc::Layout::from_size_align(STACK_SIZE, STACK_ALIGN).unwrap());
+ println!("new stack is between {:p} and {:p}", new_stack, new_stack.offset(STACK_SIZE as isize));
+ psm::replace_stack(new_stack, STACK_SIZE, || {
+ println!("after replacement stack pointer is {:p}", psm::stack_pointer());
+ ::std::process::exit(0);
+ });
+ }
+ }
+ }
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}
+
+#[test]
+fn run_example() {
+ // NOTE: intentionally out-of-processes, as the example exits with `process::exit(0)`.
+ main()
+}
diff --git a/vendor/psm/examples/thread.rs b/vendor/psm/examples/thread.rs
new file mode 100644
index 000000000..eb335a5a4
--- /dev/null
+++ b/vendor/psm/examples/thread.rs
@@ -0,0 +1,60 @@
+extern crate psm;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+
+ const STACK_ALIGN: usize = 4096;
+ const FRAME_SIZE: usize = 4096;
+ const FIB_COUNTS: [(usize, u64); 3] = [
+ (8, 21),
+ (16, 987),
+ (24, 46368),
+ ];
+
+ #[inline(never)]
+ fn fib(n: usize) -> u64 {
+ unsafe {
+ let layout = alloc::Layout::from_size_align(FRAME_SIZE, STACK_ALIGN).unwrap();
+ let new_stack = alloc::alloc(layout);
+ assert!(!new_stack.is_null(), "allocations must succeed!");
+ let r = match n {
+ 0 => 0,
+ 1 => 1,
+ _ => {
+ psm::on_stack(new_stack, FRAME_SIZE, || {
+ fib(n - 1) + fib(n - 2)
+ })
+ }
+ };
+ alloc::dealloc(new_stack, layout);
+ r
+ }
+ }
+
+ fn main() {
+ for (n, expected, handle) in FIB_COUNTS.iter().map(|&(n, expected)|
+ (n, expected, std::thread::spawn(move || {
+ fib(n)
+ }))
+ ) {
+ if let Ok(res) = handle.join() {
+ assert_eq!(res, expected);
+ println!("fib({}) = {}", n, res);
+ } else {
+ panic!("joining a thread returned an Err");
+ }
+ }
+ }
+ }
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}
+
+#[test]
+fn run_example() {
+ main()
+}