Promote aligned_malloc and aligned_free
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 13 Jan 2018 01:59:41 +0000 (17:59 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 13 Jan 2018 02:07:47 +0000 (18:07 -0800)
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

16 files changed:
folly/Makefile.am
folly/Memory.h
folly/Range.h
folly/concurrency/CacheLocality.cpp
folly/concurrency/CacheLocality.h
folly/concurrency/test/CacheLocalityBenchmark.cpp
folly/concurrency/test/CacheLocalityTest.cpp
folly/detail/RangeCommon.cpp
folly/detail/RangeCommon.h
folly/detail/RangeSse42.cpp
folly/detail/RangeSse42.h
folly/portability/Memory.cpp [deleted file]
folly/portability/Memory.h
folly/test/MemoryTest.cpp
folly/test/RangeFindBenchmark.cpp
folly/test/RangeTest.cpp

index 60a66c8e5e5489e034c86c0fc26b1301db411181..e5e0f9b968f72bc6e2c18be721ec840dec60d531 100644 (file)
@@ -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 \
index ce0ff05d3290605f6dd08ba28ad2caae4aca47b4..0a0f75789d01e3c70af29218adbc4800fb582777 100644 (file)
@@ -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 <folly/Traits.h>
-#include <folly/functional/Invoke.h>
-
+#include <cassert>
+#include <cerrno>
 #include <cstddef>
 #include <cstdlib>
 #include <exception>
 #include <type_traits>
 #include <utility>
 
+#include <folly/Traits.h>
+#include <folly/functional/Invoke.h>
+#include <folly/portability/Config.h>
+#include <folly/portability/Malloc.h>
+
 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().
index 228a8d5535c213194b579ccda1e1144b16e7e45b..c4e8b27de1f95e53001a90ae70493cafe1d9c33a 100644 (file)
@@ -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.
index d400ddb2ab0d77297133914150fd1fa159ea841b..38dd957904f456dc29f534ee8d13a4efd6081e04 100644 (file)
@@ -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<std::mutex> g(m_);
   for (auto& block : blocks_) {
-    detail::aligned_free(block);
+    folly::aligned_free(block);
   }
 }
 
 void* SimpleAllocator::allocateHard() {
   // Allocate a new slab.
-  mem_ = static_cast<uint8_t*>(detail::aligned_malloc(allocSize_, allocSize_));
+  mem_ = static_cast<uint8_t*>(folly::aligned_malloc(allocSize_, allocSize_));
   if (!mem_) {
     std::__throw_bad_alloc();
   }
index cd6c06e210df0bcb12c9ebc2b90612a599603e41..0f13b3745df7de97b49aed9332e77b3fab2b2b33 100644 (file)
@@ -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 <folly/hash/Hash.h>
 #include <folly/lang/Align.h>
 #include <folly/portability/BitsFunctexcept.h>
-#include <folly/portability/Memory.h>
 #include <folly/system/ThreadId.h>
 
 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<SimpleAllocator**>(addr);
         allocator->deallocate(mem);
       } else {
-        detail::aligned_free(mem);
+        aligned_free(mem);
       }
     }
   };
index 75acbe18628ba301d5aa0272d2c17f1c7034f54e..03e7d4228f51fc614b882791c598d1263297d33b 100644 (file)
@@ -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.
index 5005d65acac6c46f9a884f3d839306cffb9c2498..8122826174cafe4134218ed1a9fdb2e1595b41cf 100644 (file)
@@ -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.
index 87467b6204d3292a74393ad912a7582e94c306c7..829713845562435a2581d02f72480555ebe71fe7 100644 (file)
@@ -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.
index 6a7e8074eb5cf163f3339ba39ac127f5670abd73..6a4321337f6db6795dc2eb1c99a21c3f264be7f0 100644 (file)
@@ -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.
index 0d82df362190118aee17f4009061cdda9aef6c11..01c7c954735af8c3f6d5f47941af4112db7e08e8 100644 (file)
@@ -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.
index 0a5901fc0f3576a22fc8c63e3c54fc47ff32696b..d2a7d7d75d8acba08e613ddd4242b8223212244b 100644 (file)
@@ -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 (file)
index 7ad9a6c..0000000
+++ /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 <folly/portability/Memory.h>
-
-#include <folly/portability/Config.h>
-#include <folly/portability/Malloc.h>
-
-#include <errno.h>
-
-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
index d932a5dc13422224ca1b38b98418b8bd2632e3db..60e2e2ad9bfe09d67253099cd701c507dd76cdce 100644 (file)
@@ -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.
 
 #pragma once
 
-#include <stdlib.h>
+#include <folly/Memory.h> // @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
index f3e6f8d383b4b8ae6af147df0df1256695366d8f..747d6ac7b2ca5aa1691164852c8b327328fb1e0d 100644 (file)
@@ -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.
 
 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<std::string>());
index 7b2bda4168d445a5485aa9394073a9490865e790..cab1d046aa839e2e3e7ce34bf5a23b5da0fddcf4 100644 (file)
@@ -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.
index 42faf4dea14af05817c88cef57316889254ba343..5581adcb7f7b3bd1b68b0c29061d7d674b1e11b3 100644 (file)
@@ -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 <boost/algorithm/string/trim.hpp>
 #include <boost/range/concepts.hpp>
 
+#include <folly/Memory.h>
 #include <folly/portability/GMock.h>
 #include <folly/portability/GTest.h>
-#include <folly/portability/Memory.h>
 #include <folly/portability/SysMman.h>
 
 using namespace folly;