nuttx/mm/umm_heap/umm_realloc.c

109 lines
3.3 KiB
C
Raw Normal View History

/****************************************************************************
* mm/umm_heap/umm_realloc.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <nuttx/mm/mm.h>
#include "umm_heap/umm_heap.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: realloc
*
* Description:
* Re-allocate memory in the user heap.
*
* Input Parameters:
* oldmem - The old memory allocated
* newsize - Size (in bytes) of the new memory region to be re-allocated.
*
* Returned Value:
* The address of the re-allocated memory (NULL on failure to re-allocate)
*
****************************************************************************/
#undef realloc /* See mm/README.txt */
FAR void *realloc(FAR void *oldmem, size_t size)
{
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
FAR void *brkaddr;
FAR void *mem;
/* Initialize the user heap if it wasn't yet */
umm_try_initialize();
/* Loop until we successfully allocate the memory or until an error
* occurs. If we fail to allocate memory on the first pass, then call
* sbrk to extend the heap by one page. This may require several
* passes if more the size of the allocation is more than one page.
*
* An alternative would be to increase the size of the heap by the
* full requested allocation in sbrk(). Then the loop should never
* execute more than twice (but more memory than we need may be
* allocated).
*/
do
{
mem = mm_realloc(USR_HEAP, oldmem, size);
if (!mem)
{
mm: memory allocations return valid pointer when request 0 size This change introduce 2 items: 1. If the size of the space requested is 0, the behavior is implementation-defined: either a null pointer shall be returned, or the behavior shall be as if the size were some non-zero value, except that the behavior is undefined if the returned pointer is used to access an object. Change the behavior to be similar to Linux and Android and allocates an object of a minimum size instead of returning null pointer. https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/calloc.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html 2. The description of realloc() has been modified from previous versions of this standard to align with the ISO/IEC 9899:1999 standard. Previous versions explicitly permitted a call to realloc (p, 0) to free the space pointed to by p and return a null pointer. While this behavior could be interpreted as permitted by this version of the standard, the C language committee have indicated that this interpretation is incorrect. Applications should assume that if realloc() returns a null pointer, the space pointed to by p has not been freed. Since this could lead to double-frees, implementations should also set errno if a null pointer actually indicates a failure, and applications should only free the space if errno was changed. Do not free memory of zero-length reallocation is requested https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html Co-authored-by: fangxinyong <fangxinyong@xiaomi.com> Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-05-02 14:58:22 +02:00
brkaddr = sbrk(size < 1 ? 1 : size);
if (brkaddr == (FAR void *)-1)
{
return NULL;
}
}
}
while (mem == NULL);
return mem;
#else
FAR void *ret;
ret = mm_realloc(USR_HEAP, oldmem, size);
if (ret == NULL)
{
set_errno(ENOMEM);
}
else
{
mm_notify_pressure(mm_heapfree(USR_HEAP),
mm_heapfree_largest(USR_HEAP));
}
return ret;
#endif
}