fixed macOS segfault correct way
This commit is contained in:
parent
06f6550624
commit
5997cbd714
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#else
|
||||||
|
#include <cstdlib>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
@ -51,20 +53,23 @@ namespace util {
|
|||||||
std::queue<void*> freeObjects;
|
std::queue<void*> freeObjects;
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
|
|
||||||
bool allocateNew() {
|
void allocateNew() {
|
||||||
std::unique_ptr<void, AlignedDeleter> ptr(
|
constexpr size_t alignment = alignof(T) < sizeof(void*) ? sizeof(void*) : alignof(T);
|
||||||
|
constexpr size_t size = sizeof(T);
|
||||||
|
void* rawPtr = nullptr;
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
_aligned_malloc(sizeof(T), alignof(T))
|
rawPtr = _aligned_malloc(size, alignment);
|
||||||
#else
|
#else
|
||||||
std::aligned_alloc(alignof(T), sizeof(T))
|
if (posix_memalign(&rawPtr, alignment, size) != 0) {
|
||||||
#endif
|
rawPtr = nullptr;
|
||||||
);
|
|
||||||
if (ptr == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
if (rawPtr == nullptr) {
|
||||||
|
throw std::bad_alloc();
|
||||||
|
}
|
||||||
|
std::unique_ptr<void, AlignedDeleter> ptr(rawPtr);
|
||||||
freeObjects.push(ptr.get());
|
freeObjects.push(ptr.get());
|
||||||
objects.push_back(std::move(ptr));
|
objects.push_back(std::move(ptr));
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user