feat(cache): added cache_prefer_m/calloc

This commit is contained in:
Armando 2024-03-22 12:24:03 +08:00
parent 950740dc62
commit de70ed1c84
3 changed files with 95 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -130,6 +130,28 @@ esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t flags, void **out_ptr,
return ESP_OK;
}
esp_err_t esp_cache_aligned_malloc_prefer(size_t size, void **out_ptr, size_t *actual_size, size_t flag_nums, ...)
{
ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
esp_err_t ret = ESP_FAIL;
va_list argp;
uint32_t flags = 0;
va_start(argp, flag_nums);
*out_ptr = NULL;
while (flag_nums--) {
flags = va_arg(argp, uint32_t);
ret = esp_cache_aligned_malloc(size, flags, out_ptr, actual_size);
if (ret == ESP_OK) {
break;
}
}
va_end(argp);
return ret;
}
esp_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t flags, void **out_ptr, size_t *actual_size)
{
ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
@ -151,6 +173,39 @@ esp_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t flags, void *
return ret;
}
esp_err_t esp_cache_aligned_calloc_prefer(size_t n, size_t size, void **out_ptr, size_t *actual_size, size_t flag_nums, ...)
{
ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer");
esp_err_t ret = ESP_FAIL;
size_t size_bytes = 0;
bool ovf = false;
*out_ptr = NULL;
ovf = __builtin_mul_overflow(n, size, &size_bytes);
ESP_RETURN_ON_FALSE_ISR(!ovf, ESP_ERR_INVALID_ARG, TAG, "wrong size, total size overflow");
void *ptr = NULL;
va_list argp;
va_start(argp, flag_nums);
int arg;
for (int i = 0; i < flag_nums; i++) {
arg = va_arg(argp, int);
ret = esp_cache_aligned_malloc_prefer(size_bytes, &ptr, actual_size, flag_nums, arg);
if (ret == ESP_OK) {
memset(ptr, 0, size_bytes);
*out_ptr = ptr;
arg = va_arg(argp, int);
break;
}
}
va_end(argp);
return ret;
}
esp_err_t esp_cache_get_alignment(uint32_t flags, size_t *out_alignment)
{
ESP_RETURN_ON_FALSE(out_alignment, ESP_ERR_INVALID_ARG, TAG, "null pointer");

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -42,6 +42,24 @@ extern "C" {
*/
esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size);
/**
* @brief Helper function for malloc a cache aligned data memory buffer as preference in decreasing order.
*
* @param[in] size Size in bytes, the amount of memory to allocate
* @param[out] out_ptr A pointer to the memory allocated successfully
* @param[out] actual_size Actual size for allocation in bytes, when the size you specified doesn't meet the cache alignment requirements, this value might be bigger than the size you specified. Set null if you don't care this value.
* @param[in] flag_nums Number of variable parameters
* @param[in] spread param The spread params are bitwise OR of Flags, see `ESP_CACHE_MALLOC_FLAG_x`. This API prefers to allocate memory with the first parameter. If failed, allocate memory with
* the next parameter. It will try in this order until allocating a chunk of memory successfully
* or fail to allocate memories with any of the parameters.
*
* @return
* - ESP_OK:
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_NO_MEM: No enough memory for allocation
*/
esp_err_t esp_cache_aligned_malloc_prefer(size_t size, void **out_ptr, size_t *actual_size, size_t flag_nums, ...);
/**
* @brief Helper function for calloc a cache aligned data memory buffer
*
@ -58,6 +76,25 @@ esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t flags, void **out_ptr,
*/
esp_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t flags, void **out_ptr, size_t *actual_size);
/**
* @brief Helper function for calloc a cache aligned data memory buffer as preference in decreasing order.
*
* @param[in] n Number of continuing chunks of memory to allocate
* @param[in] size Size in bytes, the amount of memory to allocate
* @param[out] out_ptr A pointer to the memory allocated successfully
* @param[out] actual_size Actual size for allocation in bytes, when the size you specified doesn't meet the cache alignment requirements, this value might be bigger than the size you specified. Set null if you don't care this value.
* @param[in] flag_nums Number of variable parameters
* @param[in] spread param The spread params are bitwise OR of Flags, see `ESP_CACHE_MALLOC_FLAG_x`. This API prefers to allocate memory with the first parameter. If failed, allocate memory with
* the next parameter. It will try in this order until allocating a chunk of memory successfully
* or fail to allocate memories with any of the parameters.
*
* @return
* - ESP_OK:
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_NO_MEM: No enough memory for allocation
*/
esp_err_t esp_cache_aligned_calloc_prefer(size_t n, size_t size, void **out_ptr, size_t *actual_size, size_t flag_nums, ...);
/**
* @brief Get Cache alignment requirement for data
*

View File

@ -1,6 +1,6 @@
set(srcs "test_app_main.c")
list(APPEND srcs "test_mmap.c")
list(APPEND srcs "test_mmap.c" "test_cache_msync_malloc.c")
if(CONFIG_SOC_CACHE_WRITEBACK_SUPPORTED)
list(APPEND srcs "test_cache_msync.c")