@@ -126,13 +126,15 @@ uint8_t value_out = 0;
126
126
static size_t PLACE_IN_DTCM_BSS (_pystack [CIRCUITPY_PYSTACK_SIZE / sizeof (size_t )]);
127
127
#endif
128
128
129
+ supervisor_allocation prev_traceback ;
130
+
129
131
static void reset_devices (void ) {
130
132
#if CIRCUITPY_BLEIO_HCI
131
133
bleio_reset ();
132
134
#endif
133
135
}
134
136
135
- STATIC void start_mp (supervisor_allocation * heap ) {
137
+ STATIC void start_mp (void ) {
136
138
supervisor_workflow_reset ();
137
139
138
140
// Stack limit should be less than real stack size, so we have a chance
@@ -163,9 +165,7 @@ STATIC void start_mp(supervisor_allocation *heap) {
163
165
mp_pystack_init (_pystack , _pystack + (sizeof (_pystack ) / sizeof (size_t )));
164
166
#endif
165
167
166
- #if MICROPY_ENABLE_GC
167
- gc_init (heap -> ptr , heap -> ptr + get_allocation_length (heap ) / 4 );
168
- #endif
168
+ supervisor_gc_init ();
169
169
mp_init ();
170
170
mp_obj_list_init ((mp_obj_list_t * )mp_sys_path , 0 );
171
171
mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR_ )); // current dir (or base dir of the script)
@@ -196,7 +196,7 @@ STATIC void stop_mp(void) {
196
196
usb_background ();
197
197
#endif
198
198
199
- gc_deinit ();
199
+ supervisor_gc_deinit ();
200
200
}
201
201
202
202
STATIC const char * _current_executing_filename = NULL ;
@@ -264,30 +264,29 @@ STATIC void count_strn(void *data, const char *str, size_t len) {
264
264
* (size_t * )data += len ;
265
265
}
266
266
267
- STATIC void cleanup_after_vm (supervisor_allocation * heap , mp_obj_t exception ) {
267
+ STATIC void cleanup_after_vm (mp_obj_t exception ) {
268
268
// Get the traceback of any exception from this run off the heap.
269
269
// MP_OBJ_SENTINEL means "this run does not contribute to traceback storage, don't touch it"
270
270
// MP_OBJ_NULL (=0) means "this run completed successfully, clear any stored traceback"
271
271
if (exception != MP_OBJ_SENTINEL ) {
272
- free_memory (prev_traceback_allocation );
272
+ free_memory (& prev_traceback );
273
273
// ReloadException is exempt from traceback printing in pyexec_file(), so treat it as "no
274
274
// traceback" here too.
275
275
if (exception && exception != MP_OBJ_FROM_PTR (& MP_STATE_VM (mp_reload_exception ))) {
276
276
size_t traceback_len = 0 ;
277
277
mp_print_t print_count = {& traceback_len , count_strn };
278
278
mp_obj_print_exception (& print_count , exception );
279
- prev_traceback_allocation = allocate_memory (align32_size (traceback_len + 1 ), false, true);
280
279
// Empirically, this never fails in practice - even when the heap is totally filled up
281
280
// with single-block-sized objects referenced by a root pointer, exiting the VM frees
282
281
// up several hundred bytes, sufficient for the traceback (which tends to be shortened
283
282
// because there wasn't memory for the full one). There may be convoluted ways of
284
283
// making it fail, but at this point I believe they are not worth spending code on.
285
- if (prev_traceback_allocation != NULL ) {
284
+ if (allocate_memory ( & prev_traceback , traceback_len + 1 , supervisor_simple_move ) ) {
286
285
vstr_t vstr ;
287
- vstr_init_fixed_buf (& vstr , traceback_len , (char * )prev_traceback_allocation -> ptr );
286
+ vstr_init_fixed_buf (& vstr , traceback_len + 1 , (char * )prev_traceback_allocation -> ptr );
288
287
mp_print_t print = {& vstr , (mp_print_strn_t )vstr_add_strn };
289
288
mp_obj_print_exception (& print , exception );
290
- (( char * ) prev_traceback_allocation -> ptr )[ traceback_len ] = '\0' ;
289
+ vstr_null_terminated_str ( & vstr ) ;
291
290
}
292
291
} else {
293
292
prev_traceback_allocation = NULL ;
@@ -344,8 +343,6 @@ STATIC void cleanup_after_vm(supervisor_allocation *heap, mp_obj_t exception) {
344
343
// Free the heap last because other modules may reference heap memory and need to shut down.
345
344
filesystem_flush ();
346
345
stop_mp ();
347
- free_memory (heap );
348
- supervisor_move_memory ();
349
346
350
347
// Let the workflows know we've reset in case they want to restart.
351
348
supervisor_workflow_reset ();
@@ -399,18 +396,16 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
399
396
};
400
397
#endif
401
398
402
- supervisor_allocation * heap = allocate_remaining_memory ();
403
-
404
399
// Prepare the VM state.
405
- start_mp (heap );
400
+ start_mp ();
406
401
407
402
#if CIRCUITPY_USB
408
403
usb_setup_with_vm ();
409
404
#endif
410
405
411
406
// Check if a different run file has been allocated
412
- if (next_code_allocation ) {
413
- next_code_info_t * info = ((next_code_info_t * )next_code_allocation -> ptr );
407
+ if (next_code_allocation . ptr ) {
408
+ next_code_info_t * info = ((next_code_info_t * )next_code_allocation . ptr );
414
409
info -> options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET ;
415
410
next_code_options = info -> options ;
416
411
if (info -> filename [0 ] != '\0' ) {
@@ -450,15 +445,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
450
445
451
446
452
447
// Finished executing python code. Cleanup includes filesystem flush and a board reset.
453
- cleanup_after_vm (heap , _exec_result .exception );
448
+ cleanup_after_vm (_exec_result .exception );
454
449
_exec_result .exception = NULL ;
455
450
456
451
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
457
452
// the options because it can be treated like any other reason-for-stickiness bit. The
458
453
// source is different though: it comes from the options that will apply to the next run,
459
454
// while the rest of next_code_options is what applied to this run.
460
- if (next_code_allocation != NULL &&
461
- (((next_code_info_t * )next_code_allocation -> ptr )-> options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET )) {
455
+ if (next_code_allocation . ptr &&
456
+ (((next_code_info_t * )next_code_allocation . ptr )-> options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET )) {
462
457
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET ;
463
458
}
464
459
@@ -706,8 +701,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
706
701
707
702
// free code allocation if unused
708
703
if ((next_code_options & next_code_stickiness_situation ) == 0 ) {
709
- free_memory (next_code_allocation );
710
- next_code_allocation = NULL ;
704
+ free_memory (& next_code_allocation );
711
705
}
712
706
713
707
#if CIRCUITPY_STATUS_LED
@@ -746,9 +740,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
746
740
747
741
// Do USB setup even if boot.py is not run.
748
742
749
- supervisor_allocation * heap = allocate_remaining_memory ();
750
-
751
- start_mp (heap );
743
+ start_mp ();
752
744
753
745
#if CIRCUITPY_USB
754
746
// Set up default USB values after boot.py VM starts but before running boot.py.
@@ -834,7 +826,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
834
826
835
827
port_post_boot_py (true);
836
828
837
- cleanup_after_vm (heap , _exec_result .exception );
829
+ cleanup_after_vm (_exec_result .exception );
838
830
_exec_result .exception = NULL ;
839
831
840
832
port_post_boot_py (false);
@@ -849,8 +841,7 @@ STATIC int run_repl(void) {
849
841
int exit_code = PYEXEC_FORCED_EXIT ;
850
842
stack_resize ();
851
843
filesystem_flush ();
852
- supervisor_allocation * heap = allocate_remaining_memory ();
853
- start_mp (heap );
844
+ start_mp ();
854
845
855
846
#if CIRCUITPY_USB
856
847
usb_setup_with_vm ();
@@ -893,7 +884,7 @@ STATIC int run_repl(void) {
893
884
exit_code = PYEXEC_DEEP_SLEEP ;
894
885
}
895
886
#endif
896
- cleanup_after_vm (heap , MP_OBJ_SENTINEL );
887
+ cleanup_after_vm (MP_OBJ_SENTINEL );
897
888
898
889
// Also reset bleio. The above call omits it in case workflows should continue. In this case,
899
890
// we're switching straight to another VM so we want to reset.
0 commit comments