@@ -398,21 +398,16 @@ class aligned_array {
398398
399399 type_ *data_ = nullptr ;
400400 std::size_t size_ = 0 ;
401+ std::size_t alignment_ = 0 ;
401402
402403 public:
403- #if defined(_MSC_VER) // ! MSVC doesn't support `std::aligned_alloc` yet
404- aligned_array (std::size_t size, std::size_t alignment = 64 ) : size_(size) {
405- data_ = static_cast <type_ *>(_aligned_malloc (sizeof (type_) * size_, alignment));
406- if (!data_) throw std::bad_alloc ();
404+ aligned_array (std::size_t size, std::size_t alignment = 64 ) : size_(size), alignment_(alignment) {
405+ // With `std::aligned_alloc` in C++17, an exception won't be raised, which may be preferred in
406+ // some environments. MSVC was late to adopt it, and developers would often use a combination
407+ // of lower-level `posix_memalign` and `_aligned_malloc`/`_aligned_free` across Unix and Windows.
408+ data_ = (type_ *)::operator new (sizeof (type_) * size_, std::align_val_t (alignment_));
407409 }
408- ~aligned_array () noexcept { _aligned_free (data_); }
409- #else
410- aligned_array (std::size_t size, std::size_t alignment = 64 ) : size_(size) {
411- data_ = static_cast <type_ *>(std::aligned_alloc (alignment, sizeof (type_) * size_));
412- if (!data_) throw std::bad_alloc ();
413- }
414- ~aligned_array () noexcept { std::free (data_); }
415- #endif
410+ ~aligned_array () noexcept { ::operator delete (data_, sizeof (type_) * size_, std::align_val_t (alignment_)); }
416411
417412 aligned_array (aligned_array const &) = delete ;
418413 aligned_array &operator =(aligned_array const &) = delete ;
0 commit comments