Fix error in realloc when memory is extended downward
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@295 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
parent
2f8be29403
commit
513b157ce0
|
@ -190,5 +190,7 @@
|
|||
on thread/task exit.
|
||||
* Add environment variables APIs: environ, getenv, putenv, clearenv, setenv,
|
||||
unsetenv
|
||||
* Correct an error in realloc() when the block is extended "down" in memory.
|
||||
In this case, the old memory contents need to be copied to the new location.
|
||||
* Started m68322
|
||||
|
||||
|
|
|
@ -627,6 +627,8 @@ Other memory:
|
|||
unsetenv
|
||||
* Add environment variables APIs: environ, getenv, putenv, clearenv, setenv,
|
||||
unsetenv
|
||||
* Correct an error in realloc() when the block is extended "down" in memory.
|
||||
In this case, the old memory contents need to be copied to the new location.
|
||||
* Started m68322
|
||||
</pre></ul>
|
||||
|
||||
|
|
|
@ -81,6 +81,7 @@ FAR void *realloc(FAR void *oldmem, size_t size)
|
|||
size_t oldsize;
|
||||
size_t prevsize = 0;
|
||||
size_t nextsize = 0;
|
||||
FAR void *newmem;
|
||||
|
||||
/* If oldmem is NULL, then realloc is equivalent to malloc */
|
||||
|
||||
|
@ -279,7 +280,7 @@ FAR void *realloc(FAR void *oldmem, size_t size)
|
|||
next->flink->blink = next->blink;
|
||||
}
|
||||
|
||||
/* Extend the node into the previous next chunk */
|
||||
/* Extend the node into the next chunk */
|
||||
|
||||
oldnode->size = oldsize + takenext;
|
||||
newnode = (FAR struct mm_freenode_s *)((char*)oldnode + oldnode->size);
|
||||
|
@ -308,17 +309,20 @@ FAR void *realloc(FAR void *oldmem, size_t size)
|
|||
}
|
||||
}
|
||||
|
||||
/* Now we have to move the user contents 'down' in memory. memcpy should
|
||||
* should be save for this.
|
||||
*/
|
||||
|
||||
newmem = (FAR void*)((FAR char*)oldnode + SIZEOF_MM_ALLOCNODE);
|
||||
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);
|
||||
mm_givesemaphore();
|
||||
return (FAR void*)((FAR char*)oldnode + SIZEOF_MM_ALLOCNODE);
|
||||
return newmem;
|
||||
}
|
||||
|
||||
/* The current chunk cannot be extended. Just allocate a new chunk and copy */
|
||||
|
||||
else
|
||||
{
|
||||
FAR void *newmem;
|
||||
|
||||
/* Allocate a new block. On failure, realloc must return NULL but
|
||||
* leave the original memory in place.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue