mm/heap: Simplify the condition check in mm_realloc

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-10-26 03:27:23 +08:00 committed by Gustavo Henrique Nihei
parent a16a9f80e2
commit 5cb6b042aa

View File

@ -154,15 +154,15 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
if (nextsize + prevsize + oldsize >= newsize) if (nextsize + prevsize + oldsize >= newsize)
{ {
size_t needed = newsize - oldsize; size_t needed = newsize - oldsize;
size_t takeprev = 0; size_t takeprev;
size_t takenext = 0; size_t takenext;
/* Check if we can extend into the previous chunk and if the /* Check if we can extend into the previous chunk and if the
* previous chunk is smaller than the next chunk. * previous chunk is smaller than the next chunk.
*/ */
if (prevsize > 0 && (nextsize >= prevsize || nextsize < 1)) if (nextsize > prevsize)
{ {
/* Can we get everything we need from the previous chunk? */ /* Can we get everything we need from the previous chunk? */
@ -182,15 +182,13 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
takeprev = needed; takeprev = needed;
takenext = 0; takenext = 0;
} }
needed = 0;
} }
/* Check if we can extend into the next chunk and if we still need /* Check if we can extend into the next chunk and if we still need
* more memory. * more memory.
*/ */
if (nextsize > 0 && needed) else
{ {
/* Can we get everything we need from the next chunk? */ /* Can we get everything we need from the next chunk? */
@ -276,7 +274,6 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
/* Now we want to return newnode */ /* Now we want to return newnode */
oldnode = newnode; oldnode = newnode;
oldsize = newnode->size;
} }
/* Extend into the next free chunk */ /* Extend into the next free chunk */
@ -306,9 +303,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
/* Extend the node into the next chunk */ /* Extend the node into the next chunk */
oldnode->size = oldsize + takenext; oldnode->size += takenext;
newnode = (FAR struct mm_freenode_s *)
((FAR char *)oldnode + oldnode->size);
/* Did we consume the entire preceding chunk? */ /* Did we consume the entire preceding chunk? */
@ -318,6 +313,8 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
* the free nodelist. * the free nodelist.
*/ */
newnode = (FAR struct mm_freenode_s *)
((FAR char *)oldnode + oldnode->size);
newnode->size = nextsize - takenext; newnode->size = nextsize - takenext;
DEBUGASSERT(newnode->size >= SIZEOF_MM_FREENODE); DEBUGASSERT(newnode->size >= SIZEOF_MM_FREENODE);
newnode->preceding = oldnode->size; newnode->preceding = oldnode->size;
@ -352,7 +349,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
*/ */
mm_givesemaphore(heap); mm_givesemaphore(heap);
newmem = (FAR void *)mm_malloc(heap, size); newmem = mm_malloc(heap, size);
if (newmem) if (newmem)
{ {
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE); memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);