fixed macOS segfault correct way
Some checks failed
Build / Build (push) Failing after 17s
MSVC Build / build-windows (windows-latest) (push) Has been cancelled

This commit is contained in:
ShiftyX1 2025-12-08 19:23:57 +03:00
parent 06f6550624
commit 5997cbd714

View File

@ -7,6 +7,8 @@
#if defined(_WIN32)
#include <malloc.h>
#else
#include <cstdlib>
#endif
namespace util {
@ -51,20 +53,23 @@ namespace util {
std::queue<void*> freeObjects;
std::mutex mutex;
bool allocateNew() {
std::unique_ptr<void, AlignedDeleter> ptr(
void allocateNew() {
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))
#endif
);
if (ptr == nullptr) {
return false;
if (posix_memalign(&rawPtr, alignment, size) != 0) {
rawPtr = nullptr;
}
#endif
if (rawPtr == nullptr) {
throw std::bad_alloc();
}
std::unique_ptr<void, AlignedDeleter> ptr(rawPtr);
freeObjects.push(ptr.get());
objects.push_back(std::move(ptr));
return true;
}
};
}