diff options
author | Chris Robinson <[email protected]> | 2024-01-01 00:39:33 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2024-01-01 00:39:33 -0800 |
commit | 2f2edb326128c56e269a171961d991d8d7936e4f (patch) | |
tree | d71b748970e81ef91d2d21f0ca824843f18ebaee | |
parent | 32de7572ea2f97fd87f94cbf0843b6aaa551b560 (diff) |
Use standard operator new[] and delete[] for aligned allocations
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | al/auxeffectslot.cpp | 2 | ||||
-rw-r--r-- | al/buffer.cpp | 2 | ||||
-rw-r--r-- | al/effect.cpp | 2 | ||||
-rw-r--r-- | al/filter.cpp | 2 | ||||
-rw-r--r-- | al/source.cpp | 2 | ||||
-rw-r--r-- | common/almalloc.cpp | 50 | ||||
-rw-r--r-- | common/almalloc.h | 5 | ||||
-rw-r--r-- | config.h.in | 6 |
9 files changed, 9 insertions, 64 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f53236fb..14ee18f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -565,8 +565,6 @@ if(HAVE_INTRIN_H) }" HAVE_CPUID_INTRINSIC) endif() -check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN) -check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC) check_symbol_exists(proc_pidpath libproc.h HAVE_PROC_PIDPATH) if(NOT WIN32) diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index 695c5788..d95748d7 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -1004,7 +1004,7 @@ EffectSlotSubList::~EffectSlotSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(EffectSlots); + al_free(alignof(ALeffectslot), EffectSlots); EffectSlots = nullptr; } diff --git a/al/buffer.cpp b/al/buffer.cpp index 46ef8ece..646ec8ea 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -1490,7 +1490,7 @@ BufferSubList::~BufferSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Buffers); + al_free(alignof(ALbuffer), Buffers); Buffers = nullptr; } diff --git a/al/effect.cpp b/al/effect.cpp index 1024de80..2f5422fd 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -572,7 +572,7 @@ EffectSubList::~EffectSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Effects); + al_free(alignof(ALeffect), Effects); Effects = nullptr; } diff --git a/al/filter.cpp b/al/filter.cpp index 0a999169..b67a65f7 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -700,6 +700,6 @@ FilterSubList::~FilterSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Filters); + al_free(alignof(ALfilter), Filters); Filters = nullptr; } diff --git a/al/source.cpp b/al/source.cpp index af58379b..425acbfa 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3643,7 +3643,7 @@ SourceSubList::~SourceSubList() std::destroy_at(al::to_address(Sources->begin() + idx)); } FreeMask = ~usemask; - al_free(Sources); + al_free(alignof(ALsource), Sources); Sources = nullptr; } diff --git a/common/almalloc.cpp b/common/almalloc.cpp index 71953d8b..2249b988 100644 --- a/common/almalloc.cpp +++ b/common/almalloc.cpp @@ -3,59 +3,13 @@ #include "almalloc.h" -#include <cassert> -#include <cstddef> -#include <cstdlib> +#include <new> #include <cstring> -#include <memory> -#ifdef HAVE_MALLOC_H -#include <malloc.h> -#endif -gsl::owner<void*> al_malloc(size_t alignment, size_t size) -{ - assert((alignment & (alignment-1)) == 0); - alignment = std::max(alignment, alignof(std::max_align_t)); - -#if defined(HAVE_POSIX_MEMALIGN) - gsl::owner<void*> ret{}; - if(posix_memalign(&ret, alignment, size) == 0) - return ret; - return nullptr; -#elif defined(HAVE__ALIGNED_MALLOC) - return _aligned_malloc(size, alignment); -#else - size_t total_size{size + alignment-1 + sizeof(void*)}; - void *base{std::malloc(total_size)}; - if(base != nullptr) - { - void *aligned_ptr{static_cast<char*>(base) + sizeof(void*)}; - total_size -= sizeof(void*); - - std::align(alignment, size, aligned_ptr, total_size); - *(static_cast<void**>(aligned_ptr)-1) = base; - base = aligned_ptr; - } - return base; -#endif -} - gsl::owner<void*> al_calloc(size_t alignment, size_t size) { - gsl::owner<void*> ret{al_malloc(alignment, size)}; + gsl::owner<void*> ret{::operator new[](size, std::align_val_t{alignment}, std::nothrow)}; if(ret) std::memset(ret, 0, size); return ret; } - -void al_free(gsl::owner<void*> ptr) noexcept -{ -#if defined(HAVE_POSIX_MEMALIGN) - std::free(ptr); -#elif defined(HAVE__ALIGNED_MALLOC) - _aligned_free(ptr); -#else - if(ptr != nullptr) - std::free(*(static_cast<gsl::owner<void*>*>(ptr) - 1)); -#endif -} diff --git a/common/almalloc.h b/common/almalloc.h index 82f050e4..a03a0f43 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -17,9 +17,8 @@ namespace gsl { template<typename T> using owner = T; }; -void al_free(gsl::owner<void*> ptr) noexcept; -[[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]] -gsl::owner<void*> al_malloc(size_t alignment, size_t size); +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); diff --git a/config.h.in b/config.h.in index 20df5b46..19cb79e0 100644 --- a/config.h.in +++ b/config.h.in @@ -7,12 +7,6 @@ /* Define if HRTF data is embedded in the library */ #cmakedefine ALSOFT_EMBED_HRTF_DATA -/* Define if we have the posix_memalign function */ -#cmakedefine HAVE_POSIX_MEMALIGN - -/* Define if we have the _aligned_malloc function */ -#cmakedefine HAVE__ALIGNED_MALLOC - /* Define if we have the proc_pidpath function */ #cmakedefine HAVE_PROC_PIDPATH |