Skip to content

Commit dfa2581

Browse files
authored
Merge pull request micropython#1057 from tannewt/flexible_heap
Add basic memory allocation outside Python runtime
2 parents 88ae7a9 + 4b247ea commit dfa2581

File tree

12 files changed

+407
-44
lines changed

12 files changed

+407
-44
lines changed

main.c

+62-31
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@
4545

4646
#include "mpconfigboard.h"
4747
#include "supervisor/cpu.h"
48+
#include "supervisor/memory.h"
4849
#include "supervisor/port.h"
4950
#include "supervisor/filesystem.h"
5051
// TODO(tannewt): Figure out how to choose language at compile time.
5152
#include "supervisor/messages/en-US.h"
5253
#include "supervisor/shared/autoreload.h"
5354
#include "supervisor/shared/rgb_led_status.h"
55+
#include "supervisor/shared/stack.h"
5456
#include "supervisor/serial.h"
5557

5658
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
@@ -73,12 +75,21 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
7375
}
7476
}
7577

76-
static char heap[PORT_HEAP_SIZE];
77-
78-
void reset_mp(void) {
78+
void start_mp(supervisor_allocation* heap) {
7979
reset_status_led();
8080
autoreload_stop();
8181

82+
// Stack limit should be less than real stack size, so we have a chance
83+
// to recover from limit hit. (Limit is measured in bytes.)
84+
mp_stack_ctrl_init();
85+
mp_stack_set_limit(stack_alloc->length - 1024);
86+
87+
#if MICROPY_MAX_STACK_USAGE
88+
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
89+
mp_stack_set_bottom(stack_alloc->ptr);
90+
mp_stack_fill_with_sentinel();
91+
#endif
92+
8293
// Sync the file systems in case any used RAM from the GC to cache. As soon
8394
// as we re-init the GC all bets are off on the cache.
8495
filesystem_flush();
@@ -87,7 +98,7 @@ void reset_mp(void) {
8798
readline_init0();
8899

89100
#if MICROPY_ENABLE_GC
90-
gc_init(heap, heap + sizeof(heap));
101+
gc_init(heap->ptr, heap->ptr + heap->length / 4);
91102
#endif
92103
mp_init();
93104
mp_obj_list_init(mp_sys_path, 0);
@@ -99,6 +110,11 @@ void reset_mp(void) {
99110

100111
mp_obj_list_init(mp_sys_argv, 0);
101112
}
113+
114+
void stop_mp(void) {
115+
116+
}
117+
102118
#define STRING_LIST(...) {__VA_ARGS__, ""}
103119

104120
// Look for the first file that exists in the list of filenames, using mp_import_stat().
@@ -124,7 +140,7 @@ bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
124140
return true;
125141
}
126142

127-
bool start_mp(safe_mode_t safe_mode) {
143+
bool run_code_py(safe_mode_t safe_mode) {
128144
bool serial_connected_at_start = serial_connected();
129145
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
130146
if (serial_connected_at_start) {
@@ -155,14 +171,20 @@ bool start_mp(safe_mode_t safe_mode) {
155171
const char *supported_filenames[] = STRING_LIST("code.txt", "code.py", "main.py", "main.txt");
156172
const char *double_extension_filenames[] = STRING_LIST("code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
157173
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
158-
reset_mp();
174+
175+
stack_resize();
176+
filesystem_flush();
177+
supervisor_allocation* heap = allocate_remaining_memory();
178+
start_mp(heap);
159179
found_main = maybe_run_list(supported_filenames, &result);
160180
if (!found_main){
161181
found_main = maybe_run_list(double_extension_filenames, &result);
162182
if (found_main) {
163183
serial_write(MSG_DOUBLE_FILE_EXTENSION);
164184
}
165185
}
186+
stop_mp();
187+
free_memory(heap);
166188

167189
reset_port();
168190
reset_board();
@@ -291,6 +313,12 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
291313
}
292314
#endif
293315

316+
stack_init();
317+
// TODO(tannewt): Allocate temporary space to hold custom usb descriptors.
318+
filesystem_flush();
319+
supervisor_allocation* heap = allocate_remaining_memory();
320+
start_mp(heap);
321+
294322
// TODO(tannewt): Re-add support for flashing boot error output.
295323
bool found_boot = maybe_run_list(boot_py_filenames, NULL);
296324
(void) found_boot;
@@ -306,27 +334,41 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
306334
// Reset to remove any state that boot.py setup. It should only be used to
307335
// change internal state that's not in the heap.
308336
reset_port();
309-
reset_mp();
337+
reset_board();
338+
stop_mp();
339+
free_memory(heap);
340+
}
341+
}
342+
343+
int run_repl(void) {
344+
int exit_code = PYEXEC_FORCED_EXIT;
345+
stack_resize();
346+
filesystem_flush();
347+
supervisor_allocation* heap = allocate_remaining_memory();
348+
start_mp(heap);
349+
autoreload_suspend();
350+
new_status_color(REPL_RUNNING);
351+
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
352+
exit_code = pyexec_raw_repl();
353+
} else {
354+
exit_code = pyexec_friendly_repl();
310355
}
356+
reset_port();
357+
reset_board();
358+
stop_mp();
359+
free_memory(heap);
360+
autoreload_resume();
361+
return exit_code;
311362
}
312363

313364
int __attribute__((used)) main(void) {
365+
memory_init();
366+
314367
// initialise the cpu and peripherals
315368
safe_mode_t safe_mode = port_init();
316369

317370
rgb_led_status_init();
318371

319-
// Stack limit should be less than real stack size, so we have a chance
320-
// to recover from limit hit. (Limit is measured in bytes.)
321-
mp_stack_set_top((char*)&_estack);
322-
mp_stack_set_limit((char*)&_estack - (char*)&_ebss - 1024);
323-
324-
#if MICROPY_MAX_STACK_USAGE
325-
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
326-
mp_stack_set_bottom(&_ezero + 1);
327-
mp_stack_fill_with_sentinel();
328-
#endif
329-
330372
// Create a new filesystem only if we're not in a safe mode.
331373
// A power brownout here could make it appear as if there's
332374
// no SPI flash filesystem, and we might erase the existing one.
@@ -335,7 +377,6 @@ int __attribute__((used)) main(void) {
335377
// Reset everything and prep MicroPython to run boot.py.
336378
reset_port();
337379
reset_board();
338-
reset_mp();
339380

340381
// Turn on autoreload by default but before boot.py in case it wants to change it.
341382
autoreload_enable();
@@ -355,24 +396,14 @@ int __attribute__((used)) main(void) {
355396
bool first_run = true;
356397
for (;;) {
357398
if (!skip_repl) {
358-
reset_mp();
359-
autoreload_suspend();
360-
new_status_color(REPL_RUNNING);
361-
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
362-
exit_code = pyexec_raw_repl();
363-
} else {
364-
exit_code = pyexec_friendly_repl();
365-
}
366-
autoreload_resume();
367-
reset_port();
368-
reset_board();
399+
exit_code = run_repl();
369400
}
370401
if (exit_code == PYEXEC_FORCED_EXIT) {
371402
if (!first_run) {
372403
serial_write(MSG_SOFT_REBOOT MSG_NEWLINE);
373404
}
374405
first_run = false;
375-
skip_repl = start_mp(safe_mode);
406+
skip_repl = run_code_py(safe_mode);
376407
} else if (exit_code != 0) {
377408
break;
378409
}

ports/atmel-samd/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ SRC_C = \
288288
lib/libc/string0.c \
289289
lib/mp-readline/readline.c \
290290
$(BUILD)/autogen_usb_descriptor.c \
291-
freetouch/adafruit_ptc.c
291+
freetouch/adafruit_ptc.c \
292+
supervisor/shared/memory.c
292293

293294
# Choose which flash filesystem impl to use.
294295
# (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive.

ports/atmel-samd/external_flash/external_flash.c

+27-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "py/runtime.h"
3838
#include "lib/oofatfs/ff.h"
3939
#include "shared-bindings/microcontroller/__init__.h"
40+
#include "supervisor/memory.h"
4041
#include "supervisor/shared/rgb_led_status.h"
4142

4243
#include "hal_gpio.h"
@@ -59,6 +60,8 @@ static const external_flash_device* flash_device = NULL;
5960
// cache.
6061
static uint32_t dirty_mask;
6162

63+
static supervisor_allocation* supervisor_cache = NULL;
64+
6265
// Wait until both the write enable and write in progress bits have cleared.
6366
static bool wait_for_flash_ready(void) {
6467
uint8_t read_status_response[1] = {0x00};
@@ -308,6 +311,23 @@ static bool flush_scratch_flash(void) {
308311
static bool allocate_ram_cache(void) {
309312
uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE;
310313
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
314+
315+
uint32_t table_size = blocks_per_sector * pages_per_block * sizeof(uint32_t);
316+
// Attempt to allocate outside the heap first.
317+
supervisor_cache = allocate_memory(table_size + SPI_FLASH_ERASE_SIZE, false);
318+
if (supervisor_cache != NULL) {
319+
MP_STATE_VM(flash_ram_cache) = (uint8_t **) supervisor_cache->ptr;
320+
uint8_t* page_start = (uint8_t *) supervisor_cache->ptr + table_size;
321+
322+
for (uint8_t i = 0; i < blocks_per_sector; i++) {
323+
for (uint8_t j = 0; j < pages_per_block; j++) {
324+
uint32_t offset = i * pages_per_block + j;
325+
MP_STATE_VM(flash_ram_cache)[offset] = page_start + offset * SPI_FLASH_PAGE_SIZE;
326+
}
327+
}
328+
return true;
329+
}
330+
311331
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
312332
if (MP_STATE_VM(flash_ram_cache) == NULL) {
313333
return false;
@@ -383,14 +403,19 @@ static bool flush_ram_cache(bool keep_cache) {
383403
write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
384404
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
385405
SPI_FLASH_PAGE_SIZE);
386-
if (!keep_cache) {
406+
if (!keep_cache && supervisor_cache == NULL) {
387407
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
388408
}
389409
}
390410
}
391411
// We're done with the cache for now so give it back.
392412
if (!keep_cache) {
393-
m_free(MP_STATE_VM(flash_ram_cache));
413+
if (supervisor_cache != NULL) {
414+
free_memory(supervisor_cache);
415+
supervisor_cache = NULL;
416+
} else {
417+
m_free(MP_STATE_VM(flash_ram_cache));
418+
}
394419
MP_STATE_VM(flash_ram_cache) = NULL;
395420
}
396421
return true;

ports/atmel-samd/mpconfigport.h

+4
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,8 @@ void run_background_tasks(void);
338338
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
339339
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
340340

341+
// TODO(tannewt): Make this 6k+ for any non-express M4 boards because they cache sectors on the
342+
// stack.
343+
#define CIRCUITPY_DEFAULT_STACK_SIZE 2048
344+
341345
#endif // __INCLUDED_MPCONFIGPORT_H

ports/nrf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SRC_C += \
119119
lib/utils/sys_stdio_mphal.c \
120120
lib/libc/string0.c \
121121
lib/mp-readline/readline.c \
122+
supervisor/shared/memory.c
122123

123124
DRIVERS_SRC_C += $(addprefix modules/,\
124125
ubluepy/modubluepy.c \

ports/nrf/mpconfigport.h

+1
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,6 @@ void run_background_tasks(void);
242242
#define MICROPY_VM_HOOK_RETURN run_background_tasks();
243243

244244
//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
245+
#define CIRCUITPY_DEFAULT_STACK_SIZE 2048
245246

246247
#endif

shared-bindings/supervisor/__init__.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26-
#include "py/obj.h"
27-
#include "py/runtime.h"
28-
#include "py/reload.h"
26+
#include "py/obj.h"
27+
#include "py/runtime.h"
28+
#include "py/reload.h"
2929

30-
#include "lib/utils/interrupt_char.h"
31-
#include "supervisor/shared/autoreload.h"
30+
#include "lib/utils/interrupt_char.h"
31+
#include "supervisor/shared/autoreload.h"
32+
#include "supervisor/shared/rgb_led_status.h"
33+
#include "supervisor/shared/stack.h"
3234

33-
#include "supervisor/shared/rgb_led_status.h"
34-
35-
#include "shared-bindings/supervisor/__init__.h"
36-
#include "shared-bindings/supervisor/Runtime.h"
35+
#include "shared-bindings/supervisor/__init__.h"
36+
#include "shared-bindings/supervisor/Runtime.h"
3737

3838
//| :mod:`supervisor` --- Supervisor settings
3939
//| =================================================
@@ -107,6 +107,21 @@ STATIC mp_obj_t supervisor_reload(void) {
107107
}
108108
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);
109109

110+
//| .. method:: set_next_stack_limit(size)
111+
//|
112+
//| Set the size of the stack for the next vm run. If its too large, the default will be used.
113+
//|
114+
STATIC mp_obj_t supervisor_set_next_stack_limit(mp_obj_t size_obj) {
115+
mp_int_t size = mp_obj_get_int(size_obj);
116+
117+
if (size < 256) {
118+
mp_raise_ValueError("Stack size must be at least 256");
119+
}
120+
set_next_stack_size(size);
121+
122+
return mp_const_none;
123+
}
124+
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_next_stack_limit);
110125

111126
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
112127
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
@@ -115,6 +130,8 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
115130
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
116131
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
117132
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
133+
{ MP_ROM_QSTR(MP_QSTR_set_next_stack_limit), MP_ROM_PTR(&supervisor_set_next_stack_limit_obj) },
134+
118135
};
119136

120137
STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table);

0 commit comments

Comments
 (0)