From 47b2f8dfdc39b149accf9ae830551eee670d98d1 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Fri, 12 Jan 2018 17:59:41 -0800 Subject: [PATCH] Promote aligned_malloc and aligned_free Summary: [Folly] Promote `aligned_malloc` and `aligned_free` from `namespace folly::detail` to `namespace folly`. And move them from `folly/portability/Memory.h` to `folly/Memory.h`. Differential Revision: D6153394 fbshipit-source-id: eef314d2bc171910ea3c8403da9e9e1d1858ce15 --- folly/Makefile.am | 1 - folly/Memory.h | 51 ++++++++++++-- folly/Range.h | 2 +- folly/concurrency/CacheLocality.cpp | 6 +- folly/concurrency/CacheLocality.h | 9 ++- .../test/CacheLocalityBenchmark.cpp | 2 +- folly/concurrency/test/CacheLocalityTest.cpp | 2 +- folly/detail/RangeCommon.cpp | 2 +- folly/detail/RangeCommon.h | 2 +- folly/detail/RangeSse42.cpp | 2 +- folly/detail/RangeSse42.h | 2 +- folly/portability/Memory.cpp | 66 ------------------- folly/portability/Memory.h | 8 +-- folly/test/MemoryTest.cpp | 17 ++++- folly/test/RangeFindBenchmark.cpp | 2 +- folly/test/RangeTest.cpp | 4 +- 16 files changed, 84 insertions(+), 94 deletions(-) delete mode 100644 folly/portability/Memory.cpp diff --git a/folly/Makefile.am b/folly/Makefile.am index 60a66c8e..e5e0f9b9 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -594,7 +594,6 @@ libfolly_la_SOURCES = \ portability/Fcntl.cpp \ portability/Libgen.cpp \ portability/Malloc.cpp \ - portability/Memory.cpp \ portability/OpenSSL.cpp \ portability/PThread.cpp \ portability/Sockets.cpp \ diff --git a/folly/Memory.h b/folly/Memory.h index ce0ff05d..0a0f7578 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,8 @@ #pragma once -#include -#include - +#include +#include #include #include #include @@ -28,8 +27,52 @@ #include #include +#include +#include +#include +#include + namespace folly { +#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \ + (defined(__ANDROID__) && (__ANDROID_API__ > 15)) || \ + (defined(__APPLE__) && \ + (__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6 || \ + __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0)) + +inline void* aligned_malloc(size_t size, size_t align) { + // use posix_memalign, but mimic the behaviour of memalign + void* ptr = nullptr; + int rc = posix_memalign(&ptr, align, size); + return rc == 0 ? (errno = 0, ptr) : (errno = rc, nullptr); +} + +inline void aligned_free(void* aligned_ptr) { + free(aligned_ptr); +} + +#elif defined(_WIN32) + +inline void* aligned_malloc(size_t size, size_t align) { + return _aligned_malloc(size, align); +} + +inline void aligned_free(void* aligned_ptr) { + _aligned_free(aligned_ptr); +} + +#else + +inline void* aligned_malloc(size_t size, size_t align) { + return memalign(align, size); +} + +inline void aligned_free(void* aligned_ptr) { + free(aligned_ptr); +} + +#endif + /** * For exception safety and consistency with make_shared. Erase me when * we have std::make_unique(). diff --git a/folly/Range.h b/folly/Range.h index 228a8d55..c4e8b27d 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/concurrency/CacheLocality.cpp b/folly/concurrency/CacheLocality.cpp index d400ddb2..38dd9579 100644 --- a/folly/concurrency/CacheLocality.cpp +++ b/folly/concurrency/CacheLocality.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -241,13 +241,13 @@ SimpleAllocator::SimpleAllocator(size_t allocSize, size_t sz) SimpleAllocator::~SimpleAllocator() { std::lock_guard g(m_); for (auto& block : blocks_) { - detail::aligned_free(block); + folly::aligned_free(block); } } void* SimpleAllocator::allocateHard() { // Allocate a new slab. - mem_ = static_cast(detail::aligned_malloc(allocSize_, allocSize_)); + mem_ = static_cast(folly::aligned_malloc(allocSize_, allocSize_)); if (!mem_) { std::__throw_bad_alloc(); } diff --git a/folly/concurrency/CacheLocality.h b/folly/concurrency/CacheLocality.h index cd6c06e2..0f13b374 100644 --- a/folly/concurrency/CacheLocality.h +++ b/folly/concurrency/CacheLocality.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,6 @@ #include #include #include -#include #include namespace folly { @@ -435,8 +434,8 @@ class CoreAllocator { // Align to a cacheline size = size + (hardware_destructive_interference_size - 1); size &= ~size_t(hardware_destructive_interference_size - 1); - void* mem = detail::aligned_malloc( - size, hardware_destructive_interference_size); + void* mem = + aligned_malloc(size, hardware_destructive_interference_size); if (!mem) { std::__throw_bad_alloc(); } @@ -456,7 +455,7 @@ class CoreAllocator { auto allocator = *static_cast(addr); allocator->deallocate(mem); } else { - detail::aligned_free(mem); + aligned_free(mem); } } }; diff --git a/folly/concurrency/test/CacheLocalityBenchmark.cpp b/folly/concurrency/test/CacheLocalityBenchmark.cpp index 75acbe18..03e7d422 100644 --- a/folly/concurrency/test/CacheLocalityBenchmark.cpp +++ b/folly/concurrency/test/CacheLocalityBenchmark.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2016-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/concurrency/test/CacheLocalityTest.cpp b/folly/concurrency/test/CacheLocalityTest.cpp index 5005d65a..81228261 100644 --- a/folly/concurrency/test/CacheLocalityTest.cpp +++ b/folly/concurrency/test/CacheLocalityTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/detail/RangeCommon.cpp b/folly/detail/RangeCommon.cpp index 87467b62..82971384 100644 --- a/folly/detail/RangeCommon.cpp +++ b/folly/detail/RangeCommon.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/detail/RangeCommon.h b/folly/detail/RangeCommon.h index 6a7e8074..6a432133 100644 --- a/folly/detail/RangeCommon.h +++ b/folly/detail/RangeCommon.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/detail/RangeSse42.cpp b/folly/detail/RangeSse42.cpp index 0d82df36..01c7c954 100644 --- a/folly/detail/RangeSse42.cpp +++ b/folly/detail/RangeSse42.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/detail/RangeSse42.h b/folly/detail/RangeSse42.h index 0a5901fc..d2a7d7d7 100644 --- a/folly/detail/RangeSse42.h +++ b/folly/detail/RangeSse42.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/portability/Memory.cpp b/folly/portability/Memory.cpp deleted file mode 100644 index 7ad9a6c4..00000000 --- a/folly/portability/Memory.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2017 Facebook, Inc. - * - * 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 - -#include -#include - -#include - -namespace folly { -namespace detail { -#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \ - (defined(__ANDROID__) && (__ANDROID_API__ > 15)) || \ - (defined(__APPLE__) && \ - (__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6 || \ - __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0)) - -// Use posix_memalign, but mimic the behaviour of memalign -void* aligned_malloc(size_t size, size_t align) { - void* ptr = nullptr; - int rc = posix_memalign(&ptr, align, size); - if (rc == 0) { - return ptr; - } - errno = rc; - return nullptr; -} - -void aligned_free(void* aligned_ptr) { - free(aligned_ptr); -} -#elif defined(_WIN32) - -void* aligned_malloc(size_t size, size_t align) { - return _aligned_malloc(size, align); -} - -void aligned_free(void* aligned_ptr) { - _aligned_free(aligned_ptr); -} -#else - -void* aligned_malloc(size_t size, size_t align) { - return memalign(align, size); -} - -void aligned_free(void* aligned_ptr) { - free(aligned_ptr); -} -#endif -} // namespace detail -} // namespace folly diff --git a/folly/portability/Memory.h b/folly/portability/Memory.h index d932a5dc..60e2e2ad 100644 --- a/folly/portability/Memory.h +++ b/folly/portability/Memory.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2016-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,11 @@ #pragma once -#include +#include // @shim namespace folly { namespace detail { -void* aligned_malloc(size_t size, size_t align); -void aligned_free(void* aligned_ptr); +using folly::aligned_free; +using folly::aligned_malloc; } // namespace detail } // namespace folly diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp index f3e6f8d3..747d6ac7 100644 --- a/folly/test/MemoryTest.cpp +++ b/folly/test/MemoryTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2013-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,21 @@ using namespace folly; +TEST(aligned_malloc, examples) { + auto trial = [](size_t align) { + auto const ptr = aligned_malloc(1, align); + return (aligned_free(ptr), uintptr_t(ptr)); + }; + + if (!kIsSanitize) { // asan allocator raises SIGABRT instead + EXPECT_EQ(EINVAL, (trial(2), errno)) << "too small"; + EXPECT_EQ(EINVAL, (trial(513), errno)) << "not power of two"; + } + + EXPECT_EQ(0, trial(512) % 512); + EXPECT_EQ(0, trial(8192) % 8192); +} + TEST(make_unique, compatible_with_std_make_unique) { // HACK: To enforce that `folly::` is imported here. to_shared_ptr(std::unique_ptr()); diff --git a/folly/test/RangeFindBenchmark.cpp b/folly/test/RangeFindBenchmark.cpp index 7b2bda41..cab1d046 100644 --- a/folly/test/RangeFindBenchmark.cpp +++ b/folly/test/RangeFindBenchmark.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2012-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 42faf4de..5581adcb 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,9 +30,9 @@ #include #include +#include #include #include -#include #include using namespace folly; -- 2.34.1