8000 py: Clear finalizer flag when calling gc_free. · micropython/micropython@7f3c0d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f3c0d1

Browse files
dhylandsdpgeorge
authored andcommitted
py: Clear finalizer flag when calling gc_free.
Currently, the only place that clears the bit is in gc_collect. So if a block with a finalizer is allocated, and subsequently freed, and then the block is reallocated with no finalizer then the bit remains set. This could also be fixed by having gc_alloc clear the bit, but I'm pretty sure that free is called way less than alloc, so doing it in free is more efficient.
1 parent 41b688e commit 7f3c0d1

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

py/gc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ void *gc_alloc_with_finaliser(mp_uint_t n_bytes) {
442442
*/
443443

444444
// force the freeing of a piece of memory
445+
// TODO: freeing here does not call finaliser
445446
void gc_free(void *ptr_in) {
446447
if (MP_STATE_MEM(gc_lock_depth) > 0) {
447448
// TODO how to deal with this error?
@@ -454,6 +455,9 @@ void gc_free(void *ptr_in) {
454455
if (VERIFY_PTR(ptr)) {
455456
mp_uint_t block = BLOCK_FROM_PTR(ptr);
456457
if (ATB_GET_KIND(block) == AT_HEAD) {
458+
#if MICROPY_ENABLE_FINALISER
459+
FTB_CLEAR(block);
460+
#endif
457461
// set the last_free pointer to this block if it's earlier in the heap
458462
if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
459463
MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;

py/gc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void gc_collect_root(void **ptrs, mp_uint_t len);
4646
void gc_collect_end(void);
4747

4848
void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser);
49-
void gc_free(void *ptr);
49+
void gc_free(void *ptr); // does not call finaliser
5050
mp_uint_t gc_nbytes(const void *ptr);
5151
void *gc_realloc(void *ptr, mp_uint_t n_bytes, bool allow_move);
5252

0 commit comments

Comments
 (0)
0