Skip to content

Commit fe083ad

Browse files
mikeysklarclaude
andcommitted
supervisor: add port_gc_deinit(), called after the VM heap is torn down
stop_mp() runs the heap finalisers in gc_deinit(). A port that keeps memory outside the VM heap which heap objects point into, such as native code copied into executable RAM, can only free it after that point. reset_port() runs before stop_mp(), which is too early. The weak default does nothing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 5dfccee commit fe083ad

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static void stop_mp(void) {
249249
qstr_reset();
250250

251251
gc_deinit();
252+
port_gc_deinit();
252253
port_free(_heap);
253254
_heap = NULL;
254255

‎supervisor/port.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ void port_boot_info(void);
115115
// A default weak implementation is provided that does nothing.
116116
void port_gc_collect(void);
117117

118+
// Called once the VM heap is torn down and its finalisers have run. Ports free
119+
// memory here that heap objects may point into.
120+
// A default weak implementation is provided that does nothing.
121+
void port_gc_deinit(void);
122+
118123
// Most ports that implement CIRCUITPY_BOOT_BUTTON use a generic version of
119124
// this function to sense the button. Ports that need to can override this
120125
// function to provide their own implementation.

‎supervisor/shared/port.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ MP_WEAK bool port_boot_button_pressed(void) {
119119
MP_WEAK void port_gc_collect(void) {
120120
}
121121

122+
// Ports may provide an implementation of this function if it is needed
123+
MP_WEAK void port_gc_deinit(void) {
124+
}
125+
122126
// Allocates an object in the port heap, not the VM heap, and also sets type, for mp_obj_malloc{,_var} macros.
123127
MP_NOINLINE void *mp_obj_port_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
124128
mp_obj_base_t *base = (mp_obj_base_t *)port_malloc_zero(num_bytes, false);

0 commit comments

Comments
 (0)