|
@@ -6,6 +6,7 @@
|
|
|
#include <ctime>
|
|
|
|
|
|
#include "core/Logger.hpp"
|
|
|
+#include "core/Thread.hpp"
|
|
|
|
|
|
static Core::ExitHandler exitHandler = nullptr;
|
|
|
static void* exitData = nullptr;
|
|
@@ -31,6 +32,14 @@ void Core::setExitHandler(ExitHandler eh, void* data) {
|
|
|
void Core::setOutOfMemoryHandler(OutOfMemoryHandler h, void* data) {
|
|
|
outOfMemoryHandler = h;
|
|
|
outOfMemoryData = data;
|
|
|
+ std::set_new_handler([]() {
|
|
|
+ if(outOfMemoryHandler != nullptr) {
|
|
|
+ outOfMemoryHandler(outOfMemoryData);
|
|
|
+ } else {
|
|
|
+ LOG_ERROR("Out of memory");
|
|
|
+ EXIT(1);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
static void* exitOnNull(void* p, size_t n) {
|
|
@@ -72,6 +81,7 @@ static void realFree(void* p) {
|
|
|
#ifdef CHECK_MEMORY
|
|
|
static const u8 CANARY[16] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
|
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
|
|
|
+static Core::Mutex memoryInfoMutex;
|
|
|
|
|
|
struct MemoryInfo {
|
|
|
MemoryInfo* next;
|
|
@@ -92,9 +102,8 @@ static void addMemoryInfo(MemoryInfo* i, const char* file, int line, size_t n) {
|
|
|
i->line = line;
|
|
|
snprintf(i->buffer, sizeof(i->buffer), "%s", Core::getShortFileName(file));
|
|
|
memcpy(i->canary, CANARY, sizeof(CANARY));
|
|
|
- memcpy(
|
|
|
- reinterpret_cast<char*>(i) + n - sizeof(CANARY), CANARY,
|
|
|
- sizeof(CANARY));
|
|
|
+ memcpy(reinterpret_cast<char*>(i) + n, CANARY, sizeof(CANARY));
|
|
|
+ Core::MutexGuard mg(memoryInfoMutex);
|
|
|
if(headMemoryInfo == nullptr) {
|
|
|
headMemoryInfo = i;
|
|
|
} else {
|
|
@@ -105,6 +114,7 @@ static void addMemoryInfo(MemoryInfo* i, const char* file, int line, size_t n) {
|
|
|
}
|
|
|
|
|
|
static void removeMemoryInfo(MemoryInfo* info) {
|
|
|
+ Core::MutexGuard mg(memoryInfoMutex);
|
|
|
if(info->previous == nullptr) {
|
|
|
if(info->next == nullptr) {
|
|
|
headMemoryInfo = nullptr;
|
|
@@ -123,7 +133,7 @@ static void removeMemoryInfo(MemoryInfo* info) {
|
|
|
}
|
|
|
|
|
|
void* Core::debugAllocateRaw(const char* file, int line, size_t n) {
|
|
|
- n += sizeof(MemoryInfo) + sizeof(CANARY);
|
|
|
+ n += sizeof(MemoryInfo);
|
|
|
void* p = realAllocate(n + sizeof(CANARY));
|
|
|
addMemoryInfo(static_cast<MemoryInfo*>(p), file, line, n);
|
|
|
return static_cast<char*>(p) + sizeof(MemoryInfo);
|
|
@@ -148,7 +158,7 @@ void* Core::debugReallocateRaw(const char* file, int line, void* p, size_t n) {
|
|
|
if(np == nullptr) {
|
|
|
return nullptr;
|
|
|
}
|
|
|
- addMemoryInfo(static_cast<MemoryInfo*>(np), file, line, n);
|
|
|
+ addMemoryInfo(static_cast<MemoryInfo*>(np), file, line, n - sizeof(CANARY));
|
|
|
return static_cast<char*>(np) + sizeof(MemoryInfo);
|
|
|
}
|
|
|
|
|
@@ -166,8 +176,7 @@ void Core::debugDeallocateRaw(void* p) {
|
|
|
if(checkCanary(rp->canary)) {
|
|
|
LOG_ERROR("Free at #:# violated pre canary", rp->buffer, rp->line);
|
|
|
EXIT(1);
|
|
|
- } else if(checkCanary(
|
|
|
- reinterpret_cast<char*>(rp) + rp->size - sizeof(CANARY))) {
|
|
|
+ } else if(checkCanary(reinterpret_cast<char*>(rp) + rp->size)) {
|
|
|
LOG_ERROR("Free at #:# violated post canary", rp->buffer, rp->line);
|
|
|
EXIT(1);
|
|
|
}
|
|
@@ -176,6 +185,7 @@ void Core::debugDeallocateRaw(void* p) {
|
|
|
}
|
|
|
|
|
|
void Core::printMemoryReport() {
|
|
|
+ Core::MutexGuard mg(memoryInfoMutex);
|
|
|
size_t counter = 0;
|
|
|
for(MemoryInfo* i = headMemoryInfo; i != nullptr; i = i->next) {
|
|
|
if(i->line < 0) {
|