2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:53:50 +02:00
|
|
|
* mm/mm_heap/mm_realloc.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <string.h>
|
2012-07-15 01:31:12 +02:00
|
|
|
#include <stdio.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2013-03-08 21:36:18 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-09-24 15:29:09 +02:00
|
|
|
#include <nuttx/mm/mm.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-03-02 09:03:00 +01:00
|
|
|
#include "mm_heap/mm.h"
|
2021-10-09 09:22:28 +02:00
|
|
|
#include "kasan/kasan.h"
|
2021-03-02 09:03:00 +01:00
|
|
|
|
2014-08-31 18:54:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/****************************************************************************
|
2013-03-08 21:36:18 +01:00
|
|
|
* Name: mm_realloc
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the reallocation is for less space, then:
|
2012-07-15 01:31:12 +02:00
|
|
|
*
|
2007-02-18 00:21:28 +01:00
|
|
|
* (1) the current allocation is reduced in size
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
* (3) Or both
|
|
|
|
*
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
*
|
2012-07-15 01:31:12 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 21:36:18 +01:00
|
|
|
FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
|
|
|
size_t size)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_allocnode_s *oldnode;
|
2022-12-22 09:02:42 +01:00
|
|
|
FAR struct mm_freenode_s *prev = NULL;
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct mm_freenode_s *next;
|
2017-03-28 15:23:46 +02:00
|
|
|
size_t newsize;
|
2007-02-21 22:55:16 +01:00
|
|
|
size_t oldsize;
|
|
|
|
size_t prevsize = 0;
|
|
|
|
size_t nextsize = 0;
|
2007-06-30 22:38:16 +02:00
|
|
|
FAR void *newmem;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* If oldmem is NULL, then realloc is equivalent to malloc */
|
|
|
|
|
2017-03-28 15:23:46 +02:00
|
|
|
if (oldmem == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-08-31 18:54:55 +02:00
|
|
|
return mm_malloc(heap, size);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-08-04 06:10:10 +02:00
|
|
|
DEBUGASSERT(mm_heapmember(heap, oldmem));
|
|
|
|
|
2024-06-18 06:26:27 +02:00
|
|
|
#ifdef CONFIG_MM_HEAP_MEMPOOL
|
|
|
|
if (heap->mm_mpool)
|
2022-10-30 04:34:24 +01:00
|
|
|
{
|
2024-06-18 06:26:27 +02:00
|
|
|
newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
|
2022-10-30 04:34:24 +01:00
|
|
|
if (newmem != NULL)
|
|
|
|
{
|
2024-06-18 06:26:27 +02:00
|
|
|
return newmem;
|
|
|
|
}
|
|
|
|
else if (size <= heap->mm_threshold ||
|
|
|
|
mempool_multiple_alloc_size(heap->mm_mpool, oldmem) >= 0)
|
|
|
|
{
|
|
|
|
newmem = mm_malloc(heap, size);
|
|
|
|
if (newmem != NULL)
|
|
|
|
{
|
|
|
|
memcpy(newmem, oldmem,
|
|
|
|
MIN(size, mm_malloc_size(heap, oldmem)));
|
|
|
|
mm_free(heap, oldmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newmem;
|
2022-10-30 04:34:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Adjust the size to account for (1) the size of the allocated node and
|
2022-12-30 09:30:55 +01:00
|
|
|
* (2) to make sure that it is aligned with MM_ALIGN and its size is at
|
|
|
|
* least MM_MIN_CHUNK.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
if (size < MM_MIN_CHUNK - MM_ALLOCNODE_OVERHEAD)
|
2022-12-30 09:30:55 +01:00
|
|
|
{
|
2023-09-06 05:48:30 +02:00
|
|
|
size = MM_MIN_CHUNK - MM_ALLOCNODE_OVERHEAD;
|
2022-12-30 09:30:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
newsize = MM_ALIGN_UP(size + MM_ALLOCNODE_OVERHEAD);
|
2021-01-31 09:08:36 +01:00
|
|
|
if (newsize < size)
|
|
|
|
{
|
|
|
|
/* There must have been an integer overflow */
|
|
|
|
|
2022-07-13 19:35:07 +02:00
|
|
|
DEBUGPANIC();
|
2021-01-31 09:08:36 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Map the memory chunk into an allocated node structure */
|
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
oldnode = (FAR struct mm_allocnode_s *)
|
2023-09-06 05:48:30 +02:00
|
|
|
((FAR char *)oldmem - MM_SIZEOF_ALLOCNODE);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* We need to hold the MM mutex while we muck with the nodelist. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-11-05 23:41:26 +01:00
|
|
|
DEBUGVERIFY(mm_lock(heap));
|
2023-09-06 06:30:24 +02:00
|
|
|
DEBUGASSERT(MM_NODE_IS_ALLOC(oldnode));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if this is a request to reduce the size of the allocation. */
|
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
oldsize = MM_SIZEOF_NODE(oldnode);
|
2017-03-28 15:23:46 +02:00
|
|
|
if (newsize <= oldsize)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Handle the special case where we are not going to change the size
|
|
|
|
* of the allocation.
|
2007-07-01 00:24:19 +02:00
|
|
|
*/
|
2012-07-15 01:31:12 +02:00
|
|
|
|
2017-03-28 15:23:46 +02:00
|
|
|
if (newsize < oldsize)
|
2007-07-01 00:24:19 +02:00
|
|
|
{
|
2024-06-06 10:01:56 +02:00
|
|
|
heap->mm_curused += newsize - oldsize;
|
2017-03-28 15:23:46 +02:00
|
|
|
mm_shrinkchunk(heap, oldnode, newsize);
|
2023-09-06 05:48:30 +02:00
|
|
|
kasan_poison((FAR char *)oldnode + MM_SIZEOF_NODE(oldnode) +
|
|
|
|
sizeof(mmsize_t), oldsize - MM_SIZEOF_NODE(oldnode));
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2007-07-01 00:24:19 +02:00
|
|
|
|
|
|
|
/* Then return the original address */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mm_unlock(heap);
|
2022-08-01 09:59:37 +02:00
|
|
|
MM_ADD_BACKTRACE(heap, oldnode);
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return oldmem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to increase the size of the allocation, Get the
|
2012-07-15 01:31:12 +02:00
|
|
|
* available sizes before and after the oldnode so that we can make the
|
|
|
|
* best decision
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
next = (FAR struct mm_freenode_s *)((FAR char *)oldnode + oldsize);
|
2023-09-06 06:30:24 +02:00
|
|
|
if (MM_NODE_IS_FREE(next))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2023-09-06 06:30:24 +02:00
|
|
|
DEBUGASSERT(MM_PREVNODE_IS_ALLOC(next));
|
2023-09-06 05:48:30 +02:00
|
|
|
nextsize = MM_SIZEOF_NODE(next);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 06:30:24 +02:00
|
|
|
if (MM_PREVNODE_IS_FREE(oldnode))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2022-12-22 09:02:42 +01:00
|
|
|
prev = (FAR struct mm_freenode_s *)
|
|
|
|
((FAR char *)oldnode - oldnode->preceding);
|
2023-09-06 06:30:24 +02:00
|
|
|
DEBUGASSERT(MM_NODE_IS_FREE(prev));
|
2023-09-06 05:48:30 +02:00
|
|
|
prevsize = MM_SIZEOF_NODE(prev);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, check if we can extend the current allocation or not */
|
|
|
|
|
2017-03-28 15:23:46 +02:00
|
|
|
if (nextsize + prevsize + oldsize >= newsize)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2021-10-25 21:27:23 +02:00
|
|
|
size_t needed = newsize - oldsize;
|
2022-12-15 12:55:16 +01:00
|
|
|
size_t nodesize = oldsize;
|
2021-10-25 21:27:23 +02:00
|
|
|
size_t takeprev;
|
|
|
|
size_t takenext;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if we can extend into the previous chunk and if the
|
|
|
|
* previous chunk is smaller than the next chunk.
|
|
|
|
*/
|
|
|
|
|
2021-10-25 21:27:23 +02:00
|
|
|
if (nextsize > prevsize)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2021-10-25 21:27:23 +02:00
|
|
|
else
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the previous free chunk */
|
|
|
|
|
2007-07-01 00:24:19 +02:00
|
|
|
newmem = oldmem;
|
2007-02-18 00:21:28 +01:00
|
|
|
if (takeprev)
|
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
FAR struct mm_allocnode_s *newnode;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Remove the previous node. There must be a predecessor, but
|
|
|
|
* there may not be a successor node.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
DEBUGASSERT(prev && prev->blink);
|
2012-07-15 01:31:12 +02:00
|
|
|
prev->blink->flink = prev->flink;
|
|
|
|
if (prev->flink)
|
|
|
|
{
|
|
|
|
prev->flink->blink = prev->blink;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-30 09:30:55 +01:00
|
|
|
/* Make sure the new previous node has enough space */
|
|
|
|
|
|
|
|
if (prevsize < takeprev + MM_MIN_CHUNK)
|
|
|
|
{
|
2024-06-06 10:01:56 +02:00
|
|
|
heap->mm_curused += prevsize - takeprev;
|
|
|
|
takeprev = prevsize;
|
2022-12-30 09:30:55 +01:00
|
|
|
}
|
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Extend the node into the previous free chunk */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-02-13 14:58:07 +01:00
|
|
|
newnode = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)oldnode - takeprev);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Did we consume the entire preceding chunk? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02: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 00:21:28 +01:00
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
prevsize -= takeprev;
|
2022-12-22 09:02:42 +01:00
|
|
|
prev->size = prevsize | (prev->size & MM_MASK_BIT);
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize += takeprev;
|
2022-12-22 09:02:42 +01:00
|
|
|
newnode->size = nodesize | MM_ALLOC_BIT | MM_PREVFREE_BIT;
|
2022-12-15 12:55:16 +01:00
|
|
|
newnode->preceding = prevsize;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-06-28 21:13:00 +02:00
|
|
|
/* Return the previous free node to the nodelist
|
|
|
|
* (with the new size)
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_addfreechunk(heap, prev);
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes.. update its size (newnode->preceding is already set) */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
nodesize += prevsize;
|
|
|
|
newnode->size = nodesize | MM_ALLOC_BIT |
|
|
|
|
(newnode->size & MM_MASK_BIT);
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
newmem = (FAR void *)((FAR char *)newnode + MM_SIZEOF_ALLOCNODE);
|
2020-06-28 20:57:38 +02:00
|
|
|
|
|
|
|
/* Now we want to return newnode */
|
|
|
|
|
|
|
|
oldnode = newnode;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend into the next free chunk */
|
|
|
|
|
|
|
|
if (takenext)
|
|
|
|
{
|
2007-06-30 22:38:16 +02:00
|
|
|
FAR struct mm_freenode_s *newnode;
|
|
|
|
FAR struct mm_allocnode_s *andbeyond;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Get the chunk following the next node (which could be the tail
|
|
|
|
* chunk)
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-06-28 21:13:00 +02:00
|
|
|
andbeyond = (FAR struct mm_allocnode_s *)
|
|
|
|
((FAR char *)next + nextsize);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Remove the next node. There must be a predecessor, but there
|
|
|
|
* may not be a successor node.
|
2007-06-30 22:38:16 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
DEBUGASSERT(next->blink);
|
|
|
|
next->blink->flink = next->flink;
|
|
|
|
if (next->flink)
|
|
|
|
{
|
|
|
|
next->flink->blink = next->blink;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-30 09:30:55 +01:00
|
|
|
/* Make sure the new next node has enough space */
|
|
|
|
|
|
|
|
if (nextsize < takenext + MM_MIN_CHUNK)
|
|
|
|
{
|
2024-06-06 10:01:56 +02:00
|
|
|
heap->mm_curused += nextsize - takenext;
|
|
|
|
takenext = nextsize;
|
2022-12-30 09:30:55 +01:00
|
|
|
}
|
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
/* Extend the node into the next chunk */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize += takenext;
|
|
|
|
oldnode->size = nodesize | (oldnode->size & MM_MASK_BIT);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
/* Did we consume the entire preceding chunk? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
if (takenext < nextsize)
|
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* No, take what we need from the next chunk and return it to
|
|
|
|
* the free nodelist.
|
2007-06-30 22:38:16 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2021-10-25 21:27:23 +02:00
|
|
|
newnode = (FAR struct mm_freenode_s *)
|
2022-12-15 12:55:16 +01:00
|
|
|
((FAR char *)oldnode + nodesize);
|
2007-06-30 22:38:16 +02:00
|
|
|
newnode->size = nextsize - takenext;
|
2022-12-15 12:55:16 +01:00
|
|
|
andbeyond->preceding = newnode->size;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
/* Add the new free node to the nodelist (with the new size) */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-08 19:29:56 +01:00
|
|
|
mm_addfreechunk(heap, newnode);
|
2007-06-30 22:38:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes, just update some pointers. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-12-22 09:02:42 +01:00
|
|
|
andbeyond->size &= ~MM_PREVFREE_BIT;
|
2007-06-30 22:38:16 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-10-31 09:37:14 +01:00
|
|
|
/* Update heap statistics */
|
|
|
|
|
|
|
|
heap->mm_curused += newsize - oldsize;
|
|
|
|
if (heap->mm_curused > heap->mm_maxused)
|
|
|
|
{
|
|
|
|
heap->mm_maxused = heap->mm_curused;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mm_unlock(heap);
|
2023-09-06 05:48:30 +02:00
|
|
|
MM_ADD_BACKTRACE(heap, (FAR char *)newmem - MM_SIZEOF_ALLOCNODE);
|
2022-08-01 09:59:37 +02:00
|
|
|
|
2022-11-24 06:46:53 +01:00
|
|
|
kasan_unpoison(newmem, mm_malloc_size(heap, newmem));
|
2021-10-09 09:22:28 +02:00
|
|
|
if (newmem != oldmem)
|
|
|
|
{
|
|
|
|
/* Now we have to move the user contents 'down' in memory. memcpy
|
|
|
|
* should be safe for this.
|
|
|
|
*/
|
|
|
|
|
2023-09-06 05:48:30 +02:00
|
|
|
memcpy(newmem, oldmem, oldsize - MM_ALLOCNODE_OVERHEAD);
|
2021-10-09 09:22:28 +02:00
|
|
|
}
|
|
|
|
|
2007-06-30 22:38:16 +02:00
|
|
|
return newmem;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 21:13:00 +02:00
|
|
|
/* The current chunk cannot be extended.
|
|
|
|
* Just allocate a new chunk and copy
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2012-07-15 01:31:12 +02:00
|
|
|
/* Allocate a new block. On failure, realloc must return NULL but
|
|
|
|
* leave the original memory in place.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mm_unlock(heap);
|
2021-10-25 21:27:23 +02:00
|
|
|
newmem = mm_malloc(heap, size);
|
2012-07-15 01:31:12 +02:00
|
|
|
if (newmem)
|
|
|
|
{
|
2023-09-06 05:48:30 +02:00
|
|
|
memcpy(newmem, oldmem, oldsize - MM_ALLOCNODE_OVERHEAD);
|
2014-08-31 18:54:55 +02:00
|
|
|
mm_free(heap, oldmem);
|
2012-07-15 01:31:12 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-15 01:31:12 +02:00
|
|
|
return newmem;
|
|
|
|
}
|
|
|
|
}
|