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;
}
}

View file

@ -12,110 +12,110 @@
namespace tp {
class CallStackCapture {
public:
typedef tp::alni FramePointer;
public:
typedef tp::alni FramePointer;
class CallStack {
friend CallStackCapture;
FramePointer frames[MAX_CALL_DEPTH_CAPTURE];
class CallStack {
friend CallStackCapture;
FramePointer frames[MAX_CALL_DEPTH_CAPTURE];
public:
[[nodiscard]] ualni getDepth() const;
public:
[[nodiscard]] ualni getDepth() const;
class Iterator {
const FramePointer* mFrame;
public:
explicit Iterator(const FramePointer* frame) : mFrame(frame) {};
FramePointer getFrame() { return *mFrame; }
bool operator==(const Iterator& in) const { return in.mFrame == mFrame; }
void operator++() { mFrame++; }
const Iterator& operator*() const { return *this; }
};
class Iterator {
const FramePointer* mFrame;
public:
explicit Iterator(const FramePointer* frame) : mFrame(frame) {};
FramePointer getFrame() { return *mFrame; }
bool operator==(const Iterator& in) const { return in.mFrame == mFrame; }
void operator++() { mFrame++; }
const Iterator& operator*() const { return *this; }
};
[[nodiscard]] Iterator begin() const { return Iterator(frames + FRAMES_TO_SKIP_START); }
[[nodiscard]] Iterator end() const { return Iterator(frames + FRAMES_TO_SKIP_START + getDepth()); }
};
[[nodiscard]] Iterator begin() const { return Iterator(frames + FRAMES_TO_SKIP_START); }
[[nodiscard]] Iterator end() const { return Iterator(frames + FRAMES_TO_SKIP_START + getDepth()); }
};
class DebugSymbols {
friend CallStackCapture;
char function[MAX_DEBUG_INFO_LEN + 1] = { 0 };
char file[MAX_DEBUG_INFO_LEN + 1] = { 0 };
ualni line = 0;
public:
[[nodiscard]] const char* getFunc() const { return function; }
[[nodiscard]] const char* getFile() const { return file; }
[[nodiscard]] ualni getLine() const { return line; }
};
class DebugSymbols {
friend CallStackCapture;
char function[MAX_DEBUG_INFO_LEN + 1] = { 0 };
char file[MAX_DEBUG_INFO_LEN + 1] = { 0 };
ualni line = 0;
public:
[[nodiscard]] const char* getFunc() const { return function; }
[[nodiscard]] const char* getFile() const { return file; }
[[nodiscard]] ualni getLine() const { return line; }
};
public:
CallStackCapture();
~CallStackCapture();
public:
CallStackCapture();
~CallStackCapture();
[[nodiscard]] const CallStack* getSnapshot();
const DebugSymbols* getSymbols(FramePointer fp);
[[nodiscard]] const CallStack* getSnapshot();
const DebugSymbols* getSymbols(FramePointer fp);
public:
public:
template<class Saver>
void write(Saver& file) {
file.write(mBuffLoad);
for (auto cs : *this) {
file.write(cs.getCallStack()->getDepth());
for (auto frame : *cs.getCallStack()) {
file.write((ualni) frame.getFrame());
}
}
file.write(mSymbols);
}
template<class Saver>
void write(Saver& file) {
file.write(mBuffLoad);
for (auto cs : *this) {
file.write(cs.getCallStack()->getDepth());
for (auto frame : *cs.getCallStack()) {
file.write((ualni) frame.getFrame());
}
}
file.write(mSymbols);
}
// independent of the configuration
template<class Loader>
void read(Loader& file) {
clear();
ualni loadLen;
file.read(loadLen);
for (auto cs = loadLen; cs; cs--) {
ualni callStackLen;
file.read(callStackLen);
for (auto fp = callStackLen; fp; fp--) {
// --
}
}
}
// independent of the configuration
template<class Loader>
void read(Loader& file) {
clear();
ualni loadLen;
file.read(loadLen);
for (auto cs = loadLen; cs; cs--) {
ualni callStackLen;
file.read(callStackLen);
for (auto fp = callStackLen; fp; fp--) {
// --
}
}
}
public:
class Iterator {
const CallStack* mSnapshot;
public:
explicit Iterator(const CallStack* start) : mSnapshot(start) {};
const CallStack* getCallStack() { return mSnapshot; }
bool operator==(const Iterator& in) const { return in.mSnapshot == mSnapshot; }
void operator++() { mSnapshot++; }
const Iterator& operator*() const { return *this; }
};
public:
class Iterator {
const CallStack* mSnapshot;
public:
explicit Iterator(const CallStack* start) : mSnapshot(start) {};
const CallStack* getCallStack() { return mSnapshot; }
bool operator==(const Iterator& in) const { return in.mSnapshot == mSnapshot; }
void operator++() { mSnapshot++; }
const Iterator& operator*() const { return *this; }
};
[[nodiscard]] Iterator begin() const { return Iterator(mBuff); }
[[nodiscard]] Iterator end() const { return Iterator(mBuff + mBuffLoad); }
[[nodiscard]] Iterator begin() const { return Iterator(mBuff); }
[[nodiscard]] Iterator end() const { return Iterator(mBuff + mBuffLoad); }
private:
private:
struct CallStackKey {
CallStack* cs;
bool operator==(const CallStackKey& in) const;
};
struct CallStackKey {
CallStack* cs;
bool operator==(const CallStackKey& in) const;
};
static void platformWriteStackTrace(CallStack* stack);
static void platformWriteDebugSymbols(FramePointer frame, DebugSymbols* out);
[[nodiscard]] static ualni hashCallStack(CallStackKey key);
static void platformWriteStackTrace(CallStack* stack);
static void platformWriteDebugSymbols(FramePointer frame, DebugSymbols* out);
[[nodiscard]] static ualni hashCallStack(CallStackKey key);
void clear();
void clear();
private:
ualni mBuffLen;
ualni mBuffLoad;
CallStack* mBuff;
Map<CallStackKey, CallStack*, DefaultAllocator, hashCallStack> mSnapshots;
Map<FramePointer, DebugSymbols> mSymbols;
private:
ualni mBuffLen;
ualni mBuffLoad;
CallStack* mBuff;
Map<CallStackKey, CallStack*, DefaultAllocator, hashCallStack> mSnapshots;
Map<FramePointer, DebugSymbols> mSymbols;
};
extern CallStackCapture* gCSCapture;

View file

@ -41,23 +41,23 @@ namespace tp {
while (i < n1 && j < n2) {
if (!(grater(Left[i], Right[j]))) {
buff[k] = Left[i];
buff[k] = Left[i];
i++;
} else {
buff[k] = Right[j];
buff[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
buff[k] = Left[i];
buff[k] = Left[i];
i++;
k++;
}
while (j < n2) {
buff[k] = Right[j];
buff[k] = Right[j];
j++;
k++;
}

View file

@ -4,61 +4,61 @@
#include "List.hpp"
namespace tp {
class Testing {
public:
class Testing {
public:
struct FailedCheck {
const char* expression = nullptr;
const char* file = nullptr;
ualni line = 0;
};
struct FailedCheck {
const char* expression = nullptr;
const char* file = nullptr;
ualni line = 0;
};
Testing() = default;
void startTest(const char* name);
void endTest();
void addFailedCheck(const FailedCheck& info);
void reportState();
void setRootName(const char* name);
[[nodiscard]] bool hasFailed();
Testing() = default;
void startTest(const char* name);
void endTest();
void addFailedCheck(const FailedCheck& info);
void reportState();
void setRootName(const char* name);
[[nodiscard]] bool hasFailed();
private:
struct TestingNode {
struct FailedCheckRecord { FailedCheck failedCheck; ualni times; };
List<FailedCheckRecord> mFailedChecks;
List<TestingNode*> mSubTests;
const char* mName = "Unnamed";
TestingNode* mParent = nullptr;
bool mHasFailed = false;
private:
struct TestingNode {
struct FailedCheckRecord { FailedCheck failedCheck; ualni times; };
List<FailedCheckRecord> mFailedChecks;
List<TestingNode*> mSubTests;
const char* mName = "Unnamed";
TestingNode* mParent = nullptr;
bool mHasFailed = false;
void report(const char* path = nullptr) const;
void updateState();
~TestingNode();
};
void report(const char* path = nullptr) const;
void updateState();
~TestingNode();
};
TestingNode mRootTest;
TestingNode* mCurrent = &mRootTest;
};
TestingNode mRootTest;
TestingNode* mCurrent = &mRootTest;
};
extern Testing gTesting;
extern Testing gTesting;
}
#define TEST_DEF(Name)\
static void Name##FunctorBody();\
void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
static void Name##FunctorBody();\
void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
#define TEST_DEF_STATIC(Name)\
static void Name##FunctorBody();\
static void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
static void Name##FunctorBody();\
static void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
#define TEST(expr) if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ })
#define TEST_EQUAL(l, r) if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l" == "#r, __FILE__, __LINE__ })

View file

@ -4,55 +4,55 @@
namespace tp {
typedef alni time_ms;
typedef alni time_ns;
typedef alni time_ms;
typedef alni time_ns;
extern time_ms gCurrentTime;
extern time_ms gCurrentTime;
class Timer {
class Timer {
time_ms mStart;
time_ms mDuration;
time_ms mStart;
time_ms mDuration;
public:
public:
Timer();
explicit Timer(time_ms time);
Timer();
explicit Timer(time_ms time);
time_ms start();
time_ms duration();
void setDuration(time_ms dur);
time_ms start();
time_ms duration();
void setDuration(time_ms dur);
bool isTimeout();
void reset();
time_ms timePassed();
time_ms remainder();
void wait();
bool isTimeout();
void reset();
time_ms timePassed();
time_ms remainder();
void wait();
float easeIn(time_ms duration = 0);
float easeOut(time_ms duration = 0);
};
float easeIn(time_ms duration = 0);
float easeOut(time_ms duration = 0);
};
void sleep(time_ms duration);
time_ms get_time();
void sleep(time_ms duration);
time_ms get_time();
struct FpsCounter {
halni frames = 0;
Timer time;
halni fps = 0;
struct FpsCounter {
halni frames = 0;
Timer time;
halni fps = 0;
FpsCounter() : time(1000) {}
FpsCounter() : time(1000) {}
void update(bool log = true) {
frames++;
if (time.isTimeout()) {
fps = frames;
if (log) {
// printf("fps %i \n", fps);
}
frames = 0;
time.reset();
}
}
};
void update(bool log = true) {
frames++;
if (time.isTimeout()) {
fps = frames;
if (log) {
// printf("fps %i \n", fps);
}
frames = 0;
time.reset();
}
}
};
}

View file

@ -7,73 +7,73 @@
namespace tp {
extern ModuleManifest gModuleUtils;
extern ModuleManifest gModuleUtils;
void memsetv(void* p, uhalni bytesize, uint1 val);
void memcp(void* left, const void* right, uhalni len);
int1 memequal(const void* left, const void* right, uhalni len);
void memsetv(void* p, uhalni bytesize, uint1 val);
void memcp(void* left, const void* right, uhalni len);
int1 memequal(const void* left, const void* right, uhalni len);
}
namespace tp {
[[nodiscard]] alnf randf();
[[nodiscard]] alnf randf();
}
namespace tp {
template <typename T1, typename T2>
class Pair {
public:
Pair() {}
Pair(T1 t1, T2 t2) : head(t1), tail(t2) {}
union { T1 t1; T1 head; T1 x; };
union { T2 t2; T2 tail; T2 y; };
};
template <typename T1, typename T2>
class Pair {
public:
Pair() {}
Pair(T1 t1, T2 t2) : head(t1), tail(t2) {}
union { T1 t1; T1 head; T1 x; };
union { T2 t2; T2 tail; T2 y; };
};
template <typename Type = alni>
class Bits {
Type mFlags = 0;
public:
Bits() = default;
explicit Bits(Type val) { mFlags = val; }
explicit Bits(bool val) { for (int bit = 0; bit < sizeof(Type); bit++) { set(bit, val); } }
bool get(int1 idx) { return mFlags & (1l << idx); }
void set(int1 idx, bool val) {
if (val) {
mFlags |= (1l << idx);
} else {
mFlags &= ~(1l << idx);
}
}
};
template <typename Type = alni>
class Bits {
Type mFlags = 0;
public:
Bits() = default;
explicit Bits(Type val) { mFlags = val; }
explicit Bits(bool val) { for (int bit = 0; bit < sizeof(Type); bit++) { set(bit, val); } }
bool get(int1 idx) { return mFlags & (1l << idx); }
void set(int1 idx, bool val) {
if (val) {
mFlags |= (1l << idx);
} else {
mFlags &= ~(1l << idx);
}
}
};
template<typename tType = ualni>
class Range {
public:
class Iterator {
public:
tType mIndex;
explicit Iterator(tType pStartIndex) : mIndex(pStartIndex) {}
tType index() const { return mIndex; }
inline void operator++() { mIndex++; }
inline operator tType() const { return mIndex; }
inline bool operator==(Iterator pIndex) { return mIndex == pIndex.mIndex; }
inline bool operator!=(Iterator pIndex) { return mIndex != pIndex.mIndex; }
inline const Iterator& operator*() { return *this; }
};
template<typename tType = ualni>
class Range {
public:
class Iterator {
public:
tType mIndex;
explicit Iterator(tType pStartIndex) : mIndex(pStartIndex) {}
tType index() const { return mIndex; }
inline void operator++() { mIndex++; }
inline operator tType() const { return mIndex; }
inline bool operator==(Iterator pIndex) { return mIndex == pIndex.mIndex; }
inline bool operator!=(Iterator pIndex) { return mIndex != pIndex.mIndex; }
inline const Iterator& operator*() { return *this; }
};
tType mBegin = 0;
tType mEnd = 0;
tType mBegin = 0;
tType mEnd = 0;
Range() = default;
explicit Range(tType pEndIndex) : mBegin(0), mEnd(pEndIndex) {}
Range(tType pStartIndex, tType pEndIndex) : mBegin(pStartIndex), mEnd(pEndIndex) {}
Range() = default;
explicit Range(tType pEndIndex) : mBegin(0), mEnd(pEndIndex) {}
Range(tType pStartIndex, tType pEndIndex) : mBegin(pStartIndex), mEnd(pEndIndex) {}
bool valid() { return mBegin < mEnd; }
bool valid() { return mBegin < mEnd; }
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
Iterator begin() { return Iterator(mBegin); }
Iterator end() { return Iterator(mEnd); }
};
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
Iterator begin() { return Iterator(mBegin); }
Iterator end() { return Iterator(mEnd); }
};
}

View file

@ -8,60 +8,60 @@
using namespace tp;
void printSnapshot(const tp::CallStackCapture::CallStack* snapshot) {
printf("CallStack: \n");
for (auto frame : *snapshot) {
auto symbols = gCSCapture->getSymbols(frame.getFrame());
printf(" %s ----- %s:%llu\n", symbols->getFunc(), symbols->getFile(), symbols->getLine());
}
printf("\n");
printf("CallStack: \n");
for (auto frame : *snapshot) {
auto symbols = gCSCapture->getSymbols(frame.getFrame());
printf(" %s ----- %s:%llu\n", symbols->getFunc(), symbols->getFile(), symbols->getLine());
}
printf("\n");
}
void common() {
gCSCapture->getSnapshot();
gCSCapture->getSnapshot();
}
void first() {
common();
common();
common();
common();
common();
common();
}
void second() {
common();
common();
common();
common();
common();
common();
common();
common();
}
void third() {
common();
common();
common();
common();
}
void root() {
first();
second();
third();
first();
second();
third();
}
TEST_DEF(Debugging) {
root();
root();
for (auto cs : *gCSCapture) {
printSnapshot(cs.getCallStack());
}
for (auto cs : *gCSCapture) {
printSnapshot(cs.getCallStack());
}
}
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("UtilsTest", nullptr, nullptr, deps);
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("UtilsTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
if (!testModule.initialize()) {
return 1;
}
testDebugging();
testDebugging();
testModule.deinitialize();
testModule.deinitialize();
}