From: Yedidya Feldblum <yfeldblum@fb.com>
Date: Sat, 13 Jan 2018 01:59:41 +0000 (-0800)
Subject: Promote aligned_malloc and aligned_free
X-Git-Tag: v2018.01.15.00~3
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=47b2f8dfdc39b149accf9ae830551eee670d98d1;p=folly.git

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
---

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 <folly/Traits.h>
-#include <folly/functional/Invoke.h>
-
+#include <cassert>
+#include <cerrno>
 #include <cstddef>
 #include <cstdlib>
 #include <exception>
@@ -28,8 +27,52 @@
 #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().
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<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();
   }
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 <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);
       }
     }
   };
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 <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
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 <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
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<std::string>());
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 <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;