Allocators Tools Initial

This commit is contained in:
IlushaShurupov 2023-07-07 00:10:00 +03:00
parent 4a2ab6f5d0
commit 6d0a4676ab
34 changed files with 3847 additions and 0 deletions

View file

@ -0,0 +1,26 @@
#pragma once
#include "HeapAllocator.hpp"
#include "ChunkAllocator.hpp"
#include "PoolAllocator.hpp"
namespace tp {
extern ModuleManifest gModuleAllocator;
};
inline void* operator new(size_t aSize, void* aWhere) noexcept { return aWhere; }
void* operator new(size_t aSize);
void* operator new[](size_t _Size);
void operator delete(void* aPtr);
void operator delete[](void* aPtr);
void* operator new(size_t aSize, tp::HeapAlloc& aAlloc);
void* operator new[](size_t _Size, tp::HeapAlloc& aAlloc);
void operator delete(void* aPtr, tp::HeapAlloc& aAlloc);
void operator delete[](void* aPtr, tp::HeapAlloc& aAlloc);
void* operator new(size_t aSize, tp::HeapAllocGlobal& aAlloc);
void* operator new[](size_t _Size, tp::HeapAllocGlobal& aAlloc);
void operator delete(void* aPtr, tp::HeapAllocGlobal& aAlloc);
void operator delete[](void* aPtr, tp::HeapAllocGlobal& aAlloc);

View file

@ -0,0 +1,33 @@
#pragma once
#include "common.h"
#include "PublicConfig.hpp"
namespace tp {
// Chunk Allocator
// Constant time allocations and deallocations in any order.
// Memory blocks are fixed in size and number of blocks can not exceed given parameter.
struct ChunkAlloc {
ChunkAlloc(ualni aBlockSize, void* aMemory, ualni aMemSize);
ChunkAlloc(ualni aBlockSize, ualni aNBlocks);
void* allocate();
void deallocate(void* aPtr);
bool isFull();
bool isEmpty();
~ChunkAlloc();
private:
ualni mBSize; // Size of data in aligned units
ualni mNBlocks;
ualni* mBuff;
ualni* mNextBlock = 0;
ualni mNFreeBlocks;
ualni mNInitBlocks = 0;
bool mOwnBuff = false;
};
};

View file

@ -0,0 +1,20 @@
#pragma once
#include "HeapAllocatorGlobal.hpp"
namespace tp {
struct HeapAlloc {
#ifdef MEM_DEBUG
HeapAllocGlobal mAlloc;
ualni mNumAllocations = 0;
struct MemHeadLocal* mEntry = NULL;
#endif
void* allocate(ualni aBlockSize);
void deallocate(void* aPtr);
~HeapAlloc();
};
};

View file

@ -0,0 +1,19 @@
#pragma once
#include "common.h"
#include "PublicConfig.hpp"
namespace tp {
struct HeapAllocGlobal {
#ifdef MEM_DEBUG
static ualni mNumAllocations;
static struct MemHead* mEntry;
#endif
static void* allocate(ualni aBlockSize);
static void deallocate(void* aPtr);
~HeapAllocGlobal();
};
};

View file

@ -0,0 +1,13 @@
#pragma once
#include "HeapAllocatorGlobal.hpp"
namespace tp {
struct PickAlloc {
PickAlloc();
void* allocate(ualni aBlockSize);
void deallocate(void* aPtr);
~PickAlloc();
};
};

View file

@ -0,0 +1,42 @@
#pragma once
#include "ChunkAllocator.hpp"
namespace tp {
// Pool Allocator
// Overcomes chunk allocator fixed number of max allocations
struct PoolAlloc {
PoolAlloc(ualni aBlockSize, ualni aChunkSize);
void* allocate();
void deallocate(void* aPtr);
~PoolAlloc();
private:
struct Chunk : public ChunkAlloc {
Chunk(ualni aBlockSize, ualni aChunlSize) : ChunkAlloc(aBlockSize, aChunlSize) {}
Chunk* mNext = NULL;
Chunk* mPrev = NULL;
};
struct Chunks {
void add(Chunk*);
void remove(Chunk*);
Chunk* find(void* aPtr);
Chunk* findNotFull();
Chunk** mBuff = NULL;
ualni mUsedLen = 0;
ualni mLen = 0;
private:
Chunk** findUtil(Chunk** aLeft, Chunk** aRight, void* aPtr);
};
Chunks mChunks;
Chunk* mFreeChunk = NULL;
ualni mBlockSize;
ualni mChunkSize;
};
};

View file

@ -0,0 +1,12 @@
#pragma once
#include "PublicConfig.hpp"
#define MEM_WRAP_SIZE 8 // Wrapping Size in aligned units
#define MEM_WRAP_FILL_VAL 0xBB // Wrapping Fill Value
#define MEM_CLEAR_ON_ALLOC // Clear data on allocation
#define MEM_CLEAR_ON_DEALLOC // Clear data on free
#define MEM_CLEAR_ON_DEALLOC_VAL 0xAA // Clear data on free
#define MEM_CLEAR_ON_ALLOC_VAL 0xCC // Clear data on free
#define MEM_STACK_TRACE // Save stack on allocation call
#define MEM_STACK_TRACE_MAX_DEPTH 32 // Call stack max depth

View file

@ -0,0 +1,3 @@
#pragma once
#define MEM_DEBUG // Memory Debugging

View file

@ -0,0 +1,43 @@
#pragma once
#include "common.h"
namespace tp {
struct CallStackSnapshots {
typedef alni FramePointer;
typedef FramePointer* StackShapshot; // NULL Terminated Frames
StackShapshot capture();
void saveToFile(StackShapshot* snapshots, const char* filepath);
StackShapshot* loadFromFile(const char* filepath);
private:
struct SnapshotsDict {
SnapshotsDict();
StackShapshot newStackSnapshot(ualni aDepth);
void put(StackShapshot aPtr);
StackShapshot getSlot(ualni aIdx);
alni presents(StackShapshot aPtr);
~SnapshotsDict();
private:
StackShapshot* mTable = NULL;
uhalni mSize = 512;
uhalni mEntries = 0;
ualni hash(StackShapshot snapshot);
bool compare(StackShapshot left, StackShapshot right);
alni findSlotRead(StackShapshot key);
ualni findSlotWrite(StackShapshot key);
void resize();
} mSnapshots;
StackShapshot getStack(ualni& len);
};
extern CallStackSnapshots gCallStackSnapshots;
};