diff --git a/components/esp_psram/Kconfig.spiram.common b/components/esp_psram/Kconfig.spiram.common index efce1faedf1..2e41bef18d0 100644 --- a/components/esp_psram/Kconfig.spiram.common +++ b/components/esp_psram/Kconfig.spiram.common @@ -104,7 +104,7 @@ config SPIRAM_MALLOC_RESERVE_INTERNAL that the internal memory is entirely filled up. This causes allocations that are specifically done in internal memory, for example the stack for new tasks or memory to service DMA or have memory that's also available when SPI cache is down, to fail. This option reserves a pool specifically for requests - like that; a normal malloc() can fall back to the pool only after all other internal are exhausted. + like that; the memory in this pool is not given out when a normal malloc() is called. Set this to 0 to disable this feature. diff --git a/components/esp_psram/esp_psram.c b/components/esp_psram/esp_psram.c index 2028f8a3045..65a9b2b3791 100644 --- a/components/esp_psram/esp_psram.c +++ b/components/esp_psram/esp_psram.c @@ -488,7 +488,7 @@ esp_err_t esp_psram_extram_reserve_dma_pool(size_t size) return ESP_ERR_NO_MEM; } - uint32_t caps[] = {0, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT | MALLOC_CAP_32BIT}; + uint32_t caps[] = {0, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT}; esp_err_t e = heap_caps_add_region_with_caps(caps, (intptr_t)dma_heap, (intptr_t)dma_heap + next_size - 1); if (e != ESP_OK) { return e; diff --git a/components/esp_psram/include/esp_private/esp_psram_extram.h b/components/esp_psram/include/esp_private/esp_psram_extram.h index 574a6e82604..f0e25041b6a 100644 --- a/components/esp_psram/include/esp_private/esp_psram_extram.h +++ b/components/esp_psram/include/esp_private/esp_psram_extram.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -37,11 +37,6 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void); /** * @brief Reserve a pool of internal memory for specific DMA/internal allocations * - * Carves out @p size bytes from the regular internal heaps and registers the - * region as separate heap pool with new priority-based caps: medium priority for - * MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, and low priority for MALLOC_CAP_DEFAULT. - * malloc() uses the pool only after other internal heaps are exhausted. - * * @param size Size of reserved pool in bytes * * @return diff --git a/components/heap/test_apps/heap_tests/main/test_malloc.c b/components/heap/test_apps/heap_tests/main/test_malloc.c index e78cdc1d144..38b4ea18a0c 100644 --- a/components/heap/test_apps/heap_tests/main/test_malloc.c +++ b/components/heap/test_apps/heap_tests/main/test_malloc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -64,6 +64,30 @@ TEST_CASE("Malloc/overwrite, then free all available DRAM", "[heap]") TEST_ASSERT(m1==m2); } +#if CONFIG_SPIRAM_USE_MALLOC + +#if (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024) +TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap]") +{ + char** dmaMem=malloc(sizeof(char*)*512); + assert(dmaMem); + int m=tryAllocMem(); + int i=0; + for (i=0; i<512; i++) { + dmaMem[i]=heap_caps_malloc(1024, MALLOC_CAP_DMA); + if (dmaMem[i]==NULL) break; + } + for (int j=0; j` and ``MALLOC_CAP_INTERNAL`` flags can be used to allocate memory from this pool. .. _external_ram_config_bss: diff --git a/docs/zh_CN/api-guides/external-ram.rst b/docs/zh_CN/api-guides/external-ram.rst index b0d31ec9ea7..6a4ea712031 100644 --- a/docs/zh_CN/api-guides/external-ram.rst +++ b/docs/zh_CN/api-guides/external-ram.rst @@ -104,14 +104,7 @@ ESP-IDF 启动过程中,片外 RAM 被映射到数据虚拟地址空间,该 如果优先考虑的内部或外部存储器中没有可用的存储块,分配程序则会选择其他类型存储。 -由于有些内存缓冲器仅可在内部存储器中分配,因此需要使用第二个配置项 :ref:`CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL` 在启动阶段(PSRAM 初始化完成后,于 ``main_task`` 中)预留一块内部 DMA 可用内存池。该内存从常规内部堆中分配出,并重新注册为独立的内存池。 - -预留池通过堆能力优先级机制管理: - -- **中优先级** — ``MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL``:显式使用这些标志的 ``heap_caps_malloc()`` 请求会优先从此池分配。 -- **低优先级** — ``MALLOC_CAP_DEFAULT``:仅当其他具有更高优先级 ``MALLOC_CAP_DEFAULT`` 的内部堆耗尽后,``malloc()`` 才会回退使用此池。 - -因此,在正常情况下 ``malloc()`` 不会从该池分配;仅当其他内部内存耗尽后,``malloc()`` 才会将其作为最后的回退来源以避免分配完全失败。 +由于有些内存缓冲器仅可在内部存储器中分配,因此需要使用第二个配置项 :ref:`CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL` 定义一个内部内存池,仅限显式的内部存储器分配使用(例如用于 DMA 的存储器)。常规 ``malloc()`` 将不会从该池中分配,但可以使用 :ref:`MALLOC_CAP_DMA ` 和 ``MALLOC_CAP_INTERNAL`` 标志从该池中分配存储器。 .. _external_ram_config_bss: