2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2014-09-23 00:53:50 +08:00
|
|
|
* mm/mm_heap/mm_realloc.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 04:36:18 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <string.h>
|
2012-07-15 07:31:12 +08:00
|
|
|
#include <stdio.h>
|
2013-03-09 04:36:18 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-09-24 21:29:09 +08:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-03-02 16:03:00 +08:00
|
|
|
#include "mm_heap/mm.h"
|
2021-10-09 15:22:28 +08:00
|
|
|
#include "kasan/kasan.h"
|
2021-03-02 16:03:00 +08:00
|
|
|
|
2014-09-01 00:54:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/****************************************************************************
|
2013-03-09 04:36:18 +08:00
|
|
|
* Name: mm_realloc
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the reallocation is for less space, then:
|
2012-07-15 07:31:12 +08:00
|
|
|
*
|
2007-02-18 07:21:28 +08:00
|
|
|
* (1) the current allocation is reduced in size
|
2012-07-15 07:31:12 +08: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:
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-07-15 07:31:12 +08:00
|
|
|
* (1) Taking the additional space from the following free chunk, or
|
|
|
|
* (2) Taking the additional space from the preceding free chunk.
|
2007-02-18 07:21:28 +08:00
|
|
|
* (3) Or both
|
|
|
|
*
|
2012-07-15 07:31:12 +08: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.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2012-07-15 07:31:12 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 04:36:18 +08:00
|
|
|
FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
|
|
|
size_t size)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2007-02-28 05:17:21 +08:00
|
|
|
FAR struct mm_allocnode_s *oldnode;
|
|
|
|
FAR struct mm_freenode_s *prev;
|
|
|
|
FAR struct mm_freenode_s *next;
|
2017-03-28 21:23:46 +08:00
|
|
|
size_t newsize;
|
2007-02-22 05:55:16 +08:00
|
|
|
size_t oldsize;
|
|
|
|
size_t prevsize = 0;
|
|
|
|
size_t nextsize = 0;
|
2007-07-01 04:38:16 +08:00
|
|
|
FAR void *newmem;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* If oldmem is NULL, then realloc is equivalent to malloc */
|
|
|
|
|
2017-03-28 21:23:46 +08:00
|
|
|
if (oldmem == NULL)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2014-09-01 00:54:55 +08:00
|
|
|
return mm_malloc(heap, size);
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If size is zero, then realloc is equivalent to free */
|
|
|
|
|
2014-11-26 03:46:14 +08:00
|
|
|
if (size < 1)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2014-09-01 00:54:55 +08:00
|
|
|
mm_free(heap, oldmem);
|
2007-02-18 07:21:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-15 07:31:12 +08: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.
|
2007-02-18 07:21:28 +08:00
|
|
|
*/
|
|
|
|
|
2017-03-28 21:23:46 +08:00
|
|
|
newsize = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
|
2021-01-31 16:08:36 +08:00
|
|
|
if (newsize < size)
|
|
|
|
{
|
|
|
|
/* There must have been an integer overflow */
|
|
|
|
|
|
|
|
DEBUGASSERT(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Map the memory chunk into an allocated node structure */
|
|
|
|
|
2020-02-13 21:58:07 +08:00
|
|
|
oldnode = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)oldmem - SIZEOF_MM_ALLOCNODE);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* We need to hold the MM semaphore while we muck with the nodelist. */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-07-07 21:21:42 +08:00
|
|
|
DEBUGVERIFY(mm_takesemaphore(heap));
|
2020-02-13 16:06:05 +08:00
|
|
|
DEBUGASSERT(oldnode->preceding & MM_ALLOC_BIT);
|
|
|
|
DEBUGASSERT(mm_heapmember(heap, oldmem));
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Check if this is a request to reduce the size of the allocation. */
|
|
|
|
|
|
|
|
oldsize = oldnode->size;
|
2017-03-28 21:23:46 +08:00
|
|
|
if (newsize <= oldsize)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Handle the special case where we are not going to change the size
|
|
|
|
* of the allocation.
|
2007-07-01 06:24:19 +08:00
|
|
|
*/
|
2012-07-15 07:31:12 +08:00
|
|
|
|
2017-03-28 21:23:46 +08:00
|
|
|
if (newsize < oldsize)
|
2007-07-01 06:24:19 +08:00
|
|
|
{
|
2017-03-28 21:23:46 +08:00
|
|
|
mm_shrinkchunk(heap, oldnode, newsize);
|
2021-10-09 15:22:28 +08:00
|
|
|
kasan_poison((FAR char *)oldnode + oldnode->size,
|
|
|
|
oldsize - oldnode->size);
|
2012-07-15 07:31:12 +08:00
|
|
|
}
|
2007-07-01 06:24:19 +08:00
|
|
|
|
2022-03-17 14:02:37 +08:00
|
|
|
MM_ADD_BACKTRACE(heap, oldnode);
|
2022-01-20 17:17:29 +08:00
|
|
|
|
2007-07-01 06:24:19 +08:00
|
|
|
/* Then return the original address */
|
|
|
|
|
2013-03-09 02:29:56 +08:00
|
|
|
mm_givesemaphore(heap);
|
2007-02-18 07:21:28 +08:00
|
|
|
return oldmem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to increase the size of the allocation, Get the
|
2012-07-15 07:31:12 +08:00
|
|
|
* available sizes before and after the oldnode so that we can make the
|
|
|
|
* best decision
|
2007-02-18 07:21:28 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-13 21:58:07 +08:00
|
|
|
next = (FAR struct mm_freenode_s *)
|
|
|
|
((FAR char *)oldnode + oldnode->size);
|
2007-02-18 07:21:28 +08:00
|
|
|
if ((next->preceding & MM_ALLOC_BIT) == 0)
|
|
|
|
{
|
|
|
|
nextsize = next->size;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:58:07 +08:00
|
|
|
prev = (FAR struct mm_freenode_s *)
|
|
|
|
((FAR char *)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT));
|
2007-02-18 07:21:28 +08:00
|
|
|
if ((prev->preceding & MM_ALLOC_BIT) == 0)
|
|
|
|
{
|
|
|
|
prevsize = prev->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, check if we can extend the current allocation or not */
|
|
|
|
|
2017-03-28 21:23:46 +08:00
|
|
|
if (nextsize + prevsize + oldsize >= newsize)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2021-10-26 03:27:23 +08:00
|
|
|
size_t needed = newsize - oldsize;
|
|
|
|
size_t takeprev;
|
|
|
|
size_t takenext;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Check if we can extend into the previous chunk and if the
|
|
|
|
* previous chunk is smaller than the next chunk.
|
|
|
|
*/
|
|
|
|
|
2021-10-26 03:27:23 +08:00
|
|
|
if (nextsize > prevsize)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2012-07-15 07:31:12 +08: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can extend into the next chunk and if we still need
|
|
|
|
* more memory.
|
2007-02-18 07:21:28 +08:00
|
|
|
*/
|
|
|
|
|
2021-10-26 03:27:23 +08:00
|
|
|
else
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2012-07-15 07:31:12 +08: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;
|
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the previous free chunk */
|
|
|
|
|
2007-07-01 06:24:19 +08:00
|
|
|
newmem = oldmem;
|
2007-02-18 07:21:28 +08:00
|
|
|
if (takeprev)
|
|
|
|
{
|
2012-07-15 07:31:12 +08:00
|
|
|
FAR struct mm_allocnode_s *newnode;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Remove the previous node. There must be a predecessor, but
|
|
|
|
* there may not be a successor node.
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
DEBUGASSERT(prev->blink);
|
|
|
|
prev->blink->flink = prev->flink;
|
|
|
|
if (prev->flink)
|
|
|
|
{
|
|
|
|
prev->flink->blink = prev->blink;
|
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Extend the node into the previous free chunk */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-02-13 21:58:07 +08:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)oldnode - takeprev);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Did we consume the entire preceding chunk? */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
if (takeprev < prevsize)
|
|
|
|
{
|
|
|
|
/* No.. just take what we need from the previous chunk and put
|
|
|
|
* it back into the free list
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
prev->size -= takeprev;
|
2020-02-13 16:06:05 +08:00
|
|
|
DEBUGASSERT(prev->size >= SIZEOF_MM_FREENODE);
|
2012-07-15 07:31:12 +08:00
|
|
|
newnode->size = oldsize + takeprev;
|
|
|
|
newnode->preceding = prev->size | MM_ALLOC_BIT;
|
2020-02-13 21:58:07 +08:00
|
|
|
next->preceding = newnode->size |
|
|
|
|
(next->preceding & MM_ALLOC_BIT);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-06-29 03:13:00 +08:00
|
|
|
/* Return the previous free node to the nodelist
|
|
|
|
* (with the new size)
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 02:29:56 +08:00
|
|
|
mm_addfreechunk(heap, prev);
|
2012-07-15 07:31:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes.. update its size (newnode->preceding is already set) */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
newnode->size += oldsize;
|
|
|
|
newnode->preceding |= MM_ALLOC_BIT;
|
2020-02-13 21:58:07 +08:00
|
|
|
next->preceding = newnode->size |
|
|
|
|
(next->preceding & MM_ALLOC_BIT);
|
2012-07-15 07:31:12 +08:00
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-08 23:10:22 +08:00
|
|
|
newmem = (FAR void *)((FAR char *)newnode + SIZEOF_MM_ALLOCNODE);
|
2020-06-29 02:57:38 +08:00
|
|
|
|
|
|
|
/* Now we want to return newnode */
|
|
|
|
|
|
|
|
oldnode = newnode;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the next free chunk */
|
|
|
|
|
|
|
|
if (takenext)
|
|
|
|
{
|
2007-07-01 04:38:16 +08:00
|
|
|
FAR struct mm_freenode_s *newnode;
|
|
|
|
FAR struct mm_allocnode_s *andbeyond;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Get the chunk following the next node (which could be the tail
|
|
|
|
* chunk)
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-06-29 03:13:00 +08:00
|
|
|
andbeyond = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)next + nextsize);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Remove the next node. There must be a predecessor, but there
|
|
|
|
* may not be a successor node.
|
2007-07-01 04:38:16 +08:00
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
DEBUGASSERT(next->blink);
|
|
|
|
next->blink->flink = next->flink;
|
|
|
|
if (next->flink)
|
|
|
|
{
|
|
|
|
next->flink->blink = next->blink;
|
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
/* Extend the node into the next chunk */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-10-26 03:27:23 +08:00
|
|
|
oldnode->size += takenext;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
/* Did we consume the entire preceding chunk? */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
if (takenext < nextsize)
|
|
|
|
{
|
2012-07-15 07:31:12 +08:00
|
|
|
/* No, take what we need from the next chunk and return it to
|
|
|
|
* the free nodelist.
|
2007-07-01 04:38:16 +08:00
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-10-26 03:27:23 +08:00
|
|
|
newnode = (FAR struct mm_freenode_s *)
|
|
|
|
((FAR char *)oldnode + oldnode->size);
|
2007-07-01 04:38:16 +08:00
|
|
|
newnode->size = nextsize - takenext;
|
2020-02-13 16:06:05 +08:00
|
|
|
DEBUGASSERT(newnode->size >= SIZEOF_MM_FREENODE);
|
2007-07-01 04:38:16 +08:00
|
|
|
newnode->preceding = oldnode->size;
|
2020-02-13 21:58:07 +08:00
|
|
|
andbeyond->preceding = newnode->size |
|
|
|
|
(andbeyond->preceding & MM_ALLOC_BIT);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
/* Add the new free node to the nodelist (with the new size) */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 02:29:56 +08:00
|
|
|
mm_addfreechunk(heap, newnode);
|
2007-07-01 04:38:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, just update some pointers. */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-02-13 21:58:07 +08:00
|
|
|
andbeyond->preceding = oldnode->size |
|
|
|
|
(andbeyond->preceding & MM_ALLOC_BIT);
|
2007-07-01 04:38:16 +08:00
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
2022-03-17 14:02:37 +08:00
|
|
|
MM_ADD_BACKTRACE(heap, (FAR char *)newmem - SIZEOF_MM_ALLOCNODE);
|
2022-01-20 17:17:29 +08:00
|
|
|
|
2013-03-09 02:29:56 +08:00
|
|
|
mm_givesemaphore(heap);
|
2021-10-09 15:22:28 +08:00
|
|
|
|
|
|
|
kasan_unpoison(newmem, mm_malloc_size(newmem));
|
|
|
|
if (newmem != oldmem)
|
|
|
|
{
|
|
|
|
/* Now we have to move the user contents 'down' in memory. memcpy
|
|
|
|
* should be safe for this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);
|
|
|
|
}
|
|
|
|
|
2007-07-01 04:38:16 +08:00
|
|
|
return newmem;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 03:13:00 +08:00
|
|
|
/* The current chunk cannot be extended.
|
|
|
|
* Just allocate a new chunk and copy
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-07-15 07:31:12 +08:00
|
|
|
/* Allocate a new block. On failure, realloc must return NULL but
|
|
|
|
* leave the original memory in place.
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-09 02:29:56 +08:00
|
|
|
mm_givesemaphore(heap);
|
2021-10-26 03:27:23 +08:00
|
|
|
newmem = mm_malloc(heap, size);
|
2012-07-15 07:31:12 +08:00
|
|
|
if (newmem)
|
|
|
|
{
|
2021-10-26 16:10:39 +08:00
|
|
|
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);
|
2014-09-01 00:54:55 +08:00
|
|
|
mm_free(heap, oldmem);
|
2012-07-15 07:31:12 +08:00
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2012-07-15 07:31:12 +08:00
|
|
|
return newmem;
|
|
|
|
}
|
|
|
|
}
|