From e3b8f8fe272503ef7f738da2f577f68b0fe16eeb Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 8 Oct 2021 11:05:36 -0700 Subject: Make a construct_at method amd use it --- core/hrtf.cpp | 21 ++++++++++----------- core/mastering.cpp | 12 +++++------- core/mastering.h | 1 + core/voice.cpp | 6 ++++-- 4 files changed, 20 insertions(+), 20 deletions(-) (limited to 'core') diff --git a/core/hrtf.cpp b/core/hrtf.cpp index e0ab8f0a..74483c26 100644 --- a/core/hrtf.cpp +++ b/core/hrtf.cpp @@ -368,19 +368,18 @@ std::unique_ptr CreateHrtfStore(uint rate, ushort irSize, const al::span elevs, const HrirArray *coeffs, const ubyte2 *delays, const char *filename) { - std::unique_ptr Hrtf; - const size_t irCount{size_t{elevs.back().azCount} + elevs.back().irOffset}; size_t total{sizeof(HrtfStore)}; total = RoundUp(total, alignof(HrtfStore::Field)); /* Align for field infos */ - total += sizeof(HrtfStore::Field)*fields.size(); + total += sizeof(std::declval().field[0])*fields.size(); total = RoundUp(total, alignof(HrtfStore::Elevation)); /* Align for elevation infos */ - total += sizeof(Hrtf->elev[0])*elevs.size(); + total += sizeof(std::declval().elev[0])*elevs.size(); total = RoundUp(total, 16); /* Align for coefficients using SIMD */ - total += sizeof(Hrtf->coeffs[0])*irCount; - total += sizeof(Hrtf->delays[0])*irCount; + total += sizeof(std::declval().coeffs[0])*irCount; + total += sizeof(std::declval().delays[0])*irCount; - Hrtf.reset(new (al_calloc(16, total)) HrtfStore{}); + void *ptr{al_calloc(16, total)}; + std::unique_ptr Hrtf{al::construct_at(static_cast(ptr))}; if(!Hrtf) ERR("Out of memory allocating storage for %s.\n", filename); else @@ -412,10 +411,10 @@ std::unique_ptr CreateHrtfStore(uint rate, ushort irSize, assert(offset == total); /* Copy input data to storage. */ - std::copy(fields.cbegin(), fields.cend(), field_); - std::copy(elevs.cbegin(), elevs.cend(), elev_); - std::copy_n(coeffs, irCount, coeffs_); - std::copy_n(delays, irCount, delays_); + std::uninitialized_copy(fields.cbegin(), fields.cend(), field_); + std::uninitialized_copy(elevs.cbegin(), elevs.cend(), elev_); + std::uninitialized_copy_n(coeffs, irCount, coeffs_); + std::uninitialized_copy_n(delays, irCount, delays_); /* Finally, assign the storage pointers. */ Hrtf->field = field_; diff --git a/core/mastering.cpp b/core/mastering.cpp index e0cb2ca7..3a577109 100644 --- a/core/mastering.cpp +++ b/core/mastering.cpp @@ -334,7 +334,7 @@ std::unique_ptr Compressor::Create(const size_t NumChans, const floa size += sizeof(*Compressor::mHold); } - auto Comp = std::unique_ptr{new (al_calloc(16, size)) Compressor{}}; + auto Comp = CompressorPtr{al::construct_at(static_cast(al_calloc(16, size)))}; Comp->mNumChans = NumChans; Comp->mAuto.Knee = AutoKnee; Comp->mAuto.Attack = AutoAttack; @@ -361,17 +361,15 @@ std::unique_ptr Compressor::Create(const size_t NumChans, const floa { if(hold > 1) { - Comp->mHold = ::new (static_cast(Comp.get() + 1)) SlidingHold{}; + Comp->mHold = al::construct_at(reinterpret_cast(Comp.get() + 1)); Comp->mHold->mValues[0] = -std::numeric_limits::infinity(); Comp->mHold->mExpiries[0] = hold; Comp->mHold->mLength = hold; - Comp->mDelay = ::new(static_cast(Comp->mHold + 1)) FloatBufferLine[NumChans]; + Comp->mDelay = reinterpret_cast(Comp->mHold + 1); } else - { - Comp->mDelay = ::new(static_cast(Comp.get() + 1)) FloatBufferLine[NumChans]; - } - std::fill_n(Comp->mDelay, NumChans, FloatBufferLine{}); + Comp->mDelay = reinterpret_cast(Comp.get() + 1); + std::uninitialized_fill_n(Comp->mDelay, NumChans, FloatBufferLine{}); } Comp->mCrestCoeff = std::exp(-1.0f / (0.200f * SampleRate)); // 200ms diff --git a/core/mastering.h b/core/mastering.h index 322d3654..1a36937c 100644 --- a/core/mastering.h +++ b/core/mastering.h @@ -100,5 +100,6 @@ struct Compressor { const float ThresholdDb, const float Ratio, const float KneeDb, const float AttackTime, const float ReleaseTime); }; +using CompressorPtr = std::unique_ptr; #endif /* CORE_MASTERING_H */ diff --git a/core/voice.cpp b/core/voice.cpp index ea99f8ad..fd2b7089 100644 --- a/core/voice.cpp +++ b/core/voice.cpp @@ -164,7 +164,8 @@ void SendSourceStoppedEvent(ContextBase *context, uint id) auto evt_vec = ring->getWriteVector(); if(evt_vec.first.len < 1) return; - AsyncEvent *evt{::new(evt_vec.first.buf) AsyncEvent{EventType_SourceStateChange}}; + AsyncEvent *evt{al::construct_at(reinterpret_cast(evt_vec.first.buf), + EventType_SourceStateChange)}; evt->u.srcstate.id = id; evt->u.srcstate.state = AsyncEvent::SrcState::Stop; @@ -792,7 +793,8 @@ void Voice::mix(const State vstate, ContextBase *Context, const uint SamplesToDo auto evt_vec = ring->getWriteVector(); if(evt_vec.first.len > 0) { - AsyncEvent *evt{::new(evt_vec.first.buf) AsyncEvent{EventType_BufferCompleted}}; + AsyncEvent *evt{al::construct_at(reinterpret_cast(evt_vec.first.buf), + EventType_BufferCompleted)}; evt->u.bufcomp.id = SourceID; evt->u.bufcomp.count = buffers_done; ring->writeAdvance(1); -- cgit v1.2.3