Commit 5ae81138 authored by Michael Lee's avatar Michael Lee Committed by Facebook Github Bot 7

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
parent b1eb6819
...@@ -271,7 +271,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -271,7 +271,7 @@ nobase_follyinclude_HEADERS = \
portability/Constexpr.h \ portability/Constexpr.h \
portability/Environment.h \ portability/Environment.h \
portability/GFlags.h \ portability/GFlags.h \
portability/Stdlib.h \ portability/Memory.h \
portability/Strings.h \ portability/Strings.h \
portability/Syscall.h \ portability/Syscall.h \
portability/SysStat.h \ portability/SysStat.h \
...@@ -406,7 +406,6 @@ libfolly_la_SOURCES = \ ...@@ -406,7 +406,6 @@ libfolly_la_SOURCES = \
MacAddress.cpp \ MacAddress.cpp \
MemoryMapping.cpp \ MemoryMapping.cpp \
portability/Environment.cpp \ portability/Environment.cpp \
portability/Stdlib.cpp \
portability/Strings.cpp \ portability/Strings.cpp \
portability/SysStat.cpp \ portability/SysStat.cpp \
portability/SysTime.cpp \ portability/SysTime.cpp \
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#define FOLLY_MEMORY_H_ #define FOLLY_MEMORY_H_
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/portability/Memory.h>
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
......
...@@ -14,29 +14,42 @@ ...@@ -14,29 +14,42 @@
* limitations under the License. * limitations under the License.
*/ */
#include <folly/portability/Stdlib.h> #include <folly/portability/Memory.h>
#include <errno.h> #include <cerrno>
#include <cstdlib>
#if defined(__ANDROID__)
#ifdef __ANDROID__
#include <android/api-level.h> #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)
#if (__ANDROID_API__ <= 15) void* aligned_malloc(size_t size, size_t align) { return memalign(align, size) }
int posix_memalign(void** memptr, size_t alignment, size_t size) { void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
int rc = 0;
int saved_errno = errno; #else
// Use poxis_memalign, but mimic the behavior of memalign
void* aligned_malloc(size_t size, size_t align) {
void* ptr = nullptr; void* ptr = nullptr;
ptr = memalign(alignment, size); int rc = posix_memalign(&ptr, align, size);
if (nullptr == ptr) { if (rc == 0) {
rc = errno; return ptr;
} else {
*memptr = ptr;
} }
errno = saved_errno; errno = rc;
return rc; return nullptr;
} }
void aligned_free(void* aligned_ptr) { free(aligned_ptr); }
#endif #endif
#endif }
}
...@@ -18,4 +18,11 @@ ...@@ -18,4 +18,11 @@
#include <cstdlib> #include <cstdlib>
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);
}
}
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/portability/Stdlib.h> #include <folly/portability/Memory.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <array> #include <array>
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
using namespace folly; using namespace folly;
using namespace folly::detail;
using namespace std; using namespace std;
static_assert(std::is_literal_type<StringPiece>::value, ""); static_assert(std::is_literal_type<StringPiece>::value, "");
...@@ -977,18 +978,19 @@ const size_t kPageSize = 4096; ...@@ -977,18 +978,19 @@ const size_t kPageSize = 4096;
void createProtectedBuf(StringPiece& contents, char** buf) { void createProtectedBuf(StringPiece& contents, char** buf) {
ASSERT_LE(contents.size(), kPageSize); ASSERT_LE(contents.size(), kPageSize);
const size_t kSuccess = 0; const size_t kSuccess = 0;
if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) { char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
ASSERT_FALSE(true); // Protect the page after the first full page-aligned region of the
} // malloc'ed buffer
mprotect(*buf + kPageSize, kPageSize, PROT_NONE); mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
size_t newBegin = kPageSize - contents.size(); size_t newBegin = kPageSize - contents.size();
memcpy(*buf + newBegin, contents.data(), contents.size()); memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
contents.reset(*buf + newBegin, contents.size()); contents.reset(pageAlignedBuf + newBegin, contents.size());
*buf = pageAlignedBuf;
} }
void freeProtectedBuf(char* buf) { void freeProtectedBuf(char* buf) {
mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE); mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
free(buf); aligned_free(buf);
} }
TYPED_TEST(NeedleFinderTest, NoSegFault) { TYPED_TEST(NeedleFinderTest, NoSegFault) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment