portability/Constexpr.h \
portability/Environment.h \
portability/GFlags.h \
- portability/Stdlib.h \
+ portability/Memory.h \
portability/Strings.h \
portability/Syscall.h \
portability/SysStat.h \
MacAddress.cpp \
MemoryMapping.cpp \
portability/Environment.cpp \
- portability/Stdlib.cpp \
portability/Strings.cpp \
portability/SysStat.cpp \
portability/SysTime.cpp \
#define FOLLY_MEMORY_H_
#include <folly/Traits.h>
+#include <folly/portability/Memory.h>
#include <cstddef>
#include <cstdlib>
--- /dev/null
+/*
+ * 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
+}
+}
--- /dev/null
+/*
+ * 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);
+}
+}
+++ /dev/null
-/*
- * 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
+++ /dev/null
-/*
- * 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);
#include <folly/Range.h>
-#include <folly/portability/Stdlib.h>
+#include <folly/portability/Memory.h>
#include <sys/mman.h>
#include <array>
#include <gtest/gtest.h>
using namespace folly;
+using namespace folly::detail;
using namespace std;
static_assert(std::is_literal_type<StringPiece>::value, "");
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) {