Remove portability/Stdlib.{h,cpp}
authorMichael Lee <mzlee@fb.com>
Sat, 12 Mar 2016 00:08:55 +0000 (16:08 -0800)
committerFacebook Github Bot 7 <facebook-github-bot-7-bot@fb.com>
Sat, 12 Mar 2016 00:20:22 +0000 (16:20 -0800)
Summary: This was wrong and is like better solved, not with a shim layer, but by not using such a platform-specific function in the first place.

Reviewed By: yfeldblum

Differential Revision: D3001253

fb-gh-sync-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0
shipit-source-id: 460cc46b1c9adc3d229a07cb290270f7afbbb1e0

folly/Makefile.am
folly/Memory.h
folly/portability/Memory.cpp [new file with mode: 0644]
folly/portability/Memory.h [new file with mode: 0644]
folly/portability/Stdlib.cpp [deleted file]
folly/portability/Stdlib.h [deleted file]
folly/test/RangeTest.cpp

index 5ff054158b556e4af4903e375701524b66877cfa..904d70a331783aa00e2b11708a613d290041b052 100644 (file)
@@ -271,7 +271,7 @@ nobase_follyinclude_HEADERS = \
        portability/Constexpr.h \
        portability/Environment.h \
        portability/GFlags.h \
-       portability/Stdlib.h \
+       portability/Memory.h \
        portability/Strings.h \
        portability/Syscall.h \
        portability/SysStat.h \
@@ -406,7 +406,6 @@ libfolly_la_SOURCES = \
        MacAddress.cpp \
        MemoryMapping.cpp \
        portability/Environment.cpp \
-       portability/Stdlib.cpp \
        portability/Strings.cpp \
        portability/SysStat.cpp \
        portability/SysTime.cpp \
index bcfa321c8635d6d20a411e1b828366cea1e4cb98..69af41d175e5e57cd0502192854c9ebbb3c63f23 100644 (file)
@@ -18,6 +18,7 @@
 #define FOLLY_MEMORY_H_
 
 #include <folly/Traits.h>
+#include <folly/portability/Memory.h>
 
 #include <cstddef>
 #include <cstdlib>
diff --git a/folly/portability/Memory.cpp b/folly/portability/Memory.cpp
new file mode 100644 (file)
index 0000000..60ce033
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2016 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 <cerrno>
+#include <cstdlib>
+
+#ifdef __ANDROID__
+#include <android/api-level.h>
+#endif
+
+namespace folly {
+namespace detail {
+
+#ifdef _WIN32
+void* aligned_malloc(size_t size, size_t align) { return nullptr; }
+
+void aligned_free(void* aligned_ptr) {}
+#elif defined(__ANDROID__) && (__ANDROID_API__ <= 15)
+
+void* aligned_malloc(size_t size, size_t align) { return memalign(align, size) }
+
+void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
+
+#else
+// Use poxis_memalign, but mimic the behavior 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); }
+
+#endif
+}
+}
diff --git a/folly/portability/Memory.h b/folly/portability/Memory.h
new file mode 100644 (file)
index 0000000..f46e9ce
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2016 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.
+ */
+
+#pragma once
+
+#include <cstdlib>
+
+namespace folly {
+namespace detail {
+
+void* aligned_malloc(size_t size, size_t align);
+
+void aligned_free(void* aligned_ptr);
+}
+}
diff --git a/folly/portability/Stdlib.cpp b/folly/portability/Stdlib.cpp
deleted file mode 100644 (file)
index e3f275b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright 2016 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/Stdlib.h>
-
-#include <errno.h>
-
-#if defined(__ANDROID__)
-
-#include <android/api-level.h>
-
-#if (__ANDROID_API__ <= 15)
-
-int posix_memalign(void** memptr, size_t alignment, size_t size) {
-  int rc = 0;
-  int saved_errno = errno;
-  void* ptr = nullptr;
-  ptr = memalign(alignment, size);
-  if (nullptr == ptr) {
-    rc = errno;
-  } else {
-    *memptr = ptr;
-  }
-  errno = saved_errno;
-  return rc;
-}
-
-#endif
-#endif
diff --git a/folly/portability/Stdlib.h b/folly/portability/Stdlib.h
deleted file mode 100644 (file)
index 2e7c1c0..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright 2016 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.
- */
-
-#pragma once
-
-#include <cstdlib>
-
-extern int posix_memalign(void** memptr, size_t alignment, size_t size);
index aed47dc2ef13a05dce1c461cee99edf9e0013da8..8cbdb04d7b4d87620ede819c51e48e0444e6c357 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <folly/Range.h>
 
-#include <folly/portability/Stdlib.h>
+#include <folly/portability/Memory.h>
 
 #include <sys/mman.h>
 #include <array>
@@ -34,6 +34,7 @@
 #include <gtest/gtest.h>
 
 using namespace folly;
+using namespace folly::detail;
 using namespace std;
 
 static_assert(std::is_literal_type<StringPiece>::value, "");
@@ -977,18 +978,19 @@ const size_t kPageSize = 4096;
 void createProtectedBuf(StringPiece& contents, char** buf) {
   ASSERT_LE(contents.size(), kPageSize);
   const size_t kSuccess = 0;
-  if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
-    ASSERT_FALSE(true);
-  }
-  mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
+  char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
+  // Protect the page after the first full page-aligned region of the
+  // malloc'ed buffer
+  mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
   size_t newBegin = kPageSize - contents.size();
-  memcpy(*buf + newBegin, contents.data(), contents.size());
-  contents.reset(*buf + newBegin, contents.size());
+  memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
+  contents.reset(pageAlignedBuf + newBegin, contents.size());
+  *buf = pageAlignedBuf;
 }
 
 void freeProtectedBuf(char* buf) {
   mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
-  free(buf);
+  aligned_free(buf);
 }
 
 TYPED_TEST(NeedleFinderTest, NoSegFault) {