|
@@ -10,15 +10,15 @@
|
|
|
#include "ErrorSimulator.h"
|
|
|
#include "core/Logger.h"
|
|
|
|
|
|
-extern inline size_t coreMaxSize(size_t a, size_t b);
|
|
|
-extern inline size_t coreMinSize(size_t a, size_t b);
|
|
|
+extern inline size_t maxSize(size_t a, size_t b);
|
|
|
+extern inline size_t minSize(size_t a, size_t b);
|
|
|
|
|
|
-static CoreExitHandler exitHandler = nullptr;
|
|
|
+static ExitHandler exitHandler = nullptr;
|
|
|
static void* exitData = nullptr;
|
|
|
-static CoreOutOfMemoryHandler outOfMemoryHandler = nullptr;
|
|
|
+static OutOfMemoryHandler outOfMemoryHandler = nullptr;
|
|
|
static void* outOfMemoryData = nullptr;
|
|
|
|
|
|
-size_t corePopCount(u64 u) {
|
|
|
+size_t popCount(u64 u) {
|
|
|
static const u64 map[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
|
|
|
size_t sum = 0;
|
|
|
for(size_t i = 0; i < sizeof(u64) * 8; i += 4) {
|
|
@@ -27,7 +27,7 @@ size_t corePopCount(u64 u) {
|
|
|
return sum;
|
|
|
}
|
|
|
|
|
|
-size_t coreToString(char* buffer, size_t n, const char* format, ...) {
|
|
|
+size_t toString(char* buffer, size_t n, const char* format, ...) {
|
|
|
va_list args;
|
|
|
va_start(args, format);
|
|
|
int w = vsnprintf(buffer, n, format, args);
|
|
@@ -35,15 +35,15 @@ size_t coreToString(char* buffer, size_t n, const char* format, ...) {
|
|
|
return w < 0 ? 0 : (size_t)w;
|
|
|
}
|
|
|
|
|
|
-size_t coreToStringSize(const void* p, char* buffer, size_t n) {
|
|
|
- return coreToString(buffer, n, "%zu", *(const size_t*)p);
|
|
|
+size_t toStringSize(const void* p, char* buffer, size_t n) {
|
|
|
+ return toString(buffer, n, "%zu", *(const size_t*)p);
|
|
|
}
|
|
|
|
|
|
-size_t coreToStringInt(const void* p, char* buffer, size_t n) {
|
|
|
- return coreToString(buffer, n, "%d", *(const int*)p);
|
|
|
+size_t toStringInt(const void* p, char* buffer, size_t n) {
|
|
|
+ return toString(buffer, n, "%d", *(const int*)p);
|
|
|
}
|
|
|
|
|
|
-void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
|
|
|
+void stringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
|
|
|
*w += shift;
|
|
|
if(*n > shift) {
|
|
|
*buffer += shift;
|
|
@@ -55,7 +55,7 @@ void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
|
|
|
|
|
|
[[noreturn]] void coreExitWithHandler(const char* file, int line, int value) {
|
|
|
if(value != 0) {
|
|
|
- file = coreGetShortFileName(file);
|
|
|
+ file = getShortFileName(file);
|
|
|
CORE_LOG_ERROR("Exit from %s:%d with value %d", file, line, value);
|
|
|
}
|
|
|
if(exitHandler != nullptr) {
|
|
@@ -64,12 +64,12 @@ void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
|
|
|
exit(value);
|
|
|
}
|
|
|
|
|
|
-void coreSetExitHandler(CoreExitHandler eh, void* data) {
|
|
|
+void setExitHandler(ExitHandler eh, void* data) {
|
|
|
exitHandler = eh;
|
|
|
exitData = data;
|
|
|
}
|
|
|
|
|
|
-void coreSetOutOfMemoryHandler(CoreOutOfMemoryHandler h, void* data) {
|
|
|
+void setOutOfMemoryHandler(OutOfMemoryHandler h, void* data) {
|
|
|
outOfMemoryHandler = h;
|
|
|
outOfMemoryData = data;
|
|
|
}
|
|
@@ -82,7 +82,7 @@ static void* exitOnNull(void* p, size_t n) {
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
-static void* coreRealAllocate(size_t n) {
|
|
|
+static void* RealAllocate(size_t n) {
|
|
|
void* p = malloc(n);
|
|
|
while(p == nullptr && outOfMemoryHandler != nullptr) {
|
|
|
outOfMemoryHandler(outOfMemoryData);
|
|
@@ -91,7 +91,7 @@ static void* coreRealAllocate(size_t n) {
|
|
|
return exitOnNull(p, n);
|
|
|
}
|
|
|
|
|
|
-static void* coreRealReallocate(void* oldP, size_t n) {
|
|
|
+static void* RealReallocate(void* oldP, size_t n) {
|
|
|
if(n <= 0) {
|
|
|
free(oldP);
|
|
|
return nullptr;
|
|
@@ -106,7 +106,7 @@ static void* coreRealReallocate(void* oldP, size_t n) {
|
|
|
return exitOnNull(p, n);
|
|
|
}
|
|
|
|
|
|
-static void coreRealFree(void* p) {
|
|
|
+static void RealFree(void* p) {
|
|
|
free(p);
|
|
|
}
|
|
|
|
|
@@ -114,26 +114,26 @@ static void coreRealFree(void* p) {
|
|
|
static const u8 CANARY[16] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
|
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
|
|
|
|
|
|
-struct CoreMemoryInfo;
|
|
|
-typedef struct CoreMemoryInfo CoreMemoryInfo;
|
|
|
-struct CoreMemoryInfo {
|
|
|
- CoreMemoryInfo* next;
|
|
|
- CoreMemoryInfo* previous;
|
|
|
+struct MemoryInfo;
|
|
|
+typedef struct MemoryInfo MemoryInfo;
|
|
|
+struct MemoryInfo {
|
|
|
+ MemoryInfo* next;
|
|
|
+ MemoryInfo* previous;
|
|
|
size_t size;
|
|
|
int line;
|
|
|
char buffer[64 - 2 * sizeof(void*) - sizeof(size_t) - sizeof(int)];
|
|
|
char canary[sizeof(CANARY)];
|
|
|
};
|
|
|
-static_assert(sizeof(CoreMemoryInfo) == 80, "memory info has invalid size");
|
|
|
-static CoreMemoryInfo* headMemoryInfo = nullptr;
|
|
|
+static_assert(sizeof(MemoryInfo) == 80, "memory info has invalid size");
|
|
|
+static MemoryInfo* headMemoryInfo = nullptr;
|
|
|
|
|
|
-static void addMemoryInfo(CoreMemoryInfo* info, const char* file, int line,
|
|
|
+static void addMemoryInfo(MemoryInfo* info, const char* file, int line,
|
|
|
size_t n) {
|
|
|
info->next = nullptr;
|
|
|
info->previous = nullptr;
|
|
|
info->size = n;
|
|
|
info->line = line;
|
|
|
- strncpy(info->buffer, coreGetShortFileName(file), sizeof(info->buffer));
|
|
|
+ strncpy(info->buffer, getShortFileName(file), sizeof(info->buffer));
|
|
|
memcpy(info->canary, CANARY, sizeof(CANARY));
|
|
|
memcpy((char*)info + n - sizeof(CANARY), CANARY, sizeof(CANARY));
|
|
|
if(headMemoryInfo == nullptr) {
|
|
@@ -145,7 +145,7 @@ static void addMemoryInfo(CoreMemoryInfo* info, const char* file, int line,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static void removeMemoryInfo(CoreMemoryInfo* info) {
|
|
|
+static void removeMemoryInfo(MemoryInfo* info) {
|
|
|
if(info->previous == nullptr) {
|
|
|
if(info->next == nullptr) {
|
|
|
headMemoryInfo = nullptr;
|
|
@@ -164,27 +164,27 @@ static void removeMemoryInfo(CoreMemoryInfo* info) {
|
|
|
}
|
|
|
|
|
|
void* coreDebugAllocate(const char* file, int line, size_t n) {
|
|
|
- n += sizeof(CoreMemoryInfo) + sizeof(CANARY);
|
|
|
- void* p = coreRealAllocate(n + sizeof(CANARY));
|
|
|
+ n += sizeof(MemoryInfo) + sizeof(CANARY);
|
|
|
+ void* p = RealAllocate(n + sizeof(CANARY));
|
|
|
addMemoryInfo(p, file, line, n);
|
|
|
- return (char*)p + sizeof(CoreMemoryInfo);
|
|
|
+ return (char*)p + sizeof(MemoryInfo);
|
|
|
}
|
|
|
|
|
|
void* coreDebugReallocate(const char* file, int line, void* p, size_t n) {
|
|
|
if(n > 0) {
|
|
|
- n += sizeof(CoreMemoryInfo) + sizeof(CANARY);
|
|
|
+ n += sizeof(MemoryInfo) + sizeof(CANARY);
|
|
|
}
|
|
|
void* rp = (void*)p;
|
|
|
if(rp != nullptr) {
|
|
|
- rp = (char*)rp - sizeof(CoreMemoryInfo);
|
|
|
+ rp = (char*)rp - sizeof(MemoryInfo);
|
|
|
removeMemoryInfo(rp);
|
|
|
}
|
|
|
- void* np = coreRealReallocate(rp, n);
|
|
|
+ void* np = RealReallocate(rp, n);
|
|
|
if(np == nullptr) {
|
|
|
return nullptr;
|
|
|
}
|
|
|
addMemoryInfo(np, file, line, n);
|
|
|
- return (char*)np + sizeof(CoreMemoryInfo);
|
|
|
+ return (char*)np + sizeof(MemoryInfo);
|
|
|
}
|
|
|
|
|
|
static bool checkCanary(void* p) {
|
|
@@ -195,51 +195,51 @@ void coreFreeDebug(const char* file, int line, void* p) {
|
|
|
if(p == nullptr) {
|
|
|
return;
|
|
|
}
|
|
|
- void* w = (char*)p - sizeof(CoreMemoryInfo);
|
|
|
- CoreMemoryInfo* rp = w;
|
|
|
+ void* w = (char*)p - sizeof(MemoryInfo);
|
|
|
+ MemoryInfo* rp = w;
|
|
|
if(checkCanary(rp->canary)) {
|
|
|
- file = coreGetShortFileName(file);
|
|
|
+ file = getShortFileName(file);
|
|
|
CORE_LOG_ERROR("Free at %s:%d violated pre canary", file, line);
|
|
|
CORE_EXIT(1);
|
|
|
} else if(checkCanary((char*)rp + rp->size - sizeof(CANARY))) {
|
|
|
- file = coreGetShortFileName(file);
|
|
|
+ file = getShortFileName(file);
|
|
|
CORE_LOG_ERROR("Free at %s:%d violated post canary", file, line);
|
|
|
CORE_EXIT(1);
|
|
|
}
|
|
|
removeMemoryInfo(rp);
|
|
|
- coreRealFree(rp);
|
|
|
+ RealFree(rp);
|
|
|
}
|
|
|
|
|
|
-void corePrintMemoryReport() {
|
|
|
- for(CoreMemoryInfo* i = headMemoryInfo; i != nullptr; i = i->next) {
|
|
|
+void printMemoryReport() {
|
|
|
+ for(MemoryInfo* i = headMemoryInfo; i != nullptr; i = i->next) {
|
|
|
CORE_LOG_ERROR("%s:%d was not freed", i->buffer, i->line);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
-void* coreAllocate(size_t n) {
|
|
|
- return coreRealAllocate(n);
|
|
|
+void* Allocate(size_t n) {
|
|
|
+ return RealAllocate(n);
|
|
|
}
|
|
|
|
|
|
-void* coreReallocate(void* p, size_t n) {
|
|
|
- return coreRealReallocate(p, n);
|
|
|
+void* Reallocate(void* p, size_t n) {
|
|
|
+ return RealReallocate(p, n);
|
|
|
}
|
|
|
|
|
|
-void coreFree(void* p) {
|
|
|
- coreRealFree(p);
|
|
|
+void Free(void* p) {
|
|
|
+ RealFree(p);
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
-bool coreSleepNanos(i64 nanos) {
|
|
|
+bool sleepNanos(i64 nanos) {
|
|
|
struct timespec t;
|
|
|
t.tv_nsec = nanos % 1'000'000'000;
|
|
|
t.tv_sec = nanos / 1'000'000'000;
|
|
|
return thrd_sleep(&t, nullptr) != 0;
|
|
|
}
|
|
|
|
|
|
-i64 coreNanos(void) {
|
|
|
+i64 getNanos(void) {
|
|
|
struct timespec ts;
|
|
|
if(timespec_get(&ts, TIME_UTC) == 0 || CORE_TIME_GET_FAIL) {
|
|
|
return -1;
|