From 796be200e2fec5fa919409aa6f2004d94ec89789 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Mon, 15 Jan 2024 15:31:18 +0800 Subject: [PATCH 1/6] ESP32: Add menuconfig to use PSRAM for Matter memory allocation --- config/esp32/args.gni | 3 + config/esp32/components/chip/Kconfig | 21 +++ .../esp32/common/CommonDeviceCallbacks.cpp | 7 +- src/platform/ESP32/BUILD.gn | 5 + src/platform/ESP32/CHIPMem-Platform.cpp | 143 ++++++++++++++++++ 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/platform/ESP32/CHIPMem-Platform.cpp diff --git a/config/esp32/args.gni b/config/esp32/args.gni index f818a37f258e49..e52423f288c9d5 100644 --- a/config/esp32/args.gni +++ b/config/esp32/args.gni @@ -29,6 +29,9 @@ lwip_platform = "external" chip_inet_config_enable_tcp_endpoint = false chip_inet_config_enable_udp_endpoint = true +#Use platform memory management +chip_config_memory_management = "platform" + custom_toolchain = "//third_party/connectedhomeip/config/esp32/toolchain:esp32" # Avoid constraint forcing for ESP32: diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index f8885b77e9739f..78e3a210894173 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -191,6 +191,27 @@ menu "CHIP Core" Enable dynamic server to handle a different interaction model dispatch. Can be implied when users do not want to use the same server clusters. + choice CHIP_MEM_ALLOC_MODE + prompt "Memory allocation strategy" + default CHIP_MEM_ALLOC_MODE_DEFAULT + help + Strategy for Matter memory management + - Internal DRAM memory only + - External SPIRAM memory only + - Either internal or external memory based on default malloc() behavior in ESP-IDF + + config CHIP_MEM_ALLOC_MODE_INTERNAL + bool "Internal memory" + + config CHIP_MEM_ALLOC_MODE_EXTERNAL + bool "External SPIRAM" + depends on SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC + + config CHIP_MEM_ALLOC_MODE_DEFAULT + bool "Default alloc mode" + + endchoice # CHIP_MEM_ALLOC_MODE + endmenu # "General Options" menu "Networking Options" diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index 2dfcffef3dece8..4581b576500df8 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -95,7 +95,12 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i break; } - ESP_LOGI(TAG, "Current free heap: %u\n", static_cast(heap_caps_get_free_size(MALLOC_CAP_8BIT))); + ESP_LOGI(TAG, "Current free heap: Internal: %u/%u External: %u/%u", + static_cast(heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)), + static_cast(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)), + static_cast(heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM)), + static_cast(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM))); + } void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 77cbc94bb43650..aa4f7eb41118f0 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -15,6 +15,7 @@ import("//build_overrides/chip.gni") import("${chip_root}/src/inet/inet.gni") +import("${chip_root}/src/lib/core/core.gni") import("${chip_root}/src/platform/device.gni") assert(chip_device_platform == "esp32") @@ -81,6 +82,10 @@ static_library("ESP32") { "${chip_root}/src/platform:platform_base", ] + if (chip_config_memory_management == "platform") { + sources += [ "CHIPMem-Platform.cpp" ] + } + if (chip_enable_ota_requestor) { sources += [ "OTAImageProcessorImpl.cpp", diff --git a/src/platform/ESP32/CHIPMem-Platform.cpp b/src/platform/ESP32/CHIPMem-Platform.cpp new file mode 100644 index 00000000000000..f155d9f7415a55 --- /dev/null +++ b/src/platform/ESP32/CHIPMem-Platform.cpp @@ -0,0 +1,143 @@ +/* + * + * Copyright (c) 2025 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file implements heap memory allocation APIs for CHIP. These functions are platform + * specific and might be C Standard Library heap functions re-direction in most of cases. + * + */ + +#include +#include + +#include +#include +#include +#include +#include + + +namespace chip { +namespace Platform { + +#define VERIFY_INITIALIZED() VerifyInitialized(__func__) + +static std::atomic_int memoryInitialized{ 0 }; + +static void VerifyInitialized(const char * func) +{ + if (!memoryInitialized) + { + ChipLogError(DeviceLayer, "ABORT: chip::Platform::%s() called before chip::Platform::MemoryInit()", func); + abort(); + } +} + +CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize) +{ + if (memoryInitialized++ > 0) + { + ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryInit() called twice."); + abort(); + } + + return CHIP_NO_ERROR; +} + +void MemoryAllocatorShutdown() +{ + if (--memoryInitialized < 0) + { + ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryShutdown() called twice."); + abort(); + } +} + +void * MemoryAlloc(size_t size) +{ + void * ptr; + VERIFY_INITIALIZED(); +#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL + ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); +#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) + ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +#else + ptr = malloc(size); +#endif + return ptr; +} + +void * MemoryAlloc(size_t size, bool isLongTermAlloc) +{ + void * ptr; + VERIFY_INITIALIZED(); +#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL + ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); +#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) + ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +#else + ptr = malloc(size); +#endif + return ptr; +} + +void * MemoryCalloc(size_t num, size_t size) +{ + void * ptr; + VERIFY_INITIALIZED(); +#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL + ptr = heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); +#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) + ptr = heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +#else + ptr = calloc(num, size); +#endif + return ptr; +} + +void * MemoryRealloc(void * p, size_t size) +{ + VERIFY_INITIALIZED(); +#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL + p = heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); +#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) + p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +#else + p = realloc(p, size); +#endif + return p; +} + +void MemoryFree(void * p) +{ + VERIFY_INITIALIZED(); +#ifdef CONFIG_MATTER_MEM_ALLOC_MODE_DEFAULT + free(p); +#else + heap_caps_free(p); +#endif +} + +bool MemoryInternalCheckPointer(const void * p, size_t min_size) +{ + return (p != nullptr); +} + +} // namespace Platform +} // namespace chip From 42ff12fd600449269352592941f72fcaff450914 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sat, 8 Feb 2025 08:02:35 +0000 Subject: [PATCH 2/6] Restyled by clang-format --- examples/platform/esp32/common/CommonDeviceCallbacks.cpp | 1 - src/platform/ESP32/CHIPMem-Platform.cpp | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index 4581b576500df8..d4ad1cc2da6764 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -100,7 +100,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i static_cast(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)), static_cast(heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM)), static_cast(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM))); - } void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) diff --git a/src/platform/ESP32/CHIPMem-Platform.cpp b/src/platform/ESP32/CHIPMem-Platform.cpp index f155d9f7415a55..1a99fa8cc9880d 100644 --- a/src/platform/ESP32/CHIPMem-Platform.cpp +++ b/src/platform/ESP32/CHIPMem-Platform.cpp @@ -29,9 +29,8 @@ #include #include #include -#include #include - +#include namespace chip { namespace Platform { @@ -117,9 +116,9 @@ void * MemoryRealloc(void * p, size_t size) #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL p = heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); #else - p = realloc(p, size); + p = realloc(p, size); #endif return p; } From ffea3112297b29e220fc957b7cbf29e76d300783 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Wed, 12 Feb 2025 14:52:34 +0800 Subject: [PATCH 3/6] review changes --- config/esp32/args.gni | 1 - src/platform/ESP32/CHIPMem-Platform.cpp | 64 +++++-------------------- 2 files changed, 12 insertions(+), 53 deletions(-) diff --git a/config/esp32/args.gni b/config/esp32/args.gni index e52423f288c9d5..33de39cfc513ff 100644 --- a/config/esp32/args.gni +++ b/config/esp32/args.gni @@ -29,7 +29,6 @@ lwip_platform = "external" chip_inet_config_enable_tcp_endpoint = false chip_inet_config_enable_udp_endpoint = true -#Use platform memory management chip_config_memory_management = "platform" custom_toolchain = "//third_party/connectedhomeip/config/esp32/toolchain:esp32" diff --git a/src/platform/ESP32/CHIPMem-Platform.cpp b/src/platform/ESP32/CHIPMem-Platform.cpp index 1a99fa8cc9880d..c91ac9eb6de5d6 100644 --- a/src/platform/ESP32/CHIPMem-Platform.cpp +++ b/src/platform/ESP32/CHIPMem-Platform.cpp @@ -26,106 +26,66 @@ #include #include -#include -#include -#include #include #include namespace chip { namespace Platform { -#define VERIFY_INITIALIZED() VerifyInitialized(__func__) - -static std::atomic_int memoryInitialized{ 0 }; - -static void VerifyInitialized(const char * func) -{ - if (!memoryInitialized) - { - ChipLogError(DeviceLayer, "ABORT: chip::Platform::%s() called before chip::Platform::MemoryInit()", func); - abort(); - } -} - CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize) { - if (memoryInitialized++ > 0) - { - ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryInit() called twice."); - abort(); - } - return CHIP_NO_ERROR; } -void MemoryAllocatorShutdown() -{ - if (--memoryInitialized < 0) - { - ChipLogError(DeviceLayer, "ABORT: chip::Platform::MemoryShutdown() called twice."); - abort(); - } -} +void MemoryAllocatorShutdown() {} void * MemoryAlloc(size_t size) { - void * ptr; - VERIFY_INITIALIZED(); #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL - ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); #else - ptr = malloc(size); + return malloc(size); #endif - return ptr; } void * MemoryAlloc(size_t size, bool isLongTermAlloc) { - void * ptr; - VERIFY_INITIALIZED(); #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL - ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); #else - ptr = malloc(size); + return malloc(size); #endif - return ptr; } void * MemoryCalloc(size_t num, size_t size) { - void * ptr; - VERIFY_INITIALIZED(); #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL - ptr = heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + return heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - ptr = heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + return heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); #else - ptr = calloc(num, size); + return calloc(num, size); #endif - return ptr; } void * MemoryRealloc(void * p, size_t size) { - VERIFY_INITIALIZED(); #ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL p = heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); #elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); #else - p = realloc(p, size); + p = realloc(p, size); #endif return p; } void MemoryFree(void * p) { - VERIFY_INITIALIZED(); #ifdef CONFIG_MATTER_MEM_ALLOC_MODE_DEFAULT free(p); #else From 792b0eff50fdda0f4c2fcee87269661e6be56d26 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Mon, 17 Feb 2025 15:51:43 +0800 Subject: [PATCH 4/6] split to 3 separate files --- config/esp32/components/chip/CMakeLists.txt | 8 ++ src/platform/ESP32/BUILD.gn | 9 ++- .../ESP32/CHIPMem-PlatformDefault.cpp | 73 +++++++++++++++++++ .../ESP32/CHIPMem-PlatformExternal.cpp | 73 +++++++++++++++++++ ...tform.cpp => CHIPMem-PlatformInternal.cpp} | 31 +------- 5 files changed, 163 insertions(+), 31 deletions(-) create mode 100644 src/platform/ESP32/CHIPMem-PlatformDefault.cpp create mode 100644 src/platform/ESP32/CHIPMem-PlatformExternal.cpp rename src/platform/ESP32/{CHIPMem-Platform.cpp => CHIPMem-PlatformInternal.cpp} (64%) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 71dca52bc0320a..f63d8a90387694 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -149,6 +149,14 @@ if(CONFIG_ENABLE_ICD_SERVER) endif() endif() +if(CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL) + chip_gn_arg_append("chip_memory_alloc_mode" "\"internal\"") +elseif(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) + chip_gn_arg_append("chip_memory_alloc_mode" "\"external\"") +elseif(CONFIG_CHIP_MEM_ALLOC_MODE_DEFAULT) + chip_gn_arg_append("chip_memory_alloc_mode" "\"default\"") +endif() + if(CONFIG_ENABLE_PW_RPC) string(APPEND chip_gn_args "import(\"//build_overrides/pigweed.gni\")\n") chip_gn_arg_append("remove_default_configs" "[\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:toolchain_cpp_standard\"]") diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index aa4f7eb41118f0..a1128c54b44c3e 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -34,6 +34,7 @@ declare_args() { chip_use_esp32_ecdsa_peripheral = false chip_enable_ethernet = false chip_enable_route_hook = false + chip_memory_alloc_mode = "default" } defines = [ @@ -83,7 +84,13 @@ static_library("ESP32") { ] if (chip_config_memory_management == "platform") { - sources += [ "CHIPMem-Platform.cpp" ] + if (chip_memory_alloc_mode == "internal") { + sources += [ "CHIPMem-PlatformInternal.cpp" ] + } else if (chip_memory_alloc_mode == "external") { + sources += [ "CHIPMem-PlatformExternal.cpp" ] + } else { + sources += [ "CHIPMem-PlatformDefault.cpp" ] + } } if (chip_enable_ota_requestor) { diff --git a/src/platform/ESP32/CHIPMem-PlatformDefault.cpp b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp new file mode 100644 index 00000000000000..5f8e493b439c56 --- /dev/null +++ b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2025 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file implements heap memory allocation APIs for CHIP. These functions are platform + * specific and might be C Standard Library heap functions re-direction in most of cases. + * + */ + +#include +#include + +#include +#include + +namespace chip { +namespace Platform { + +CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize) +{ + return CHIP_NO_ERROR; +} + +void MemoryAllocatorShutdown() {} + +void * MemoryAlloc(size_t size) +{ + return malloc(size); +} + +void * MemoryAlloc(size_t size, bool isLongTermAlloc) +{ + return malloc(size); +} + +void * MemoryCalloc(size_t num, size_t size) +{ + return calloc(num, size); +} + +void * MemoryRealloc(void * p, size_t size) +{ + return realloc(p, size); +} + +void MemoryFree(void * p) +{ + free(p); +} + +bool MemoryInternalCheckPointer(const void * p, size_t min_size) +{ + return (p != nullptr); +} + +} // namespace Platform +} // namespace chip diff --git a/src/platform/ESP32/CHIPMem-PlatformExternal.cpp b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp new file mode 100644 index 00000000000000..7ff9ee43afed2c --- /dev/null +++ b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2025 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * This file implements heap memory allocation APIs for CHIP. These functions are platform + * specific and might be C Standard Library heap functions re-direction in most of cases. + * + */ + +#include +#include + +#include +#include + +namespace chip { +namespace Platform { + +CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize) +{ + return CHIP_NO_ERROR; +} + +void MemoryAllocatorShutdown() {} + +void * MemoryAlloc(size_t size) +{ + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +} + +void * MemoryAlloc(size_t size, bool isLongTermAlloc) +{ + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +} + +void * MemoryCalloc(size_t num, size_t size) +{ + return heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +} + +void * MemoryRealloc(void * p, size_t size) +{ + return heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); +} + +void MemoryFree(void * p) +{ + heap_caps_free(p); +} + +bool MemoryInternalCheckPointer(const void * p, size_t min_size) +{ + return (p != nullptr); +} + +} // namespace Platform +} // namespace chip diff --git a/src/platform/ESP32/CHIPMem-Platform.cpp b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp similarity index 64% rename from src/platform/ESP32/CHIPMem-Platform.cpp rename to src/platform/ESP32/CHIPMem-PlatformInternal.cpp index c91ac9eb6de5d6..186546942a2792 100644 --- a/src/platform/ESP32/CHIPMem-Platform.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp @@ -41,56 +41,27 @@ void MemoryAllocatorShutdown() {} void * MemoryAlloc(size_t size) { -#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); -#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); -#else - return malloc(size); -#endif } void * MemoryAlloc(size_t size, bool isLongTermAlloc) { -#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); -#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); -#else - return malloc(size); -#endif } void * MemoryCalloc(size_t num, size_t size) { -#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL return heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); -#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - return heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); -#else - return calloc(num, size); -#endif } void * MemoryRealloc(void * p, size_t size) { -#ifdef CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL - p = heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); -#elif defined(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL) - p = heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); -#else - p = realloc(p, size); -#endif - return p; + return heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); } void MemoryFree(void * p) { -#ifdef CONFIG_MATTER_MEM_ALLOC_MODE_DEFAULT - free(p); -#else heap_caps_free(p); -#endif } bool MemoryInternalCheckPointer(const void * p, size_t min_size) From e022f276501f202b9e4df33672c0d0ec6fd9f145 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Mon, 17 Feb 2025 15:53:42 +0800 Subject: [PATCH 5/6] doc changes --- src/platform/ESP32/CHIPMem-PlatformDefault.cpp | 7 ------- src/platform/ESP32/CHIPMem-PlatformExternal.cpp | 7 ------- src/platform/ESP32/CHIPMem-PlatformInternal.cpp | 7 ------- 3 files changed, 21 deletions(-) diff --git a/src/platform/ESP32/CHIPMem-PlatformDefault.cpp b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp index 5f8e493b439c56..3ff9200db6bdf9 100644 --- a/src/platform/ESP32/CHIPMem-PlatformDefault.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp @@ -16,13 +16,6 @@ * limitations under the License. */ -/** - * @file - * This file implements heap memory allocation APIs for CHIP. These functions are platform - * specific and might be C Standard Library heap functions re-direction in most of cases. - * - */ - #include #include diff --git a/src/platform/ESP32/CHIPMem-PlatformExternal.cpp b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp index 7ff9ee43afed2c..ab7dad25b5cf1f 100644 --- a/src/platform/ESP32/CHIPMem-PlatformExternal.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp @@ -16,13 +16,6 @@ * limitations under the License. */ -/** - * @file - * This file implements heap memory allocation APIs for CHIP. These functions are platform - * specific and might be C Standard Library heap functions re-direction in most of cases. - * - */ - #include #include diff --git a/src/platform/ESP32/CHIPMem-PlatformInternal.cpp b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp index 186546942a2792..bdb61e97117297 100644 --- a/src/platform/ESP32/CHIPMem-PlatformInternal.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp @@ -16,13 +16,6 @@ * limitations under the License. */ -/** - * @file - * This file implements heap memory allocation APIs for CHIP. These functions are platform - * specific and might be C Standard Library heap functions re-direction in most of cases. - * - */ - #include #include From 4e632368d9d8b0811a7a4ae2398ddfb23bcff443 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Thu, 6 Mar 2025 11:25:30 +0800 Subject: [PATCH 6/6] add comment on MemoryInternalCheckPointer --- src/platform/ESP32/CHIPMem-PlatformDefault.cpp | 1 + src/platform/ESP32/CHIPMem-PlatformExternal.cpp | 1 + src/platform/ESP32/CHIPMem-PlatformInternal.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/src/platform/ESP32/CHIPMem-PlatformDefault.cpp b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp index 3ff9200db6bdf9..729f9ac87b1f8a 100644 --- a/src/platform/ESP32/CHIPMem-PlatformDefault.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformDefault.cpp @@ -59,6 +59,7 @@ void MemoryFree(void * p) bool MemoryInternalCheckPointer(const void * p, size_t min_size) { + // We don't have a way to know if p is allocated from the heap, just do a null check here. return (p != nullptr); } diff --git a/src/platform/ESP32/CHIPMem-PlatformExternal.cpp b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp index ab7dad25b5cf1f..0e7a9ee1970f49 100644 --- a/src/platform/ESP32/CHIPMem-PlatformExternal.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformExternal.cpp @@ -59,6 +59,7 @@ void MemoryFree(void * p) bool MemoryInternalCheckPointer(const void * p, size_t min_size) { + // We don't have a way to know if p is allocated from the heap, just do a null check here. return (p != nullptr); } diff --git a/src/platform/ESP32/CHIPMem-PlatformInternal.cpp b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp index bdb61e97117297..931841802a4d44 100644 --- a/src/platform/ESP32/CHIPMem-PlatformInternal.cpp +++ b/src/platform/ESP32/CHIPMem-PlatformInternal.cpp @@ -59,6 +59,7 @@ void MemoryFree(void * p) bool MemoryInternalCheckPointer(const void * p, size_t min_size) { + // We don't have a way to know if p is allocated from the heap, just do a null check here. return (p != nullptr); }