diff options
author | Mattias Andrée <maandree@kth.se> | 2024-07-02 14:05:37 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-07-02 14:05:37 +0200 |
commit | 2268eb37c71d8cc83887b93ca8d81289798a0552 (patch) | |
tree | 4b3a10898aa4978730e94a357f21ba9a1f02b8b1 /needstack.c | |
parent | Add missing .c files (diff) | |
download | libsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.gz libsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.bz2 libsimple-2268eb37c71d8cc83887b93ca8d81289798a0552.tar.xz |
Add needstack, get_stack_direction, get_stack_limit, and get_stack_space
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | needstack.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/needstack.c b/needstack.c new file mode 100644 index 0000000..11d8ed4 --- /dev/null +++ b/needstack.c @@ -0,0 +1,53 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libsimple_needstack(size_t n) +{ + size_t limit1, limit2, limit, used; + uintptr_t low, high, current; + void *currentptr; + + currentptr = __builtin_frame_address(0); + current = (uintptr_t)currentptr; + if (libsimple_get_stack_space(&low, &high)) + return -1; + if (libsimple_get_stack_limit(&limit1, NULL)) + return -1; + limit2 = (size_t)(high - low); + limit = limit1 < limit2 ? limit1 : limit2; + + switch (libsimple_get_stack_direction()) { + case +1: + if (current < low) + goto enotsup; + used = (size_t)(current - low); + break; + case -1: + if (current > high) + goto enotsup; + used = (size_t)(high - current); + break; + default: + enotsup: + errno = ENOTSUP; + return -1; + } + + /* TODO it would be nice to be able to grow the stack */ + return used <= limit && n <= limit - used; +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif |