fixed macOS segfault correct way
This commit is contained in:
parent
06f6550624
commit
5997cbd714
@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user