diff options
author | Chris Robinson <[email protected]> | 2024-01-01 05:11:53 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2024-01-01 05:53:12 -0800 |
commit | 45fca6a301a6d424bd0d856d0ae29ce56c5dc9ac (patch) | |
tree | 483daf3d82c03da685650840261cddded6c493cb | |
parent | 2f2edb326128c56e269a171961d991d8d7936e4f (diff) |
Use an allocator to allocate uninitilized sublists
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | al/auxeffectslot.cpp | 31 | ||||
-rw-r--r-- | al/buffer.cpp | 31 | ||||
-rw-r--r-- | al/effect.cpp | 31 | ||||
-rw-r--r-- | al/filter.cpp | 31 | ||||
-rw-r--r-- | al/source.cpp | 31 | ||||
-rw-r--r-- | common/almalloc.cpp | 15 | ||||
-rw-r--r-- | common/almalloc.h | 19 |
8 files changed, 86 insertions, 104 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 14ee18f5..b4ced226 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -610,7 +610,6 @@ set(COMMON_OBJS common/alcomplex.h common/alfstream.cpp common/alfstream.h - common/almalloc.cpp common/almalloc.h common/alnumbers.h common/alnumeric.h diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index d95748d7..5eea3425 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -53,6 +53,8 @@ namespace { +using SubListAllocator = typename al::allocator<std::array<ALeffectslot,64>>; + struct FactoryItem { EffectSlotType Type; EffectStateFactory* (&GetFactory)(); @@ -238,22 +240,21 @@ bool EnsureEffectSlots(ALCcontext *context, size_t needed) [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(context->mEffectSlotList.size() >= 1<<25) UNLIKELY - return false; - - context->mEffectSlotList.emplace_back(); - auto sublist = context->mEffectSlotList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->EffectSlots = static_cast<gsl::owner<std::array<ALeffectslot,64>*>>( - al_calloc(alignof(ALeffectslot), sizeof(*sublist->EffectSlots))); - if(!sublist->EffectSlots) UNLIKELY + try { + while(needed > count) { - context->mEffectSlotList.pop_back(); - return false; + if(context->mEffectSlotList.size() >= 1<<25) UNLIKELY + return false; + + EffectSlotSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.EffectSlots = SubListAllocator{}.allocate(1); + context->mEffectSlotList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -1004,7 +1005,7 @@ EffectSlotSubList::~EffectSlotSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALeffectslot), EffectSlots); + SubListAllocator{}.deallocate(EffectSlots, 1); EffectSlots = nullptr; } diff --git a/al/buffer.cpp b/al/buffer.cpp index 646ec8ea..2aafbf2d 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -68,6 +68,8 @@ namespace { +using SubListAllocator = typename al::allocator<std::array<ALbuffer,64>>; + std::optional<AmbiLayout> AmbiLayoutFromEnum(ALenum layout) { switch(layout) @@ -178,22 +180,21 @@ bool EnsureBuffers(ALCdevice *device, size_t needed) [](size_t cur, const BufferSubList &sublist) noexcept -> size_t { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->BufferList.size() >= 1<<25) UNLIKELY - return false; - - device->BufferList.emplace_back(); - auto sublist = device->BufferList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Buffers = static_cast<gsl::owner<std::array<ALbuffer,64>*>>(al_calloc( - alignof(ALbuffer), sizeof(*sublist->Buffers))); - if(!sublist->Buffers) UNLIKELY + try { + while(needed > count) { - device->BufferList.pop_back(); - return false; + if(device->BufferList.size() >= 1<<25) UNLIKELY + return false; + + BufferSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Buffers = SubListAllocator{}.allocate(1); + device->BufferList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -1490,7 +1491,7 @@ BufferSubList::~BufferSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALbuffer), Buffers); + SubListAllocator{}.deallocate(Buffers, 1); Buffers = nullptr; } diff --git a/al/effect.cpp b/al/effect.cpp index 2f5422fd..071b32c6 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -89,6 +89,8 @@ effect_exception::~effect_exception() = default; namespace { +using SubListAllocator = typename al::allocator<std::array<ALeffect,64>>; + auto GetDefaultProps(ALenum type) -> const EffectProps& { switch(type) @@ -126,22 +128,21 @@ bool EnsureEffects(ALCdevice *device, size_t needed) [](size_t cur, const EffectSubList &sublist) noexcept -> size_t { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->EffectList.size() >= 1<<25) UNLIKELY - return false; - - device->EffectList.emplace_back(); - auto sublist = device->EffectList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Effects = static_cast<gsl::owner<std::array<ALeffect,64>*>>(al_calloc( - alignof(ALeffect), sizeof(*sublist->Effects))); - if(!sublist->Effects) UNLIKELY + try { + while(needed > count) { - device->EffectList.pop_back(); - return false; + if(device->EffectList.size() >= 1<<25) UNLIKELY + return false; + + EffectSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Effects = SubListAllocator{}.allocate(1); + device->EffectList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -572,7 +573,7 @@ EffectSubList::~EffectSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALeffect), Effects); + SubListAllocator{}.deallocate(Effects, 1); Effects = nullptr; } diff --git a/al/filter.cpp b/al/filter.cpp index b67a65f7..528d6059 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -49,6 +49,8 @@ namespace { +using SubListAllocator = typename al::allocator<std::array<ALfilter,64>>; + class filter_exception final : public al::base_exception { ALenum mErrorCode; @@ -121,22 +123,21 @@ bool EnsureFilters(ALCdevice *device, size_t needed) [](size_t cur, const FilterSubList &sublist) noexcept -> size_t { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->FilterList.size() >= 1<<25) UNLIKELY - return false; - - device->FilterList.emplace_back(); - auto sublist = device->FilterList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Filters = static_cast<gsl::owner<std::array<ALfilter,64>*>>(al_calloc( - alignof(ALfilter), sizeof(*sublist->Filters))); - if(!sublist->Filters) UNLIKELY + try { + while(needed > count) { - device->FilterList.pop_back(); - return false; + if(device->FilterList.size() >= 1<<25) UNLIKELY + return false; + + FilterSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Filters = SubListAllocator{}.allocate(1); + device->FilterList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -700,6 +701,6 @@ FilterSubList::~FilterSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALfilter), Filters); + SubListAllocator{}.deallocate(Filters, 1); Filters = nullptr; } diff --git a/al/source.cpp b/al/source.cpp index 425acbfa..27144389 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -80,7 +80,7 @@ namespace { -using namespace std::placeholders; +using SubListAllocator = typename al::allocator<std::array<ALsource,64>>; using std::chrono::nanoseconds; using seconds_d = std::chrono::duration<double>; @@ -721,22 +721,21 @@ bool EnsureSources(ALCcontext *context, size_t needed) [](size_t cur, const SourceSubList &sublist) noexcept -> size_t { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(context->mSourceList.size() >= 1<<25) UNLIKELY - return false; - - context->mSourceList.emplace_back(); - auto sublist = context->mSourceList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Sources = static_cast<gsl::owner<std::array<ALsource,64>*>>(al_calloc( - alignof(ALsource), sizeof(*sublist->Sources))); - if(!sublist->Sources) UNLIKELY + try { + while(needed > count) { - context->mSourceList.pop_back(); - return false; + if(context->mSourceList.size() >= 1<<25) UNLIKELY + return false; + + SourceSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Sources = SubListAllocator{}.allocate(1); + context->mSourceList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -3643,7 +3642,7 @@ SourceSubList::~SourceSubList() std::destroy_at(al::to_address(Sources->begin() + idx)); } FreeMask = ~usemask; - al_free(alignof(ALsource), Sources); + SubListAllocator{}.deallocate(Sources, 1); Sources = nullptr; } diff --git a/common/almalloc.cpp b/common/almalloc.cpp deleted file mode 100644 index 2249b988..00000000 --- a/common/almalloc.cpp +++ /dev/null @@ -1,15 +0,0 @@ - -#include "config.h" - -#include "almalloc.h" - -#include <new> -#include <cstring> - - -gsl::owner<void*> al_calloc(size_t alignment, size_t size) -{ - gsl::owner<void*> ret{::operator new[](size, std::align_val_t{alignment}, std::nothrow)}; - if(ret) std::memset(ret, 0, size); - return ret; -} diff --git a/common/almalloc.h b/common/almalloc.h index a03a0f43..3b9965e6 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -17,11 +17,6 @@ namespace gsl { template<typename T> using owner = T; }; -inline void al_free(size_t alignment, gsl::owner<void*> ptr) noexcept -{ ::operator delete[](ptr, std::align_val_t{alignment}); } -[[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]] -gsl::owner<void*> al_calloc(size_t alignment, size_t size); - #define DISABLE_ALLOC \ void *operator new(size_t) = delete; \ @@ -61,18 +56,18 @@ struct allocator { static constexpr auto Alignment = std::max(AlignV, alignof(T)); static constexpr auto AlignVal = std::align_val_t{Alignment}; - using value_type = T; - using reference = T&; - using const_reference = const T&; - using pointer = T*; - using const_pointer = const T*; + using value_type = std::remove_cv_t<std::remove_reference_t<T>>; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using is_always_equal = std::true_type; - template<typename U> + template<typename U, std::enable_if_t<alignof(U) <= Alignment,bool> = true> struct rebind { - using other = std::enable_if_t<alignof(U) <= Alignment, allocator<U,Alignment>>; + using other = allocator<U,Alignment>; }; constexpr explicit allocator() noexcept = default; |