Skip to content

Commit 016242a

Browse files
committed
Get rid of a memset() for the case where finalisers are enabled
This saves 24 bytes of flash on trinket m0
1 parent 9fb4582 commit 016242a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

py/gc.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ void gc_init(void *start, void *end) {
138138
MP_STATE_MEM(gc_alloc_table_start) = (byte *)start;
139139

140140
#if MICROPY_ENABLE_FINALISER
141-
size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
142141
MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len) + 1;
143142
#endif
144143

@@ -150,15 +149,11 @@ void gc_init(void *start, void *end) {
150149
assert(MP_STATE_MEM(gc_pool_start) >= MP_STATE_MEM(gc_finaliser_table_start) + gc_finaliser_table_byte_len);
151150
#endif
152151

153-
// Clear ATBs plus one more byte. The extra byte might be read when we read the final ATB and
154-
// then try to count its tail. Clearing the byte ensures it is 0 and ends the chain. Without an
155-
// FTB, it'll just clear the pool byte early.
156-
memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_alloc_table_byte_len) + 1);
157-
158-
#if MICROPY_ENABLE_FINALISER
159-
// clear FTBs
160-
memset(MP_STATE_MEM(gc_finaliser_table_start), 0, gc_finaliser_table_byte_len);
161-
#endif
152+
// Clear ATBs & finalisers (if enabled). This also clears the extra byte
153+
// which appears between ATBs and finalisers that ensures every chain in
154+
// the ATB terminates, rather than erroneously using bits from the
155+
// finalisers.
156+
memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_pool_start) - MP_STATE_MEM(gc_alloc_table_start));
162157

163158
// Set first free ATB index to the start of the heap.
164159
for (size_t i = 0; i < MICROPY_ATB_INDICES; i++) {

0 commit comments

Comments
 (0)