summaryrefslogtreecommitdiffstats
path: root/vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs
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/on_stack_fibo_alloc_each_frame.rs
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/on_stack_fibo_alloc_each_frame.rs')
-rw-r--r--vendor/psm/examples/on_stack_fibo_alloc_each_frame.rs53
1 files changed, 53 insertions, 0 deletions
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()
+}