Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ TAGS
#################
*.orig

*.DS_Store
8 changes: 8 additions & 0 deletions lib/utils/pyexec.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "py/runtime.h"
#include "py/repl.h"
#include "py/gc.h"
#include "py/gc_long_lived.h"
#include "py/frozenmod.h"
#include "py/mphal.h"
#if defined(USE_DEVICE_MODE)
Expand Down Expand Up @@ -97,6 +98,13 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
#endif
}

// If the code was loaded from a file its likely to be running for a while so we'll long
// live it and collect any garbage before running.
if (input_kind == MP_PARSE_FILE_INPUT) {
module_fun = make_obj_long_lived(module_fun, 6);
gc_collect();
}

// execute code
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
start = mp_hal_ticks_ms();
Expand Down
10 changes: 6 additions & 4 deletions main.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "lib/utils/pyexec.h"

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

void gc_collect(void) {
// WARNING: This gc_collect implementation doesn't try to get root
// pointers from CPU registers, and thus may function incorrectly.
void *dummy;
gc_collect_start();

mp_uint_t regs[10];
mp_uint_t sp = cpu_get_regs_and_sp(regs);

// This collects root pointers from the VFS mount table. Some of them may
// have lost their references in the VM even though they are mounted.
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
// This naively collects all object references from an approximate stack
// range.
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t));
gc_collect_end();
}

Expand Down
3 changes: 3 additions & 0 deletions ports/atmel-samd/Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,16 @@ endif
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE))

SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s

OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))

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

Expand Down
35 changes: 35 additions & 0 deletions ports/atmel-samd/supervisor/samd21_cpu.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.syntax unified
.cpu cortex-m0
.thumb
.text
.align 2

@ uint cpu_get_regs_and_sp(r0=uint regs[10])
.global cpu_get_regs_and_sp
.thumb
.thumb_func
.type cpu_get_regs_and_sp, %function
cpu_get_regs_and_sp:
@ store registers into given array
str r4, [r0, #0]
str r5, [r0, #4]
str r6, [r0, #8]
str r7, [r0, #12]
push {r1}
mov r1, r8
str r1, [r0, #16]
mov r1, r9
str r1, [r0, #20]
mov r1, r10
str r1, [r0, #24]
mov r1, r11
str r1, [r0, #28]
mov r1, r12
str r1, [r0, #32]
mov r1, r13
str r1, [r0, #36]
pop {r1}

@ return the sp
mov r0, sp
bx lr
27 changes: 27 additions & 0 deletions ports/atmel-samd/supervisor/samd51_cpu.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.syntax unified
.cpu cortex-m4
.thumb
.text
.align 2

@ uint cpu_get_regs_and_sp(r0=uint regs[10])
.global cpu_get_regs_and_sp
.thumb
.thumb_func
.type cpu_get_regs_and_sp, %function
cpu_get_regs_and_sp:
@ store registers into given array
str r4, [r0], #4
str r5, [r0], #4
str r6, [r0], #4
str r7, [r0], #4
str r8, [r0], #4
str r9, [r0], #4
str r10, [r0], #4
str r11, [r0], #4
str r12, [r0], #4
str r13, [r0], #4

@ return the sp
mov r0, sp
bx lr
3 changes: 3 additions & 0 deletions ports/nrf/Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ SRC_SHARED_BINDINGS = \
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE))

SRC_S = supervisor/cpu.s

FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))

Expand All @@ -219,6 +221,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))

$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os
Expand Down
27 changes: 27 additions & 0 deletions ports/nrf/supervisor/cpu.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.syntax unified
.cpu cortex-m4
.thumb
.text
.align 2

@ uint cpu_get_regs_and_sp(r0=uint regs[10])
.global cpu_get_regs_and_sp
.thumb
.thumb_func
.type cpu_get_regs_and_sp, %function
cpu_get_regs_and_sp:
@ store registers into given array
str r4, [r0], #4
str r5, [r0], #4
str r6, [r0], #4
str r7, [r0], #4
str r8, [r0], #4
str r9, [r0], #4
str r10, [r0], #4
str r11, [r0], #4
str r12, [r0], #4
str r13, [r0], #4

@ return the sp
mov r0, sp
bx lr
5 changes: 5 additions & 0 deletions py/builtinimport.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "py/compile.h"
#include "py/gc_long_lived.h"
#include "py/gc.h"
#include "py/objmodule.h"
#include "py/persistentcode.h"
#include "py/runtime.h"
Expand Down Expand Up @@ -468,6 +469,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
// (the module that was just loaded) is not a package. This will be caught
// on the next iteration because the file will not exist.
}

// Loading a module thrashes the heap significantly so we explicitly clean up
// afterwards.
gc_collect();
}
if (outer_module_obj != MP_OBJ_NULL) {
qstr s = qstr_from_strn(mod_str + last, i - last);
Expand Down
Empty file modified py/gc.c
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion py/gc_long_lived.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mp_obj_t make_obj_long_lived(mp_obj_t obj, uint8_t max_depth){
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_property)) {
mp_obj_property_t *prop = MP_OBJ_TO_PTR(obj);
return MP_OBJ_FROM_PTR(make_property_long_lived(prop, max_depth));
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_str)) {
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_str) || MP_OBJ_IS_TYPE(obj, &mp_type_bytes)) {
mp_obj_str_t *str = MP_OBJ_TO_PTR(obj);
return MP_OBJ_FROM_PTR(make_str_long_lived(str));
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
Expand Down
6 changes: 6 additions & 0 deletions py/mpconfig.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
#define MICROPY_ALLOC_QSTR_CHUNK_INIT (128)
#endif

// Max number of entries in newly allocated QSTR pools. Smaller numbers may make QSTR lookups
// slightly slower but reduce the waste of unused spots.
#ifndef MICROPY_QSTR_POOL_MAX_ENTRIES
#define MICROPY_QSTR_POOL_MAX_ENTRIES (64)
#endif

// Initial amount for lexer indentation level
#ifndef MICROPY_ALLOC_LEXER_INDENT_INIT
#define MICROPY_ALLOC_LEXER_INDENT_INIT (10)
Expand Down
10 changes: 7 additions & 3 deletions py/qstr.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,18 @@ STATIC qstr qstr_add(const byte *q_ptr) {

// make sure we have room in the pool for a new qstr
if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
uint32_t new_pool_length = MP_STATE_VM(last_pool)->alloc * 2;
if (new_pool_length > MICROPY_QSTR_POOL_MAX_ENTRIES) {
new_pool_length = MICROPY_QSTR_POOL_MAX_ENTRIES;
}
qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, new_pool_length);
if (pool == NULL) {
QSTR_EXIT();
m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2);
m_malloc_fail(new_pool_length);
}
pool->prev = MP_STATE_VM(last_pool);
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
pool->alloc = new_pool_length;
pool->len = 0;
MP_STATE_VM(last_pool) = pool;
DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
Expand Down
34 changes: 34 additions & 0 deletions supervisor/cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_SUPERVISOR_CPU_H
#define MICROPY_INCLUDED_SUPERVISOR_CPU_H

// Adds up to 10 pointers from the CPUs registers to regs. This is used to make sure no actively
// used heap memory is freed. Its usually implemented in assembly.
mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs);

#endif // MICROPY_INCLUDED_SUPERVISOR_CPU_H
Loading