Skip to content

Commit 4ff22ab

Browse files
committed
feat(memory): add global allocate functions
1 parent bb9ea3d commit 4ff22ab

File tree

5 files changed

+99
-16
lines changed

5 files changed

+99
-16
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
set(SRC_DIR ./src)
2-
file(GLOB_RECURSE SRCS ${SRC_DIR}/*.c)
2+
file(GLOB_RECURSE SRCS_C ${SRC_DIR}/*.c)
3+
file(GLOB_RECURSE SRCS_CPP ${SRC_DIR}/*.cpp)
34

45
idf_component_register(
5-
SRCS ${SRCS}
6+
SRCS ${SRCS_C} ${SRCS_CPP}
67
INCLUDE_DIRS ${SRC_DIR}
78
)
89

src/memory/esp_utils_mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void *esp_utils_mem_gen_malloc(size_t size)
6060
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE
6161

6262
end:
63-
ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size);
63+
// ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size);
6464

6565
ESP_UTILS_LOG_TRACE_EXIT();
6666

@@ -71,7 +71,7 @@ void esp_utils_mem_gen_free(void *p)
7171
{
7272
ESP_UTILS_LOG_TRACE_ENTER();
7373

74-
ESP_UTILS_LOGD("Free @%p", p);
74+
// ESP_UTILS_LOGD("Free @%p", p);
7575

7676
if (!is_alloc_enabled) {
7777
free(p);

src/memory/esp_utils_mem.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <new>
7+
#include "log/esp_utils_log.h"
8+
#include "esp_utils_mem.h"
9+
10+
void *operator new (std::size_t size)
11+
{
12+
ESP_UTILS_LOG_TRACE_ENTER();
13+
14+
// ESP_UTILS_LOGD("Param: size: %zu", size);
15+
void *ptr = esp_utils_mem_gen_malloc(size);
16+
#if CONFIG_COMPILER_CXX_EXCEPTIONS
17+
if (!ptr) {
18+
throw std::bad_alloc();
19+
}
20+
#endif
21+
22+
ESP_UTILS_LOG_TRACE_EXIT();
23+
24+
return ptr;
25+
}
26+
27+
void *operator new[](std::size_t size)
28+
{
29+
ESP_UTILS_LOG_TRACE_ENTER();
30+
31+
// ESP_UTILS_LOGD("Param: size: %zu", size);
32+
void *ptr = esp_utils_mem_gen_malloc(size);
33+
#if CONFIG_COMPILER_CXX_EXCEPTIONS
34+
if (!ptr) {
35+
throw std::bad_alloc();
36+
}
37+
#endif
38+
39+
ESP_UTILS_LOG_TRACE_EXIT();
40+
41+
return ptr;
42+
}
43+
44+
void operator delete (void *ptr) noexcept
45+
{
46+
ESP_UTILS_LOG_TRACE_ENTER();
47+
48+
// ESP_UTILS_LOGD("Param: ptr: %p", ptr);
49+
esp_utils_mem_gen_free(ptr);
50+
51+
ESP_UTILS_LOG_TRACE_EXIT();
52+
}
53+
54+
void operator delete[](void *ptr) noexcept
55+
{
56+
ESP_UTILS_LOG_TRACE_ENTER();
57+
58+
// ESP_UTILS_LOGD("Param: ptr: %p", ptr);
59+
esp_utils_mem_gen_free(ptr);
60+
61+
ESP_UTILS_LOG_TRACE_EXIT();
62+
}
63+
64+
void operator delete (void *ptr, std::size_t size) noexcept
65+
{
66+
ESP_UTILS_LOG_TRACE_ENTER();
67+
68+
// ESP_UTILS_LOGD("Param: ptr: %p, size: %zu", ptr, size);
69+
esp_utils_mem_gen_free(ptr);
70+
71+
ESP_UTILS_LOG_TRACE_EXIT();
72+
}
73+
74+
void operator delete[](void *ptr, std::size_t size) noexcept
75+
{
76+
ESP_UTILS_LOG_TRACE_ENTER();
77+
78+
// ESP_UTILS_LOGD("Param: ptr: %p, size: %zu", ptr, size);
79+
esp_utils_mem_gen_free(ptr);
80+
81+
ESP_UTILS_LOG_TRACE_EXIT();
82+
}

src/memory/esp_utils_mem.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ struct GeneralMemoryAllocator {
5353
return nullptr;
5454
}
5555
void *ptr = esp_utils_mem_gen_malloc(n * sizeof(T));
56-
if (ptr == nullptr) {
5756
#if CONFIG_COMPILER_CXX_EXCEPTIONS
57+
if (ptr == nullptr) {
5858
throw std::bad_alloc();
59-
#else
60-
abort();
61-
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS
6259
}
60+
#endif
6361
return static_cast<T *>(ptr);
6462
}
6563

test_apps/main/test_on_cpp.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/
@@ -9,6 +9,8 @@
99
#include "esp_lib_utils.h"
1010
#include "esp_utils_helpers.h"
1111

12+
using namespace std;
13+
1214
TEST_CASE("Test log functions on cpp", "[utils][log][CPP]")
1315
{
1416
ESP_UTILS_LOG_TRACE_ENTER();
@@ -24,13 +26,13 @@ TEST_CASE("Test log functions on cpp", "[utils][log][CPP]")
2426
#define MALLOC_GOOD_SIZE (1 * 1024)
2527
#define MALLOC_BAD_SIZE (1 * 1024 * 1024)
2628

27-
template <typename T, typename... Args>
28-
std::shared_ptr<T> make_shared(Args &&... args)
29-
{
30-
return std::allocate_shared<T, esp_utils::GeneralMemoryAllocator<T>>(
31-
esp_utils::GeneralMemoryAllocator<T>(), std::forward<Args>(args)...
32-
);
33-
}
29+
// template <typename T, typename... Args>
30+
// std::shared_ptr<T> make_shared(Args &&... args)
31+
// {
32+
// return std::allocate_shared<T, esp_utils::GeneralMemoryAllocator<T>>(
33+
// esp_utils::GeneralMemoryAllocator<T>(), std::forward<Args>(args)...
34+
// );
35+
// }
3436

3537
template <int N>
3638
class TestClass {

0 commit comments

Comments
 (0)