|
78 | 78 | #define MP_TASK_STACK_LIMIT_MARGIN (1024)
|
79 | 79 | #endif
|
80 | 80 |
|
| 81 | +typedef struct _native_code_node_t { |
| 82 | + struct _native_code_node_t *next; |
| 83 | + uint32_t data[]; |
| 84 | +} native_code_node_t; |
| 85 | + |
| 86 | +static native_code_node_t *native_code_head = NULL; |
| 87 | + |
| 88 | +static void esp_native_code_free_all(void); |
| 89 | + |
81 | 90 | int vprintf_null(const char *format, va_list ap) {
|
82 | 91 | // do nothing: this is used as a log target during raw repl mode
|
83 | 92 | return 0;
|
@@ -130,8 +139,6 @@ void mp_task(void *pvParameter) {
|
130 | 139 | mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
|
131 | 140 | readline_init0();
|
132 | 141 |
|
133 |
| - MP_STATE_PORT(native_code_pointers) = MP_OBJ_NULL; |
134 |
| - |
135 | 142 | // initialise peripherals
|
136 | 143 | machine_pins_init();
|
137 | 144 | #if MICROPY_PY_MACHINE_I2S
|
@@ -182,18 +189,11 @@ void mp_task(void *pvParameter) {
|
182 | 189 | mp_thread_deinit();
|
183 | 190 | #endif
|
184 | 191 |
|
185 |
| - // Free any native code pointers that point to iRAM. |
186 |
| - if (MP_STATE_PORT(native_code_pointers) != MP_OBJ_NULL) { |
187 |
| - size_t len; |
188 |
| - mp_obj_t *items; |
189 |
| - mp_obj_list_get(MP_STATE_PORT(native_code_pointers), &len, &items); |
190 |
| - for (size_t i = 0; i < len; ++i) { |
191 |
| - heap_caps_free(MP_OBJ_TO_PTR(items[i])); |
192 |
| - } |
193 |
| - } |
194 |
| - |
195 | 192 | gc_sweep_all();
|
196 | 193 |
|
| 194 | + // Free any native code pointers that point to iRAM. |
| 195 | + esp_native_code_free_all(); |
| 196 | + |
197 | 197 | mp_hal_stdout_tx_str("MPY: soft reboot\r\n");
|
198 | 198 |
|
199 | 199 | // deinitialise peripherals
|
@@ -232,21 +232,27 @@ void nlr_jump_fail(void *val) {
|
232 | 232 | esp_restart();
|
233 | 233 | }
|
234 | 234 |
|
| 235 | +static void esp_native_code_free_all(void) { |
| 236 | + while (native_code_head != NULL) { |
| 237 | + native_code_node_t *next = native_code_head->next; |
| 238 | + heap_caps_free(native_code_head); |
| 239 | + native_code_head = next; |
| 240 | + } |
| 241 | +} |
| 242 | + |
235 | 243 | void *esp_native_code_commit(void *buf, size_t len, void *reloc) {
|
236 | 244 | len = (len + 3) & ~3;
|
237 |
| - uint32_t *p = heap_caps_malloc(len, MALLOC_CAP_EXEC); |
238 |
| - if (p == NULL) { |
239 |
| - m_malloc_fail(len); |
240 |
| - } |
241 |
| - if (MP_STATE_PORT(native_code_pointers) == MP_OBJ_NULL) { |
242 |
| - MP_STATE_PORT(native_code_pointers) = mp_obj_new_list(0, NULL); |
| 245 | + size_t len_node = sizeof(native_code_node_t) + len; |
| 246 | + native_code_node_t *node = heap_caps_malloc(len_node, MALLOC_CAP_EXEC); |
| 247 | + if (node == NULL) { |
| 248 | + m_malloc_fail(len_node); |
243 | 249 | }
|
244 |
| - mp_obj_list_append(MP_STATE_PORT(native_code_pointers), MP_OBJ_TO_PTR(p)); |
| 250 | + node->next = native_code_head; |
| 251 | + native_code_head = node; |
| 252 | + void *p = node->data; |
245 | 253 | if (reloc) {
|
246 | 254 | mp_native_relocate(reloc, buf, (uintptr_t)p);
|
247 | 255 | }
|
248 | 256 | memcpy(p, buf, len);
|
249 | 257 | return p;
|
250 | 258 | }
|
251 |
| - |
252 |
| -MP_REGISTER_ROOT_POINTER(mp_obj_t native_code_pointers); |
|
0 commit comments