1From 9c899f7b8089eee7ce471f237d2046882298c4fc Mon Sep 17 00:00:00 2001
2From: Hraban Luyat <hraban@0brg.net>
3Date: Sat, 13 Apr 2024 14:04:57 -0400
4Subject: [PATCH 1/2] feat: NIX_SBCL_DYNAMIC_SPACE_SIZE envvar
5
6---
7 src/runtime/runtime.c | 27 +++++++++++++++++++++++++++
8 1 file changed, 27 insertions(+)
9
10diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c
11index 2b1f8b634..4f3f51139 100644
12--- a/src/runtime/runtime.c
13+++ b/src/runtime/runtime.c
14@@ -416,6 +416,29 @@ static int is_memsize_arg(char *argv[], int argi, int argc, int *merge_core_page
15 return 0;
16 }
17
18+/**
19+ * Read memory options from the environment, if present.
20+ *
21+ * Memory settings are read in the following priority:
22+ *
23+ * 1. command line arguments
24+ * 2. environment variable
25+ * 3. embedded options in core
26+ * 4. default
27+ */
28+static void
29+read_memsize_from_env(void) {
30+ const char *val = getenv("NIX_SBCL_DYNAMIC_SPACE_SIZE");
31+ // The distinction is blurry between setting an envvar to the empty string and
32+ // unsetting it entirely. Depending on the calling environment it can even be
33+ // tricky to properly unset an envvar in the first place. An empty envvar is
34+ // practically always intended to just mean “unset”, so let’s interpret it
35+ // that way.
36+ if (val != NULL && (strcmp(val, "") != 0)) {
37+ dynamic_space_size = parse_size_arg(val, "NIX_SBCL_DYNAMIC_SPACE_SIZE");
38+ }
39+}
40+
41 static struct cmdline_options
42 parse_argv(struct memsize_options memsize_options,
43 int argc, char *argv[], char *envp[], char *core)
44@@ -457,6 +480,9 @@ parse_argv(struct memsize_options memsize_options,
45 thread_control_stack_size = memsize_options.thread_control_stack_size;
46 dynamic_values_bytes = memsize_options.thread_tls_bytes;
47 if (memsize_options.present_in_core == 2) {
48+ /* Only accept environment variable memory options where you would
49+ * accept those options as command-line arguments. */
50+ read_memsize_from_env();
51 int stop_parsing = 0; // have we seen '--'
52 int output_index = 1;
53 #ifndef LISP_FEATURE_WIN32
54@@ -490,6 +516,7 @@ parse_argv(struct memsize_options memsize_options,
55 #endif
56 }
57 } else {
58+ read_memsize_from_env();
59 bool end_runtime_options = 0;
60 /* Parse our any of the command-line options that we handle from C,
61 * stopping at the first one that we don't, and leave the rest */
62--
632.48.1
64