From 581ee4323161b9fbbe405e5de8b9293764b1f52f Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Mon, 5 May 2014 19:28:28 +0400 Subject: [PATCH] UAVCAN_IMPLEMENT_PLACEMENT_NEW --- libuavcan/include/uavcan/dynamic_memory.hpp | 2 +- libuavcan/include/uavcan/impl_constants.hpp | 10 +++++++ .../include/uavcan/util/lazy_constructor.hpp | 1 - .../include/uavcan/util/placement_new.hpp | 30 +++++++++++++++++++ .../src/transport/uc_transfer_buffer.cpp | 1 - 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 libuavcan/include/uavcan/util/placement_new.hpp diff --git a/libuavcan/include/uavcan/dynamic_memory.hpp b/libuavcan/include/uavcan/dynamic_memory.hpp index 151a6e20dd..dafe630e7a 100644 --- a/libuavcan/include/uavcan/dynamic_memory.hpp +++ b/libuavcan/include/uavcan/dynamic_memory.hpp @@ -7,9 +7,9 @@ #include #include #include -#include #include #include +#include #include namespace uavcan diff --git a/libuavcan/include/uavcan/impl_constants.hpp b/libuavcan/include/uavcan/impl_constants.hpp index b4074825a2..379ffa1596 100644 --- a/libuavcan/include/uavcan/impl_constants.hpp +++ b/libuavcan/include/uavcan/impl_constants.hpp @@ -82,6 +82,16 @@ # include #endif +/** + * Some C++ implementations are half-broken and do not implement the placement new operator. + * Setting this preprocessor symbol to 1 makes libuavcan implement its own operator new (std::size_t, void*), + * and its delete counterpart. + * This option may be removed in future. + */ +#ifndef UAVCAN_IMPLEMENT_PLACEMENT_NEW +# define UAVCAN_IMPLEMENT_PLACEMENT_NEW 0 +#endif + namespace uavcan { diff --git a/libuavcan/include/uavcan/util/lazy_constructor.hpp b/libuavcan/include/uavcan/util/lazy_constructor.hpp index c91e83f371..bea0b4f7ac 100644 --- a/libuavcan/include/uavcan/util/lazy_constructor.hpp +++ b/libuavcan/include/uavcan/util/lazy_constructor.hpp @@ -5,7 +5,6 @@ #pragma once #include -#include #include #include #include diff --git a/libuavcan/include/uavcan/util/placement_new.hpp b/libuavcan/include/uavcan/util/placement_new.hpp new file mode 100644 index 0000000000..47ef09627b --- /dev/null +++ b/libuavcan/include/uavcan/util/placement_new.hpp @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2014 Pavel Kirienko + */ + +#pragma once + +#include +#include + +#ifndef UAVCAN_IMPLEMENT_PLACEMENT_NEW +# error UAVCAN_IMPLEMENT_PLACEMENT_NEW +#endif + +#if UAVCAN_IMPLEMENT_PLACEMENT_NEW + +inline void* operator new (std::size_t, void* ptr) throw() +{ + return ptr; +} +inline void* operator new[](std::size_t, void* ptr) throw() +{ + return ptr; +} + +inline void operator delete (void*, void*) throw() { } +inline void operator delete[](void*, void*) throw() { } + +#else +# include +#endif diff --git a/libuavcan/src/transport/uc_transfer_buffer.cpp b/libuavcan/src/transport/uc_transfer_buffer.cpp index 7be655be4a..ae5d4b8c45 100644 --- a/libuavcan/src/transport/uc_transfer_buffer.cpp +++ b/libuavcan/src/transport/uc_transfer_buffer.cpp @@ -5,7 +5,6 @@ #include #include #include -#include namespace uavcan {