This commit is contained in:
IlushaShurupov 2023-07-16 18:28:24 +03:00 committed by Ilya Shurupov
parent f91b45643d
commit 53aa0e34a8
24 changed files with 116 additions and 110 deletions

View file

@ -3,8 +3,12 @@
#include <cstdlib>
static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleBase, nullptr };
tp::ModuleManifest tp::gModuleAllocators = ModuleManifest("Allocators", nullptr, nullptr, sModuleDependencies);
static void deinit(const tp::ModuleManifest* self) {
tp::HeapAllocGlobal::checkLeaks();
}
static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest tp::gModuleAllocators = ModuleManifest("Allocators", nullptr, deinit, sModuleDependencies);
void* operator new(size_t aSize) { return tp::HeapAllocGlobal::allocate(aSize); }

View file

@ -118,8 +118,15 @@ void HeapAllocGlobal::deallocate(void* aPtr) {
}
// 3) Check the wrap
ASSERT(!memCompareVal(wrap_top, WRAP_SIZE, WRAP_VAL) && "Allocated Block Wrap Corrupted!")
ASSERT(!memCompareVal(wrap_bottom, WRAP_SIZE, WRAP_VAL) && "Allocated Block Wrap Corrupted!")
if (memCompareVal(wrap_top, WRAP_SIZE, WRAP_VAL)) {
CallStackCapture::printSnapshot(head->mCallStack);
ASSERT(!"Allocated Block Wrap Corrupted!")
}
if (memCompareVal(wrap_bottom, WRAP_SIZE, WRAP_VAL)) {
CallStackCapture::printSnapshot(head->mCallStack);
ASSERT(!"Allocated Block Wrap Corrupted!")
}
// 4) clear data
#ifdef MEM_CLEAR_ON_ALLOC
@ -135,10 +142,12 @@ bool HeapAllocGlobal::checkLeaks() {
if (mNumAllocations) {
#ifdef MEM_STACK_TRACE
gCSCapture->logLeaks();
for (auto iter = mEntry; iter; iter = iter->mPrev) {
CallStackCapture::printSnapshot(iter->mCallStack);
}
#endif
DEBUG_BREAK("Destruction of not freed Allocator")
ASSERT(!"Destruction of not freed Allocator")
return true;
}
return false;

View file

@ -1,6 +1,6 @@
#pragma once
#include "Module.hpp"
#include "Utils.hpp"
#include "HeapAllocatorGlobal.hpp"
#include "HeapAllocator.hpp"
@ -12,6 +12,7 @@ namespace tp {
}
inline void* operator new(std::size_t aSize, void* aWhere) noexcept { return aWhere; }
inline void* operator new[](std::size_t aSize, void* aWhere) noexcept { return aWhere; }
void* operator new(std::size_t aSize);
void* operator new[](std::size_t aSize);

View file

@ -16,7 +16,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Modules)
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Allocators)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib)

View file

@ -80,7 +80,7 @@ namespace tp {
}
explicit Buffer(ualni size) : mSize(size), mLoad(0) {
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * tMinSize);
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * size);
}
Buffer(const Buffer& in) : mSize(in.mSize), mLoad(in.mLoad) {

View file

@ -7,7 +7,10 @@ namespace tp {
template< typename tType, ualni tSizeX, ualni tSizeY >
using ConstSizeBuffer2D = ConstSizeBuffer<ConstSizeBuffer<tType, tSizeX>, tSizeY>;
template <typename tType>
template <
typename tType,
class tAllocator = DefaultAllocator
>
class Buffer2D {
public:
typedef ualni Index;
@ -15,15 +18,28 @@ namespace tp {
typedef SelCopyArg<tType> tTypeArg;
private:
tAllocator mAlloc;
tType* mBuff = nullptr;
Index2D mSize = { 0, 0 };
void deleteBuffer() {
if (!mBuff) return;
for (ualni i = 0; i < mSize.x * mSize.y; i++) mBuff[i].~tType();
mAlloc.deallocate(mBuff);
}
void allocateBuffer(Index2D size) {
deleteBuffer();
mBuff = (tType*) mAlloc.allocate(sizeof(tType) * size.x * size.y);
for (ualni i = 0; i < mSize.x * mSize.y; i++) new (mBuff + i) tType();
}
public:
Buffer2D() = default;
~Buffer2D() {
delete mBuff;
deleteBuffer();
}
explicit Buffer2D(Index2D aSize) {
@ -55,8 +71,7 @@ namespace tp {
void reserve(const Index2D& newSize) {
if (mSize.x != newSize.x || mSize.y != newSize.y) {
delete mBuff;
mBuff = new tType[newSize.x * newSize.y];
allocateBuffer(newSize);
mSize = newSize;
}
}

View file

@ -387,6 +387,13 @@ namespace tp {
}
}
~Map() { removeAll(); }
~Map() {
for (ualni i = 0; i < mNSlots; i++) {
if (mTable[i] && !isDeletedNode(mTable[i])) {
deleteNode(mTable[i]);
}
}
deleteTable(mTable);
}
};
}

View file

@ -1,17 +1,14 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Buffer2D.hpp"
#include <iostream>
using namespace tp;
const ualni size = 1000;
TEST_DEF_STATIC(Simple1) {
Buffer2D<int> buff;
Buffer2D<int, HeapAlloc> buff;
buff.reserve({ 4, 4 });
buff.set( { 2, 2 }, 5);
TEST(buff.get( { 2, 2 } ) == 5);

View file

@ -1,18 +1,14 @@
#include "Tests.hpp"
#include "Buffer.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
const ualni size = 1000;
TEST_DEF_STATIC(Simple1) {
Buffer<TestClass> buff;
Buffer<TestClass, HeapAlloc> buff;
TEST(buff.size() == 0);
for (auto i : Range(size * 10)) {
buff.append(TestClass(i));
@ -23,7 +19,7 @@ TEST_DEF_STATIC(Simple1) {
}
TEST_DEF_STATIC(Simple2) {
Buffer<TestClass> buff(size);
Buffer<TestClass, HeapAlloc> buff(size);
TEST(buff.size() == 0);
for (auto i : Range(size * 10)) buff.append(TestClass(i));
TEST(buff.size() == size * 10);

View file

@ -2,12 +2,10 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
list.pushBack(TestClass(5));
list.pushFront(TestClass(0));
@ -21,11 +19,10 @@ TEST_DEF_STATIC(SimpleReference) {
TEST(i == 5);
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF_STATIC(SimplePointer) {
tp::List<TestClass*, TestAllocator> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
tp::List<TestClass*, HeapAlloc> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
list.pushBack(new TestClass(5));
list.pushFront(new TestClass(0));
@ -38,26 +35,25 @@ TEST_DEF_STATIC(SimplePointer) {
TEST(i == 5);
list.removeAll();
for (auto iter : list) {
delete iter.data();
}
TEST(list.getAllocator().getAllocationsCount() == 0);
list.removeAll();
}
TEST_DEF_STATIC(Copy) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list2 = list;
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list2 = list;
TEST_EQUAL(list, list2);
list.removeAll();
list2.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
TEST(list2.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF_STATIC(SaveLoad) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
TestFile file;
@ -77,8 +73,6 @@ TEST_DEF_STATIC(SaveLoad) {
TEST(i == 4);
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF(List) {

View file

@ -1,15 +1,12 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Map.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
@ -40,12 +37,10 @@ TEST_DEF_STATIC(SimpleReference) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(SimplePointer) {
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
tp::Map<tp::ualni, TestClass*, HeapAlloc> map;
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
@ -57,11 +52,14 @@ TEST_DEF_STATIC(SimplePointer) {
}
for (auto i : Range(1000)) {
auto del = map.get(i);
map.put(i, new TestClass(i));
delete del;
}
for (auto i : Range(900, 1000)) {
TEST(map.presents(i));
delete map.get(i);
map.remove(i);
TEST(!map.presents(i));
}
@ -77,30 +75,25 @@ TEST_DEF_STATIC(SimplePointer) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(Copy) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map2 = map;
TEST_EQUAL(map, map2);
map.removeAll();
map2.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map2.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(SaveLoad) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
@ -112,13 +105,13 @@ TEST_DEF_STATIC(SaveLoad) {
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.size() == 0);
file.setAddress(0);
map.read(file);
TEST(map.getAllocator().getAllocationsCount() == 11);
TEST(map.size() == 10);
for (auto i : Range(10)) {
TEST(map.presents(i));
@ -126,8 +119,6 @@ TEST_DEF_STATIC(SaveLoad) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF(Map) {

View file

@ -1,32 +1,15 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include <cstdlib>
static bool init(const tp::ModuleManifest* self) {
tp::gTesting.setRootName(self->getName());
return true;
}
void* TestAllocator::allocate(tp::ualni size) {
nAllocations++;
return malloc(size);
}
void TestAllocator::deallocate(void* p) {
nAllocations--;
free(p);
}
tp::ualni TestAllocator::getAllocationsCount() const {
return nAllocations;
}
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleContainers, &tp::gModuleUtils, nullptr };
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, &tp::gModuleAllocators, nullptr };
tp::ModuleManifest testModule("ContainersTest", init, nullptr, deps);
if (!testModule.initialize()) {
@ -36,7 +19,7 @@ int main() {
testList();
testMap();
testAvl();
testBuffer();
testBuffer();
testBuffer2d();
testModule.deinitialize();

View file

@ -1,6 +1,7 @@
#pragma once
#include "Utils.hpp"
#include "Allocators.hpp"
class TestClass {
tp::ualni val2 = 0;
@ -27,15 +28,6 @@ public:
void setVal(tp::ualni val) { val1 = val; }
};
class TestAllocator {
tp::ualni nAllocations = 0;
public:
TestAllocator() = default;
void* allocate(tp::ualni size);
void deallocate(void* p);
[[nodiscard]] tp::ualni getAllocationsCount() const;
};
class TestFile {
tp::ualni mem[1024] = { 0 };
tp::ualni address = 0;

View file

@ -1,16 +1,12 @@
#include "Tests.hpp"
#include "Tree.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(Simple) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, HeapAlloc> tree;
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
@ -27,7 +23,7 @@ TEST_DEF_STATIC(Simple) {
}
TEST_DEF_STATIC(Persistance) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, HeapAlloc> tree;
const auto size = 1000;
@ -46,7 +42,7 @@ TEST_DEF_STATIC(Persistance) {
// random load
ualni loadSize = 0;
while (loadSize < size / 2) {
ualni idx = rand() % (size - 1);
auto idx = ualni(randomFloat() * (size - 1));
DEBUG_ASSERT(idx < size)
if (!buff[idx].presents) {
tree.insert((alni) buff[idx].data.getVal(), buff[idx].data);
@ -86,7 +82,7 @@ TEST_DEF_STATIC(Persistance) {
// random unload
ualni unloadSize = 0;
while (unloadSize < size / 2) {
ualni idx = rand() % (size - 1);
auto idx = ualni(randomFloat() * (size - 1));
if (buff[idx].presents) {
tree.remove((alni) buff[idx].data.getVal());

View file

@ -14,7 +14,7 @@ void testMath();
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("ContainersTest", init, nullptr, deps);
tp::ModuleManifest testModule("MathTest", init, nullptr, deps);
if (!testModule.initialize()) {
return 1;

View file

@ -24,7 +24,7 @@ bool ModuleManifest::isInitialized() const {
return mInitialized;
}
bool ModuleManifest::initialize() {
bool ModuleManifest::initialize(const ModuleManifest* parent) {
mInitCount++;
@ -35,10 +35,10 @@ bool ModuleManifest::initialize() {
mInitialized = true;
for (auto module = mDependencies; module && *module; module++) {
mInitialized &= (*module)->initialize();
mInitialized &= (*module)->initialize(this);
}
std::cout << "====== Initializing \"" << mModuleName << "\"\n";
std::cout << "=== Initializing \"" << mModuleName << "\" from \"" << (parent ? parent->mModuleName : mModuleName ) << "\"\n";
if (mInit) mInitialized &= mInit(this);

View file

@ -15,9 +15,12 @@ namespace tp {
ModuleManifest(const char* aModuleName, ModuleInit aInit, ModuleDeinit aDeinit, ModuleManifest** aDependencies);
[[nodiscard]] bool isInitialized() const;
bool initialize();
bool initialize(const ModuleManifest* parent = nullptr);
void deinitialize();
[[nodiscard]] const char* getName() const;
void setCustomData(void* data) { mCustomData = data; }
void* getCustomData(void* data) const { return mCustomData; }
private:
const char* mModuleName = nullptr;
ModuleManifest** mDependencies; // NULL terminated
@ -25,6 +28,7 @@ namespace tp {
ModuleInit mInit = nullptr;
ModuleDeinit mDeinit = nullptr;
uhalni mInitCount = 0;
void* mCustomData = nullptr;
};
extern ModuleManifest gModuleBase;

View file

@ -17,7 +17,6 @@ void deinitialize(const ModuleManifest*) {
static tp::ModuleManifest* sModuleDependencies[] = {
&tp::gModuleContainers,
&tp::gModuleAllocators,
&tp::gModuleUtils,
nullptr
};

View file

@ -1,5 +1,6 @@
#pragma once
#include "Allocators.hpp"
#include "Utils.hpp"
#include "Buffer.hpp"

View file

@ -16,36 +16,53 @@ namespace tp::RegEx {
REPEAT,
VAL,
} mType = NONE;
AstNode() = default;
virtual ~AstNode() = default;
};
template <typename tAlphabetType>
struct AstVal : public AstNode {
explicit AstVal(tAlphabetType val) : mVal(val) { mType = VAL; }
~AstVal() override = default;
tAlphabetType mVal;
};
struct AstCompound : public AstNode {
AstCompound() { mType = COMPOUND; }
~AstCompound() override {
for (auto iter : mChilds) {
delete iter.data();
}
mChilds.removeAll();
}
List<AstNode*> mChilds;
};
struct AstAlternation : public AstNode {
AstAlternation() { mType = OR; }
~AstAlternation() override {
delete mFirst;
delete mSecond;
}
AstNode* mFirst = nullptr;
AstNode* mSecond = nullptr;
};
struct AstIf : public AstNode {
AstIf() { mType = IF; }
~AstIf() override { delete mNode; }
AstNode* mNode = nullptr;
};
struct AstAny : public AstNode {
AstAny() { mType = ANY; }
~AstAny() override = default;
};
struct AstRepetition : public AstNode {
AstRepetition() { mType = REPEAT; }
~AstRepetition() override { delete mNode; }
AstNode* mNode = nullptr;
bool mPlus = false;
};
@ -53,8 +70,9 @@ namespace tp::RegEx {
template <typename tAlphabetType>
struct AstClass : public AstNode {
AstClass() { mType = CLASS; }
~AstClass() override { mRanges.removeAll(); }
List<Range<tAlphabetType>> mRanges;
bool mExclude{};
bool mExclude = false;
};
struct ParseError {

View file

@ -1,10 +1,9 @@
#include "Testing.hpp"
#include "Tokenizer.hpp"
#include <cstdio>
#include <iostream>
#define LOG(val) std::cout << #val << " "
#define LOG(val) // std::cout << #val << " "
using namespace tp;
@ -126,7 +125,7 @@ TEST_DEF_STATIC(Simple) {
});
if (!lexer.isBuild()) {
std::cout << lexer.getBuildError().description;
printf("Error : %s", lexer.getBuildError().description);
return;
}
@ -141,7 +140,7 @@ TEST_DEF_STATIC(Simple) {
outputHash += ualni(tok);
switch (tok) {
case TokType::SPACE: { std::cout << " "; } break;
case TokType::SPACE: { printf(" "); } break;
case TokType::VAR: { LOG(VAR); } break;
case TokType::CLASS_DEF: { LOG(CLASS_DEF); } break;
case TokType::SELF: { LOG(SELF); } break;
@ -188,7 +187,7 @@ TEST_DEF_STATIC(Simple) {
} while (tok != TokType::TOK_SOURCE_END && tok != TokType::FAILED);
std::cout << "\n\nOutputHash : " << outputHash;
printf("\n\nOutputHash : %llu", outputHash);
TEST(outputHash == outputHashPassed);
}

View file

@ -208,7 +208,7 @@ void CallStackCapture::printSnapshot(const CallStack* snapshot) {
printf("\n");
}
void CallStackCapture::logLeaks() {
void CallStackCapture::logAll() {
for (auto cs : *this) {
printSnapshot(cs.getCallStack());
}

View file

@ -55,7 +55,7 @@ namespace tp {
const DebugSymbols* getSymbols(FramePointer fp);
static void printSnapshot(const CallStack* snapshot);
void logLeaks();
void logAll();
public:

View file

@ -47,7 +47,7 @@ void root() {
TEST_DEF(Debugging) {
root();
gCSCapture->logLeaks();
gCSCapture->logAll();
}
int main() {