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