From bb333b69e0cfdacd39a5078713cd530d2ad3769b Mon Sep 17 00:00:00 2001 From: Liu Zhongwei Date: Thu, 2 Jan 2025 14:25:35 +0800 Subject: [PATCH] feat(log & memory): split helper functions or macros --- .github/workflows/build_test.yml | 12 ++++-- CHANGELOG.md | 9 ++-- Kconfig | 7 ++++ README.md | 11 ++++- esp_utils_conf.h | 34 +++++++++------ src/check/esp_utils_check.h | 13 +++--- src/esp_lib_utils.h | 8 +++- src/esp_utils_conf_internal.h | 24 +++++++++-- src/esp_utils_conf_kconfig.h | 27 +++++++++--- src/esp_utils_helpers.h | 12 ++++++ src/esp_utils_types.h | 3 -- src/esp_utils_versions.h | 26 +----------- src/log/esp_utils_log.c | 7 ++-- src/log/esp_utils_log.h | 22 +--------- src/log/esp_utils_log_helper.h | 22 ++++++++++ src/memory/esp_utils_mem.c | 27 ++++++++++++ src/memory/esp_utils_mem.h | 42 ++++++++++--------- src/memory/esp_utils_mem_helper.h | 19 +++++++++ test_apps/main/CMakeLists.txt | 25 +++++------ test_apps/main/test_on_c.c | 1 + test_apps/main/test_on_cpp.cpp | 13 +++++- test_apps/sdkconfig.ci.check_none | 1 + test_apps/sdkconfig.ci.cxx_exceptions | 1 - .../sdkconfig.ci.cxx_without_cxx_exceptions | 1 + test_apps/sdkconfig.ci.log_debug | 2 + test_apps/sdkconfig.ci.log_none | 1 + ....ci.custom_mem => sdkconfig.ci.mem_custom} | 0 ...config.ci.esp_mem => sdkconfig.ci.mem_esp} | 0 test_apps/sdkconfig.defaults | 4 +- 29 files changed, 243 insertions(+), 131 deletions(-) create mode 100644 src/esp_utils_helpers.h create mode 100644 src/log/esp_utils_log_helper.h create mode 100644 src/memory/esp_utils_mem_helper.h create mode 100644 test_apps/sdkconfig.ci.check_none delete mode 100644 test_apps/sdkconfig.ci.cxx_exceptions create mode 100644 test_apps/sdkconfig.ci.cxx_without_cxx_exceptions create mode 100644 test_apps/sdkconfig.ci.log_debug create mode 100644 test_apps/sdkconfig.ci.log_none rename test_apps/{sdkconfig.ci.custom_mem => sdkconfig.ci.mem_custom} (100%) rename test_apps/{sdkconfig.ci.esp_mem => sdkconfig.ci.mem_esp} (100%) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index b3f879e..6fc7aaa 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -27,8 +27,14 @@ jobs: export EXTRA_CXXFLAGS="${PEDANTIC_FLAGS}" idf.py build rm -rf sdkconfig build managed_components dependencies.lock - idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions" build + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_without_cxx_exceptions;" build rm -rf sdkconfig build managed_components dependencies.lock - idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions;sdkconfig.ci.esp_mem" build + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.check_none;" build rm -rf sdkconfig build managed_components dependencies.lock - idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions;sdkconfig.ci.custom_mem" build + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.log_debug;" build + rm -rf sdkconfig build managed_components dependencies.lock + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.log_none;" build + rm -rf sdkconfig build managed_components dependencies.lock + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.mem_custom;" build + rm -rf sdkconfig build managed_components dependencies.lock + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.mem_esp;" build diff --git a/CHANGELOG.md b/CHANGELOG.md index 79171b2..ba9e7b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,20 @@ # ChangeLog -## v0.1.2 +## v0.1.2 - 2025-01-23 -### Enhancements: +### Enhancements: * feat(memory): add print memory info +* feat(log & memory): split helper functions and macros ## v0.1.1 -### Enhancements: +### Enhancements: * feat(repo): update config header and Kconfig files ## v0.1.0 -### Enhancements: +### Enhancements: * feat(repo): initialize with modules 'check', 'log' and 'utils' diff --git a/Kconfig b/Kconfig index 5b91d3f..956c00c 100644 --- a/Kconfig +++ b/Kconfig @@ -81,6 +81,13 @@ menu "ESP Library Utils Configurations" menu "Memory functions" depends on ESP_UTILS_CONF_FILE_SKIP + config ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE + bool "Default enable general memory allocation" + default y + help + If enabled, the driver will use general memory allocation by default, otherwise, the driver will use + `malloc` and `free` by default + choice ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_CHOICE prompt "Select general allocation type" default ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_STDLIB diff --git a/README.md b/README.md index 7257c7a..67e74cf 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,6 @@ Since `esp-lib-utils` configures its functionality through the *esp_utils_conf.h * - ESP_UTILS_LOG_LEVEL_WARNING: Error conditions from which recovery measures have been taken * - ESP_UTILS_LOG_LEVEL_ERROR: Critical errors, software module cannot recover on its own * - ESP_UTILS_LOG_LEVEL_NONE: No log output (highest level) (Minimum code size) - * */ #define ESP_UTILS_CONF_LOG_LEVEL (ESP_UTILS_LOG_LEVEL_DEBUG) ... @@ -234,6 +233,14 @@ void test_check_null_exit(void) #define ESP_UTILS_LOG_TAG "MyLibrary" #include "esp_lib_utils.h" +template +std::shared_ptr make_shared(Args &&... args) +{ + return std::allocate_shared>( + esp_utils::GeneralMemoryAllocator(), std::forward(args)... + ); +} + void test_memory(void) { // Allocate memory in C style (`malloc/calloc` and `free` are re-defined in the library) @@ -244,7 +251,7 @@ void test_memory(void) // Allocate memory in C++ style std::shared_ptr cxx_ptr = nullptr; ESP_UTILS_CHECK_EXCEPTION_EXIT( - (cxx_ptr = esp_utils::make_shared()), "Failed to allocate memory" + (cxx_ptr = make_shared()), "Failed to allocate memory" ); cxx_ptr = nullptr; } diff --git a/esp_utils_conf.h b/esp_utils_conf.h index 750551a..7091fb0 100644 --- a/esp_utils_conf.h +++ b/esp_utils_conf.h @@ -16,9 +16,8 @@ * - ESP_UTILS_CHECK_HANDLE_WITH_NONE: Do nothing when check failed (Minimum code size) * - ESP_UTILS_CHECK_HANDLE_WITH_ERROR_LOG: Print error message when check failed (Recommended) * - ESP_UTILS_CHECK_HANDLE_WITH_ASSERT: Assert when check failed - * */ -#define ESP_UTILS_CONF_CHECK_HANDLE_METHOD (ESP_UTILS_CHECK_HANDLE_WITH_ERROR_LOG) +#define ESP_UTILS_CONF_CHECK_HANDLE_METHOD (ESP_UTILS_CHECK_HANDLE_WITH_ERROR_LOG) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////// Log Configurations ////////////////////////////////////////////////// @@ -31,22 +30,33 @@ * - ESP_UTILS_LOG_LEVEL_WARNING: Error conditions from which recovery measures have been taken * - ESP_UTILS_LOG_LEVEL_ERROR: Critical errors, software module cannot recover on its own * - ESP_UTILS_LOG_LEVEL_NONE: No log output (highest level) (Minimum code size) - * */ -#define ESP_UTILS_CONF_LOG_LEVEL (ESP_UTILS_LOG_LEVEL_INFO) +#define ESP_UTILS_CONF_LOG_LEVEL (ESP_UTILS_LOG_LEVEL_INFO) #if ESP_UTILS_CONF_LOG_LEVEL == ESP_UTILS_LOG_LEVEL_DEBUG - /* Set to 1 if print trace log messages when enter/exit functions, useful for debugging */ - #define ESP_UTILS_CONF_ENABLE_LOG_TRACE (0) + /** + * @brief Set to 1 if print trace log messages when enter/exit functions, useful for debugging + */ + #define ESP_UTILS_CONF_ENABLE_LOG_TRACE (0) #endif // ESP_UTILS_CONF_LOG_LEVEL -/* Log format buffer size */ -#define ESP_UTILS_CONF_LOG_BUFFER_SIZE (256) +/** + * @brief Log format buffer size + */ +#define ESP_UTILS_CONF_LOG_BUFFER_SIZE (256) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/** + * Default enable memory allocation. + * + * If enabled, the driver will use general memory allocation by default, otherwise, the driver will use `malloc` and + * `free` by default + */ +#define ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE (1) + /** * General Memory allocation type, choose one of the following: * - ESP_UTILS_MEM_ALLOC_TYPE_STDLIB: Use the standard library memory allocation functions (malloc, free) @@ -54,9 +64,8 @@ * - ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON: Use the MicroPython memory allocation functions (m_malloc, m_free) * - ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM: Use custom memory allocation functions (ESP_UTILS_MEM_ALLOC_CUSTOM_MALLOC, * ESP_UTILS_MEM_ALLOC_CUSTOM_FREE) - * */ -#define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE (ESP_UTILS_MEM_ALLOC_TYPE_STDLIB) +#define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE (ESP_UTILS_MEM_ALLOC_TYPE_STDLIB) #if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP #define ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN (1) @@ -82,10 +91,9 @@ * 2. If the minor version is not consistent, this file might be missing some new configurations, which will be set to * default values. It is recommended to replace it with the file from the library. * 3. Even if the patch version is not consistent, it will not affect normal functionality. - * */ #define ESP_UTILS_CONF_FILE_VERSION_MAJOR 1 -#define ESP_UTILS_CONF_FILE_VERSION_MINOR 0 +#define ESP_UTILS_CONF_FILE_VERSION_MINOR 1 #define ESP_UTILS_CONF_FILE_VERSION_PATCH 0 -// *INDENT-OFF* +// *INDENT-ON* diff --git a/src/check/esp_utils_check.h b/src/check/esp_utils_check.h index 75485cd..86f9325 100644 --- a/src/check/esp_utils_check.h +++ b/src/check/esp_utils_check.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -135,7 +135,6 @@ /** * The `try {} catch {}` block is only available in C++ and `CONFIG_COMPILER_CXX_EXCEPTIONS = 1` - * */ #if defined(__cplusplus) && defined(CONFIG_COMPILER_CXX_EXCEPTIONS) /** @@ -331,7 +330,6 @@ /** * The `try {} catch {}` block is only available in C++ and `CONFIG_COMPILER_CXX_EXCEPTIONS = 1` - * */ #if defined(__cplusplus) && defined(CONFIG_COMPILER_CXX_EXCEPTIONS) /** @@ -393,7 +391,7 @@ #define ESP_UTILS_CHECK_NULL_RETURN(x, ...) assert((x) != NULL) #define ESP_UTILS_CHECK_NULL_GOTO(x, goto_tag, ...) do { \ assert((x) != NULL); \ - /* Aoivd unused tag warning */ \ + /* Avoid unused tag warning */ \ if (0) { \ goto goto_tag; \ } \ @@ -403,7 +401,7 @@ #define ESP_UTILS_CHECK_FALSE_RETURN(x, ...) assert(x) #define ESP_UTILS_CHECK_FALSE_GOTO(x, goto_tag, ...) do { \ assert(x); \ - /* Aoivd unused tag warning */ \ + /* Avoid unused tag warning */ \ if (0) { \ goto goto_tag; \ } \ @@ -413,7 +411,7 @@ #define ESP_UTILS_CHECK_ERROR_RETURN(x, ...) assert((x) == ESP_OK) #define ESP_UTILS_CHECK_ERROR_GOTO(x, goto_tag, ...) do { \ assert((x) == ESP_OK); \ - /* Aoivd unused tag warning */ \ + /* Avoid unused tag warning */ \ if (0) { \ goto goto_tag; \ } \ @@ -422,7 +420,6 @@ /** * The `try {} catch {}` block is only available in C++ and `CONFIG_COMPILER_CXX_EXCEPTIONS = 1` - * */ #if defined(__cplusplus) && defined(CONFIG_COMPILER_CXX_EXCEPTIONS) #define ESP_UTILS_CHECK_EXCEPTION_RETURN(x, ...) do {\ @@ -462,7 +459,7 @@ #ifndef ESP_UTILS_CHECK_EXCEPTION_GOTO #define ESP_UTILS_CHECK_EXCEPTION_GOTO(x, goto_tag, fmt, ...) do { \ (void)x; \ - /* Aoivd unused tag warning */ \ + /* Avoid unused tag warning */ \ if (0) { \ goto goto_tag; \ } \ diff --git a/src/esp_lib_utils.h b/src/esp_lib_utils.h index 2acff9a..994a691 100644 --- a/src/esp_lib_utils.h +++ b/src/esp_lib_utils.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,7 +10,11 @@ #include "esp_utils_versions.h" #include "esp_utils_conf_internal.h" -/* Modules */ +/* Check */ #include "check/esp_utils_check.h" + +/* Log */ #include "log/esp_utils_log.h" + +/* Memory */ #include "memory/esp_utils_mem.h" diff --git a/src/esp_utils_conf_internal.h b/src/esp_utils_conf_internal.h index 78bef16..9d40a48 100644 --- a/src/esp_utils_conf_internal.h +++ b/src/esp_utils_conf_internal.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -57,12 +57,28 @@ #ifndef ESP_UTILS_CONF_INCLUDE_INSIDE /** - * There are two purposes to include the this file: + * There are two purposes to include this file: * 1. Convert configuration items starting with `CONFIG_` to the required configuration items. * 2. Define default values for configuration items that are not defined to keep compatibility. - * */ #include "esp_utils_conf_kconfig.h" #endif -// *INDENT-OFF* +/** + * Check if the current configuration file version is compatible with the library version + */ +#include "esp_utils_versions.h" + +/* File `esp_utils_conf.h` */ +#ifndef ESP_UTILS_CONF_FILE_SKIP + // Check if the current configuration file version is compatible with the library version + #if ESP_UTILS_CONF_FILE_VERSION_MAJOR != ESP_UTILS_CONF_VERSION_MAJOR + #error "The file `esp_utils_conf.h` version is not compatible. Please update it with the file from the library" + #elif ESP_UTILS_CONF_FILE_VERSION_MINOR < ESP_UTILS_CONF_VERSION_MINOR + #warning "The file `esp_utils_conf.h` version is outdated. Some new configurations are missing" + #elif ESP_UTILS_CONF_FILE_VERSION_MINOR > ESP_UTILS_CONF_VERSION_MINOR + #warning "The file `esp_utils_conf.h` version is newer than the library. Some new configurations are not supported" + #endif /* ESP_UTILS_CONF_INCLUDE_INSIDE */ +#endif /* ESP_UTILS_CONF_FILE_SKIP */ + +// *INDENT-ON* diff --git a/src/esp_utils_conf_kconfig.h b/src/esp_utils_conf_kconfig.h index 081dd93..6858ba0 100644 --- a/src/esp_utils_conf_kconfig.h +++ b/src/esp_utils_conf_kconfig.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -49,6 +49,14 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE + #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE + #else + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE (0) + #endif +#endif + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE #define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE @@ -62,27 +70,34 @@ #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN #define ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN #else - #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` \ + is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" #endif #endif #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS - #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is \ + set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" #endif #elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE #define ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE #else - #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE` must be defined when \ + `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" #endif #endif #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC - #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` \ + is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" #endif #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE - #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` \ + is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" #endif #endif + +// *INDENT-ON* diff --git a/src/esp_utils_helpers.h b/src/esp_utils_helpers.h new file mode 100644 index 0000000..5f95999 --- /dev/null +++ b/src/esp_utils_helpers.h @@ -0,0 +1,12 @@ +/* + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +/* Log */ +#include "log/esp_utils_log_helper.h" + +/* Memory */ +#include "memory/esp_utils_mem_helper.h" diff --git a/src/esp_utils_types.h b/src/esp_utils_types.h index 94768bd..feadb6b 100644 --- a/src/esp_utils_types.h +++ b/src/esp_utils_types.h @@ -10,7 +10,6 @@ /** * @brief Macros for check handle method - * */ #define ESP_UTILS_CHECK_HANDLE_WITH_NONE (0) /*!< Do nothing when check failed */ #define ESP_UTILS_CHECK_HANDLE_WITH_ERROR_LOG (1) /*!< Print error message when check failed */ @@ -18,7 +17,6 @@ /** * @brief Macros for log level - * */ #define ESP_UTILS_LOG_LEVEL_DEBUG (0) /*!< Extra information which is not necessary for normal use (values, * pointers, sizes, etc). */ @@ -29,7 +27,6 @@ /** * @brief Macros for memory type - * */ #define ESP_UTILS_MEM_ALLOC_TYPE_STDLIB (0) #define ESP_UTILS_MEM_ALLOC_TYPE_ESP (1) diff --git a/src/esp_utils_versions.h b/src/esp_utils_versions.h index 347e15e..8a20559 100644 --- a/src/esp_utils_versions.h +++ b/src/esp_utils_versions.h @@ -1,12 +1,10 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #pragma once -#include "esp_utils_conf_internal.h" - /* Library Version */ #define ESP_UTILS_VERSION_MAJOR 0 #define ESP_UTILS_VERSION_MINOR 1 @@ -14,25 +12,5 @@ /* File `esp_utils_conf.h` */ #define ESP_UTILS_CONF_VERSION_MAJOR 1 -#define ESP_UTILS_CONF_VERSION_MINOR 0 +#define ESP_UTILS_CONF_VERSION_MINOR 1 #define ESP_UTILS_CONF_VERSION_PATCH 0 - -// *INDENT-OFF* - -/** - * Check if the current configuration file version is compatible with the library version - * - */ -/* File `esp_utils_conf.h` */ -#ifndef ESP_UTILS_CONF_FILE_SKIP - // Check if the current configuration file version is compatible with the library version - #if ESP_UTILS_CONF_FILE_VERSION_MAJOR != ESP_UTILS_CONF_VERSION_MAJOR - #error "The file `esp_utils_conf.h` version is not compatible. Please update it with the file from the library" - #elif ESP_UTILS_CONF_FILE_VERSION_MINOR < ESP_UTILS_CONF_VERSION_MINOR - #warning "The file `esp_utils_conf.h` version is outdated. Some new configurations are missing" - #elif ESP_UTILS_CONF_FILE_VERSION_PATCH > ESP_UTILS_VERSION_PATCH - #warning "The file `esp_utils_conf.h` version is newer than the library. Some new configurations are not supported" - #endif /* ESP_UTILS_CONF_INCLUDE_INSIDE */ -#endif /* ESP_UTILS_CONF_FILE_SKIP */ - -// *INDENT-OFF* diff --git a/src/log/esp_utils_log.c b/src/log/esp_utils_log.c index 2b82b50..b300665 100644 --- a/src/log/esp_utils_log.c +++ b/src/log/esp_utils_log.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,12 +7,11 @@ #include "esp_utils_conf_internal.h" /** - * @brief Extract filename from file path + * @brief Extract filename from file path * - * @param file_path File path + * @param[in] file_path File path * * @return File name - * */ const char *esp_utils_log_extract_file_name(const char *file_path) { diff --git a/src/log/esp_utils_log.h b/src/log/esp_utils_log.h index 8e358f6..e742a77 100644 --- a/src/log/esp_utils_log.h +++ b/src/log/esp_utils_log.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -25,7 +25,6 @@ namespace esp_utils { /** * Class to handle logging - * */ class Log { public: @@ -81,7 +80,6 @@ class Log { /** * Macros to simplify logging calls - * */ #define ESP_UTILS_LOGD(format, ...) \ esp_utils::Log::getInstance().print(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__) @@ -94,7 +92,6 @@ class Log { /** * Micros to log trace of function calls - * */ #if ESP_UTILS_CONF_ENABLE_LOG_TRACE #define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() ESP_UTILS_LOGD("(@%p) Enter", this) @@ -134,7 +131,6 @@ const char *esp_utils_log_extract_file_name(const char *file_path); /** * Macros to simplify logging calls - * */ #define ESP_UTILS_LOGD(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_DEBUG, format, ##__VA_ARGS__) #define ESP_UTILS_LOGI(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_INFO, format, ##__VA_ARGS__) @@ -145,7 +141,6 @@ const char *esp_utils_log_extract_file_name(const char *file_path); /** * Micros to log trace of function calls - * */ #if ESP_UTILS_CONF_ENABLE_LOG_TRACE #define ESP_UTILS_LOG_TRACE_ENTER() ESP_UTILS_LOGD("Enter") @@ -154,18 +149,3 @@ const char *esp_utils_log_extract_file_name(const char *file_path); #define ESP_UTILS_LOG_TRACE_ENTER() #define ESP_UTILS_LOG_TRACE_EXIT() #endif - -/** - * Macros to replace ESP-IDF logging functions - * - */ -#undef ESP_LOGV -#undef ESP_LOGD -#undef ESP_LOGI -#undef ESP_LOGW -#undef ESP_LOGE -#define ESP_LOGV(TAG, ...) { (void)TAG; ESP_UTILS_LOGD(__VA_ARGS__); } -#define ESP_LOGD(TAG, ...) { (void)TAG; ESP_UTILS_LOGD(__VA_ARGS__); } -#define ESP_LOGI(TAG, ...) { (void)TAG; ESP_UTILS_LOGI(__VA_ARGS__); } -#define ESP_LOGW(TAG, ...) { (void)TAG; ESP_UTILS_LOGW(__VA_ARGS__); } -#define ESP_LOGE(TAG, ...) { (void)TAG; ESP_UTILS_LOGE(__VA_ARGS__); } diff --git a/src/log/esp_utils_log_helper.h b/src/log/esp_utils_log_helper.h new file mode 100644 index 0000000..e0f2364 --- /dev/null +++ b/src/log/esp_utils_log_helper.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include "esp_utils_log.h" + +/** + * Helper macros to replace ESP-IDF logging functions + */ +#undef ESP_LOGV +#undef ESP_LOGD +#undef ESP_LOGI +#undef ESP_LOGW +#undef ESP_LOGE +#define ESP_LOGV(TAG, ...) { (void)TAG; ESP_UTILS_LOGD(__VA_ARGS__); } +#define ESP_LOGD(TAG, ...) { (void)TAG; ESP_UTILS_LOGD(__VA_ARGS__); } +#define ESP_LOGI(TAG, ...) { (void)TAG; ESP_UTILS_LOGI(__VA_ARGS__); } +#define ESP_LOGW(TAG, ...) { (void)TAG; ESP_UTILS_LOGW(__VA_ARGS__); } +#define ESP_LOGE(TAG, ...) { (void)TAG; ESP_UTILS_LOGE(__VA_ARGS__); } diff --git a/src/memory/esp_utils_mem.c b/src/memory/esp_utils_mem.c index e865582..1edab70 100644 --- a/src/memory/esp_utils_mem.c +++ b/src/memory/esp_utils_mem.c @@ -22,11 +22,29 @@ #define PRINT_INFO_BUFFER_SIZE 256 +static bool is_alloc_enabled = ESP_UTILS_CONF_MEM_GEN_ALLOC_DEFAULT_ENABLE; + +void esp_utils_mem_gen_enable_alloc(bool enable) +{ + is_alloc_enabled = enable; +} + +bool esp_utils_mem_check_alloc_enabled(void) +{ + return is_alloc_enabled; +} + void *esp_utils_mem_gen_malloc(size_t size) { ESP_UTILS_LOG_TRACE_ENTER(); void *p = NULL; + + if (!is_alloc_enabled) { + p = malloc(size); + goto end; + } + #if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB p = malloc(size); #elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP @@ -40,6 +58,8 @@ void *esp_utils_mem_gen_malloc(size_t size) p = m_malloc(size); #endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE #endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + +end: ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size); ESP_UTILS_LOG_TRACE_EXIT(); @@ -52,6 +72,12 @@ void esp_utils_mem_gen_free(void *p) ESP_UTILS_LOG_TRACE_ENTER(); ESP_UTILS_LOGD("Free @%p", p); + + if (!is_alloc_enabled) { + free(p); + goto end; + } + #if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB free(p); #elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP @@ -66,6 +92,7 @@ void esp_utils_mem_gen_free(void *p) #endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE #endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE +end: ESP_UTILS_LOG_TRACE_EXIT(); } diff --git a/src/memory/esp_utils_mem.h b/src/memory/esp_utils_mem.h index 09c3a34..999a936 100644 --- a/src/memory/esp_utils_mem.h +++ b/src/memory/esp_utils_mem.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -26,6 +26,9 @@ bool esp_utils_mem_print_info(void); #ifdef __cplusplus #include +#include +#include +#include namespace esp_utils { @@ -38,6 +41,11 @@ struct GeneralMemoryAllocator { template GeneralMemoryAllocator(const GeneralMemoryAllocator &) {} + bool operator!=(const GeneralMemoryAllocator &other) const + { + return false; + } + T *allocate(std::size_t n) { if (n == 0) { @@ -70,29 +78,25 @@ struct GeneralMemoryAllocator { { p->~U(); } + + template + struct rebind { + using other = GeneralMemoryAllocator; + }; }; -/** - * @brief Helper function to create a shared pointer using the memory allocator - * - */ -template -std::shared_ptr make_shared(Args &&... args) +template +bool operator==(const GeneralMemoryAllocator &, const GeneralMemoryAllocator &) { - return std::allocate_shared>(GeneralMemoryAllocator(), std::forward(args)...); + return true; +} + +template +bool operator!=(const GeneralMemoryAllocator &, const GeneralMemoryAllocator &) +{ + return false; } } // namespace esp_utils #endif // __cplusplus - -/** - * @brief Helper functions to allocate memory using the memory allocator - * - */ -#undef malloc -#undef free -#undef calloc -#define malloc(size) esp_utils_mem_gen_malloc(size) -#define free(p) esp_utils_mem_gen_free(p) -#define calloc(n, size) esp_utils_mem_gen_calloc(n, size) diff --git a/src/memory/esp_utils_mem_helper.h b/src/memory/esp_utils_mem_helper.h new file mode 100644 index 0000000..955c048 --- /dev/null +++ b/src/memory/esp_utils_mem_helper.h @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include "esp_utils_mem.h" + +/** + * C helper functions to allocate memory using the memory allocator + */ + +#undef malloc +#undef free +#undef calloc +#define malloc(size) esp_utils_mem_gen_malloc(size) +#define free(p) esp_utils_mem_gen_free(p) +#define calloc(n, size) esp_utils_mem_gen_calloc(n, size) diff --git a/test_apps/main/CMakeLists.txt b/test_apps/main/CMakeLists.txt index 5634a76..c669e28 100644 --- a/test_apps/main/CMakeLists.txt +++ b/test_apps/main/CMakeLists.txt @@ -3,19 +3,20 @@ idf_component_register( WHOLE_ARCHIVE ) - idf_build_get_property(build_components BUILD_COMPONENTS) - foreach(COMPONENT ${build_components}) - if(COMPONENT MATCHES "esp-lib-utils" OR COMPONENT MATCHES "espressif__esp-lib-utils") - set(TARGET_COMPONENT ${COMPONENT}) - break() - endif() - endforeach() - - if(TARGET_COMPONENT STREQUAL "") - message(FATAL_ERROR "Component 'esp-lib-utils' not found.") - else() - idf_component_get_property(ESP_LIB_UTILS_LIB ${TARGET_COMPONENT} COMPONENT_LIB) +idf_build_get_property(build_components BUILD_COMPONENTS) +foreach(COMPONENT ${build_components}) + if(COMPONENT MATCHES "esp-lib-utils" OR COMPONENT MATCHES "espressif__esp-lib-utils") + set(TARGET_COMPONENT ${COMPONENT}) + break() endif() +endforeach() + +if(TARGET_COMPONENT STREQUAL "") + message(FATAL_ERROR "Component 'esp-lib-utils' not found.") +else() + idf_component_get_property(ESP_LIB_UTILS_LIB ${TARGET_COMPONENT} COMPONENT_LIB) + set(TARGET_COMPONENT "") +endif() if(CONFIG_ESP_UTILS_CONF_MEM_GENERAL_ALLOC_TYPE_ESP) target_compile_options( diff --git a/test_apps/main/test_on_c.c b/test_apps/main/test_on_c.c index e09483e..8d4b973 100644 --- a/test_apps/main/test_on_c.c +++ b/test_apps/main/test_on_c.c @@ -6,6 +6,7 @@ #include "unity.h" #define ESP_UTILS_LOG_TAG "TestC" #include "esp_lib_utils.h" +#include "esp_utils_helpers.h" TEST_CASE("Test log functions on C", "[utils][log][C]") { diff --git a/test_apps/main/test_on_cpp.cpp b/test_apps/main/test_on_cpp.cpp index 67ba6a3..c84c6e2 100644 --- a/test_apps/main/test_on_cpp.cpp +++ b/test_apps/main/test_on_cpp.cpp @@ -7,6 +7,7 @@ #include "unity.h" #define ESP_UTILS_LOG_TAG "TestCpp" #include "esp_lib_utils.h" +#include "esp_utils_helpers.h" TEST_CASE("Test log functions on cpp", "[utils][log][CPP]") { @@ -23,6 +24,14 @@ TEST_CASE("Test log functions on cpp", "[utils][log][CPP]") #define MALLOC_GOOD_SIZE (1 * 1024) #define MALLOC_BAD_SIZE (1 * 1024 * 1024) +template +std::shared_ptr make_shared(Args &&... args) +{ + return std::allocate_shared>( + esp_utils::GeneralMemoryAllocator(), std::forward(args)... + ); +} + template class TestClass { public: @@ -38,7 +47,7 @@ TEST_CASE("Test memory functions on cpp", "[utils][memory][CPP]") std::shared_ptr good_ptr = nullptr; ESP_UTILS_CHECK_EXCEPTION_GOTO( - (good_ptr = esp_utils::make_shared()), err, "Failed to allocate memory size: %d", + (good_ptr = make_shared()), err, "Failed to allocate memory size: %d", MALLOC_GOOD_SIZE ); ESP_UTILS_LOGI("Malloced value: %d", good_ptr->buffer[0]); @@ -46,7 +55,7 @@ TEST_CASE("Test memory functions on cpp", "[utils][memory][CPP]") { std::shared_ptr bad_ptr = nullptr; ESP_UTILS_CHECK_EXCEPTION_GOTO( - (bad_ptr = esp_utils::make_shared()), end, "Failed to allocate memory size: %d", MALLOC_BAD_SIZE + (bad_ptr = make_shared()), end, "Failed to allocate memory size: %d", MALLOC_BAD_SIZE ); ESP_UTILS_LOGI("Malloced value: %d", bad_ptr->buffer[0]); } diff --git a/test_apps/sdkconfig.ci.check_none b/test_apps/sdkconfig.ci.check_none new file mode 100644 index 0000000..2641748 --- /dev/null +++ b/test_apps/sdkconfig.ci.check_none @@ -0,0 +1 @@ +CONFIG_ESP_UTILS_CHECK_HANDLE_WITH_NONE=y diff --git a/test_apps/sdkconfig.ci.cxx_exceptions b/test_apps/sdkconfig.ci.cxx_exceptions deleted file mode 100644 index 75ebeae..0000000 --- a/test_apps/sdkconfig.ci.cxx_exceptions +++ /dev/null @@ -1 +0,0 @@ -CONFIG_COMPILER_CXX_EXCEPTIONS=y diff --git a/test_apps/sdkconfig.ci.cxx_without_cxx_exceptions b/test_apps/sdkconfig.ci.cxx_without_cxx_exceptions new file mode 100644 index 0000000..574719b --- /dev/null +++ b/test_apps/sdkconfig.ci.cxx_without_cxx_exceptions @@ -0,0 +1 @@ +CONFIG_COMPILER_CXX_EXCEPTIONS=n diff --git a/test_apps/sdkconfig.ci.log_debug b/test_apps/sdkconfig.ci.log_debug new file mode 100644 index 0000000..191f9c8 --- /dev/null +++ b/test_apps/sdkconfig.ci.log_debug @@ -0,0 +1,2 @@ +CONFIG_ESP_UTILS_CONF_LOG_LEVEL_DEBUG=y +CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE=y diff --git a/test_apps/sdkconfig.ci.log_none b/test_apps/sdkconfig.ci.log_none new file mode 100644 index 0000000..c50df2d --- /dev/null +++ b/test_apps/sdkconfig.ci.log_none @@ -0,0 +1 @@ +CONFIG_ESP_UTILS_CONF_LOG_LEVEL_NONE=y diff --git a/test_apps/sdkconfig.ci.custom_mem b/test_apps/sdkconfig.ci.mem_custom similarity index 100% rename from test_apps/sdkconfig.ci.custom_mem rename to test_apps/sdkconfig.ci.mem_custom diff --git a/test_apps/sdkconfig.ci.esp_mem b/test_apps/sdkconfig.ci.mem_esp similarity index 100% rename from test_apps/sdkconfig.ci.esp_mem rename to test_apps/sdkconfig.ci.mem_esp diff --git a/test_apps/sdkconfig.defaults b/test_apps/sdkconfig.defaults index 0f0ddae..0495c8f 100644 --- a/test_apps/sdkconfig.defaults +++ b/test_apps/sdkconfig.defaults @@ -1,5 +1,3 @@ CONFIG_ESP_TASK_WDT_EN=n CONFIG_FREERTOS_HZ=1000 - -CONFIG_ESP_UTILS_CONF_LOG_LEVEL_DEBUG=y -CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE=y +CONFIG_COMPILER_CXX_EXCEPTIONS=y