From 5ae81138c02ec6e14b34ab1f383acf70546abbb8 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 11 Mar 2016 16:08:55 -0800 Subject: [PATCH] Remove portability/Stdlib.{h,cpp} 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 | 3 +- folly/Memory.h | 1 + folly/portability/Memory.cpp | 55 ++++++++++++++++++++++++ folly/portability/{Stdlib.h => Memory.h} | 9 +++- folly/portability/Stdlib.cpp | 42 ------------------ folly/test/RangeTest.cpp | 18 ++++---- 6 files changed, 75 insertions(+), 53 deletions(-) create mode 100644 folly/portability/Memory.cpp rename folly/portability/{Stdlib.h => Memory.h} (82%) delete mode 100644 folly/portability/Stdlib.cpp diff --git a/folly/Makefile.am b/folly/Makefile.am index 5ff05415..904d70a3 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 \ diff --git a/folly/Memory.h b/folly/Memory.h index bcfa321c..69af41d1 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -18,6 +18,7 @@ #define FOLLY_MEMORY_H_ #include +#include #include #include diff --git a/folly/portability/Memory.cpp b/folly/portability/Memory.cpp new file mode 100644 index 00000000..60ce0330 --- /dev/null +++ b/folly/portability/Memory.cpp @@ -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 + +#include +#include + +#ifdef __ANDROID__ +#include +#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/Stdlib.h b/folly/portability/Memory.h similarity index 82% rename from folly/portability/Stdlib.h rename to folly/portability/Memory.h index 2e7c1c0b..f46e9cea 100644 --- a/folly/portability/Stdlib.h +++ b/folly/portability/Memory.h @@ -18,4 +18,11 @@ #include -extern int posix_memalign(void** memptr, size_t alignment, size_t size); +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 index e3f275b0..00000000 --- a/folly/portability/Stdlib.cpp +++ /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 - -#include - -#if defined(__ANDROID__) - -#include - -#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/test/RangeTest.cpp b/folly/test/RangeTest.cpp index aed47dc2..8cbdb04d 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -19,7 +19,7 @@ #include -#include +#include #include #include @@ -34,6 +34,7 @@ #include using namespace folly; +using namespace folly::detail; using namespace std; static_assert(std::is_literal_type::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) { -- 2.34.1