Skip to content

Commit 0d32f1a

Browse files
committed
esp8266: When doing GC be sure to trace the memory holding native code.
Native code can hold pointers to objects on the heap, eg constant objects like big integers.
1 parent 5e1ccdd commit 0d32f1a

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

esp8266/gccollect.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void gc_collect(void) {
4646
// trace the stack, including the registers (since they live on the stack in this function)
4747
gc_collect_root((void**)sp, (STACK_END - sp) / sizeof(uint32_t));
4848

49+
#if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
50+
// trace any native code because it can contain pointers to the heap
51+
esp_native_code_gc_collect();
52+
#endif
53+
4954
// end the GC
5055
gc_collect_end();
5156
}

esp8266/gccollect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ extern uint32_t _heap_start;
3838
extern uint32_t _heap_end;
3939

4040
void gc_collect(void);
41+
void esp_native_code_gc_collect(void);

esp8266/modesp.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_esf_free_bufs_obj, esp_esf_free_bufs);
711711
// esp.set_native_code_location() function; see below. If flash is selected
712712
// then it is erased as needed.
713713

714+
#include "gccollect.h"
715+
714716
#define IRAM1_END (0x40108000)
715717
#define FLASH_START (0x40200000)
716718
#define FLASH_END (0x40300000)
@@ -734,6 +736,16 @@ void esp_native_code_init(void) {
734736
esp_native_code_erased = 0;
735737
}
736738

739+
void esp_native_code_gc_collect(void) {
740+
void *src;
741+
if (esp_native_code_location == ESP_NATIVE_CODE_IRAM1) {
742+
src = (void*)esp_native_code_start;
743+
} else {
744+
src = (void*)(FLASH_START + esp_native_code_start);
745+
}
746+
gc_collect_root(src, (esp_native_code_end - esp_native_code_start) / sizeof(uint32_t));
747+
}
748+
737749
void *esp_native_code_commit(void *buf, size_t len) {
738750
//printf("COMMIT(buf=%p, len=%u, start=%08x, cur=%08x, end=%08x, erased=%08x)\n", buf, len, esp_native_code_start, esp_native_code_cur, esp_native_code_end, esp_native_code_erased);
739751

0 commit comments

Comments
 (0)