Skip to content

Commit 13c1091

Browse files
committed
Nothing missing. Grow too little though
1 parent fe5696b commit 13c1091

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

ports/raspberrypi/supervisor/port.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,23 @@ void *port_malloc(size_t size, bool dma_capable) {
259259
if (!dma_capable && _psram_size > 0) {
260260
void *block = tlsf_malloc(_psram_heap, size);
261261
if (block) {
262+
console_uart_printf("port_malloc(%d) psram = %p\n", size, block);
262263
return block;
264+
} else {
265+
console_uart_printf("port_malloc(%d) psram failed\n", size);
263266
}
264267
}
265268
void *block = tlsf_malloc(_heap, size);
269+
console_uart_printf("port_malloc(%d) = %p\n", size, block);
266270
return block;
267271
}
268272

269273
void port_free(void *ptr) {
270274
if (((size_t)ptr) < SRAM_BASE) {
275+
console_uart_printf("port_free(%p) psram\n", ptr);
271276
tlsf_free(_psram_heap, ptr);
272277
} else {
278+
console_uart_printf("port_free(%p)\n", ptr);
273279
tlsf_free(_heap, ptr);
274280
}
275281
}
@@ -278,10 +284,13 @@ void *port_realloc(void *ptr, size_t size, bool dma_capable) {
278284
if (_psram_size > 0 && ((ptr != NULL && ((size_t)ptr) < SRAM_BASE) || (ptr == NULL && !dma_capable))) {
279285
void *block = tlsf_realloc(_psram_heap, ptr, size);
280286
if (block) {
287+
console_uart_printf("port_realloc(%d) psram = %p\n", size, block);
281288
return block;
282289
}
283290
}
284-
return tlsf_realloc(_heap, ptr, size);
291+
void *block = tlsf_realloc(_heap, ptr, size);
292+
console_uart_printf("port_realloc(%d) = %p\n", size, block);
293+
return block;
285294
}
286295

287296
static bool max_size_walker(void *ptr, size_t size, int used, void *user) {

py/gc.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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'.
298320
static 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:

supervisor/shared/usb/usb_msc_flash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ bool tud_msc_is_writable_cb(uint8_t lun) {
249249
// Lock the blockdev once we say we're writable.
250250
if (!locked[lun] && !blockdev_lock(vfs)) {
251251
console_uart_printf("lun %u could not be locked\r\n", lun);
252+
console_uart_printf("locked[%u] = %d\r\n", lun, locked[lun]);
253+
console_uart_printf("vfs %p\r\n", vfs);
252254
return false;
253255
}
254256
locked[lun] = true;

0 commit comments

Comments
 (0)