Skip to content

Commit a215b09

Browse files
committed
Move gc_collect to py/gc.c
* Move gc_collect from main to py/gc.c * Define GC's memory boundaries in linker script * Issue adafruit#220
1 parent 40048ad commit a215b09

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

py/gc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ void gc_collect_end(void) {
187187
gc_sweep();
188188
}
189189

190+
extern void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
191+
192+
void gc_collect(void) {
193+
extern char _ram_start; /* defined by linker script */
194+
extern char _ram_end; /* defined by linker script */
195+
extern char _heap_start; /* defined by linker script */
196+
extern char _heap_end; /* defined by linker script */
197+
198+
gc_collect_start();
199+
gc_collect_root((void**)&_ram_start, (&_heap_start - &_ram_start) / 4);
200+
machine_uint_t regs[10];
201+
gc_helper_get_regs_and_clean_stack(regs, (uint32_t) &_heap_end);
202+
gc_collect_root((void**)&_heap_end, (&_ram_end - &_heap_end) / 4); // will trace regs since they now live in this function on the stack
203+
gc_collect_end();
204+
}
205+
190206
void gc_info(gc_info_t *info) {
191207
info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
192208
info->used = 0;

stm/main.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
int errno;
4949

5050
extern uint32_t _heap_start;
51+
extern uint32_t _heap_end;
5152

5253
static FATFS fatfs0;
5354

@@ -455,21 +456,12 @@ bool do_file(const char *filename) {
455456
}
456457
}
457458

458-
#define RAM_START (0x20000000) // fixed for chip
459-
#define HEAP_END (0x2001c000) // tunable
460-
#define RAM_END (0x20020000) // fixed for chip
461-
462-
void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
459+
mp_obj_t pyb_gc(void) {
460+
uint32_t start,ticks;
463461

464-
void gc_collect(void) {
465-
uint32_t start = sys_tick_counter;
466-
gc_collect_start();
467-
gc_collect_root((void**)RAM_START, (((uint32_t)&_heap_start) - RAM_START) / 4);
468-
machine_uint_t regs[10];
469-
gc_helper_get_regs_and_clean_stack(regs, HEAP_END);
470-
gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack
471-
gc_collect_end();
472-
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
462+
start = sys_tick_counter;
463+
gc_collect();
464+
ticks = sys_tick_counter - start; // TODO implement a function that does this properly
473465

474466
if (0) {
475467
// print GC info
@@ -480,10 +472,7 @@ void gc_collect(void) {
480472
printf(" %lu : %lu\n", info.used, info.free);
481473
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
482474
}
483-
}
484475

485-
mp_obj_t pyb_gc(void) {
486-
gc_collect();
487476
return mp_const_none;
488477
}
489478

@@ -609,7 +598,7 @@ int main(void) {
609598
soft_reset:
610599

611600
// GC init
612-
gc_init(&_heap_start, (void*)HEAP_END);
601+
gc_init(&_heap_start, &_heap_end);
613602

614603
// Micro Python init
615604
qstr_init();

stm/stm32f405.ld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ _minimum_heap_size = 16K;
1919
/* top end of the stack */
2020
_estack = ORIGIN(RAM) + LENGTH(RAM);
2121

22+
_ram_start = 0x20000000;
23+
_ram_end = 0x20020000;
24+
_heap_end = 0x2001c000;
25+
2226
/* define output sections */
2327
SECTIONS
2428
{

0 commit comments

Comments
 (0)