Formatting code

This commit is contained in:
Ilusha 2023-06-30 14:02:55 +00:00
parent 6dc64ce76b
commit 13a8f07e32
37 changed files with 2242 additions and 2281 deletions

View file

@ -9,103 +9,103 @@ using namespace tp;
CallStackCapture* tp::gCSCapture = nullptr;
void initializeCallStackCapture() {
gCSCapture = new CallStackCapture();
gCSCapture = new CallStackCapture();
}
void deinitializeCallStackCapture() {
delete gCSCapture;
delete gCSCapture;
}
ualni CallStackCapture::CallStack::getDepth() const {
ualni len = 0;
for (long long frame : frames) {
if (!frame) { break; }
len++;
}
ualni stripedLen = 0;
for (auto i = FRAMES_TO_SKIP_START; i < len - FRAMES_TO_SKIP_END; i++) {
if (!frames[i]) { break; }
stripedLen++;
}
return stripedLen;
ualni len = 0;
for (long long frame : frames) {
if (!frame) { break; }
len++;
}
ualni stripedLen = 0;
for (auto i = FRAMES_TO_SKIP_START; i < len - FRAMES_TO_SKIP_END; i++) {
if (!frames[i]) { break; }
stripedLen++;
}
return stripedLen;
}
ualni CallStackCapture::hashCallStack(CallStackKey key) {
auto const cs = key.cs;
ualni out = 0;
for (ualni i = 0; cs->frames[i]; i++) { out += cs->frames[i]; }
return out;
auto const cs = key.cs;
ualni out = 0;
for (ualni i = 0; cs->frames[i]; i++) { out += cs->frames[i]; }
return out;
}
bool CallStackCapture::CallStackKey::operator==(const CallStackCapture::CallStackKey &in) const {
for (auto i : Range(MAX_CALL_DEPTH_CAPTURE)) {
if (cs->frames[i] != in.cs->frames[i]) {
return false;
}
if (cs->frames[i] == 0 && in.cs->frames[i] == 0) {
return true;
}
}
DEBUG_ASSERT(0 && "Must Not Happen")
return true;
for (auto i : Range(MAX_CALL_DEPTH_CAPTURE)) {
if (cs->frames[i] != in.cs->frames[i]) {
return false;
}
if (cs->frames[i] == 0 && in.cs->frames[i] == 0) {
return true;
}
}
DEBUG_ASSERT(0 && "Must Not Happen")
return true;
}
CallStackCapture::CallStackCapture() {
static_assert(MAX_CALL_DEPTH_CAPTURE >= 1);
static_assert(MAX_CALL_CAPTURES_MEM_SIZE_MB > 0);
static_assert(MAX_DEBUG_INFO_LEN > sizeof("unresolved"));
static_assert(FRAMES_TO_SKIP_END >= 0 && FRAMES_TO_SKIP_START >= 0);
static_assert(MAX_CALL_DEPTH_CAPTURE > FRAMES_TO_SKIP_START + FRAMES_TO_SKIP_END);
static_assert(MAX_CALL_CAPTURES_MEM_SIZE_MB * 1024 * 1024 > sizeof(CallStack));
static_assert(MAX_CALL_DEPTH_CAPTURE >= 1);
static_assert(MAX_CALL_CAPTURES_MEM_SIZE_MB > 0);
static_assert(MAX_DEBUG_INFO_LEN > sizeof("unresolved"));
static_assert(FRAMES_TO_SKIP_END >= 0 && FRAMES_TO_SKIP_START >= 0);
static_assert(MAX_CALL_DEPTH_CAPTURE > FRAMES_TO_SKIP_START + FRAMES_TO_SKIP_END);
static_assert(MAX_CALL_CAPTURES_MEM_SIZE_MB * 1024 * 1024 > sizeof(CallStack));
MODULE_SANITY_CHECK(gModuleUtils)
mBuffLoad = 0;
mBuffLen = (MAX_CALL_CAPTURES_MEM_SIZE_MB * 1024 * 1024) / sizeof(CallStack);
mBuff = (CallStack*) malloc(mBuffLen * sizeof(CallStack));
MODULE_SANITY_CHECK(gModuleUtils)
mBuffLoad = 0;
mBuffLen = (MAX_CALL_CAPTURES_MEM_SIZE_MB * 1024 * 1024) / sizeof(CallStack);
mBuff = (CallStack*) malloc(mBuffLen * sizeof(CallStack));
}
const CallStackCapture::CallStack* CallStackCapture::getSnapshot() {
if (mBuffLoad > mBuffLen) {
static CallStack cs;
cs.frames[0] = 0;
return &cs;
}
if (mBuffLoad > mBuffLen) {
static CallStack cs;
cs.frames[0] = 0;
return &cs;
}
CallStack* cs = &mBuff[mBuffLoad];
platformWriteStackTrace(cs);
CallStack* cs = &mBuff[mBuffLoad];
platformWriteStackTrace(cs);
auto idx = mSnapshots.presents({ cs });
if (idx) {
return mSnapshots.getSlotVal(idx);
}
auto idx = mSnapshots.presents({ cs });
if (idx) {
return mSnapshots.getSlotVal(idx);
}
mSnapshots.put({ cs }, cs);
mSnapshots.put({ cs }, cs);
mBuffLoad++;
return cs;
mBuffLoad++;
return cs;
}
const CallStackCapture::DebugSymbols* CallStackCapture::getSymbols(FramePointer frame) {
auto idx = mSymbols.presents(frame);
if (idx) {
return &mSymbols.getSlotVal(idx);
}
auto idx = mSymbols.presents(frame);
if (idx) {
return &mSymbols.getSlotVal(idx);
}
mSymbols.put(frame, {});
auto symbols = &mSymbols.get(frame);
mSymbols.put(frame, {});
auto symbols = &mSymbols.get(frame);
platformWriteDebugSymbols(frame, symbols);
return symbols;
platformWriteDebugSymbols(frame, symbols);
return symbols;
}
void CallStackCapture::clear() {
mBuffLoad = 0;
mSnapshots.removeAll();
mSymbols.removeAll();
mBuffLoad = 0;
mSnapshots.removeAll();
mSymbols.removeAll();
}
CallStackCapture::~CallStackCapture() {
free(mBuff);
free(mBuff);
}
// ---------------------------------- Platform Depended ---------------------------------- //
@ -118,90 +118,90 @@ CallStackCapture::~CallStackCapture() {
#include <cxxabi.h>
void CallStackCapture::platformWriteStackTrace(CallStack* stack) {
auto depth = backtrace((void**)stack->frames, (int) MAX_CALL_DEPTH_CAPTURE - 1);
stack->frames[depth] = 0;
auto depth = backtrace((void**)stack->frames, (int) MAX_CALL_DEPTH_CAPTURE - 1);
stack->frames[depth] = 0;
}
static void getGetSourceFromBinaryAddress(const char* binary, const char* address, char* file, ualni* line) {
static char buff[1024];
static char buff[1024];
snprintf(buff, sizeof(buff), "addr2line -e %s -a %s", binary, address);
FILE* pipe = popen(buff, "r");
if (pipe) {
fgets(buff, sizeof(buff), pipe);
fgets(buff, sizeof(buff), pipe);
pclose(pipe);
char* linePtr = strchr(buff, ':');
snprintf(buff, sizeof(buff), "addr2line -e %s -a %s", binary, address);
FILE* pipe = popen(buff, "r");
if (pipe) {
fgets(buff, sizeof(buff), pipe);
fgets(buff, sizeof(buff), pipe);
pclose(pipe);
char* linePtr = strchr(buff, ':');
printf("%s\n", buff);
printf("%s\n", buff);
if (linePtr != nullptr && buff[0] != '?' && buff[1] != '?' && buff[2] != ':') {
*linePtr = '\0';
auto sourceLen = std::strlen(buff);
std::strcpy(file, buff + ((sourceLen > MAX_DEBUG_INFO_LEN) ? (sourceLen - MAX_DEBUG_INFO_LEN) : 0));
*line = strtoul(linePtr + 1, nullptr, 10);
return;
}
}
if (linePtr != nullptr && buff[0] != '?' && buff[1] != '?' && buff[2] != ':') {
*linePtr = '\0';
auto sourceLen = std::strlen(buff);
std::strcpy(file, buff + ((sourceLen > MAX_DEBUG_INFO_LEN) ? (sourceLen - MAX_DEBUG_INFO_LEN) : 0));
*line = strtoul(linePtr + 1, nullptr, 10);
return;
}
}
std::strcpy(file, "unresolved");
*line = 0;
std::strcpy(file, "unresolved");
*line = 0;
}
static void getDemangledName(const char* func, char* out) {
int status;
size_t funcDemangledSize = MAX_DEBUG_INFO_LEN;
char* funcDemangled = (char*)malloc(funcDemangledSize);
char* ret = abi::__cxa_demangle(func, funcDemangled, &funcDemangledSize, &status);
if (status == 0) {
funcDemangled = ret;
auto funcLen = std::strlen(funcDemangled);
std::strcpy(out, funcDemangled + ((funcLen > MAX_DEBUG_INFO_LEN) ? (funcLen - MAX_DEBUG_INFO_LEN) : 0));
free(ret);
return;
}
auto funcLen = std::strlen(func);
std::strcpy(out, func + ((funcLen > MAX_DEBUG_INFO_LEN) ? (funcLen - MAX_DEBUG_INFO_LEN) : 0));
free(funcDemangled);
int status;
size_t funcDemangledSize = MAX_DEBUG_INFO_LEN;
char* funcDemangled = (char*)malloc(funcDemangledSize);
char* ret = abi::__cxa_demangle(func, funcDemangled, &funcDemangledSize, &status);
if (status == 0) {
funcDemangled = ret;
auto funcLen = std::strlen(funcDemangled);
std::strcpy(out, funcDemangled + ((funcLen > MAX_DEBUG_INFO_LEN) ? (funcLen - MAX_DEBUG_INFO_LEN) : 0));
free(ret);
return;
}
auto funcLen = std::strlen(func);
std::strcpy(out, func + ((funcLen > MAX_DEBUG_INFO_LEN) ? (funcLen - MAX_DEBUG_INFO_LEN) : 0));
free(funcDemangled);
}
void CallStackCapture::platformWriteDebugSymbols(FramePointer frame, DebugSymbols* out) {
void* addrList[1] = { (void*) frame };
auto symbolsArray = backtrace_symbols(addrList, 1);
void* addrList[1] = { (void*) frame };
auto symbolsArray = backtrace_symbols(addrList, 1);
// 'bin(fun+addr)'
char* bin = *symbolsArray;
char* func = nullptr;
char* offset = nullptr;
// 'bin(fun+addr)'
char* bin = *symbolsArray;
char* func = nullptr;
char* offset = nullptr;
// 'bin fun+addr'
for (char *p = bin; *p; ++p) {
if (*p == '(') { *p = 0; func = p + 1; }
else if (*p == '+') { offset = p; }
else if (*p == ')' && offset) { *p = 0; }
}
// 'bin fun+addr'
for (char *p = bin; *p; ++p) {
if (*p == '(') { *p = 0; func = p + 1; }
else if (*p == '+') { offset = p; }
else if (*p == ')' && offset) { *p = 0; }
}
if (func && offset) {
getGetSourceFromBinaryAddress(bin, func, out->file, &out->line);
if (func && offset) {
getGetSourceFromBinaryAddress(bin, func, out->file, &out->line);
if (offset != func) {
*offset = 0;
getDemangledName(func, out->function);
} else {
std::strcpy(out->function, "unresolved");
}
} else {
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
}
if (offset != func) {
*offset = 0;
getDemangledName(func, out->function);
} else {
std::strcpy(out->function, "unresolved");
}
} else {
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
}
free(symbolsArray);
free(symbolsArray);
}
#else
void CallStackCapture::platformWriteStackTrace(CallStack* stack) { stack->frames[0] = 0; }
void CallStackCapture::platformWriteDebugSymbols(FramePointer frame, DebugSymbols* out) {
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
}
#endif

View file

@ -10,72 +10,72 @@ using namespace tp;
Testing tp::gTesting;
void Testing::startTest(const char* name) {
MODULE_SANITY_CHECK(gModuleUtils)
mCurrent->mSubTests.pushBack(new TestingNode{ {}, {}, name, mCurrent });
mCurrent = mCurrent->mSubTests.last()->data;
MODULE_SANITY_CHECK(gModuleUtils)
mCurrent->mSubTests.pushBack(new TestingNode{ {}, {}, name, mCurrent });
mCurrent = mCurrent->mSubTests.last()->data;
}
void Testing::endTest() {
if (mCurrent->mParent) {
mCurrent = mCurrent->mParent;
}
if (mCurrent->mParent) {
mCurrent = mCurrent->mParent;
}
}
void Testing::addFailedCheck(const FailedCheck& info) {
DEBUG_BREAK(0 && info.expression);
auto lastRecord = &mCurrent->mFailedChecks.last()->data;
if (lastRecord && lastRecord->failedCheck.file == info.file && lastRecord->failedCheck.line == info.line) {
lastRecord->times++;
return;
}
mCurrent->mFailedChecks.pushBack({ info, 1 });
DEBUG_BREAK(0 && info.expression);
auto lastRecord = &mCurrent->mFailedChecks.last()->data;
if (lastRecord && lastRecord->failedCheck.file == info.file && lastRecord->failedCheck.line == info.line) {
lastRecord->times++;
return;
}
mCurrent->mFailedChecks.pushBack({ info, 1 });
}
void Testing::reportState() {
mRootTest.updateState();
mRootTest.updateState();
printf("\n");
mRootTest.report();
printf("\n");
mRootTest.report();
}
bool Testing::hasFailed() {
mRootTest.updateState();
return mRootTest.mHasFailed;
mRootTest.updateState();
return mRootTest.mHasFailed;
}
void Testing::setRootName(const char* name) {
mRootTest.mName = name;
mRootTest.mName = name;
}
void Testing::TestingNode::updateState() {
for (auto child : mSubTests) {
child->updateState();
mHasFailed = child->mHasFailed;
if (mHasFailed) return;
}
mHasFailed = mFailedChecks.length();
for (auto child : mSubTests) {
child->updateState();
mHasFailed = child->mHasFailed;
if (mHasFailed) return;
}
mHasFailed = mFailedChecks.length();
}
void Testing::TestingNode::report(const char* path) const {
if (!mHasFailed) {
return;
}
if (!mHasFailed) {
return;
}
auto newPath = path ? std::string(path) + "/" + mName : std::string(mName);
auto newPath = path ? std::string(path) + "/" + mName : std::string(mName);
for (auto check : mFailedChecks) {
auto failedCheck = &check.data().failedCheck;
auto times = check.data().times;
printf("%s Failed - (%s) %s:%llu x%i\n", newPath.c_str(), failedCheck->expression, failedCheck->file, failedCheck->line, (halni) times);
}
for (auto check : mFailedChecks) {
auto failedCheck = &check.data().failedCheck;
auto times = check.data().times;
printf("%s Failed - (%s) %s:%llu x%i\n", newPath.c_str(), failedCheck->expression, failedCheck->file, failedCheck->line, (halni) times);
}
for (const auto& child : mSubTests) {
child->report(newPath.c_str());
}
for (const auto& child : mSubTests) {
child->report(newPath.c_str());
}
}
Testing::TestingNode::~TestingNode() {
for (const auto& child : mSubTests) {
delete child.data();
}
for (const auto& child : mSubTests) {
delete child.data();
}
}

View file

@ -5,77 +5,73 @@
#include "Timing.hpp"
#include "Utils.hpp"
#define GETTIMEMSC() \
(time_ms)( \
std::chrono::duration_cast<std::chrono::milliseconds>( \
std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()) \
.time_since_epoch()) \
.count())
#define GETTIMEMSC() (time_ms)\
(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch()).count())
#define THREAD_SLEEP(time_ms) std::this_thread::sleep_for(std::chrono::milliseconds(time_ms))
tp::time_ms tp::gCurrentTime = tp::get_time();
namespace tp {
time_ms get_time() {
gCurrentTime = GETTIMEMSC();
return gCurrentTime;
}
time_ms get_time() {
gCurrentTime = GETTIMEMSC();
return gCurrentTime;
}
void sleep(time_ms mDuration) {
THREAD_SLEEP(mDuration);
}
void sleep(time_ms mDuration) {
THREAD_SLEEP(mDuration);
}
Timer::Timer() {
mDuration = 0;
mStart = GETTIMEMSC();
}
Timer::Timer() {
mDuration = 0;
mStart = GETTIMEMSC();
}
Timer::Timer(time_ms mDuration) {
mStart = GETTIMEMSC();
this->mDuration = mDuration;
}
Timer::Timer(time_ms mDuration) {
mStart = GETTIMEMSC();
this->mDuration = mDuration;
}
bool Timer::isTimeout() {
return mDuration < GETTIMEMSC() - mStart;
}
bool Timer::isTimeout() {
return mDuration < GETTIMEMSC() - mStart;
}
void Timer::reset() {
mStart = GETTIMEMSC();
}
void Timer::reset() {
mStart = GETTIMEMSC();
}
time_ms Timer::timePassed() {
return GETTIMEMSC() - mStart;
}
time_ms Timer::timePassed() {
return GETTIMEMSC() - mStart;
}
time_ms Timer::remainder() {
return mDuration - (GETTIMEMSC() - mStart);
}
time_ms Timer::remainder() {
return mDuration - (GETTIMEMSC() - mStart);
}
time_ms Timer::start() { return mStart; }
time_ms Timer::duration() { return mDuration; }
void Timer::setDuration(time_ms dur) { mDuration = dur; }
time_ms Timer::start() { return mStart; }
time_ms Timer::duration() { return mDuration; }
void Timer::setDuration(time_ms dur) { mDuration = dur; }
void Timer::wait() {
if (!isTimeout()) {
sleep(remainder());
}
}
void Timer::wait() {
if (!isTimeout()) {
sleep(remainder());
}
}
float Timer::easeIn(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((1.1f * x) / (x + 0.1f), 0.f, 1.f);
}
float Timer::easeIn(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((1.1f * x) / (x + 0.1f), 0.f, 1.f);
}
float Timer::easeOut(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((0.1f * (1 - x)) / (x + 0.1f), 0.f, 1.f);
}
float Timer::easeOut(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((0.1f * (1 - x)) / (x + 0.1f), 0.f, 1.f);
}
}

View file

@ -11,84 +11,84 @@ void initializeCallStackCapture();
void deinitializeCallStackCapture();
static bool initialize(const tp::ModuleManifest* self) {
initializeCallStackCapture();
return true;
initializeCallStackCapture();
return true;
}
static void deinitialize(const tp::ModuleManifest* self) {
deinitializeCallStackCapture();
tp::gTesting.reportState();
if (tp::gTesting.hasFailed()) { exit(1); }
deinitializeCallStackCapture();
tp::gTesting.reportState();
if (tp::gTesting.hasFailed()) { exit(1); }
}
namespace tp {
static ModuleManifest* sModuleUtilsDeps[] = { &gModuleContainers, nullptr };
ModuleManifest gModuleUtils = ModuleManifest("Utils", initialize, deinitialize, sModuleUtilsDeps);
static ModuleManifest* sModuleUtilsDeps[] = { &gModuleContainers, nullptr };
ModuleManifest gModuleUtils = ModuleManifest("Utils", initialize, deinitialize, sModuleUtilsDeps);
void memsetv(void* p, uhalni bytesize, uint1 val) {
MODULE_SANITY_CHECK(gModuleBase)
void memsetv(void* p, uhalni bytesize, uint1 val) {
MODULE_SANITY_CHECK(gModuleBase)
alni alignedval = 0;
for (ualni idx = 0; idx < sizeof(alni); idx++) {
((uint1*) &alignedval)[idx] = val;
}
alni alignedval = 0;
for (ualni idx = 0; idx < sizeof(alni); idx++) {
((uint1*) &alignedval)[idx] = val;
}
ualni alignedlen = bytesize / sizeof(alni);
for (ualni idx = 0; idx < alignedlen; idx++) {
((alni*) p)[idx] = alignedval;
}
ualni alignedlen = bytesize / sizeof(alni);
for (ualni idx = 0; idx < alignedlen; idx++) {
((alni*) p)[idx] = alignedval;
}
ualni unalignedlen = bytesize - (alignedlen * sizeof(alni));
for (ualni idx = 0; idx < unalignedlen; idx++) {
((uint1*) p)[bytesize - idx - 1] = val;
}
}
ualni unalignedlen = bytesize - (alignedlen * sizeof(alni));
for (ualni idx = 0; idx < unalignedlen; idx++) {
((uint1*) p)[bytesize - idx - 1] = val;
}
}
void memcp(void* left, const void* right, uhalni len) {
MODULE_SANITY_CHECK(gModuleBase)
void memcp(void* left, const void* right, uhalni len) {
MODULE_SANITY_CHECK(gModuleBase)
ualni alignedlen = len / sizeof(alni);
for (ualni idx = 0; idx < alignedlen; idx++) {
((alni*) left)[idx] = ((alni*) right)[idx];
}
ualni alignedlen = len / sizeof(alni);
for (ualni idx = 0; idx < alignedlen; idx++) {
((alni*) left)[idx] = ((alni*) right)[idx];
}
ualni unalignedlen = len - (alignedlen * sizeof(alni));
for (ualni idx = 0; idx < unalignedlen; idx++) {
((uint1*) left)[len - idx - 1] = ((uint1*) right)[len - idx - 1];
}
}
ualni unalignedlen = len - (alignedlen * sizeof(alni));
for (ualni idx = 0; idx < unalignedlen; idx++) {
((uint1*) left)[len - idx - 1] = ((uint1*) right)[len - idx - 1];
}
}
int1 memecomp(const void* left, const void* right, uhalni len) {
MODULE_SANITY_CHECK(gModuleBase)
if (!len) return 0;
int1 memecomp(const void* left, const void* right, uhalni len) {
MODULE_SANITY_CHECK(gModuleBase)
if (!len) return 0;
ualni alignedLength = len / sizeof(alni);
for (ualni idx = 0; idx < alignedLength; idx++) {
if (((alni*) left)[idx] == ((alni*) right)[idx]) {
continue;
}
if (((alni*) left)[idx] > ((alni*) right)[idx]) {
return 1;
}
return -1;
}
ualni alignedLength = len / sizeof(alni);
for (ualni idx = 0; idx < alignedLength; idx++) {
if (((alni*) left)[idx] == ((alni*) right)[idx]) {
continue;
}
if (((alni*) left)[idx] > ((alni*) right)[idx]) {
return 1;
}
return -1;
}
ualni unalignedLength = len - (alignedLength * sizeof(alni));
for (ualni idx = 0; idx < unalignedLength; idx++) {
if (((uint1*) left)[len - idx - 1] == ((uint1*) right)[len - idx - 1]) {
continue;
}
if (((uint1*) left)[len - idx - 1] > ((uint1*) right)[len - idx - 1]) {
return 1;
}
return -1;
}
return 0;
}
ualni unalignedLength = len - (alignedLength * sizeof(alni));
for (ualni idx = 0; idx < unalignedLength; idx++) {
if (((uint1*) left)[len - idx - 1] == ((uint1*) right)[len - idx - 1]) {
continue;
}
if (((uint1*) left)[len - idx - 1] > ((uint1*) right)[len - idx - 1]) {
return 1;
}
return -1;
}
return 0;
}
alnf randf() {
alnf r = static_cast<alnf>(std::rand()) / static_cast<alnf>(RAND_MAX);
return r;
}
alnf randf() {
alnf r = static_cast<alnf>(std::rand()) / static_cast<alnf>(RAND_MAX);
return r;
}
}