Skip to content

Commit

Permalink
Merge pull request Samsung#2474 from jeongchanKim/fix_heap
Browse files Browse the repository at this point in the history
mm/realloc: Fix realloc when alloc to another heap and Fix mm_get_hea…
  • Loading branch information
sunghan-chang authored Nov 22, 2018
2 parents 94815ee + 288dbee commit f390805
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions os/mm/mm_heap/mm_get_heapindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ extern struct mm_heap_s g_mmheap[CONFIG_MM_NHEAPS];
int mm_get_heapindex(void *mem)
{
int heap_idx;
if (mem == NULL) {
return 0;
}
for (heap_idx = 0; heap_idx < CONFIG_MM_NHEAPS; heap_idx++) {
if (mem < (void *)(g_mmheap[heap_idx].mm_heapstart + g_mmheap[heap_idx].mm_heapsize) && mem >= (void *)g_mmheap[heap_idx].mm_heapstart) {
return heap_idx;
Expand Down
6 changes: 6 additions & 0 deletions os/mm/umm_heap/umm_realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,15 @@ void *realloc_at(int heap_index, void *oldmem, size_t size)
FAR void *realloc(FAR void *oldmem, size_t size)
{
int heap_idx;
int prev_heap_idx;
void *ret;
#ifdef CONFIG_DEBUG_MM_HEAPINFO
ARCH_GET_RET_ADDRESS
#endif
heap_idx = mm_get_heapindex(oldmem);
if (heap_idx < 0) {
return NULL;
}
#ifdef CONFIG_DEBUG_MM_HEAPINFO
ret = mm_realloc(&g_mmheap[heap_idx], oldmem, size, retaddr);
#else
Expand All @@ -131,13 +135,15 @@ FAR void *realloc(FAR void *oldmem, size_t size)
}
/* Try to mm_malloc to another heap */
mdbg("After realloc, memory can be allocated to another heap which is not as same as previous.\n");
prev_heap_idx = heap_idx;
for (heap_idx = 0; heap_idx < CONFIG_MM_NHEAPS; heap_idx++) {
#ifdef CONFIG_DEBUG_MM_HEAPINFO
ret = mm_malloc(&g_mmheap[heap_idx], size, retaddr);
#else
ret = mm_malloc(&g_mmheap[heap_idx], size);
#endif
if (ret != NULL) {
mm_free(&g_mmheap[prev_heap_idx], oldmem);
return ret;
}
}
Expand Down

0 comments on commit f390805

Please sign in to comment.