@@ -188,6 +188,8 @@ static void gc_setup_area(mp_state_mem_area_t *area, void *start, void *end) {
188188
189189 // Total number of blocks in the pool
190190 size_t gc_pool_block_len = area -> gc_alloc_table_byte_len * BLOCKS_PER_ATB ;
191+ console_uart_printf ("gc_pool_block_len = %d\r\n" , gc_pool_block_len );
192+ console_uart_printf ("total free %d\r\n" , gc_pool_block_len * BYTES_PER_BLOCK );
191193
192194 // Calculate table sizes and set start pointers
193195 #if MICROPY_ENABLE_FINALISER
@@ -294,16 +296,36 @@ void gc_add(void *start, void *end) {
294296}
295297
296298#if MICROPY_GC_SPLIT_HEAP_AUTO
299+ static size_t compute_heap_size (size_t total_blocks ) {
300+ console_uart_printf ("total_blocks = %d\r\n" , total_blocks );
301+ // Compute bytes needed to build a heap with total_blocks blocks.
302+ size_t total_heap =
303+ (total_blocks + BLOCKS_PER_ATB - 1 ) / BLOCKS_PER_ATB
304+ #if MICROPY_ENABLE_FINALISER
305+ + (total_blocks + BLOCKS_PER_FTB - 1 ) / BLOCKS_PER_FTB
306+ #endif
307+ #if MICROPY_ENABLE_SELECTIVE_COLLECT
308+ + (total_blocks + BLOCKS_PER_CTB - 1 ) / BLOCKS_PER_CTB
309+ #endif
310+ + total_blocks * BYTES_PER_BLOCK
311+ + ALLOC_TABLE_GAP_BYTE
312+ + sizeof (mp_state_mem_area_t );
313+
314+ // Round up size to the nearest multiple of BYTES_PER_BLOCK.
315+ total_heap = (total_heap + BYTES_PER_BLOCK - 1 ) / BYTES_PER_BLOCK ;
316+ return total_heap ;
317+ }
318+
297319// Try to automatically add a heap area large enough to fulfill 'failed_alloc'.
298320static bool gc_try_add_heap (size_t failed_alloc ) {
299321 // 'needed' is the size of a heap large enough to hold failed_alloc, with
300322 // the additional metadata overheads as calculated in gc_setup_area().
301323 //
302324 // Rather than reproduce all of that logic here, we approximate that adding
303325 // (13/512) is enough overhead for sufficiently large heap areas (the
304- // overhead converges to 3/128 , but there's some fixed overhead and some
326+ // overhead converges to 12/512 , but there's some fixed overhead and some
305327 // rounding up of partial block sizes).
306- size_t needed = failed_alloc + MAX ( 2048 , failed_alloc * 13 / 512 );
328+ size_t needed = compute_heap_size (( failed_alloc + BYTES_PER_BLOCK - 1 ) / BYTES_PER_BLOCK );
307329
308330 size_t avail = gc_get_max_new_split ();
309331
@@ -312,6 +334,7 @@ static bool gc_try_add_heap(size_t failed_alloc) {
312334 failed_alloc ,
313335 needed ,
314336 avail );
337+ console_uart_printf ("gc_try_add_heap failed_alloc %d, needed %d, avail %d\r\n" , failed_alloc , needed , avail );
315338
316339 if (avail < needed ) {
317340 // Can't fit this allocation, or system heap has nearly run out anyway
@@ -347,27 +370,18 @@ static bool gc_try_add_heap(size_t failed_alloc) {
347370 total_blocks += area -> gc_alloc_table_byte_len * BLOCKS_PER_ATB ;
348371 }
349372
350- // Compute bytes needed to build a heap with total_blocks blocks.
351- size_t total_heap =
352- total_blocks / BLOCKS_PER_ATB
353- #if MICROPY_ENABLE_FINALISER
354- + total_blocks / BLOCKS_PER_FTB
355- #endif
356- + total_blocks * BYTES_PER_BLOCK
357- + ALLOC_TABLE_GAP_BYTE
358- + sizeof (mp_state_mem_area_t );
359-
360- // Round up size to the nearest multiple of BYTES_PER_BLOCK.
361- total_heap = (total_heap + BYTES_PER_BLOCK - 1 ) & (~(BYTES_PER_BLOCK - 1 ));
373+ size_t total_heap = compute_heap_size (total_blocks );
362374
363375 DEBUG_printf ("total_heap " UINT_FMT " bytes\n" , total_heap );
364376
365377 size_t to_alloc = MIN (avail , MAX (total_heap , needed ));
366378
379+ console_uart_printf ("avail = %d\r\n" , avail );
380+ console_uart_printf ("to_alloc = %d\r\n" , to_alloc );
381+
367382 mp_state_mem_area_t * new_heap = MP_PLAT_ALLOC_HEAP (to_alloc );
368383
369- DEBUG_printf ("MP_PLAT_ALLOC_HEAP " UINT_FMT " = %p\n" ,
370- to_alloc , new_heap );
384+ console_uart_printf ("MP_PLAT_ALLOC_HEAP %d = %p\r\n" , to_alloc , new_heap );
371385
372386 if (new_heap == NULL ) {
373387 // This should only fail:
0 commit comments