-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
soc: esp32: unify runtime heap usage
This commit applies several changes in the way "heap_runtime" feature is used. It can't be split due to bisectabily issues. Whenever the feature is enabled, a new heap is created and custom malloc/calloc/free functions are added into the build system. Those functions are currently used for internal Wi-Fi and BLE drivers only. Such changes are described below: 1) Rename heap.c to esp_heap_runtime.c for better readability. 2) Rename RUNTIME_HEAP to HEAP_RUNTIME to make it similar to what is available in Zephyr. 3) Add runtime heap to BT as such as Wi-Fi. Fixes #79490 Fixes #79470 Signed-off-by: Sylvio Alves <[email protected]>
- Loading branch information
1 parent
2784ddc
commit 5c7a7a1
Showing
10 changed files
with
151 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
if BT_ESP32 | ||
|
||
choice ESP_BT_HEAP | ||
prompt "Bluetooth adapter heap in use" | ||
default ESP_BT_HEAP_RUNTIME | ||
|
||
config ESP_BT_HEAP_RUNTIME | ||
bool "Bluetooth adapter use ESP runtime heap" | ||
depends on ESP_HEAP_RUNTIME | ||
|
||
config ESP_BT_HEAP_SYSTEM | ||
bool "Bluetooth adapter use system heap" | ||
|
||
endchoice # ESP_BT_HEAP | ||
|
||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
if(CONFIG_SOC_SERIES_ESP32 OR CONFIG_SOC_SERIES_ESP32S2 OR CONFIG_SOC_SERIES_ESP32S3) | ||
zephyr_include_directories(include) | ||
endif() | ||
zephyr_include_directories(include) | ||
|
||
if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU) | ||
zephyr_sources_ifdef(CONFIG_ESP_SPIRAM psram.c) | ||
zephyr_sources_ifdef(CONFIG_ESP_RUNTIME_HEAP heap.c) | ||
zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <zephyr/types.h> | ||
#include <zephyr/kernel.h> | ||
#include <soc.h> | ||
#include <esp_err.h> | ||
#include <esp_heap_runtime.h> | ||
#include "esp_log.h" | ||
|
||
#define TAG "heap_runtime" | ||
|
||
/* ESP dynamic pool heap */ | ||
extern unsigned int z_mapped_end; | ||
extern unsigned int _heap_sentry; | ||
static void *esp_heap_runtime_init_mem = &z_mapped_end; | ||
|
||
#define ESP_HEAP_RUNTIME_MAX_SIZE ((uintptr_t)&_heap_sentry - (uintptr_t)&z_mapped_end) | ||
|
||
static struct k_heap esp_heap_runtime; | ||
|
||
static int esp_heap_runtime_init(void) | ||
{ | ||
ESP_EARLY_LOGI(TAG, "ESP heap runtime init at 0x%x size %d kB.\n", | ||
esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE / 1024); | ||
|
||
k_heap_init(&esp_heap_runtime, esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE); | ||
|
||
#if defined(CONFIG_WIFI_ESP32) && defined(CONFIG_BT_ESP32) | ||
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 65535); | ||
#elif defined(CONFIG_WIFI_ESP32) | ||
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 51200); | ||
#elif defined(CONFIG_BT_ESP32) | ||
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 40960); | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
void *esp_heap_runtime_malloc(size_t size) | ||
{ | ||
return k_heap_alloc(&esp_heap_runtime, size, K_NO_WAIT); | ||
} | ||
|
||
void *esp_heap_runtime_calloc(size_t n, size_t size) | ||
{ | ||
size_t sz; | ||
|
||
if (__builtin_mul_overflow(n, size, &sz)) { | ||
return NULL; | ||
} | ||
void *ptr = k_heap_alloc(&esp_heap_runtime, sz, K_NO_WAIT); | ||
|
||
if (ptr) { | ||
memset(ptr, 0, sz); | ||
} | ||
|
||
return ptr; | ||
} | ||
|
||
void *esp_heap_runtime_realloc(void *ptr, size_t bytes) | ||
{ | ||
return k_heap_realloc(&esp_heap_runtime, ptr, bytes, K_NO_WAIT); | ||
} | ||
|
||
void esp_heap_runtime_free(void *mem) | ||
{ | ||
k_heap_free(&esp_heap_runtime, mem); | ||
} | ||
|
||
SYS_INIT(esp_heap_runtime_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @brief Allocate memory from the esp_heap_runtime. | ||
* | ||
* @param size Amount of memory requested (in bytes). | ||
* | ||
* @return Address of the allocated memory if successful; otherwise NULL. | ||
*/ | ||
void *esp_heap_runtime_malloc(size_t size); | ||
|
||
/** | ||
* @brief Allocate memory from esp_heap_runtime, array style | ||
* | ||
* @param n Number of elements in the requested array | ||
* @param size Size of each array element (in bytes). | ||
* | ||
* @return Address of the allocated memory if successful; otherwise NULL. | ||
*/ | ||
void *esp_heap_runtime_calloc(size_t n, size_t size); | ||
|
||
/** | ||
* @brief Reallocate memory from a esp_heap_runtime | ||
* | ||
* @param ptr Original pointer returned from a previous allocation | ||
* @param bytes Desired size of block to allocate | ||
* | ||
* @return Pointer to memory the caller can now use, or NULL | ||
*/ | ||
void *esp_heap_runtime_realloc(void *ptr, size_t bytes); | ||
|
||
/** | ||
* @brief Free memory allocated from esp_heap_runtime. | ||
* | ||
* If @a ptr is NULL, no operation is performed. | ||
* | ||
* @param ptr Pointer to previously allocated memory. | ||
*/ | ||
void esp_heap_runtime_free(void *mem); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters