2014-08-31 18:54:55 +02:00
|
|
|
/****************************************************************************
|
2014-09-22 18:48:58 +02:00
|
|
|
* mm/umm_heap/umm_malloc.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
|
|
|
|
*
|
|
|
|
* 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.
|
2014-08-31 18:54:55 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-09-01 18:46:51 +02: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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-31 18:54:55 +02:00
|
|
|
* Name: malloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate memory from the user heap.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-08-31 18:54:55 +02:00
|
|
|
* size - Size (in bytes) of the memory region to be allocated.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-08-31 18:54:55 +02:00
|
|
|
* The address of the allocated memory (NULL on failure to allocate)
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2014-08-31 18:54:55 +02:00
|
|
|
|
2021-04-14 09:43:16 +02:00
|
|
|
#undef malloc /* See mm/README.txt */
|
2014-08-31 18:54:55 +02:00
|
|
|
FAR void *malloc(size_t size)
|
|
|
|
{
|
2020-04-29 21:21:23 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2021-07-03 19:34:13 +02:00
|
|
|
/* Use memalign() because it implements the sbrk() logic */
|
2014-09-01 18:46:51 +02:00
|
|
|
|
2021-07-03 19:34:13 +02:00
|
|
|
return memalign(sizeof(FAR void *), size);
|
2014-09-01 18:46:51 +02:00
|
|
|
#else
|
2023-06-15 04:38:38 +02:00
|
|
|
FAR void *ret;
|
|
|
|
|
2021-07-03 19:34:13 +02:00
|
|
|
/* Use mm_malloc() because it implements the clear */
|
|
|
|
|
2023-06-15 04:38:38 +02:00
|
|
|
ret = mm_malloc(USR_HEAP, 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;
|
2014-09-01 18:46:51 +02:00
|
|
|
#endif
|
2014-08-31 18:54:55 +02:00
|
|
|
}
|