30#if defined(__unix__) || defined(__APPLE__)
35# define STACK_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
37# define STACK_FLAGS MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_STACK
60 const size_t guard_size = guard_pages * os_page_size;
66 desired_size > SIZE_MAX - guard_size - os_page_size,
"Requested fiber stack size is too large"
70 const size_t usable = (desired_size + os_page_size - 1) & ~(os_page_size - 1);
72 const size_t total = usable + guard_size;
78 nullptr,
total, PROT_READ | PROT_WRITE, STACK_FLAGS,
85 PANIC_IF(end == MAP_FAILED,
"Failed to allocate the stack for the fiber subsystem");
87 if (mprotect(end, guard_size, PROT_NONE) == -1) {
89 PANIC(
"Failed to allocate the guard band for the fiber subsystem");
92 const uintptr_t start = (uintptr_t)end + (uintptr_t)
total;
93 const uintptr_t limit = (uintptr_t)end + (uintptr_t)guard_size;
95 srn_fiber_stack_t stack = {.start = (
void *)start, .limit = (
void *)limit, .guard = end};
The single place that holds every runtime knob.
#define SRN_CONFIG_DEFAULT_FIBER_STACK_SIZE
Size of every fiber stack, in bytes.
size_t srn_mm_get_os_page_size(void)
Retutrns the OS page size.
AI Generated (🤦) Fiber subsystem overview.
srn_fiber_stack_t srn_fiber_stack_alloc(size_t size, size_t guard_pages)
Allocate a stack of at least size usable bytes, or SRN_CONFIG_DEFAULT_FIBER_STACK_SIZE when size is 0...
void srn_fiber_stack_free(srn_fiber_stack_t stack)
One stack per fiber, mapped with a guard band at the low end so an overflow faults deterministically ...
void * guard
Low base of the protected guard band that detects stack overflows.
void * start
High end, stack pointer initialises to this address.
#define PANIC_IF(cond, msg)