Commit 9ae803c7 authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

checkedMalloc/free substitutes that performs sized deallocation

Summary:
Sized deallocation makes it faster for jemalloc to locate an
allocation's metadata.  Accessing this functionality via ::operator
delete(void*,size_t) is both portable and a bit more direct than
calling sdallocx after a dynamic jemalloc check.  This diff adds small
functions allocateBytes and deallocateBytes that are replacements
for folly::checkedMalloc and free (and should be paired), to localize
the #ifdef.

Reviewed By: marksantaniello

Differential Revision: D10496934

fbshipit-source-id: eb193e1c315ca88286126f5eb68c705301ad5177
parent aee5ab34
......@@ -38,6 +38,21 @@
namespace folly {
/// allocateBytes and deallocateBytes work like a checkedMalloc/free pair,
/// but take advantage of sized deletion when available
inline void* allocateBytes(size_t n) {
return ::operator new(n);
}
inline void deallocateBytes(void* p, size_t n) {
#if __cpp_sized_deallocation
return ::operator delete(p, n);
#else
(void)n;
return ::operator delete(p);
#endif
}
#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \
(defined(__ANDROID__) && (__ANDROID_API__ > 16)) || \
(defined(__APPLE__) && \
......
......@@ -35,6 +35,12 @@ static constexpr std::size_t kTooBig = folly::constexpr_max(
std::size_t{std::numeric_limits<uint32_t>::max()},
std::size_t{1} << (8 * sizeof(std::size_t) - 14));
TEST(allocateBytes, simple) {
auto p = allocateBytes(10);
EXPECT_TRUE(p != nullptr);
deallocateBytes(p, 10);
}
TEST(aligned_malloc, examples) {
auto trial = [](size_t align) {
auto const ptr = aligned_malloc(1, align);
......
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