Skip to content

Instantly share code, notes, and snippets.

@cptwunderlich
Last active September 16, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cptwunderlich/423994070700f68557533ea90400d23b to your computer and use it in GitHub Desktop.
Save cptwunderlich/423994070700f68557533ea90400d23b to your computer and use it in GitHub Desktop.
2nd OSMem.c fix for rlimit crash
#if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_SYS_TIME_H)
struct rlimit asLimit;
/* rlim_t is signed on some platforms, including FreeBSD;
* explicitly cast to avoid sign compare error */
if (!getrlimit(RLIMIT_AS, &asLimit)
&& asLimit.rlim_cur > 0
&& *len > (W_) asLimit.rlim_cur) {
// TODO: get stack limit from pthread_attr_getstacksize
struct rlimit stackLimit;
if (getrlimit(RLIMIT_STACK, &stackLimit)) {
// Default stack size on Linux is 8MB
stackLimit.rlim_cur = 8388608;
debugBelch("Setting default stack limit\n");
} else {
debugBelch("Stack limit is: %zu\n", stackLimit.rlim_cur);
}
size_t pageSize = getPageSize();
// 2/3rds of limit, round down to multiple of PAGE_SIZE
W_ *len = ((asLimit.rlim_cur * ((double) 2/3)) / pageSize) * pageSize;
if (((W_) (asLimit.rlim_cur - *len )) < ((W_) (stackLimit.rlim_cur * 3))) {
sysErrorBelch("not enough available virtual memory! Please check your resource limits!");
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment