Skip to content

Commit 81f9804

Browse files
committed
Add a function to query remaining stack.
Closes #2
1 parent 92c6a10 commit 81f9804

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,20 @@ fn set_stack_limit(l: usize) {
6363
pub fn maybe_grow<R, F: FnOnce() -> R>(red_zone: usize,
6464
stack_size: usize,
6565
f: F) -> R {
66+
if remaining_stack() >= red_zone {
67+
f()
68+
} else {
69+
grow_the_stack(stack_size, f)
70+
}
71+
}
72+
73+
/// Queries the amount of remaining stack as interpreted by this library.
74+
///
75+
/// This function will return the amount of stack space left which will be used
76+
/// to determine whether a stack switch should be made or not.
77+
pub fn remaining_stack() -> usize {
6678
unsafe {
67-
if __stacker_stack_pointer() - get_stack_limit() >= red_zone {
68-
f()
69-
} else {
70-
grow_the_stack(stack_size, f)
71-
}
79+
__stacker_stack_pointer() - get_stack_limit()
7280
}
7381
}
7482

0 commit comments

Comments
 (0)