diff --git a/components/heap/heap_caps_init.c b/components/heap/heap_caps_init.c index 3e842367d2..264e1bf0f1 100644 --- a/components/heap/heap_caps_init.c +++ b/components/heap/heap_caps_init.c @@ -69,7 +69,7 @@ void heap_caps_init(void) num_regions = soc_get_available_memory_regions(regions); // the following for loop will calculate the number of possible heaps - // based on how many regions were coalesed. + // based on how many regions were coalesced. size_t num_heaps = num_regions; //The heap allocator will treat every region given to it as separate. In order to get bigger ranges of contiguous memory, @@ -83,7 +83,7 @@ void heap_caps_init(void) b->size += a->size; // remove one heap from the number of heaps as - // 2 regions just got coalesed. + // 2 regions just got coalesced. num_heaps--; } } @@ -132,8 +132,14 @@ void heap_caps_init(void) heap_t *heaps_array = NULL; for (size_t i = 0; i < num_heaps; i++) { if (heap_caps_match(&temp_heaps[i], MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL)) { - /* use the first DRAM heap which can fit the data */ - heaps_array = multi_heap_malloc(temp_heaps[i].heap, sizeof(heap_t) * num_heaps); + /* use the first DRAM heap which can fit the data. + * the allocated block won't include the block owner bytes since this operation + * is done by the top level API heap_caps_malloc(). So we need to add it manually + * after successful allocation. Allocate extra 4 bytes for that purpose. */ + heaps_array = multi_heap_malloc(temp_heaps[i].heap, MULTI_HEAP_ADD_BLOCK_OWNER_SIZE(sizeof(heap_t) * num_heaps)); + assert(heaps_array != NULL); + MULTI_HEAP_SET_BLOCK_OWNER(heaps_array); + heaps_array = (heap_t *)MULTI_HEAP_ADD_BLOCK_OWNER_OFFSET(heaps_array); if (heaps_array != NULL) { break; } @@ -186,7 +192,7 @@ bool heap_caps_check_add_region_allowed(intptr_t heap_start, intptr_t heap_end, * cannot be added twice. In fact, registering the same memory region as a heap twice * would cause a corruption and then an exception at runtime. * - * the existing heap region s(tart) e(nd) + * the existing heap region start end * |----------------------| * * 1.add region (e1