2014-08-31 18:54:55 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:48:58 +02:00
|
|
|
* mm/umm_heap/umm_memalign.c
|
2014-08-31 18:54:55 +02:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: memalign
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* memalign requests more than enough space from malloc, finds a region
|
|
|
|
* within that chunk that meets the alignment request and then frees any
|
|
|
|
* leading or trailing space.
|
|
|
|
*
|
|
|
|
* The alignment argument must be a power of two (not checked). 8-byte
|
|
|
|
* alignment is guaranteed by normal malloc calls.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-14 09:43:16 +02:00
|
|
|
#undef memalign /* See mm/README.txt */
|
2014-08-31 18:54:55 +02:00
|
|
|
FAR void *memalign(size_t alignment, 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_memalign(USR_HEAP, alignment, 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_memalign(USR_HEAP, alignment, size);
|
2023-08-01 13:27:13 +02:00
|
|
|
if (ret == NULL)
|
2023-06-15 04:38:38 +02:00
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
|
|
|
}
|
2024-01-24 16:33:02 +01:00
|
|
|
else
|
|
|
|
{
|
2024-06-30 12:00:12 +02:00
|
|
|
mm_notify_pressure(mm_heapfree(USR_HEAP),
|
|
|
|
mm_heapfree_largest(USR_HEAP));
|
2024-01-24 16:33:02 +01:00
|
|
|
}
|
2023-06-15 04:38:38 +02:00
|
|
|
|
|
|
|
return ret;
|
2019-01-26 17:42:36 +01:00
|
|
|
#endif
|
2014-08-31 18:54:55 +02:00
|
|
|
}
|