From 0df418facd8df6b7475c2c2792cd86da09037864 Mon Sep 17 00:00:00 2001 From: Armando Date: Mon, 8 Apr 2024 10:59:13 +0800 Subject: [PATCH] change(cache): update to use heap cap malloc flags --- components/esp_hw_support/dma/esp_dma_utils.c | 3 +- components/esp_mm/esp_cache.c | 24 +++----- .../include/esp_private/esp_cache_private.h | 56 +++++++++---------- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/components/esp_hw_support/dma/esp_dma_utils.c b/components/esp_hw_support/dma/esp_dma_utils.c index 41fbe0503e..6cf9cdc64c 100644 --- a/components/esp_hw_support/dma/esp_dma_utils.c +++ b/components/esp_hw_support/dma/esp_dma_utils.c @@ -18,7 +18,6 @@ #include "hal/hal_utils.h" static const char *TAG = "dma_utils"; -_Static_assert(ESP_DMA_MALLOC_FLAG_PSRAM == ESP_CACHE_MALLOC_FLAG_PSRAM); #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1)) #define ALIGN_DOWN_BY(num, align) ((num) & (~((align) - 1))) @@ -38,7 +37,7 @@ esp_err_t esp_dma_capable_malloc(size_t size, const esp_dma_mem_info_t *dma_mem_ int heap_caps = dma_mem_info->extra_heap_caps | MALLOC_CAP_DMA; if (dma_mem_info->extra_heap_caps & MALLOC_CAP_SPIRAM) { - cache_flags |= ESP_DMA_MALLOC_FLAG_PSRAM; + cache_flags |= MALLOC_CAP_SPIRAM; heap_caps = dma_mem_info->extra_heap_caps | MALLOC_CAP_SPIRAM; } diff --git a/components/esp_mm/esp_cache.c b/components/esp_mm/esp_cache.c index a8f341e0d8..213a809431 100644 --- a/components/esp_mm/esp_cache.c +++ b/components/esp_mm/esp_cache.c @@ -93,23 +93,18 @@ esp_err_t esp_cache_msync(void *addr, size_t size, int flags) return ESP_OK; } -esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size) +esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t heap_caps, void **out_ptr, size_t *actual_size) { ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer"); + uint32_t valid_caps = MALLOC_CAP_SPIRAM | MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA; + ESP_RETURN_ON_FALSE_ISR((heap_caps & valid_caps) > 0, ESP_ERR_INVALID_ARG, TAG, "not supported cap matches"); uint32_t cache_level = CACHE_LL_LEVEL_INT_MEM; - uint32_t heap_caps = 0; uint32_t data_cache_line_size = 0; void *ptr = NULL; - if (flags & ESP_CACHE_MALLOC_FLAG_PSRAM) { + if (heap_caps & MALLOC_CAP_SPIRAM) { cache_level = CACHE_LL_LEVEL_EXT_MEM; - heap_caps |= MALLOC_CAP_SPIRAM; - } else { - heap_caps |= MALLOC_CAP_INTERNAL; - if (flags & ESP_CACHE_MALLOC_FLAG_DMA) { - heap_caps |= MALLOC_CAP_DMA; - } } data_cache_line_size = cache_hal_get_cache_line_size(cache_level, CACHE_TYPE_DATA); @@ -119,9 +114,8 @@ esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t flags, void **out_ptr, } size = ALIGN_UP_BY(size, data_cache_line_size); - ptr = heap_caps_aligned_alloc(data_cache_line_size, size, heap_caps); + ptr = heap_caps_aligned_alloc(data_cache_line_size, size, (uint32_t)heap_caps); if (!ptr) { - ESP_LOGW(TAG, "no enough heap memory for (%"PRId32")B alignment", data_cache_line_size); return ESP_ERR_NO_MEM; } @@ -155,7 +149,7 @@ esp_err_t esp_cache_aligned_malloc_prefer(size_t size, void **out_ptr, size_t *a 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_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t heap_caps, void **out_ptr, size_t *actual_size) { ESP_RETURN_ON_FALSE_ISR(out_ptr, ESP_ERR_INVALID_ARG, TAG, "null pointer"); @@ -167,7 +161,7 @@ esp_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t flags, void * ESP_RETURN_ON_FALSE_ISR(!ovf, ESP_ERR_INVALID_ARG, TAG, "wrong size, total size overflow"); void *ptr = NULL; - ret = esp_cache_aligned_malloc(size_bytes, flags, &ptr, actual_size); + ret = esp_cache_aligned_malloc(size_bytes, heap_caps, &ptr, actual_size); if (ret == ESP_OK) { memset(ptr, 0, size_bytes); *out_ptr = ptr; @@ -209,14 +203,14 @@ esp_err_t esp_cache_aligned_calloc_prefer(size_t n, size_t size, void **out_ptr, return ret; } -esp_err_t esp_cache_get_alignment(uint32_t flags, size_t *out_alignment) +esp_err_t esp_cache_get_alignment(uint32_t heap_caps, size_t *out_alignment) { ESP_RETURN_ON_FALSE(out_alignment, ESP_ERR_INVALID_ARG, TAG, "null pointer"); uint32_t cache_level = CACHE_LL_LEVEL_INT_MEM; uint32_t data_cache_line_size = 0; - if (flags & ESP_CACHE_MALLOC_FLAG_PSRAM) { + if (heap_caps & MALLOC_CAP_SPIRAM) { cache_level = CACHE_LL_LEVEL_EXT_MEM; } diff --git a/components/esp_mm/include/esp_private/esp_cache_private.h b/components/esp_mm/include/esp_private/esp_cache_private.h index 63d0d89719..97a4afafcc 100644 --- a/components/esp_mm/include/esp_private/esp_cache_private.h +++ b/components/esp_mm/include/esp_private/esp_cache_private.h @@ -9,47 +9,39 @@ #include #include "esp_err.h" #include "esp_bit_defs.h" +#include "esp_heap_caps.h" #ifdef __cplusplus extern "C" { #endif -/** - * Cache malloc flags - */ -/** - * @brief Memory is in PSRAM - */ -#define ESP_CACHE_MALLOC_FLAG_PSRAM BIT(0) - -/** - * @brief Memory is capable to be accessed by DMA - */ -#define ESP_CACHE_MALLOC_FLAG_DMA BIT(1) - /** * @brief Helper function for malloc a cache aligned data memory buffer * - * @param[in] size Size in bytes, the amount of memory to allocate - * @param[in] flags Flags, see `ESP_CACHE_MALLOC_FLAG_x` - * @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. + * @note Now only support 'MALLOC_CAP_INTERNAL', 'MALLOC_CAP_DMA' and 'MALLOC_CAP_SPIRAM' + * + * @param[in] size Size in bytes, the amount of memory to allocate + * @param[in] heap_caps Flags, see `MALLOC_CAP_x` + * @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. * * @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(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size); +esp_err_t esp_cache_aligned_malloc(size_t size, uint32_t heap_caps, void **out_ptr, size_t *actual_size); /** * @brief Helper function for malloc a cache aligned data memory buffer as preference in decreasing order. * + * @note Now only support 'MALLOC_CAP_INTERNAL', 'MALLOC_CAP_DMA' and 'MALLOC_CAP_SPIRAM' + * * @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 + * @param[in] spread param The spread params are bitwise OR of Flags, see `MALLOC_CAP_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. * @@ -63,28 +55,32 @@ esp_err_t esp_cache_aligned_malloc_prefer(size_t size, void **out_ptr, size_t *a /** * @brief Helper function for calloc a cache aligned data memory buffer * - * @param[in] n Number of continuing chunks of memory to allocate - * @param[in] size Size of one chunk, in bytes - * @param[in] flags Flags, see `ESP_CACHE_MALLOC_FLAG_x` - * @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. + * @note Now only support 'MALLOC_CAP_INTERNAL', 'MALLOC_CAP_DMA' and 'MALLOC_CAP_SPIRAM' + * + * @param[in] n Number of continuing chunks of memory to allocate + * @param[in] size Size of one chunk, in bytes + * @param[in] heap_caps Flags, see `MALLOC_CAP_x` + * @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. * * @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(size_t n, size_t size, uint32_t flags, void **out_ptr, size_t *actual_size); +esp_err_t esp_cache_aligned_calloc(size_t n, size_t size, uint32_t heap_caps, void **out_ptr, size_t *actual_size); /** * @brief Helper function for calloc a cache aligned data memory buffer as preference in decreasing order. * + * @note Now only support 'MALLOC_CAP_INTERNAL', 'MALLOC_CAP_DMA' and 'MALLOC_CAP_SPIRAM' + * * @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 + * @param[in] spread param The spread params are bitwise OR of Flags, see `MALLOC_CAP_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. * @@ -98,14 +94,16 @@ esp_err_t esp_cache_aligned_calloc_prefer(size_t n, size_t size, void **out_ptr, /** * @brief Get Cache alignment requirement for data * - * @param[in] flags Flags, see `ESP_CACHE_MALLOC_FLAG_x` - * @param[out] out_alignment Alignment + * @note Now only support 'MALLOC_CAP_INTERNAL', 'MALLOC_CAP_DMA' and 'MALLOC_CAP_SPIRAM' + * + * @param[in] heap_caps Flags, see `MALLOC_CAP_x` + * @param[out] out_alignment Alignment * * @return * - ESP_OK: * - ESP_ERR_INVALID_ARG: Invalid argument */ -esp_err_t esp_cache_get_alignment(uint32_t flags, size_t *out_alignment); +esp_err_t esp_cache_get_alignment(uint32_t heap_caps, size_t *out_alignment); #ifdef __cplusplus }