Skip to content

feat(log & memory): split helper functions or macros #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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'
7 changes: 7 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
...
Expand Down Expand Up @@ -234,6 +233,14 @@ void test_check_null_exit(void)
#define ESP_UTILS_LOG_TAG "MyLibrary"
#include "esp_lib_utils.h"

template <typename T, typename... Args>
std::shared_ptr<T> make_shared(Args &&... args)
{
return std::allocate_shared<T, esp_utils::GeneralMemoryAllocator<T>>(
esp_utils::GeneralMemoryAllocator<T>(), std::forward<Args>(args)...
);
}

void test_memory(void)
{
// Allocate memory in C style (`malloc/calloc` and `free` are re-defined in the library)
Expand All @@ -244,7 +251,7 @@ void test_memory(void)
// Allocate memory in C++ style
std::shared_ptr<int> cxx_ptr = nullptr;
ESP_UTILS_CHECK_EXCEPTION_EXIT(
(cxx_ptr = esp_utils::make_shared<int>()), "Failed to allocate memory"
(cxx_ptr = make_shared<int>()), "Failed to allocate memory"
);
cxx_ptr = nullptr;
}
Expand Down
34 changes: 21 additions & 13 deletions esp_utils_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 //////////////////////////////////////////////////
Expand All @@ -31,32 +30,42 @@
* - 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)
* - ESP_UTILS_MEM_ALLOC_TYPE_ESP: Use the ESP-IDF memory allocation functions (heap_caps_aligned_alloc, heap_caps_free)
* - 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)
Expand All @@ -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*
13 changes: 5 additions & 8 deletions src/check/esp_utils_check.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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)
/**
Expand Down Expand Up @@ -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)
/**
Expand Down Expand Up @@ -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; \
} \
Expand All @@ -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; \
} \
Expand All @@ -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; \
} \
Expand All @@ -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 {\
Expand Down Expand Up @@ -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; \
} \
Expand Down
8 changes: 6 additions & 2 deletions src/esp_lib_utils.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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"
24 changes: 20 additions & 4 deletions src/esp_utils_conf_internal.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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*
27 changes: 21 additions & 6 deletions src/esp_utils_conf_kconfig.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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*
12 changes: 12 additions & 0 deletions src/esp_utils_helpers.h
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 0 additions & 3 deletions src/esp_utils_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

/**
* @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 */
#define ESP_UTILS_CHECK_HANDLE_WITH_ASSERT (2) /*!< Assert when check failed */

/**
* @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). */
Expand All @@ -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)
Expand Down
Loading
Loading