diff options
author | Kenneth Heafield <github@kheafield.com> | 2020-08-11 01:36:24 +0300 |
---|---|---|
committer | Kenneth Heafield <github@kheafield.com> | 2020-08-11 01:36:24 +0300 |
commit | 94b2f0b8601758539f73f6b40952ca82e900b7fb (patch) | |
tree | b862e668893d76f44d0f0a15a9825f0d277e9c10 | |
parent | 10fcb2a324d17cb883125cc19c7d76ec7f847096 (diff) |
Windows aligned allocator
-rw-r--r-- | aligned.h | 16 |
1 files changed, 15 insertions, 1 deletions
@@ -2,6 +2,9 @@ #include <cstdlib> #include <new> #include <stdlib.h> +#ifdef _MSC_VER +#include <malloc.h> +#endif // 64-byte aligned simple vector. @@ -11,15 +14,26 @@ template <class T> class AlignedVector { public: explicit AlignedVector(std::size_t size) : size_(size) { +#ifdef _MSC_VER + mem_ = _aligned_malloc(size * sizeof(T), 64); + if (!mem_) throw std::bad_alloc(); +#else if (posix_memalign(reinterpret_cast<void **>(&mem_), 64, size * sizeof(T))) { throw std::bad_alloc(); } +#endif } AlignedVector(const AlignedVector&) = delete; AlignedVector& operator=(const AlignedVector&) = delete; - ~AlignedVector() { std::free(mem_); } + ~AlignedVector() { +#ifdef _MSC_VER + _aligned_free(mem_); +#else + std::free(mem_); +#endif + } std::size_t size() const { return size_; } |