Skip to content

Commit fc99e53

Browse files
committed
cpp: Use malloc/free instead of kernel variants in new/delete
Use malloc/free instead of k_malloc/k_free in operator new/delete implementation or use libstdc++ implementation when available. Further updated cpp_synchronization sample to enable minimal libc heap as virtual destructor requires operator delete which depends on free. Signed-off-by: Jan Van Winkel <[email protected]>
1 parent 86461ac commit fc99e53

File tree

7 files changed

+11
-104
lines changed

7 files changed

+11
-104
lines changed

samples/cpp_synchronization/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CONFIG_CPLUSPLUS=y
2+
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128

subsys/cpp/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ zephyr_sources(
66
cpp_dtors.c
77
)
88

9-
if (NOT CONFIG_LIB_CPLUSPLUS OR CONFIG_ZEPHYR_CPLUSPLUS)
9+
if (NOT CONFIG_LIB_CPLUSPLUS AND
10+
(NOT CONFIG_MINIMAL_LIBC OR
11+
(CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE GREATER 0)))
1012
zephyr_sources(
1113
cpp_virtual.c
1214
cpp_vtable.cpp

subsys/cpp/Kconfig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ config RTTI
6363
help
6464
This option enables support of C++ RTTI.
6565

66-
67-
config ZEPHYR_CPLUSPLUS
68-
bool "Use Zephyr C++ Implementation"
69-
help
70-
Use Zephyr implementation for operator new, delete, pure virtual
71-
functions and vtables.
72-
7366
endif # LIB_CPLUSPLUS
7467

7568
endif # ! MINIMAL_LIBC

subsys/cpp/cpp_new.cpp

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,115 +4,36 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#if defined(CONFIG_LIB_CPLUSPLUS)
8-
#include <new>
9-
#endif // CONFIG_LIB_CPLUSPLUS
10-
#include <kernel.h>
7+
#include <stdlib.h>
118

129
void* operator new(size_t size)
1310
{
14-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
15-
void* ptr = k_malloc(size);
16-
#if defined(__cpp_exceptions) && defined(CONFIG_LIB_CPLUSPLUS)
17-
if (!ptr)
18-
throw std::bad_alloc();
19-
#endif
20-
return ptr;
21-
#else
22-
ARG_UNUSED(size);
23-
return NULL;
24-
#endif
11+
return malloc(size);
2512
}
2613

2714
void* operator new[](size_t size)
2815
{
29-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
30-
void* ptr = k_malloc(size);
31-
#if defined(__cpp_exceptions) && defined(CONFIG_LIB_CPLUSPLUS)
32-
if (!ptr)
33-
throw std::bad_alloc();
34-
#endif
35-
return ptr;
36-
#else
37-
ARG_UNUSED(size);
38-
return NULL;
39-
#endif
16+
return malloc(size);
4017
}
4118

4219
void operator delete(void* ptr) noexcept
4320
{
44-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
45-
k_free(ptr);
46-
#else
47-
ARG_UNUSED(ptr);
48-
#endif
21+
free(ptr);
4922
}
5023

5124
void operator delete[](void* ptr) noexcept
5225
{
53-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
54-
k_free(ptr);
55-
#else
56-
ARG_UNUSED(ptr);
57-
#endif
26+
free(ptr);
5827
}
5928

60-
#if defined(CONFIG_LIB_CPLUSPLUS)
61-
void* operator new(size_t size, const std::nothrow_t&) noexcept
62-
{
63-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
64-
return k_malloc(size);
65-
#else
66-
ARG_UNUSED(size);
67-
return NULL;
68-
#endif
69-
}
70-
71-
void* operator new[](size_t size, const std::nothrow_t&) noexcept
72-
{
73-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
74-
return k_malloc(size);
75-
#else
76-
ARG_UNUSED(size);
77-
return NULL;
78-
#endif
79-
}
80-
81-
void operator delete(void* ptr, const std::nothrow_t&) noexcept
82-
{
83-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
84-
k_free(ptr);
85-
#else
86-
ARG_UNUSED(ptr);
87-
#endif
88-
}
89-
90-
void operator delete[](void* ptr, const std::nothrow_t&) noexcept
91-
{
92-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
93-
k_free(ptr);
94-
#else
95-
ARG_UNUSED(ptr);
96-
#endif
97-
}
98-
#endif // CONFIG_LIB_CPLUSPLUS
99-
10029
#if (__cplusplus > 201103L)
10130
void operator delete(void* ptr, size_t) noexcept
10231
{
103-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
104-
k_free(ptr);
105-
#else
106-
ARG_UNUSED(ptr);
107-
#endif
32+
free(ptr);
10833
}
10934

11035
void operator delete[](void* ptr, size_t) noexcept
11136
{
112-
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
113-
k_free(ptr);
114-
#else
115-
ARG_UNUSED(ptr);
116-
#endif
37+
free(ptr);
11738
}
11839
#endif // __cplusplus > 201103L

subsys/cpp/cpp_vtable.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* @brief basic virtual tables required for classes to build
1515
*
1616
*/
17-
#if !defined(CONFIG_LIB_CPLUSPLUS)
18-
1917
namespace __cxxabiv1 {
2018
class __class_type_info {
2119
virtual void dummy();
@@ -26,4 +24,3 @@ namespace __cxxabiv1 {
2624
void __class_type_info::dummy() { } // causes the vtable to get created here
2725
void __si_class_type_info::dummy() { } // causes the vtable to get created here
2826
};
29-
#endif // !defined(CONFIG_LIB_CPLUSPLUS)

tests/application_development/libcxx/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ CONFIG_NEWLIB_LIBC=y
22
CONFIG_CPLUSPLUS=y
33
CONFIG_LIB_CPLUSPLUS=y
44
CONFIG_STD_CPP17=y
5-
CONFIG_HEAP_MEM_POOL_SIZE=1024
65
CONFIG_ZTEST=y
76
CONFIG_ZTEST_STACKSIZE=2048

tests/application_development/libcxx/testcase.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ tests:
44
platform_exclude: qemu_x86_coverage
55
min_flash: 54
66
tags: cpp
7-
misc.app_dev.libcxx.zephyr_cpp:
8-
arch_exclude: posix
9-
platform_exclude: qemu_x86_coverage
10-
tags: cpp
11-
extra_configs:
12-
- CONFIG_ZEPHYR_CPLUSPLUS=y
137
misc.app_dev.libcxx.exceptions:
148
arch_exclude: posix
159
platform_exclude: qemu_x86_coverage

0 commit comments

Comments
 (0)