Skip to content

Commit 64b9ee9

Browse files
authored
Merge pull request adafruit#985 from tannewt/heap_tweaks3
A few heap related tweaks
2 parents a45659c + 54179a0 commit 64b9ee9

File tree

16 files changed

+206
-35
lines changed

16 files changed

+206
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ TAGS
5858
#################
5959
*.orig
6060

61+
*.DS_Store

lib/utils/pyexec.c

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "py/runtime.h"
3434
#include "py/repl.h"
3535
#include "py/gc.h"
36+
#include "py/gc_long_lived.h"
3637
#include "py/frozenmod.h"
3738
#include "py/mphal.h"
3839
#if defined(USE_DEVICE_MODE)
@@ -97,6 +98,13 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
9798
#endif
9899
}
99100

101+
// If the code was loaded from a file its likely to be running for a while so we'll long
102+
// live it and collect any garbage before running.
103+
if (input_kind == MP_PARSE_FILE_INPUT) {
104+
module_fun = make_obj_long_lived(module_fun, 6);
105+
gc_collect();
106+
}
107+
100108
// execute code
101109
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
102110
start = mp_hal_ticks_ms();

main.c

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "lib/utils/pyexec.h"
4545

4646
#include "mpconfigboard.h"
47+
#include "supervisor/cpu.h"
4748
#include "supervisor/port.h"
4849
#include "supervisor/filesystem.h"
4950
// TODO(tannewt): Figure out how to choose language at compile time.
@@ -381,16 +382,17 @@ int __attribute__((used)) main(void) {
381382
}
382383

383384
void gc_collect(void) {
384-
// WARNING: This gc_collect implementation doesn't try to get root
385-
// pointers from CPU registers, and thus may function incorrectly.
386-
void *dummy;
387385
gc_collect_start();
386+
387+
mp_uint_t regs[10];
388+
mp_uint_t sp = cpu_get_regs_and_sp(regs);
389+
388390
// This collects root pointers from the VFS mount table. Some of them may
389391
// have lost their references in the VM even though they are mounted.
390392
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
391393
// This naively collects all object references from an approximate stack
392394
// range.
393-
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
395+
gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t));
394396
gc_collect_end();
395397
}
396398

ports/atmel-samd/Makefile

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,16 @@ endif
412412
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
413413
$(addprefix shared-module/, $(SRC_SHARED_MODULE))
414414

415+
SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s
416+
415417
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
416418
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
417419
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
418420
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
419421
ifeq ($(INTERNAL_LIBM),1)
420422
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
421423
endif
424+
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
422425

423426
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(STM_SRC_C)
424427

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.syntax unified
2+
.cpu cortex-m0
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint cpu_get_regs_and_sp(r0=uint regs[10])
8+
.global cpu_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type cpu_get_regs_and_sp, %function
12+
cpu_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0, #0]
15+
str r5, [r0, #4]
16+
str r6, [r0, #8]
17+
str r7, [r0, #12]
18+
push {r1}
19+
mov r1, r8
20+
str r1, [r0, #16]
21+
mov r1, r9
22+
str r1, [r0, #20]
23+
mov r1, r10
24+
str r1, [r0, #24]
25+
mov r1, r11
26+
str r1, [r0, #28]
27+
mov r1, r12
28+
str r1, [r0, #32]
29+
mov r1, r13
30+
str r1, [r0, #36]
31+
pop {r1}
32+
33+
@ return the sp
34+
mov r0, sp
35+
bx lr
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.syntax unified
2+
.cpu cortex-m4
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint cpu_get_regs_and_sp(r0=uint regs[10])
8+
.global cpu_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type cpu_get_regs_and_sp, %function
12+
cpu_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0], #4
15+
str r5, [r0], #4
16+
str r6, [r0], #4
17+
str r7, [r0], #4
18+
str r8, [r0], #4
19+
str r9, [r0], #4
20+
str r10, [r0], #4
21+
str r11, [r0], #4
22+
str r12, [r0], #4
23+
str r13, [r0], #4
24+
25+
@ return the sp
26+
mov r0, sp
27+
bx lr

ports/nrf/Makefile

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ SRC_SHARED_BINDINGS = \
208208
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \
209209
$(addprefix shared-module/, $(SRC_SHARED_MODULE))
210210

211+
SRC_S = supervisor/cpu.s
212+
211213
FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
212214
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
213215

@@ -218,6 +220,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o))
218220
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
219221
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
220222
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
223+
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
221224

222225
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
223226
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os

ports/nrf/supervisor/cpu.s

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.syntax unified
2+
.cpu cortex-m4
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint cpu_get_regs_and_sp(r0=uint regs[10])
8+
.global cpu_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type cpu_get_regs_and_sp, %function
12+
cpu_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0], #4
15+
str r5, [r0], #4
16+
str r6, [r0], #4
17+
str r7, [r0], #4
18+
str r8, [r0], #4
19+
str r9, [r0], #4
20+
str r10, [r0], #4
21+
str r11, [r0], #4
22+
str r12, [r0], #4
23+
str r13, [r0], #4
24+
25+
@ return the sp
26+
mov r0, sp
27+
bx lr

py/builtinimport.c

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "py/compile.h"
3333
#include "py/gc_long_lived.h"
34+
#include "py/gc.h"
3435
#include "py/objmodule.h"
3536
#include "py/persistentcode.h"
3637
#include "py/runtime.h"
@@ -468,6 +469,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
468469
// (the module that was just loaded) is not a package. This will be caught
469470
// on the next iteration because the file will not exist.
470471
}
472+
473+
// Loading a module thrashes the heap significantly so we explicitly clean up
474+
// afterwards.
475+
gc_collect();
471476
}
472477
if (outer_module_obj != MP_OBJ_NULL) {
473478
qstr s = qstr_from_strn(mod_str + last, i - last);

py/gc.c

100644100755
File mode changed.

0 commit comments

Comments
 (0)