Apply formating to all files. CLeanup

This commit is contained in:
IlyaShurupov 2023-10-22 17:07:28 +03:00
parent 43e374f269
commit 744c01c5d0
928 changed files with 14515 additions and 21480 deletions

View file

@ -8,9 +8,7 @@ using namespace tp;
CallStackCapture* tp::gCSCapture = nullptr;
void initializeCallStackCapture() {
gCSCapture = new (malloc(sizeof(CallStackCapture))) CallStackCapture();
}
void initializeCallStackCapture() { gCSCapture = new (malloc(sizeof(CallStackCapture))) CallStackCapture(); }
void deinitializeCallStackCapture() {
gCSCapture->~CallStackCapture();
@ -20,12 +18,16 @@ void deinitializeCallStackCapture() {
ualni CallStackCapture::CallStack::getDepth() const {
ualni len = 0;
for (long long frame : frames) {
if (!frame) { break; }
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; }
if (!frames[i]) {
break;
}
stripedLen++;
}
return stripedLen;
@ -34,11 +36,13 @@ ualni CallStackCapture::CallStack::getDepth() const {
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]; }
for (ualni i = 0; cs->frames[i]; i++) {
out += cs->frames[i];
}
return out;
}
bool CallStackCapture::CallStackKey::operator==(const CallStackCapture::CallStackKey &in) const {
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;
@ -105,21 +109,19 @@ void CallStackCapture::clear() {
mSymbols.removeAll();
}
CallStackCapture::~CallStackCapture() {
free(mBuff);
}
CallStackCapture::~CallStackCapture() { free(mBuff); }
// ---------------------------------- Platform Depended ---------------------------------- //
#if defined(ENV_OS_LINUX)
#include <malloc.h>
#include <execinfo.h>
#include <cstring>
#include <cxxabi.h>
#include <execinfo.h>
#include <malloc.h>
void CallStackCapture::platformWriteStackTrace(CallStack* stack) {
auto depth = backtrace((void**)stack->frames, (int) MAX_CALL_DEPTH_CAPTURE - 1);
auto depth = backtrace((void**) stack->frames, (int) MAX_CALL_DEPTH_CAPTURE - 1);
stack->frames[depth] = 0;
}
@ -139,20 +141,20 @@ static void getGetSourceFromBinaryAddress(const char* binary, const char* addres
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));
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");
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* funcDemangled = (char*) malloc(funcDemangledSize);
char* ret = abi::__cxa_demangle(func, funcDemangled, &funcDemangledSize, &status);
if (status == 0) {
funcDemangled = ret;
@ -176,10 +178,15 @@ void CallStackCapture::platformWriteDebugSymbols(FramePointer frame, DebugSymbol
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; }
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) {
@ -189,11 +196,11 @@ void CallStackCapture::platformWriteDebugSymbols(FramePointer frame, DebugSymbol
*offset = 0;
getDemangledName(func, out->function);
} else {
std::strcpy(out->function, "unresolved");
std::strcpy(out->function, "unresolved");
}
} else {
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
std::strcpy(out->file, "unresolved");
std::strcpy(out->function, "unresolved");
}
free(symbolsArray);
@ -217,7 +224,7 @@ void CallStackCapture::logAll() {
#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

@ -44,9 +44,7 @@ bool Testing::hasFailed() {
return mRootTest.mHasFailed;
}
void Testing::setRootName(const char* name) {
mRootTest.mName = name;
}
void Testing::setRootName(const char* name) { mRootTest.mName = name; }
void Testing::TestingNode::updateState() {
for (auto child : mSubTests) {

View file

@ -5,8 +5,7 @@
#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))
@ -18,10 +17,7 @@ namespace tp {
return gCurrentTime;
}
void sleep(time_ms mDuration) {
THREAD_SLEEP(mDuration);
}
void sleep(time_ms mDuration) { THREAD_SLEEP(mDuration); }
Timer::Timer() {
mDuration = 0;
@ -33,21 +29,13 @@ namespace tp {
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; }

View file

@ -18,7 +18,9 @@ static bool initialize(const tp::ModuleManifest*) {
static void deinitialize(const tp::ModuleManifest*) {
deinitializeCallStackCapture();
tp::gTesting.reportState();
if (tp::gTesting.hasFailed()) { exit(1); }
if (tp::gTesting.hasFailed()) {
exit(1);
}
}
namespace tp {
@ -87,9 +89,7 @@ namespace tp {
return 0;
}
bool memEqual(const void* left, const void* right, uhalni len) {
return memCompare(left, right, len) == 0;
}
bool memEqual(const void* left, const void* right, uhalni len) { return memCompare(left, right, len) == 0; }
int1 memCompareVal(const void* left, uhalni len, uint1 val) {
MODULE_SANITY_CHECK(gModuleBase)

View file

@ -24,8 +24,10 @@ namespace tp {
class Iterator {
const FramePointer* mFrame;
public:
explicit Iterator(const FramePointer* frame) : mFrame(frame) {};
explicit Iterator(const FramePointer* frame) :
mFrame(frame){};
FramePointer getFrame() { return *mFrame; }
bool operator==(const Iterator& in) const { return in.mFrame == mFrame; }
void operator++() { mFrame++; }
@ -41,6 +43,7 @@ namespace tp {
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; }
@ -58,8 +61,7 @@ namespace tp {
void logAll();
public:
template<class Saver>
template <class Saver>
void write(Saver& file) {
file.write(mBuffLoad);
for (auto cs : *this) {
@ -72,7 +74,7 @@ namespace tp {
}
// independent of the configuration
template<class Loader>
template <class Loader>
void read(Loader& file) {
clear();
ualni loadLen;
@ -89,8 +91,10 @@ namespace tp {
public:
class Iterator {
const CallStack* mSnapshot;
public:
explicit Iterator(const CallStack* start) : mSnapshot(start) {};
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++; }
@ -98,10 +102,9 @@ namespace tp {
};
[[nodiscard]] Iterator begin() const { return Iterator(mBuff); }
[[nodiscard]] Iterator end() const { return Iterator(mBuff + mBuffLoad); }
[[nodiscard]] Iterator end() const { return Iterator(mBuff + mBuffLoad); }
private:
struct CallStackKey {
CallStack* cs;
bool operator==(const CallStackKey& in) const;

View file

@ -4,16 +4,13 @@
namespace tp {
class Mutex {
pthread_mutex_t mMutex {};
pthread_mutex_t mMutex{};
public:
Mutex() = default;
void lock() {
pthread_mutex_lock(&mMutex);
}
void lock() { pthread_mutex_lock(&mMutex); }
void unlock() {
pthread_mutex_unlock(&mMutex);
}
void unlock() { pthread_mutex_unlock(&mMutex); }
};
}

View file

@ -1,97 +1,95 @@
#pragma once
#include "Utils.hpp"
namespace tp {
template <typename Type>
inline bool compare(const Type& val1, const Type& val2) {
return val1 > val2;
}
struct SortMerge {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
mergeSort(buff, 0, length - 1, grater);
}
private:
template <typename Type>
static void merge(Type* buff, int left, int middle, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
int n1 = middle - left + 1;
int n2 = right - middle;
Type* Left = new Type[n1];
Type* Right = new Type[n2];
for (int i = 0; i < n1; i++) {
Left[i] = buff[left + i];
}
for (int j = 0; j < n2; j++) {
Right[j] = buff[middle + 1 + j];
}
int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (!(grater(Left[i], Right[j]))) {
buff[k] = Left[i];
i++;
} else {
buff[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
buff[k] = Left[i];
i++;
k++;
}
while (j < n2) {
buff[k] = Right[j];
j++;
k++;
}
delete[] Left;
delete[] Right;
}
template <typename Type>
static void mergeSort(Type* buff, int left, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
if (left >= right) {
return;
}
int middle = left + (right - left) / 2;
mergeSort(buff, left, middle, grater);
mergeSort(buff, middle + 1, right, grater);
merge(buff, left, middle, right, grater);
}
};
struct SortInsert {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (grater(*buff[i], *buff[j])) {
swap(buff[i], buff[j]);
}
}
}
}
};
#pragma once
#include "Utils.hpp"
namespace tp {
template <typename Type>
inline bool compare(const Type& val1, const Type& val2) {
return val1 > val2;
}
struct SortMerge {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
mergeSort(buff, 0, length - 1, grater);
}
private:
template <typename Type>
static void merge(Type* buff, int left, int middle, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
int n1 = middle - left + 1;
int n2 = right - middle;
Type* Left = new Type[n1];
Type* Right = new Type[n2];
for (int i = 0; i < n1; i++) {
Left[i] = buff[left + i];
}
for (int j = 0; j < n2; j++) {
Right[j] = buff[middle + 1 + j];
}
int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (!(grater(Left[i], Right[j]))) {
buff[k] = Left[i];
i++;
} else {
buff[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
buff[k] = Left[i];
i++;
k++;
}
while (j < n2) {
buff[k] = Right[j];
j++;
k++;
}
delete[] Left;
delete[] Right;
}
template <typename Type>
static void mergeSort(Type* buff, int left, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
if (left >= right) {
return;
}
int middle = left + (right - left) / 2;
mergeSort(buff, left, middle, grater);
mergeSort(buff, middle + 1, right, grater);
merge(buff, left, middle, right, grater);
}
};
struct SortInsert {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (grater(*buff[i], *buff[j])) {
swap(buff[i], buff[j]);
}
}
}
}
};
}

View file

@ -6,7 +6,6 @@
namespace tp {
class Testing {
public:
struct FailedCheck {
const char* expression = nullptr;
const char* file = nullptr;
@ -23,7 +22,10 @@ namespace tp {
private:
struct TestingNode {
struct FailedCheckRecord { FailedCheck failedCheck; ualni times; };
struct FailedCheckRecord {
FailedCheck failedCheck;
ualni times;
};
List<FailedCheckRecord> mFailedChecks;
List<TestingNode*> mSubTests;
const char* mName = "Unnamed";
@ -42,24 +44,28 @@ namespace tp {
extern Testing gTesting;
}
#define TEST_DEF(Name)\
static void Name##FunctorBody();\
void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
#define TEST_DEF(Name) \
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();\
} \
#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()
#define TEST(expr) if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ })
#define TEST_ASSERT(expr) TEST(expr); if (!(expr)) return
#define TEST_EQUAL(l, r) if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l" == "#r, __FILE__, __LINE__ })
#define TEST(expr) \
if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ })
#define TEST_ASSERT(expr) \
TEST(expr); \
if (!(expr)) return
#define TEST_EQUAL(l, r) \
if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l " == " #r, __FILE__, __LINE__ })

View file

@ -15,7 +15,6 @@ namespace tp {
time_ms mDuration;
public:
Timer();
explicit Timer(time_ms time);
@ -41,7 +40,8 @@ namespace tp {
Timer time;
halni fps = 0;
FpsCounter() : time(1000) {}
FpsCounter() :
time(1000) {}
void update(bool log = true) {
frames++;

View file

@ -1,120 +1,126 @@
#pragma once
#include "Module.hpp"
#define PTR_OFFSET(first, offset) (*((&first) + offset))
#define MEMBER_OFFSET(s, m) (alni(&(((s*) 0)->m)))
namespace tp {
extern ModuleManifest gModuleUtils;
void memSetVal(void* p, uhalni byteSize, uint1 val);
void memCopy(void* left, const void* right, uhalni len);
int1 memCompare(const void* left, const void* right, uhalni len);
bool memEqual(const void* left, const void* right, uhalni len);
int1 memCompareVal(const void* left, uhalni len, uint1 val);
}
namespace tp {
[[nodiscard]] alnf randomFloat();
}
namespace tp {
template <typename T1, typename T2>
class Pair {
public:
Pair() = default;
template <typename U1, typename U2>
Pair(U1 t1, U2 t2) : head(static_cast<T1>(t1)),
tail(static_cast<T2>(t2)) {}
union {
T1 t1;
T1 head;
T1 x;
};
union {
T2 t2;
T2 tail;
T2 y;
};
bool operator==(const Pair& in) const { return in.t1 == t1 && in.t2 == t2; }
bool operator!=(const Pair& in) const { return !this->operator==(in); }
};
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; }
};
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) {}
bool valid() { return mBegin < mEnd; }
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
tType idxDiff() const { return mEnd - mBegin; }
Iterator begin() { return Iterator(mBegin); }
Iterator end() { return Iterator(mEnd); }
};
}
#pragma once
#include "Module.hpp"
#define PTR_OFFSET(first, offset) (*((&first) + offset))
#define MEMBER_OFFSET(s, m) (alni(&(((s*) 0)->m)))
namespace tp {
extern ModuleManifest gModuleUtils;
void memSetVal(void* p, uhalni byteSize, uint1 val);
void memCopy(void* left, const void* right, uhalni len);
int1 memCompare(const void* left, const void* right, uhalni len);
bool memEqual(const void* left, const void* right, uhalni len);
int1 memCompareVal(const void* left, uhalni len, uint1 val);
}
namespace tp {
[[nodiscard]] alnf randomFloat();
}
namespace tp {
template <typename T1, typename T2>
class Pair {
public:
Pair() = default;
template <typename U1, typename U2>
Pair(U1 t1, U2 t2) :
head(static_cast<T1>(t1)),
tail(static_cast<T2>(t2)) {}
union {
T1 t1;
T1 head;
T1 x;
};
union {
T2 t2;
T2 tail;
T2 y;
};
bool operator==(const Pair& in) const { return in.t1 == t1 && in.t2 == t2; }
bool operator!=(const Pair& in) const { return !this->operator==(in); }
};
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; }
};
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) {}
bool valid() { return mBegin < mEnd; }
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
tType idxDiff() const { return mEnd - mBegin; }
Iterator begin() { return Iterator(mBegin); }
Iterator end() { return Iterator(mEnd); }
};
}

View file

@ -1,7 +1,7 @@
#include "Utils.hpp"
#include "Debugging.hpp"
#include "Testing.hpp"
#include "Utils.hpp"
#include <cstdio>
@ -16,9 +16,7 @@ void printSnapshot(const tp::CallStackCapture::CallStack* snapshot) {
printf("\n");
}
void common() {
auto tmp = gCSCapture->getSnapshot();
}
void common() { auto tmp = gCSCapture->getSnapshot(); }
void first() {
common();