Skip to content

Commit c7404a3

Browse files
committed
Add movable allocation system.
This allows calls to `allocate_memory()` while the VM is running, it will then allocate from the GC heap (unless there is a suitable hole among the supervisor allocations), and when the VM exits and the GC heap is freed, the allocation will be moved to the bottom of the former GC heap and transformed into a proper supervisor allocation. Existing movable allocations will also be moved to defragment the supervisor heap and ensure that the next VM run gets as much memory as possible for the GC heap. By itself this breaks terminalio because it violates the assumption that supervisor_display_move_memory() still has access to an undisturbed heap to copy the tilegrid from. It will work in many cases, but if you're unlucky you will get garbled terminal contents after exiting from the vm run that created the display. This will be fixed in the following commit, which is separate to simplify review.
1 parent bd87201 commit c7404a3

File tree

19 files changed

+283
-137
lines changed

19 files changed

+283
-137
lines changed

main.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ void start_mp(supervisor_allocation* heap) {
123123
// to recover from limit hit. (Limit is measured in bytes.)
124124
mp_stack_ctrl_init();
125125

126-
if (stack_alloc != NULL) {
127-
mp_stack_set_limit(stack_alloc->length - 1024);
126+
if (stack_get_bottom() != NULL) {
127+
mp_stack_set_limit(stack_get_length() - 1024);
128128
}
129129

130130

131131
#if MICROPY_MAX_STACK_USAGE
132132
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
133-
if (stack_alloc != NULL) {
134-
mp_stack_set_bottom(stack_alloc->ptr);
133+
if (stack_get_bottom() != NULL) {
134+
mp_stack_set_bottom(stack_get_bottom());
135135
mp_stack_fill_with_sentinel();
136136
}
137137
#endif
@@ -148,7 +148,7 @@ void start_mp(supervisor_allocation* heap) {
148148
#endif
149149

150150
#if MICROPY_ENABLE_GC
151-
gc_init(heap->ptr, heap->ptr + heap->length / 4);
151+
gc_init(heap->ptr, heap->ptr + get_allocation_length(heap) / 4);
152152
#endif
153153
mp_init();
154154
mp_obj_list_init(mp_sys_path, 0);
@@ -451,9 +451,6 @@ int __attribute__((used)) main(void) {
451451
// initialise the cpu and peripherals
452452
safe_mode_t safe_mode = port_init();
453453

454-
// Init memory after the port in case the port needs to set aside memory.
455-
memory_init();
456-
457454
// Turn on LEDs
458455
init_status_leds();
459456
rgb_led_status_init();

ports/atmel-samd/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ void reset_cpu(void) {
390390
reset();
391391
}
392392

393-
supervisor_allocation* port_fixed_stack(void) {
394-
return NULL;
393+
bool port_has_fixed_stack(void) {
394+
return false;
395395
}
396396

397397
uint32_t *port_stack_get_limit(void) {

ports/cxd56/supervisor/port.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ void reset_to_bootloader(void) {
9898
}
9999
}
100100

101-
supervisor_allocation _fixed_stack;
102-
103-
supervisor_allocation* port_fixed_stack(void) {
104-
_fixed_stack.ptr = port_stack_get_limit();
105-
_fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
106-
return &_fixed_stack;
101+
bool port_has_fixed_stack(void) {
102+
return true;
107103
}
108104

109105
uint32_t *port_stack_get_limit(void) {

ports/esp32s2/supervisor/port.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,8 @@ uint32_t *port_stack_get_top(void) {
193193
return port_stack_get_limit() + ESP_TASK_MAIN_STACK / (sizeof(uint32_t) / sizeof(StackType_t));
194194
}
195195

196-
supervisor_allocation _fixed_stack;
197-
198-
supervisor_allocation* port_fixed_stack(void) {
199-
_fixed_stack.ptr = port_stack_get_limit();
200-
_fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
201-
return &_fixed_stack;
196+
bool port_has_fixed_stack(void) {
197+
return true;
202198
}
203199

204200
// Place the word to save just after our BSS section that gets blanked.

ports/litex/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ void reset_cpu(void) {
9898
for(;;) {}
9999
}
100100

101-
supervisor_allocation* port_fixed_stack(void) {
102-
return NULL;
101+
bool port_has_fixed_stack(void) {
102+
return false;
103103
}
104104

105105
uint32_t *port_heap_get_bottom(void) {

ports/mimxrt10xx/supervisor/port.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,8 @@ uint32_t *port_stack_get_top(void) {
334334
return &_ld_stack_top;
335335
}
336336

337-
supervisor_allocation _fixed_stack;
338-
supervisor_allocation* port_fixed_stack(void) {
339-
_fixed_stack.ptr = port_stack_get_limit();
340-
_fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
341-
return &_fixed_stack;
337+
bool port_has_fixed_stack(void) {
338+
return true;
342339
}
343340

344341
uint32_t *port_heap_get_bottom(void) {

ports/nrf/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ uint32_t *port_heap_get_top(void) {
251251
return port_stack_get_top();
252252
}
253253

254-
supervisor_allocation* port_fixed_stack(void) {
255-
return NULL;
254+
bool port_has_fixed_stack(void) {
255+
return false;
256256
}
257257

258258
uint32_t *port_stack_get_limit(void) {

ports/stm/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ uint32_t *port_heap_get_top(void) {
267267
return &_ld_heap_end;
268268
}
269269

270-
supervisor_allocation* port_fixed_stack(void) {
271-
return NULL;
270+
bool port_has_fixed_stack(void) {
271+
return false;
272272
}
273273

274274
uint32_t *port_stack_get_limit(void) {

py/circuitpy_mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ extern const struct _mp_obj_module_t wifi_module;
858858

859859
#include "supervisor/flash_root_pointers.h"
860860

861+
// From supervisor/memory.c
862+
struct _supervisor_allocation_node;
863+
861864
#define CIRCUITPY_COMMON_ROOT_POINTERS \
862865
const char *readline_hist[8]; \
863866
vstr_t *repl_line; \
@@ -869,6 +872,7 @@ extern const struct _mp_obj_module_t wifi_module;
869872
FLASH_ROOT_POINTERS \
870873
MEMORYMONITOR_ROOT_POINTERS \
871874
NETWORK_ROOT_POINTERS \
875+
struct _supervisor_allocation_node* first_embedded_allocation; \
872876

873877
void supervisor_run_background_tasks_if_tick(void);
874878
#define RUN_BACKGROUND_TASKS (supervisor_run_background_tasks_if_tick())

shared-module/rgbmatrix/RGBMatrix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void *common_hal_rgbmatrix_allocator_impl(size_t sz) {
220220
if (gc_alloc_possible()) {
221221
return m_malloc_maybe(sz + sizeof(void*), true);
222222
} else {
223-
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
223+
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false, false);
224224
return allocation ? allocation->ptr : NULL;
225225
}
226226
}

0 commit comments

Comments
 (0)