Skip to content

esp32/main: Store native code as linked list instead of list on GC heap. #15589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
#define MP_TASK_STACK_LIMIT_MARGIN (1024)
#endif

typedef struct _native_code_node_t {
struct _native_code_node_t *next;
uint32_t data[];
} native_code_node_t;

static native_code_node_t *native_code_head = NULL;

static void esp_native_code_free_all(void);

int vprintf_null(const char *format, va_list ap) {
// do nothing: this is used as a log target during raw repl mode
return 0;
Expand Down Expand Up @@ -130,8 +139,6 @@ void mp_task(void *pvParameter) {
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
readline_init0();

MP_STATE_PORT(native_code_pointers) = MP_OBJ_NULL;

// initialise peripherals
machine_pins_init();
#if MICROPY_PY_MACHINE_I2S
Expand Down Expand Up @@ -182,18 +189,11 @@ void mp_task(void *pvParameter) {
mp_thread_deinit();
#endif

// Free any native code pointers that point to iRAM.
if (MP_STATE_PORT(native_code_pointers) != MP_OBJ_NULL) {
size_t len;
mp_obj_t *items;
mp_obj_list_get(MP_STATE_PORT(native_code_pointers), &len, &items);
for (size_t i = 0; i < len; ++i) {
heap_caps_free(MP_OBJ_TO_PTR(items[i]));
}
}

gc_sweep_all();

// Free any native code pointers that point to iRAM.
esp_native_code_free_all();

mp_hal_stdout_tx_str("MPY: soft reboot\r\n");

// deinitialise peripherals
Expand Down Expand Up @@ -232,21 +232,27 @@ void nlr_jump_fail(void *val) {
esp_restart();
}

static void esp_native_code_free_all(void) {
while (native_code_head != NULL) {
native_code_node_t *next = native_code_head->next;
heap_caps_free(native_code_head);
native_code_head = next;
}
}

void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
len = (len + 3) & ~3;
uint32_t *p = heap_caps_malloc(len, MALLOC_CAP_EXEC);
if (p == NULL) {
m_malloc_fail(len);
}
if (MP_STATE_PORT(native_code_pointers) == MP_OBJ_NULL) {
MP_STATE_PORT(native_code_pointers) = mp_obj_new_list(0, NULL);
size_t len_node = sizeof(native_code_node_t) + len;
native_code_node_t *node = heap_caps_malloc(len_node, MALLOC_CAP_EXEC);
if (node == NULL) {
m_malloc_fail(len_node);
}
mp_obj_list_append(MP_STATE_PORT(native_code_pointers), MP_OBJ_TO_PTR(p));
node->next = native_code_head;
native_code_head = node;
void *p = node->data;
if (reloc) {
mp_native_relocate(reloc, buf, (uintptr_t)p);
}
memcpy(p, buf, len);
return p;
}

MP_REGISTER_ROOT_POINTER(mp_obj_t native_code_pointers);
Loading