Skip to content

Commit 5de29ac

Browse files
authored
Merge pull request #547 from tannewt/alloc_long_lived
Introduce a long lived section of the heap.
2 parents a5bfbbc + aa0ce98 commit 5de29ac

20 files changed

+1157
-479
lines changed

ports/atmel-samd/spi_flash.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include "extmod/vfs.h"
3232
#include "extmod/vfs_fat.h"
33-
#include "py/gc.h"
33+
#include "py/misc.h"
3434
#include "py/obj.h"
3535
#include "py/runtime.h"
3636
#include "lib/oofatfs/ff.h"
@@ -256,12 +256,12 @@ void spi_flash_init(void) {
256256
if (spi_flash_is_initialised) {
257257
return;
258258
}
259-
259+
260260
samd_peripherals_sercom_clock_init(SPI_FLASH_SERCOM, SPI_FLASH_SERCOM_INDEX);
261261

262262
// Set up with defaults, then change.
263263
spi_m_sync_init(&spi_flash_desc, SPI_FLASH_SERCOM);
264-
264+
265265
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
266266
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
267267

@@ -398,7 +398,7 @@ static bool flush_scratch_flash(void) {
398398
static bool allocate_ram_cache(void) {
399399
uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE;
400400
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
401-
MP_STATE_VM(flash_ram_cache) = gc_alloc(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
401+
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
402402
if (MP_STATE_VM(flash_ram_cache) == NULL) {
403403
return false;
404404
}
@@ -409,7 +409,7 @@ static bool allocate_ram_cache(void) {
409409
bool success = true;
410410
for (i = 0; i < blocks_per_sector; i++) {
411411
for (j = 0; j < pages_per_block; j++) {
412-
uint8_t *page_cache = gc_alloc(SPI_FLASH_PAGE_SIZE, false);
412+
uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false);
413413
if (page_cache == NULL) {
414414
success = false;
415415
break;
@@ -427,11 +427,11 @@ static bool allocate_ram_cache(void) {
427427
i++;
428428
for (; i > 0; i--) {
429429
for (; j > 0; j--) {
430-
gc_free(MP_STATE_VM(flash_ram_cache)[(i - 1) * pages_per_block + (j - 1)]);
430+
m_free(MP_STATE_VM(flash_ram_cache)[(i - 1) * pages_per_block + (j - 1)]);
431431
}
432432
j = pages_per_block;
433433
}
434-
gc_free(MP_STATE_VM(flash_ram_cache));
434+
m_free(MP_STATE_VM(flash_ram_cache));
435435
MP_STATE_VM(flash_ram_cache) = NULL;
436436
}
437437
return success;
@@ -474,13 +474,13 @@ static bool flush_ram_cache(bool keep_cache) {
474474
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
475475
SPI_FLASH_PAGE_SIZE);
476476
if (!keep_cache) {
477-
gc_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
477+
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
478478
}
479479
}
480480
}
481481
// We're done with the cache for now so give it back.
482482
if (!keep_cache) {
483-
gc_free(MP_STATE_VM(flash_ram_cache));
483+
m_free(MP_STATE_VM(flash_ram_cache));
484484
MP_STATE_VM(flash_ram_cache) = NULL;
485485
}
486486
return true;

py/builtinimport.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <assert.h>
3131

3232
#include "py/compile.h"
33+
#include "py/gc_long_lived.h"
3334
#include "py/objmodule.h"
3435
#include "py/persistentcode.h"
3536
#include "py/runtime.h"
@@ -144,6 +145,7 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
144145
// parse, compile and execute the module in its context
145146
mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
146147
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
148+
mp_obj_module_set_globals(module_obj, make_dict_long_lived(mod_globals, 10));
147149
}
148150
#endif
149151

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

174176
// finish nlr block, restore context
175177
nlr_pop();
178+
mp_obj_module_set_globals(module_obj,
179+
make_dict_long_lived(mp_obj_module_get_globals(module_obj), 10));
176180
mp_globals_set(old_globals);
177181
mp_locals_set(old_locals);
178182
} else {
@@ -468,6 +472,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
468472
if (outer_module_obj != MP_OBJ_NULL) {
469473
qstr s = qstr_from_strn(mod_str + last, i - last);
470474
mp_store_attr(outer_module_obj, s, module_obj);
475+
// The above store can cause a dictionary rehash and new allocation. So,
476+
// lets make sure the globals dictionary is still long lived.
477+
mp_obj_module_set_globals(outer_module_obj,
478+
make_dict_long_lived(mp_obj_module_get_globals(outer_module_obj), 10));
471479
}
472480
outer_module_obj = module_obj;
473481
if (top_module_obj == MP_OBJ_NULL) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy