From a7aeeb28d5d193f4efe01100b426be5129ccebf6 Mon Sep 17 00:00:00 2001 From: Chen Wen Date: Wed, 17 Jul 2019 20:58:34 +0800 Subject: [PATCH] feat(heap): add heap private config to enable/disable using IRAM as heap --- components/heap/Kconfig | 10 +++++++ components/heap/port/esp8266/esp_heap_init.c | 26 +++++++++++++------ .../port/esp8266/include/esp_heap_config.h | 2 +- .../esp8266/include/priv/esp_heap_caps_priv.h | 6 +++-- components/heap/src/esp_heap_caps.c | 23 ++++++++-------- components/heap/src/esp_heap_trace.c | 5 ++-- components/lwip/Kconfig | 1 + 7 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 components/heap/Kconfig diff --git a/components/heap/Kconfig b/components/heap/Kconfig new file mode 100644 index 00000000..fe01a0ef --- /dev/null +++ b/components/heap/Kconfig @@ -0,0 +1,10 @@ + +menu "Heap memory" + +config HEAP_DISABLE_IRAM + bool "Disable IRAM as heap memory" + default n + help + Disable IRAM as heap memory, and heap memory only use DRAM. + +endmenu diff --git a/components/heap/port/esp8266/esp_heap_init.c b/components/heap/port/esp8266/esp_heap_init.c index fe01be7b..a7250dcc 100644 --- a/components/heap/port/esp8266/esp_heap_init.c +++ b/components/heap/port/esp8266/esp_heap_init.c @@ -14,26 +14,36 @@ #include "esp_heap_caps.h" +#define HEAP_REGION_IRAM_MIN 512 +#define HEAP_REGION_IRAM_MAX 0x00010000 + heap_region_t g_heap_region[HEAP_REGIONS_MAX]; + /** * @brief Initialize the capability-aware heap allocator. */ void heap_caps_init(void) { extern char _bss_end; + size_t heap_region_num = 0; -#ifndef CONFIG_SOC_FULL_ICACHE +#ifndef CONFIG_HEAP_DISABLE_IRAM extern char _iram_end; + const size_t iram_size = 0x40100000 + CONFIG_SOC_IRAM_SIZE - ((size_t)&_iram_end); - g_heap_region[0].start_addr = (uint8_t *)&_iram_end; - g_heap_region[0].total_size = ((size_t)(0x4010C000 - (uint32_t)&_iram_end)); - g_heap_region[0].caps = MALLOC_CAP_32BIT; + if (iram_size > HEAP_REGION_IRAM_MIN && iram_size < HEAP_REGION_IRAM_MAX) { + g_heap_region[heap_region_num].start_addr = (uint8_t *)&_iram_end; + g_heap_region[heap_region_num].total_size = iram_size; + g_heap_region[heap_region_num].caps = MALLOC_CAP_32BIT; + heap_region_num++; + } #endif - g_heap_region[HEAP_REGIONS_MAX - 1].start_addr = (uint8_t *)&_bss_end; - g_heap_region[HEAP_REGIONS_MAX - 1].total_size = ((size_t)(0x40000000 - (uint32_t)&_bss_end)); - g_heap_region[HEAP_REGIONS_MAX - 1].caps = MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA; + g_heap_region[heap_region_num].start_addr = (uint8_t *)&_bss_end; + g_heap_region[heap_region_num].total_size = ((size_t)(0x40000000 - (uint32_t)&_bss_end)); + g_heap_region[heap_region_num].caps = MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA; + heap_region_num++; - esp_heap_caps_init_region(g_heap_region, HEAP_REGIONS_MAX); + esp_heap_caps_init_region(g_heap_region, heap_region_num); } diff --git a/components/heap/port/esp8266/include/esp_heap_config.h b/components/heap/port/esp8266/include/esp_heap_config.h index a912b951..c5882887 100644 --- a/components/heap/port/esp8266/include/esp_heap_config.h +++ b/components/heap/port/esp8266/include/esp_heap_config.h @@ -18,7 +18,7 @@ #define HEAP_ALIGN_SIZE 4 -#ifdef CONFIG_SOC_FULL_ICACHE +#ifdef CONFIG_HEAP_DISABLE_IRAM #define HEAP_REGIONS_MAX 1 #else #define HEAP_REGIONS_MAX 2 diff --git a/components/heap/port/esp8266/include/priv/esp_heap_caps_priv.h b/components/heap/port/esp8266/include/priv/esp_heap_caps_priv.h index 2c2916d9..8122b1ab 100644 --- a/components/heap/port/esp8266/include/priv/esp_heap_caps_priv.h +++ b/components/heap/port/esp8266/include/priv/esp_heap_caps_priv.h @@ -103,9 +103,11 @@ static inline size_t blk_link_size(mem_blk_t *blk) static inline size_t get_blk_region(void *ptr) { size_t num; - extern heap_region_t g_heap_region[HEAP_REGIONS_MAX]; + extern size_t g_heap_region_num; + extern heap_region_t g_heap_region[]; - for (num = 0; num < HEAP_REGIONS_MAX; num++) { + + for (num = 0; num < g_heap_region_num; num++) { if ((uint8_t *)ptr > (uint8_t *)g_heap_region[num].start_addr && (uint8_t *)ptr < ((uint8_t *)g_heap_region[num].start_addr + g_heap_region[num].total_size)) { break; diff --git a/components/heap/src/esp_heap_caps.c b/components/heap/src/esp_heap_caps.c index de284967..34d14ce7 100644 --- a/components/heap/src/esp_heap_caps.c +++ b/components/heap/src/esp_heap_caps.c @@ -28,8 +28,10 @@ #include "esp_log.h" -static const char *TAG = "heap_caps"; -extern heap_region_t g_heap_region[HEAP_REGIONS_MAX]; +extern heap_region_t g_heap_region[]; + +static const char *TAG = "heap_init"; +size_t g_heap_region_num; int __g_heap_trace_mode = HEAP_TRACE_NONE; /** @@ -47,10 +49,6 @@ void esp_heap_caps_init_region(heap_region_t *region, size_t max_num) mem_end = (mem_blk_t *)((uint8_t *)mem_end - sizeof(void *)); mem_end = (mem_blk_t *)((uint8_t *)mem_end - MEM_HEAD_SIZE); - ESP_EARLY_LOGV(TAG, "heap %d start from %p to %p total %d bytes, mem_blk from %p to %p total", - num, region[num].start_addr, region[num].start_addr + region[num].total_size, - region[num].total_size, mem_start, mem_end); - mem_start->prev = NULL; mem_start->next = mem_end; @@ -60,6 +58,7 @@ void esp_heap_caps_init_region(heap_region_t *region, size_t max_num) g_heap_region[num].free_blk = mem_start; g_heap_region[num].min_free_bytes = g_heap_region[num].free_bytes = blk_link_size(mem_start); } + g_heap_region_num = max_num; } /** @@ -69,7 +68,7 @@ size_t heap_caps_get_free_size(uint32_t caps) { size_t bytes = 0; - for (int i = 0; i < HEAP_REGIONS_MAX; i++) + for (int i = 0; i < g_heap_region_num; i++) if (caps == (caps & g_heap_region[i].caps)) bytes += g_heap_region[i].free_bytes; @@ -83,7 +82,7 @@ size_t heap_caps_get_minimum_free_size(uint32_t caps) { size_t bytes = 0; - for (int i = 0; i < HEAP_REGIONS_MAX; i++) + for (int i = 0; i < g_heap_region_num; i++) if (caps == (caps & g_heap_region[i].caps)) bytes += g_heap_region[i].min_free_bytes; @@ -106,12 +105,14 @@ void IRAM_ATTR *_heap_caps_malloc(size_t size, uint32_t caps, const char *file, ESP_EARLY_LOGV(TAG, "caller file %s line %d", file, line); } - for (num = 0; num < HEAP_REGIONS_MAX; num++) { + for (num = 0; num < g_heap_region_num; num++) { bool trace; size_t head_size; - if ((g_heap_region[num].caps & caps) != caps) + if ((g_heap_region[num].caps & caps) != caps) { + ESP_EARLY_LOGV(TAG, "caps in %x, num %d region %x @ %p", caps, num, g_heap_region[num].caps, &g_heap_region[num]); continue; + } _heap_caps_lock(num); @@ -236,7 +237,7 @@ void IRAM_ATTR _heap_caps_free(void *ptr, const char *file, size_t line) num = get_blk_region(ptr); - if (num >= HEAP_REGIONS_MAX) { + if (num >= g_heap_region_num) { ESP_EARLY_LOGE(TAG, "free(ptr_region=NULL)"); return; } diff --git a/components/heap/src/esp_heap_trace.c b/components/heap/src/esp_heap_trace.c index 8ab0805b..8e55357b 100644 --- a/components/heap/src/esp_heap_trace.c +++ b/components/heap/src/esp_heap_trace.c @@ -43,7 +43,8 @@ static const char *TAG = "heap_trace"; -extern heap_region_t g_heap_region[HEAP_REGIONS_MAX]; +extern size_t g_heap_region_num; +extern heap_region_t g_heap_region[]; extern int __g_heap_trace_mode; /** @@ -100,7 +101,7 @@ void heap_trace_dump(void) uint8_t num; mem_blk_t *mem_start, *mem_end, *p; - for (num = 0; num < HEAP_REGIONS_MAX; num++) { + for (num = 0; num < g_heap_region_num; num++) { mem_start = (mem_blk_t *)HEAP_ALIGN(g_heap_region[num].start_addr); mem_end = (mem_blk_t *)(HEAP_ALIGN(g_heap_region[num].start_addr + g_heap_region[num].total_size)); if ((uint8_t *)mem_end != g_heap_region[num].start_addr + g_heap_region[num].total_size) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index b5ab8284..c8c5bc03 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -11,6 +11,7 @@ config LWIP_HIGH_THROUGHPUT select TCP_HIGH_SPEED_RETRANSMISSION select SOC_FULL_ICACHE select WIFI_TX_RATE_SEQUENCE_FROM_HIGH + select HEAP_DISABLE_IRAM help Enable this option, also enable "TCP_QUEUE_OOSEQ", "TCP_HIGH_SPEED_RETRANSMISSION" and "SOC_FULL_ICACHE", so lwip should cache TCP message received in disorder sequence and