From cc8dd1c8ce560ef2f7d163f0c98fd95cb352be69 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 31 Dec 2023 08:00:02 -0800 Subject: Avoid placement new for FlexArray --- common/flexarray.h | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/common/flexarray.h b/common/flexarray.h index 7975a52a..b2884639 100644 --- a/common/flexarray.h +++ b/common/flexarray.h @@ -1,6 +1,7 @@ #ifndef AL_FLEXARRAY_H #define AL_FLEXARRAY_H +#include #include #include #include @@ -15,7 +16,7 @@ namespace al { */ template::value> struct FlexArrayStorage { - alignas(alignment) const ::al::span mData; + alignas(std::max(alignment, alignof(::al::span))) const ::al::span mData; static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept { return sizeof(FlexArrayStorage) + sizeof(T)*count + base; } @@ -31,7 +32,7 @@ struct FlexArrayStorage { template struct FlexArrayStorage { - alignas(alignment) const ::al::span mData; + alignas(std::max(alignment, alignof(::al::span))) const ::al::span mData; static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept { return sizeof(FlexArrayStorage) + sizeof(T)*count + base; } @@ -49,7 +50,7 @@ struct FlexArrayStorage { * struct, with placement new, to have a run-time-sized array that's embedded * with its size. */ -template +template struct FlexArray { using element_type = T; using value_type = std::remove_cv_t; @@ -66,6 +67,7 @@ struct FlexArray { using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; + static constexpr size_t alignment{std::max(alignof(T), Align)}; using Storage_t_ = FlexArrayStorage; Storage_t_ mStore; @@ -73,19 +75,7 @@ struct FlexArray { static constexpr index_type Sizeof(index_type count, index_type base=0u) noexcept { return Storage_t_::Sizeof(count, base); } static std::unique_ptr Create(index_type count) - { - if(gsl::owner ptr{al_calloc(alignof(FlexArray), Sizeof(count))}) - { - try { - return std::unique_ptr{::new(ptr) FlexArray{count}}; - } - catch(...) { - al_free(ptr); - throw; - } - } - throw std::bad_alloc(); - } + { return std::unique_ptr{new(FamCount{count}) FlexArray{count}}; } FlexArray(index_type size) noexcept(std::is_nothrow_constructible_v) : mStore{size} @@ -121,7 +111,16 @@ struct FlexArray { [[nodiscard]] auto rend() const noexcept -> const_reverse_iterator { return begin(); } [[nodiscard]] auto crend() const noexcept -> const_reverse_iterator { return cbegin(); } - DEF_PLACE_NEWDEL + gsl::owner operator new(size_t, FamCount count) + { return ::operator new[](Sizeof(count), std::align_val_t{alignof(FlexArray)}); } + void operator delete(gsl::owner block, FamCount) noexcept + { ::operator delete[](block, std::align_val_t{alignof(FlexArray)}); } + void operator delete(gsl::owner block) noexcept + { ::operator delete[](block, std::align_val_t{alignof(FlexArray)}); } + + void *operator new(size_t size) = delete; + void *operator new[](size_t size) = delete; + void operator delete[](void *block) = delete; }; } // namespace al -- cgit v1.2.3