2012-07-14 20:31:12 -03:00
|
|
|
/****************************************************************************
|
2011-12-19 15:24:09 -04:00
|
|
|
* mm/mm_realloc.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
|
2012-07-14 20:31:12 -03:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2012-07-14 20:31:12 -03:00
|
|
|
****************************************************************************/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/****************************************************************************
|
2011-12-19 15:24:09 -04:00
|
|
|
* Included Files
|
2012-07-14 20:31:12 -03:00
|
|
|
****************************************************************************/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "mm_environment.h"
|
2012-07-14 20:31:12 -03:00
|
|
|
#include <stdio.h>
|
2011-12-19 15:24:09 -04:00
|
|
|
#include "mm_internal.h"
|
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/****************************************************************************
|
2011-12-19 15:24:09 -04:00
|
|
|
* Pre-processor Definitions
|
2012-07-14 20:31:12 -03:00
|
|
|
****************************************************************************/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/****************************************************************************
|
2011-12-19 15:24:09 -04:00
|
|
|
* Global Functions
|
2012-07-14 20:31:12 -03:00
|
|
|
****************************************************************************/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: realloc
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the reallocation is for less space, then:
|
2012-07-14 20:31:12 -03:00
|
|
|
*
|
2011-12-19 15:24:09 -04:00
|
|
|
* (1) the current allocation is reduced in size
|
2012-07-14 20:31:12 -03:00
|
|
|
* (2) the remainder at the end of the allocation is returned to the
|
|
|
|
* free list.
|
|
|
|
*
|
|
|
|
* If the request is for more space and the current allocation can be
|
|
|
|
* extended, it will be extended by:
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
2012-07-14 20:31:12 -03:00
|
|
|
* (1) Taking the additional space from the following free chunk, or
|
|
|
|
* (2) Taking the additional space from the preceding free chunk.
|
2011-12-19 15:24:09 -04:00
|
|
|
* (3) Or both
|
|
|
|
*
|
2012-07-14 20:31:12 -03:00
|
|
|
* If the request is for more space but the current chunk cannot be
|
|
|
|
* extended, then malloc a new buffer, copy the data into the new buffer,
|
|
|
|
* and free the old buffer.
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
2012-07-14 20:31:12 -03:00
|
|
|
****************************************************************************/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
FAR void *realloc(FAR void *oldmem, size_t size)
|
|
|
|
{
|
|
|
|
FAR struct mm_allocnode_s *oldnode;
|
|
|
|
FAR struct mm_freenode_s *prev;
|
|
|
|
FAR struct mm_freenode_s *next;
|
|
|
|
size_t oldsize;
|
|
|
|
size_t prevsize = 0;
|
|
|
|
size_t nextsize = 0;
|
|
|
|
FAR void *newmem;
|
|
|
|
|
|
|
|
/* If oldmem is NULL, then realloc is equivalent to malloc */
|
|
|
|
|
|
|
|
if (!oldmem)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If size is zero, then realloc is equivalent to free */
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
free(oldmem);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Adjust the size to account for (1) the size of the allocated node and
|
|
|
|
* (2) to make sure that it is an even multiple of our granule size.
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
|
|
|
|
|
|
|
|
/* Map the memory chunk into an allocated node structure */
|
|
|
|
|
|
|
|
oldnode = (FAR struct mm_allocnode_s *)((FAR char*)oldmem - SIZEOF_MM_ALLOCNODE);
|
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* We need to hold the MM semaphore while we muck with the nodelist. */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
mm_takesemaphore();
|
|
|
|
|
|
|
|
/* Check if this is a request to reduce the size of the allocation. */
|
|
|
|
|
|
|
|
oldsize = oldnode->size;
|
|
|
|
if (size <= oldsize)
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Handle the special case where we are not going to change the size
|
|
|
|
* of the allocation.
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
2012-07-14 20:31:12 -03:00
|
|
|
|
2011-12-19 15:24:09 -04:00
|
|
|
if (size < oldsize)
|
|
|
|
{
|
|
|
|
mm_shrinkchunk(oldnode, size);
|
2012-07-14 20:31:12 -03:00
|
|
|
}
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Then return the original address */
|
|
|
|
|
|
|
|
mm_givesemaphore();
|
|
|
|
return oldmem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to increase the size of the allocation, Get the
|
2012-07-14 20:31:12 -03:00
|
|
|
* available sizes before and after the oldnode so that we can make the
|
|
|
|
* best decision
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
next = (FAR struct mm_freenode_s *)((FAR char*)oldnode + oldnode->size);
|
|
|
|
if ((next->preceding & MM_ALLOC_BIT) == 0)
|
|
|
|
{
|
|
|
|
nextsize = next->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = (FAR struct mm_freenode_s *)((FAR char*)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT));
|
|
|
|
if ((prev->preceding & MM_ALLOC_BIT) == 0)
|
|
|
|
{
|
|
|
|
prevsize = prev->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, check if we can extend the current allocation or not */
|
|
|
|
|
|
|
|
if (nextsize + prevsize + oldsize >= size)
|
|
|
|
{
|
|
|
|
size_t needed = size - oldsize;
|
|
|
|
size_t takeprev = 0;
|
|
|
|
size_t takenext = 0;
|
|
|
|
|
|
|
|
/* Check if we can extend into the previous chunk and if the
|
|
|
|
* previous chunk is smaller than the next chunk.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (prevsize > 0 && (nextsize >= prevsize || nextsize <= 0))
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Can we get everything we need from the previous chunk? */
|
|
|
|
|
|
|
|
if (needed > prevsize)
|
|
|
|
{
|
|
|
|
/* No, take the whole previous chunk and get the
|
|
|
|
* rest that we need from the next chunk.
|
|
|
|
*/
|
|
|
|
|
|
|
|
takeprev = prevsize;
|
|
|
|
takenext = needed - prevsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, take what we need from the previous chunk */
|
|
|
|
|
|
|
|
takeprev = needed;
|
|
|
|
takenext = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
needed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can extend into the next chunk and if we still need
|
|
|
|
* more memory.
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (nextsize > 0 && needed)
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Can we get everything we need from the next chunk? */
|
|
|
|
|
|
|
|
if (needed > nextsize)
|
|
|
|
{
|
|
|
|
/* No, take the whole next chunk and get the rest that we
|
|
|
|
* need from the previous chunk.
|
|
|
|
*/
|
|
|
|
|
|
|
|
takeprev = needed - nextsize;
|
|
|
|
takenext = nextsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, take what we need from the previous chunk */
|
|
|
|
|
|
|
|
takeprev = 0;
|
|
|
|
takenext = needed;
|
|
|
|
}
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the previous free chunk */
|
|
|
|
|
|
|
|
newmem = oldmem;
|
|
|
|
if (takeprev)
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
FAR struct mm_allocnode_s *newnode;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Remove the previous node. There must be a predecessor, but
|
|
|
|
* there may not be a successor node.
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
DEBUGASSERT(prev->blink);
|
|
|
|
prev->blink->flink = prev->flink;
|
|
|
|
if (prev->flink)
|
|
|
|
{
|
|
|
|
prev->flink->blink = prev->blink;
|
|
|
|
}
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Extend the node into the previous free chunk */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)((FAR char*)oldnode - takeprev);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Did we consume the entire preceding chunk? */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
if (takeprev < prevsize)
|
|
|
|
{
|
|
|
|
/* No.. just take what we need from the previous chunk and put
|
|
|
|
* it back into the free list
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
prev->size -= takeprev;
|
|
|
|
newnode->size = oldsize + takeprev;
|
|
|
|
newnode->preceding = prev->size | MM_ALLOC_BIT;
|
|
|
|
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Return the previous free node to the nodelist (with the new size) */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
mm_addfreechunk(prev);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Now we want to return newnode */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
oldnode = newnode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes.. update its size (newnode->preceding is already set) */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
newnode->size += oldsize;
|
|
|
|
newnode->preceding |= MM_ALLOC_BIT;
|
|
|
|
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
|
|
|
|
}
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
oldnode = newnode;
|
|
|
|
oldsize = newnode->size;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Now we have to move the user contents 'down' in memory. memcpy should
|
|
|
|
* should be save for this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newmem = (FAR void*)((FAR char*)newnode + SIZEOF_MM_ALLOCNODE);
|
|
|
|
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the next free chunk */
|
|
|
|
|
|
|
|
if (takenext)
|
|
|
|
{
|
|
|
|
FAR struct mm_freenode_s *newnode;
|
|
|
|
FAR struct mm_allocnode_s *andbeyond;
|
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Get the chunk following the next node (which could be the tail
|
|
|
|
* chunk)
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
andbeyond = (FAR struct mm_allocnode_s*)((char*)next + nextsize);
|
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Remove the next node. There must be a predecessor, but there
|
|
|
|
* may not be a successor node.
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(next->blink);
|
|
|
|
next->blink->flink = next->flink;
|
|
|
|
if (next->flink)
|
|
|
|
{
|
|
|
|
next->flink->blink = next->blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend the node into the next chunk */
|
|
|
|
|
|
|
|
oldnode->size = oldsize + takenext;
|
|
|
|
newnode = (FAR struct mm_freenode_s *)((char*)oldnode + oldnode->size);
|
|
|
|
|
|
|
|
/* Did we consume the entire preceding chunk? */
|
|
|
|
|
|
|
|
if (takenext < nextsize)
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
/* No, take what we need from the next chunk and return it to
|
|
|
|
* the free nodelist.
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
newnode->size = nextsize - takenext;
|
|
|
|
newnode->preceding = oldnode->size;
|
|
|
|
andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
|
|
|
|
|
|
|
|
/* Add the new free node to the nodelist (with the new size) */
|
|
|
|
|
|
|
|
mm_addfreechunk(newnode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, just update some pointers. */
|
|
|
|
|
|
|
|
andbeyond->preceding = oldnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mm_givesemaphore();
|
|
|
|
return newmem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The current chunk cannot be extended. Just allocate a new chunk and copy */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-07-14 20:31:12 -03:00
|
|
|
/* Allocate a new block. On failure, realloc must return NULL but
|
|
|
|
* leave the original memory in place.
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
mm_givesemaphore();
|
|
|
|
newmem = (FAR void*)malloc(size);
|
|
|
|
if (newmem)
|
|
|
|
{
|
|
|
|
memcpy(newmem, oldmem, oldsize);
|
|
|
|
free(oldmem);
|
|
|
|
}
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-07-14 20:31:12 -03:00
|
|
|
return newmem;
|
|
|
|
}
|
|
|
|
}
|