Skip to content

Commit 7ee3f30

Browse files
committed
rewrite allocate_pystack logic
1 parent c600429 commit 7ee3f30

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ msgid ""
2828
"Code stopped by auto-reload. Reloading soon.\n"
2929
msgstr ""
3030

31-
#: main.c
32-
msgid ""
33-
"\n"
34-
"Invalid CIRCUITPY_PYSTACK_SIZE\n"
35-
"\n"
36-
"\r"
37-
msgstr ""
38-
3931
#: supervisor/shared/safe_mode.c
4032
msgid ""
4133
"\n"
@@ -1252,6 +1244,10 @@ msgstr ""
12521244
msgid "Invalid BSSID"
12531245
msgstr ""
12541246

1247+
#: main.c
1248+
msgid "Invalid CIRCUITPY_PYSTACK_SIZE\n"
1249+
msgstr ""
1250+
12551251
#: shared-bindings/wifi/Radio.c
12561252
msgid "Invalid MAC address"
12571253
msgstr ""

main.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,18 @@ static void reset_devices(void) {
134134

135135
#if MICROPY_ENABLE_PYSTACK
136136
STATIC supervisor_allocation *allocate_pystack(safe_mode_t safe_mode) {
137-
mp_int_t pystack_size = CIRCUITPY_PYSTACK_SIZE;
138137
#if CIRCUITPY_OS_GETENV && CIRCUITPY_SETTABLE_PYSTACK
139-
// Fetch value if exists from settings.toml
140-
// Leaves size to build default on any failure
141-
if (safe_mode == SAFE_MODE_NONE || safe_mode == SAFE_MODE_USER) {
138+
if (safe_mode == SAFE_MODE_NONE) {
139+
mp_int_t pystack_size = CIRCUITPY_PYSTACK_SIZE;
142140
(void)common_hal_os_getenv_int("CIRCUITPY_PYSTACK_SIZE", &pystack_size);
143-
// Check if value is valid
144-
pystack_size = pystack_size - pystack_size % sizeof(size_t); // Round down to multiple of 4.
145-
if ((pystack_size < 384) || (pystack_size > 900000)) {
146-
serial_write_compressed(translate("\nInvalid CIRCUITPY_PYSTACK_SIZE\n\n\r"));
147-
pystack_size = CIRCUITPY_PYSTACK_SIZE; // Reset
141+
supervisor_allocation *pystack = allocate_memory(pystack_size >= 384 ? pystack_size : 0, false, false);
142+
if (pystack) {
143+
return pystack;
148144
}
145+
serial_write_compressed(translate("Invalid CIRCUITPY_PYSTACK_SIZE\n"));
149146
}
150147
#endif
151-
supervisor_allocation *pystack = allocate_memory(pystack_size, false, false);
152-
if (pystack == NULL) {
153-
serial_write_compressed(translate("\nInvalid CIRCUITPY_PYSTACK_SIZE\n\n\r"));
154-
pystack = allocate_memory(CIRCUITPY_PYSTACK_SIZE, false, false);
155-
}
156-
return pystack;
148+
return allocate_memory(CIRCUITPY_PYSTACK_SIZE, false, false);
157149
}
158150
#endif
159151

0 commit comments

Comments
 (0)