@@ -126,45 +126,36 @@ uint8_t value_out = 0;
126
126
#include "shared-module/os/__init__.h"
127
127
#endif
128
128
129
- typedef struct {
130
- #if MICROPY_ENABLE_PYSTACK
131
- supervisor_allocation * pystack ;
132
- #endif
133
- supervisor_allocation * heap ;
134
- } vm_memory_t ;
135
-
136
129
static void reset_devices (void ) {
137
130
#if CIRCUITPY_BLEIO_HCI
138
131
bleio_reset ();
139
132
#endif
140
133
}
141
134
142
- STATIC vm_memory_t allocate_vm_memory (void ) {
143
- vm_memory_t res ;
144
- #if MICROPY_ENABLE_PYSTACK
135
+ #if MICROPY_ENABLE_PYSTACK
136
+ STATIC supervisor_allocation * allocate_pystack (void ) {
145
137
mp_int_t pystack_size = CIRCUITPY_PYSTACK_SIZE ;
146
- #if CIRCUITPY_OS_GETENV
138
+ #if CIRCUITPY_OS_GETENV && CIRCUITPY_SETTABLE_PYSTACK
147
139
// Fetch value if exists from settings.toml
148
140
// Leaves size to build default on any failure
149
141
(void )common_hal_os_getenv_int ("CIRCUITPY_PYSTACK_SIZE" , & pystack_size );
150
142
// Check if value is valid
151
143
pystack_size = pystack_size - pystack_size % sizeof (size_t ); // Round down to multiple of 4.
152
144
if (pystack_size < 384 ) {
153
- serial_write_compressed (translate ("\nWARNING: Invalid CIRCUITPY_PYSTACK_SIZE, defaulting back to build value. \n\n" ));
145
+ serial_write_compressed (translate ("\nInvalid CIRCUITPY_PYSTACK_SIZE\n\n\r " ));
154
146
pystack_size = CIRCUITPY_PYSTACK_SIZE ; // Reset
155
147
}
156
148
#endif
157
- res . pystack = allocate_memory (pystack_size , false, false);
158
- if (res . pystack == NULL ) {
159
- serial_write_compressed (translate ("\nWARNING: Allocating pystack failed, defaulting back to build value. \n\n" ));
160
- res . pystack = allocate_memory (CIRCUITPY_PYSTACK_SIZE , false, false);
149
+ supervisor_allocation * pystack = allocate_memory (pystack_size , false, false);
150
+ if (pystack == NULL ) {
151
+ serial_write_compressed (translate ("\nInvalid CIRCUITPY_PYSTACK_SIZE \n\n\r " ));
152
+ pystack = allocate_memory (CIRCUITPY_PYSTACK_SIZE , false, false);
161
153
}
162
- #endif
163
- res .heap = allocate_remaining_memory ();
164
- return res ;
154
+ return pystack ;
165
155
}
156
+ #endif
166
157
167
- STATIC void start_mp (vm_memory_t vm_memory ) {
158
+ STATIC void start_mp (supervisor_allocation * heap , supervisor_allocation * pystack ) {
168
159
supervisor_workflow_reset ();
169
160
170
161
// Stack limit should be less than real stack size, so we have a chance
@@ -192,11 +183,11 @@ STATIC void start_mp(vm_memory_t vm_memory) {
192
183
readline_init0 ();
193
184
194
185
#if MICROPY_ENABLE_PYSTACK
195
- mp_pystack_init (vm_memory . pystack -> ptr , vm_memory . pystack -> ptr + get_allocation_length (vm_memory . pystack ) / sizeof (size_t ));
186
+ mp_pystack_init (pystack -> ptr , pystack -> ptr + get_allocation_length (pystack ) / sizeof (size_t ));
196
187
#endif
197
188
198
189
#if MICROPY_ENABLE_GC
199
- gc_init (vm_memory . heap -> ptr , vm_memory . heap -> ptr + get_allocation_length (vm_memory . heap ) / 4 );
190
+ gc_init (heap -> ptr , heap -> ptr + get_allocation_length (heap ) / 4 );
200
191
#endif
201
192
mp_init ();
202
193
mp_obj_list_init ((mp_obj_list_t * )mp_sys_path , 0 );
@@ -296,7 +287,7 @@ STATIC void count_strn(void *data, const char *str, size_t len) {
296
287
* (size_t * )data += len ;
297
288
}
298
289
299
- STATIC void cleanup_after_vm (vm_memory_t vm_memory , mp_obj_t exception ) {
290
+ STATIC void cleanup_after_vm (supervisor_allocation * heap , supervisor_allocation * pystack , mp_obj_t exception ) {
300
291
// Get the traceback of any exception from this run off the heap.
301
292
// MP_OBJ_SENTINEL means "this run does not contribute to traceback storage, don't touch it"
302
293
// MP_OBJ_NULL (=0) means "this run completed successfully, clear any stored traceback"
@@ -376,9 +367,9 @@ STATIC void cleanup_after_vm(vm_memory_t vm_memory, mp_obj_t exception) {
376
367
// Free the heap last because other modules may reference heap memory and need to shut down.
377
368
filesystem_flush ();
378
369
stop_mp ();
379
- free_memory (vm_memory . heap );
370
+ free_memory (heap );
380
371
#if MICROPY_ENABLE_PYSTACK
381
- free_memory (vm_memory . pystack );
372
+ free_memory (pystack );
382
373
#endif
383
374
supervisor_move_memory ();
384
375
@@ -434,8 +425,14 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
434
425
};
435
426
#endif
436
427
437
- vm_memory_t vm_memory = allocate_vm_memory ();
438
- start_mp (vm_memory );
428
+ supervisor_allocation * pystack ;
429
+ #if MICROPY_ENABLE_PYSTACK
430
+ pystack = allocate_pystack ();
431
+ #else
432
+ pystack = NULL ;
433
+ #endif
434
+ supervisor_allocation * heap = allocate_remaining_memory ();
435
+ start_mp (heap , pystack );
439
436
440
437
#if CIRCUITPY_USB
441
438
usb_setup_with_vm ();
@@ -483,7 +480,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
483
480
484
481
485
482
// Finished executing python code. Cleanup includes filesystem flush and a board reset.
486
- cleanup_after_vm (vm_memory , _exec_result .exception );
483
+ cleanup_after_vm (heap , pystack , _exec_result .exception );
487
484
_exec_result .exception = NULL ;
488
485
489
486
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
@@ -779,8 +776,14 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
779
776
780
777
// Do USB setup even if boot.py is not run.
781
778
782
- vm_memory_t vm_memory = allocate_vm_memory ();
783
- start_mp (vm_memory );
779
+ supervisor_allocation * pystack ;
780
+ #if MICROPY_ENABLE_PYSTACK
781
+ pystack = allocate_pystack ();
782
+ #else
783
+ pystack = NULL ;
784
+ #endif
785
+ supervisor_allocation * heap = allocate_remaining_memory ();
786
+ start_mp (heap , pystack );
784
787
785
788
#if CIRCUITPY_USB
786
789
// Set up default USB values after boot.py VM starts but before running boot.py.
@@ -866,7 +869,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
866
869
867
870
port_post_boot_py (true);
868
871
869
- cleanup_after_vm (vm_memory , _exec_result .exception );
872
+ cleanup_after_vm (heap , pystack , _exec_result .exception );
870
873
_exec_result .exception = NULL ;
871
874
872
875
port_post_boot_py (false);
@@ -881,8 +884,14 @@ STATIC int run_repl(void) {
881
884
int exit_code = PYEXEC_FORCED_EXIT ;
882
885
stack_resize ();
883
886
filesystem_flush ();
884
- vm_memory_t vm_memory = allocate_vm_memory ();
885
- start_mp (vm_memory );
887
+ supervisor_allocation * pystack ;
888
+ #if MICROPY_ENABLE_PYSTACK
889
+ pystack = allocate_pystack ();
890
+ #else
891
+ pystack = NULL ;
892
+ #endif
893
+ supervisor_allocation * heap = allocate_remaining_memory ();
894
+ start_mp (heap , pystack );
886
895
887
896
#if CIRCUITPY_USB
888
897
usb_setup_with_vm ();
@@ -925,7 +934,7 @@ STATIC int run_repl(void) {
925
934
exit_code = PYEXEC_DEEP_SLEEP ;
926
935
}
927
936
#endif
928
- cleanup_after_vm (vm_memory , MP_OBJ_SENTINEL );
937
+ cleanup_after_vm (heap , pystack , MP_OBJ_SENTINEL );
929
938
930
939
// Also reset bleio. The above call omits it in case workflows should continue. In this case,
931
940
// we're switching straight to another VM so we want to reset.
0 commit comments