Commit 5b48f33a authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot 9

Add Windows support to portability/Memory.h

Summary: This adds Windows support to portability/Memory.h and also does some refactoring to be more correct about when to use posix_memalign, and also changes it to fall back to memalign rather than posix_memalign.

Reviewed By: mzlee

Differential Revision: D3069990

fb-gh-sync-id: 88861583c6028e9fb02a3e3804bd6d476956c555
shipit-source-id: 88861583c6028e9fb02a3e3804bd6d476956c555
parent 60088aaa
...@@ -16,26 +16,15 @@ ...@@ -16,26 +16,15 @@
#include <folly/portability/Memory.h> #include <folly/portability/Memory.h>
#include <cerrno> #include <folly/portability/Config.h>
#include <cstdlib>
#ifdef __ANDROID__
#include <android/api-level.h>
#endif
namespace folly { namespace folly {
namespace detail { namespace detail {
#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \
(defined(__ANDROID__) && (__ANDROID_API__ > 15))
#include <errno.h>
#if defined(__ANDROID__) && (__ANDROID_API__ <= 15) // Use posix_memalign, but mimic the behaviour of memalign
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* aligned_malloc(size_t size, size_t align) {
void* ptr = nullptr; void* ptr = nullptr;
int rc = posix_memalign(&ptr, align, size); int rc = posix_memalign(&ptr, align, size);
...@@ -46,8 +35,29 @@ void* aligned_malloc(size_t size, size_t align) { ...@@ -46,8 +35,29 @@ void* aligned_malloc(size_t size, size_t align) {
return nullptr; return nullptr;
} }
void aligned_free(void* aligned_ptr) { free(aligned_ptr); } void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#elif defined(_WIN32)
#include <malloc.h> // nolint
void* aligned_malloc(size_t size, size_t align) {
return _aligned_malloc(size, alignment);
}
void aligned_free(void* aligned_ptr) {
_aligned_free(aligned_ptr);
}
#else
#include <malloc.h> // nolint
void* aligned_malloc(size_t size, size_t align) {
return memalign(align, size);
}
void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#endif #endif
} }
} }
...@@ -16,13 +16,11 @@ ...@@ -16,13 +16,11 @@
#pragma once #pragma once
#include <cstdlib> #include <stdlib.h>
namespace folly { namespace folly {
namespace detail { namespace detail {
void* aligned_malloc(size_t size, size_t align); void* aligned_malloc(size_t size, size_t align);
void aligned_free(void* aligned_ptr); void aligned_free(void* aligned_ptr);
} }
} }
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