Commit c465314c authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

wrappers around operator new and operator delete

Summary:
C++14 introduces sized overloads of `operator delete` and C++17 introduces aligned overloads of `operator new` and `operator delete`. Offer wrappers which call the best available versions.

Using these overloads, asking for heap storage suitable for a non-over-aligned class can be done via these wrappers without requiring call-sites to test feature macros.

Most potential use-cases should use `std::allocator<T>::allocate` and `std::allocator<T>::deallocate`. Reasons why use-cases may turn to these wrappers instead are:
* These wrappers are not temnplates and using them does not instantiate class or function templates.
* `std::allocator<T>::deallocate` may, but is not specified to, use sized deallocation when sized deallocation is available.

Reviewed By: luciang

Differential Revision: D33180956

fbshipit-source-id: 944fe6a01ca4cfff72f3b92639158e4a5ac029b8
parent 9a154f6e
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <new>
#include <folly/CppAttributes.h>
#include <folly/Portability.h>
namespace folly {
#if __cpp_aligned_new >= 201606 || _CPPLIB_VER
using std::align_val_t;
#else
enum class align_val_t : std::size_t {};
#endif
// operator_new
struct operator_new_fn {
FOLLY_NODISCARD void* operator()( //
std::size_t const s) const {
return ::operator new(s);
}
FOLLY_NODISCARD void* operator()( //
std::size_t const s,
std::nothrow_t const&) const noexcept {
return ::operator new(s, std::nothrow);
}
FOLLY_NODISCARD void* operator()( //
std::size_t const s,
FOLLY_MAYBE_UNUSED align_val_t const a) const {
#if __cpp_aligned_new >= 201606 || _CPPLIB_VER
return ::operator new(s, a);
#else
return ::operator new(s);
#endif
}
FOLLY_NODISCARD void* operator()( //
std::size_t const s,
FOLLY_MAYBE_UNUSED align_val_t const a,
std::nothrow_t const&) const noexcept {
#if __cpp_aligned_new >= 201606 || _CPPLIB_VER
return ::operator new(s, a, std::nothrow);
#else
return ::operator new(s, std::nothrow);
#endif
}
};
FOLLY_INLINE_VARIABLE constexpr operator_new_fn operator_new{};
// operator_delete
struct operator_delete_fn {
void operator()( //
void* const p) const noexcept {
return ::operator delete(p);
}
void operator()( //
void* const p,
FOLLY_MAYBE_UNUSED std::size_t const s) const noexcept {
#if __cpp_sized_deallocation >= 201309L || _CPPLIB_VER
return ::operator delete(p, s);
#else
return ::operator delete(p);
#endif
}
void operator()( //
void* const p,
FOLLY_MAYBE_UNUSED align_val_t const a) const noexcept {
#if __cpp_aligned_new >= 201606 || _CPPLIB_VER
return ::operator delete(p, a);
#else
return ::operator delete(p);
#endif
}
void operator()( //
void* const p,
FOLLY_MAYBE_UNUSED std::size_t const s,
FOLLY_MAYBE_UNUSED align_val_t const a) const noexcept {
#if __cpp_aligned_new >= 201606 || _CPPLIB_VER
#if __cpp_sized_deallocation >= 201309L || _CPPLIB_VER
return ::operator delete(p, s, a);
#else
return ::operator delete(p, a);
#endif
#else
#if __cpp_sized_deallocation >= 201309L || _CPPLIB_VER
return ::operator delete(p, s);
#else
return ::operator delete(p);
#endif
#endif
}
};
FOLLY_INLINE_VARIABLE constexpr operator_delete_fn operator_delete{};
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/New.h>
#include <folly/lang/Align.h>
#include <folly/portability/GTest.h>
class NewTest : public testing::Test {};
TEST_F(NewTest, operator_new_delete) {
constexpr auto s = std::size_t(256);
constexpr auto a = std::align_val_t(folly::max_align_v);
constexpr auto nt = std::nothrow;
folly::operator_delete(folly::operator_new(s));
folly::operator_delete(folly::operator_new(s), s);
folly::operator_delete(folly::operator_new(s, a), a);
folly::operator_delete(folly::operator_new(s, a), s, a);
folly::operator_delete(folly::operator_new(s, nt));
folly::operator_delete(folly::operator_new(s, nt), s);
folly::operator_delete(folly::operator_new(s, a, nt), a);
folly::operator_delete(folly::operator_new(s, a, nt), s, a);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment