Skip to content

Introduce a long lived section of the heap. #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 25, 2018
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
18 changes: 9 additions & 9 deletions ports/atmel-samd/spi_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "py/gc.h"
#include "py/misc.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "lib/oofatfs/ff.h"
Expand Down Expand Up @@ -256,12 +256,12 @@ void spi_flash_init(void) {
if (spi_flash_is_initialised) {
return;
}

samd_peripherals_sercom_clock_init(SPI_FLASH_SERCOM, SPI_FLASH_SERCOM_INDEX);

// Set up with defaults, then change.
spi_m_sync_init(&spi_flash_desc, SPI_FLASH_SERCOM);

hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);

Expand Down Expand Up @@ -398,7 +398,7 @@ static bool flush_scratch_flash(void) {
static bool allocate_ram_cache(void) {
uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE;
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
MP_STATE_VM(flash_ram_cache) = gc_alloc(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
if (MP_STATE_VM(flash_ram_cache) == NULL) {
return false;
}
Expand All @@ -409,7 +409,7 @@ static bool allocate_ram_cache(void) {
bool success = true;
for (i = 0; i < blocks_per_sector; i++) {
for (j = 0; j < pages_per_block; j++) {
uint8_t *page_cache = gc_alloc(SPI_FLASH_PAGE_SIZE, false);
uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false);
if (page_cache == NULL) {
success = false;
break;
Expand All @@ -427,11 +427,11 @@ static bool allocate_ram_cache(void) {
i++;
for (; i > 0; i--) {
for (; j > 0; j--) {
gc_free(MP_STATE_VM(flash_ram_cache)[(i - 1) * pages_per_block + (j - 1)]);
m_free(MP_STATE_VM(flash_ram_cache)[(i - 1) * pages_per_block + (j - 1)]);
}
j = pages_per_block;
}
gc_free(MP_STATE_VM(flash_ram_cache));
m_free(MP_STATE_VM(flash_ram_cache));
MP_STATE_VM(flash_ram_cache) = NULL;
}
return success;
Expand Down Expand Up @@ -474,13 +474,13 @@ static bool flush_ram_cache(bool keep_cache) {
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
SPI_FLASH_PAGE_SIZE);
if (!keep_cache) {
gc_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
}
}
}
// We're done with the cache for now so give it back.
if (!keep_cache) {
gc_free(MP_STATE_VM(flash_ram_cache));
m_free(MP_STATE_VM(flash_ram_cache));
MP_STATE_VM(flash_ram_cache) = NULL;
}
return true;
Expand Down
8 changes: 8 additions & 0 deletions py/builtinimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <assert.h>

#include "py/compile.h"
#include "py/gc_long_lived.h"
#include "py/objmodule.h"
#include "py/persistentcode.h"
#include "py/runtime.h"
Expand Down Expand Up @@ -144,6 +145,7 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
// parse, compile and execute the module in its context
mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
mp_obj_module_set_globals(module_obj, make_dict_long_lived(mod_globals, 10));
}
#endif

Expand Down Expand Up @@ -173,6 +175,8 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {

// finish nlr block, restore context
nlr_pop();
mp_obj_module_set_globals(module_obj,
make_dict_long_lived(mp_obj_module_get_globals(module_obj), 10));
mp_globals_set(old_globals);
mp_locals_set(old_locals);
} else {
Expand Down Expand Up @@ -468,6 +472,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
if (outer_module_obj != MP_OBJ_NULL) {
qstr s = qstr_from_strn(mod_str + last, i - last);
mp_store_attr(outer_module_obj, s, module_obj);
// The above store can cause a dictionary rehash and new allocation. So,
// lets make sure the globals dictionary is still long lived.
mp_obj_module_set_globals(outer_module_obj,
make_dict_long_lived(mp_obj_module_get_globals(outer_module_obj), 10));
}
outer_module_obj = module_obj;
if (top_module_obj == MP_OBJ_NULL) {
Expand Down
Loading