mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys/new_delete: add malloc/free based new/delete implementation
On some platforms `libstdc++` is not used or not available, like on the AVR. Such platforms can use this module to implement the C++ `new` and `delete` operators using `malloc` and `free` respectively. However, to be thread-safe, a thread-safe implementation of `malloc` and `free` must be present.
This commit is contained in:
parent
0d2ba58b89
commit
4bfd549301
@ -19,6 +19,7 @@ rsource "checksum/Kconfig"
|
||||
rsource "color/Kconfig"
|
||||
rsource "crypto/Kconfig"
|
||||
rsource "congure/Kconfig"
|
||||
rsource "cpp_new_delete/Kconfig"
|
||||
rsource "cxx_ctor_guards/Kconfig"
|
||||
rsource "div/Kconfig"
|
||||
rsource "embunit/Kconfig"
|
||||
|
17
sys/cpp_new_delete/Kconfig
Normal file
17
sys/cpp_new_delete/Kconfig
Normal file
@ -0,0 +1,17 @@
|
||||
# Copyright (C) 2021 Gunar Schorcht
|
||||
#
|
||||
# This file is subject to the terms and conditions of the GNU Lesser
|
||||
# General Public License v2.1. See the file LICENSE in the top level
|
||||
# directory for more details.
|
||||
#
|
||||
|
||||
config MODULE_CPP_NEW_DELETE
|
||||
bool
|
||||
depends on TEST_KCONFIG
|
||||
depends on MODULE_CPP
|
||||
help
|
||||
On some platforms libstdc++ is not used or not available, like on
|
||||
the AVR. Such platforms can use this module to implement the C++
|
||||
new and delete operators using malloc and free respectively. However,
|
||||
to be thread-safe, a thread-safe implementation of malloc and free
|
||||
must be present.
|
3
sys/cpp_new_delete/Makefile
Normal file
3
sys/cpp_new_delete/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
|
||||
CXXFLAGS += -std=c++11
|
22
sys/cpp_new_delete/doc.txt
Normal file
22
sys/cpp_new_delete/doc.txt
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
@defgroup sys_cpp_new_delete C++ new and delete operator module
|
||||
@ingroup sys
|
||||
@brief This module provides the `new` and `delete` operators for platforms
|
||||
that do not use `libstdc++`.
|
||||
@warning This module is automatically selected, if needed. Never add it
|
||||
manually.
|
||||
|
||||
# Background
|
||||
|
||||
On some platforms `libstdc++` is not used or not available, like on the AVR.
|
||||
Such platforms can use this module to implement the C++ `new` and `delete`
|
||||
operators using `malloc` and `free` respectively. However, to be thread-safe,
|
||||
a thread-safe implementation of `malloc` and `free` must be present.
|
||||
|
||||
# Usage
|
||||
|
||||
This module is intended to be use by platforms that not providing the required
|
||||
operators. Hence, application developers and users should never select this
|
||||
module by hand.
|
||||
|
||||
*/
|
40
sys/cpp_new_delete/new_delete.cpp
Normal file
40
sys/cpp_new_delete/new_delete.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Arduino. All right reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void *operator new(size_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void *operator new[](size_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void *operator new(size_t size, void *ptr) noexcept {
|
||||
(void)size;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void operator delete(void *ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void operator delete[](void *ptr) {
|
||||
free(ptr);
|
||||
}
|
Loading…
Reference in New Issue
Block a user