From 673983dc5d05e8b47958c95f12dba3d7a915edfa Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 8 Jan 2019 23:44:08 -0800 Subject: Make Create methods for structs with flexible array members ... ... that are used with unique_ptr. --- Alc/hrtf.cpp | 22 ++++++++++++++++------ Alc/hrtf.h | 2 ++ Alc/panning.cpp | 3 +-- 3 files changed, 19 insertions(+), 8 deletions(-) (limited to 'Alc') diff --git a/Alc/hrtf.cpp b/Alc/hrtf.cpp index ff9e2f79..422f5052 100644 --- a/Alc/hrtf.cpp +++ b/Alc/hrtf.cpp @@ -47,9 +47,17 @@ struct HrtfHandle { HrtfEntry *entry{nullptr}; char filename[]; + static std::unique_ptr Create(size_t fname_len); + DEF_PLACE_NEWDEL() }; +std::unique_ptr HrtfHandle::Create(size_t fname_len) +{ + void *ptr{al_calloc(DEF_ALIGN, FAM_SIZE(HrtfHandle, filename, fname_len))}; + return std::unique_ptr{new (ptr) HrtfHandle{}}; +} + namespace { using HrtfHandlePtr = std::unique_ptr; @@ -273,6 +281,12 @@ void GetHrtfCoeffs(const HrtfEntry *Hrtf, ALfloat elevation, ALfloat azimuth, AL } +std::unique_ptr DirectHrtfState::Create(ALsizei num_chans) +{ + void *ptr{al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, num_chans))}; + return std::unique_ptr{new (ptr) DirectHrtfState{}}; +} + void BuildBFormatHrtf(const HrtfEntry *Hrtf, DirectHrtfState *state, const ALsizei NumChannels, const AngularPoint *AmbiPoints, const ALfloat (*RESTRICT AmbiMatrix)[MAX_AMBI_COEFFS], const ALsizei AmbiCount, const ALfloat *RESTRICT AmbiOrderHFGain) { static constexpr int OrderFromChan[MAX_AMBI_COEFFS]{ @@ -980,9 +994,7 @@ void AddFileEntry(al::vector &list, const std::string &filename) { TRACE("Got new file \"%s\"\n", filename.c_str()); - LoadedHrtfs.emplace_back(HrtfHandlePtr{new - (al_calloc(DEF_ALIGN, FAM_SIZE(HrtfHandle, filename, filename.length()+1))) - HrtfHandle{}}); + LoadedHrtfs.emplace_back(HrtfHandle::Create(filename.length()+1)); loaded_entry = LoadedHrtfs.end()-1; strcpy((*loaded_entry)->filename, filename.c_str()); } @@ -1042,9 +1054,7 @@ void AddBuiltInEntry(al::vector &list, const std::string &filena TRACE("Got new file \"%s\"\n", filename.c_str()); - LoadedHrtfs.emplace_back(HrtfHandlePtr{new - (al_calloc(DEF_ALIGN, FAM_SIZE(HrtfHandle, filename, namelen))) - HrtfHandle{}}); + LoadedHrtfs.emplace_back(HrtfHandle::Create(namelen)); loaded_entry = LoadedHrtfs.end()-1; snprintf((*loaded_entry)->filename, namelen, "!%u_%s", residx, filename.c_str()); diff --git a/Alc/hrtf.h b/Alc/hrtf.h index 7954ac11..58c35466 100644 --- a/Alc/hrtf.h +++ b/Alc/hrtf.h @@ -1,6 +1,7 @@ #ifndef ALC_HRTF_H #define ALC_HRTF_H +#include #include #include "AL/al.h" @@ -64,6 +65,7 @@ struct DirectHrtfState { } Chan[]; DirectHrtfState() noexcept { } + static std::unique_ptr Create(ALsizei num_chans); DEF_PLACE_NEWDEL() }; diff --git a/Alc/panning.cpp b/Alc/panning.cpp index 502a02cd..99a0534f 100644 --- a/Alc/panning.cpp +++ b/Alc/panning.cpp @@ -697,8 +697,7 @@ void InitHrtfPanning(ALCdevice *device) count = static_cast(COUNTOF(IndexMap)); } - device->mHrtfState.reset( - new (al_calloc(16, FAM_SIZE(DirectHrtfState, Chan, count))) DirectHrtfState{}); + device->mHrtfState = DirectHrtfState::Create(count); std::transform(std::begin(IndexMap), std::begin(IndexMap)+count, std::begin(device->Dry.AmbiMap), [](const ALsizei &index) noexcept { return BFChannelConfig{1.0f, index}; } -- cgit v1.2.3