2014-09-01 00:54:55 +08:00
|
|
|
/****************************************************************************
|
2014-09-23 00:48:58 +08:00
|
|
|
* mm/umm_heap/umm_realloc.c
|
2014-09-01 00:54:55 +08:00
|
|
|
*
|
2024-09-12 22:44:55 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
2014-09-01 00:54:55 +08:00
|
|
|
*
|
2021-02-08 20:50:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-01 00:54:55 +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.
|
2014-09-01 00:54:55 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2019-01-27 00:42:36 +08:00
|
|
|
#include <unistd.h>
|
2023-06-15 10:38:38 +08:00
|
|
|
#include <errno.h>
|
2014-09-24 21:29:09 +08:00
|
|
|
#include <nuttx/mm/mm.h>
|
2014-09-01 00:54:55 +08:00
|
|
|
|
2015-07-10 22:37:02 +08:00
|
|
|
#include "umm_heap/umm_heap.h"
|
2014-09-01 00:54:55 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-01 05:27:37 +08:00
|
|
|
* Name: realloc
|
2014-09-01 00:54:55 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Re-allocate memory in the user heap.
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2014-09-01 00:54:55 +08:00
|
|
|
* oldmem - The old memory allocated
|
|
|
|
* newsize - Size (in bytes) of the new memory region to be re-allocated.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2014-09-01 00:54:55 +08:00
|
|
|
* The address of the re-allocated memory (NULL on failure to re-allocate)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-14 15:43:16 +08:00
|
|
|
#undef realloc /* See mm/README.txt */
|
2014-09-01 00:54:55 +08:00
|
|
|
FAR void *realloc(FAR void *oldmem, size_t size)
|
|
|
|
{
|
2019-02-05 00:32:31 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2019-01-27 00:42:36 +08:00
|
|
|
FAR void *brkaddr;
|
|
|
|
FAR void *mem;
|
|
|
|
|
2021-07-03 22:16:25 +08:00
|
|
|
/* Initialize the user heap if it wasn't yet */
|
|
|
|
|
|
|
|
umm_try_initialize();
|
|
|
|
|
2019-01-27 00:42:36 +08:00
|
|
|
/* 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)
|
|
|
|
{
|
2023-05-02 20:58:22 +08:00
|
|
|
brkaddr = sbrk(size < 1 ? 1 : size);
|
2019-01-27 00:42:36 +08:00
|
|
|
if (brkaddr == (FAR void *)-1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (mem == NULL);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
#else
|
2023-06-15 10:38:38 +08:00
|
|
|
FAR void *ret;
|
|
|
|
|
|
|
|
ret = mm_realloc(USR_HEAP, oldmem, size);
|
2023-08-01 19:27:13 +08:00
|
|
|
if (ret == NULL)
|
2023-06-15 10:38:38 +08:00
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
|
|
|
}
|
2024-01-24 23:33:02 +08:00
|
|
|
else
|
|
|
|
{
|
2024-06-30 18:00:12 +08:00
|
|
|
mm_notify_pressure(mm_heapfree(USR_HEAP),
|
|
|
|
mm_heapfree_largest(USR_HEAP));
|
2024-01-24 23:33:02 +08:00
|
|
|
}
|
2023-06-15 10:38:38 +08:00
|
|
|
|
|
|
|
return ret;
|
2019-01-27 00:42:36 +08:00
|
|
|
#endif
|
2014-09-01 00:54:55 +08:00
|
|
|
}
|