From 1dbb8a83e0dbf9ec7cad32815b3aceac54de706e Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 31 Dec 2023 09:02:05 -0800 Subject: Avoid al_calloc/al_free for HrtfStore --- common/almalloc.h | 7 ------- core/hrtf.cpp | 5 +++-- core/hrtf.h | 11 +++++++++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/common/almalloc.h b/common/almalloc.h index b64b6b20..82f050e4 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -30,13 +30,6 @@ gsl::owner al_calloc(size_t alignment, size_t size); void operator delete(void*) noexcept = delete; \ void operator delete[](void*) noexcept = delete; -#define DEF_PLACE_NEWDEL \ - void *operator new(size_t) = delete; \ - void *operator new[](size_t) = delete; \ - void operator delete(gsl::owner block, void*) noexcept { al_free(block); } \ - void operator delete(gsl::owner block) noexcept { al_free(block); } \ - void operator delete[](gsl::owner block, void*) noexcept { al_free(block); } \ - void operator delete[](gsl::owner block) noexcept { al_free(block); } enum FamCount : size_t { }; diff --git a/core/hrtf.cpp b/core/hrtf.cpp index 8fc4030e..d56c364e 100644 --- a/core/hrtf.cpp +++ b/core/hrtf.cpp @@ -392,10 +392,11 @@ std::unique_ptr CreateHrtfStore(uint rate, uint8_t irSize, total += sizeof(std::declval().mCoeffs[0])*irCount; total += sizeof(std::declval().mDelays[0])*irCount; + static constexpr auto AlignVal = std::align_val_t{alignof(HrtfStore)}; std::unique_ptr Hrtf{}; - if(void *ptr{al_calloc(16, total)}) + if(gsl::owner ptr{::operator new[](total, AlignVal, std::nothrow)}) { - Hrtf.reset(al::construct_at(static_cast(ptr))); + Hrtf = decltype(Hrtf){::new(ptr) HrtfStore{}}; Hrtf->mRef.store(1u, std::memory_order_relaxed); Hrtf->mSampleRate = rate & 0xff'ff'ff; Hrtf->mIrSize = irSize; diff --git a/core/hrtf.h b/core/hrtf.h index 7a1a8b69..882724b8 100644 --- a/core/hrtf.h +++ b/core/hrtf.h @@ -18,7 +18,7 @@ #include "mixer/hrtfdefs.h" -struct HrtfStore { +struct alignas(16) HrtfStore { std::atomic mRef; uint mSampleRate : 24; @@ -47,7 +47,14 @@ struct HrtfStore { void add_ref(); void dec_ref(); - DEF_PLACE_NEWDEL + void *operator new(size_t) = delete; + void *operator new[](size_t) = delete; + void operator delete[](void*) noexcept = delete; + + void operator delete(gsl::owner block, void*) noexcept + { ::operator delete[](block, std::align_val_t{alignof(HrtfStore)}); } + void operator delete(gsl::owner block) noexcept + { ::operator delete[](block, std::align_val_t{alignof(HrtfStore)}); } }; using HrtfStorePtr = al::intrusive_ptr; -- cgit v1.2.3