Query for the available stack size of a program

I’m working a lot with stack buffered allocators and they can get very large for my use case. I want to dynamically dispatch to heap memory at comptime if a requested buffer size is large enough to be compromising.

In C++, you have stuff like:

stackavail()

in C, you have this (can be used to find information about it):

#include <sys/resource.h>
int main (void)
{
  struct rlimit limit;

  getrlimit (RLIMIT_STACK, &limit);
  printf ("\nStack Limit = %ld and %ld max\n", limit.rlim_cur, limit.rlim_max);
}

And I also saw this as a proposal: Builtin function to tell you the maximum stack size of a given function · Issue #157 · ziglang/zig · GitHub

Are there any standard ways of querying for stack size at comptime?

Thanks!

std.os.getrlimit exists.

It doesn’t seem like it’d be possible to get this info at comptime, especially since setrlimit(.STACK, ...) is called in start.zig and can fail to change the stack size:

2 Likes

Interesting - thanks for the info. Either way, that eliminates some considerations I had, so thanks for the help!