diff --git a/src/util/ObjectsPool.hpp b/src/util/ObjectsPool.hpp index 55238959..dc3fdac4 100644 --- a/src/util/ObjectsPool.hpp +++ b/src/util/ObjectsPool.hpp @@ -4,9 +4,12 @@ #include #include #include +#include #if defined(_WIN32) #include +#else +#include #endif namespace util { @@ -50,13 +53,21 @@ namespace util { std::mutex mutex; void allocateNew() { - std::unique_ptr ptr( + // Use posix_memalign on POSIX systems as aligned_alloc has stricter requirements + constexpr size_t alignment = alignof(T) < sizeof(void*) ? sizeof(void*) : alignof(T); + constexpr size_t size = sizeof(T); + void* rawPtr = nullptr; #if defined(_WIN32) - _aligned_malloc(sizeof(T), alignof(T)) + rawPtr = _aligned_malloc(size, alignment); #else - std::aligned_alloc(alignof(T), sizeof(T)) + if (posix_memalign(&rawPtr, alignment, size) != 0) { + rawPtr = nullptr; + } #endif - ); + if (rawPtr == nullptr) { + throw std::bad_alloc(); + } + std::unique_ptr ptr(rawPtr); freeObjects.push(ptr.get()); objects.push_back(std::move(ptr)); }