-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add arean impl. Add default ecs allocator as an example impl.
- Loading branch information
Showing
6 changed files
with
222 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#pragma once | ||
|
||
#include "memory/Arena.h" | ||
|
||
#include "logging/Log.h" | ||
#include "meta/Reflection.h" | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <map> | ||
#include <vector> | ||
#include <algorithm> // For lower_bound | ||
#include <typeinfo> | ||
#include <optional> | ||
#include <iterator> // For std::forward_iterator_tag | ||
#include <cstddef> // For std::ptrdiff_t | ||
|
||
namespace l::memory { | ||
|
||
Arena::Arena() { | ||
mBlocks.reserve(16); | ||
|
||
} | ||
|
||
Arena::~Arena() { | ||
|
||
} | ||
|
||
std::unique_ptr<Arena> CreateArena() { | ||
return std::make_unique<Arena>(); | ||
} | ||
|
||
|
||
// push some bytes onto the 'stack' - the way to allocate | ||
void* ArenaPush(Arena* arena, uint64_t size) { | ||
if (arena->mBlocks.empty()) { | ||
arena->mBlocks.push_back({ malloc(size), size }); | ||
return arena->mBlocks.back().mBase; | ||
} | ||
|
||
MemoryBlock& lastBlock = arena->mBlocks.back(); | ||
if (lastBlock.mSize - (reinterpret_cast<uint64_t>(lastBlock.mBase) - reinterpret_cast<uint64_t>(arena->mBlocks.back().mBase)) >= size) { | ||
return reinterpret_cast<void*>(reinterpret_cast<uint64_t>(lastBlock.mBase) + size); | ||
} | ||
|
||
arena->mBlocks.push_back({ malloc(size), size }); | ||
return arena->mBlocks.back().mBase; | ||
} | ||
|
||
void* ArenaPushZero(Arena* arena, uint64_t size) { | ||
void* ptr = ArenaPush(arena, size); | ||
memset(ptr, 0, size); | ||
return ptr; | ||
} | ||
|
||
// some macro helpers that I've found nice: | ||
#define PushArray(arena, type, count) (type *)ArenaPush((arena), sizeof(type)*(count)) | ||
#define PushArrayZero(arena, type, count) (type *)ArenaPushZero((arena), sizeof(type)*(count)) | ||
#define PushStruct(arena, type) PushArray((arena), (type), 1) | ||
#define PushStructZero(arena, type) PushArrayZero((arena), (type), 1) | ||
|
||
// pop some bytes off the 'stack' - the way to free | ||
void ArenaPop(Arena* arena, uint64_t size) { | ||
if (arena->mBlocks.empty()) { | ||
LOG(LogError) << "Trying to pop from an empty arena"; | ||
return; | ||
} | ||
|
||
MemoryBlock& lastBlock = arena->mBlocks.back(); | ||
if (lastBlock.mSize - (reinterpret_cast<uint64_t>(lastBlock.mBase) - reinterpret_cast<uint64_t>(arena->mBlocks.back().mBase)) < size) { | ||
LOG(LogError) << "Trying to pop more bytes than are available in the current block"; | ||
return; | ||
} | ||
|
||
lastBlock.mSize -= size; | ||
} | ||
|
||
// get the # of bytes currently allocated. | ||
uint64_t ArenaGetPos(Arena* arena) { | ||
if (arena->mBlocks.empty()) { | ||
return 0; | ||
} | ||
|
||
MemoryBlock& lastBlock = arena->mBlocks.back(); | ||
return lastBlock.mSize - (reinterpret_cast<uint64_t>(lastBlock.mBase) - reinterpret_cast<uint64_t>(arena->mBlocks.back().mBase)); | ||
} | ||
|
||
// also some useful popping helpers: | ||
void ArenaSetPosBack(Arena* arena, uint64_t pos) { | ||
if (arena->mBlocks.empty()) { | ||
LOG(LogError) << "Trying to set position in an empty arena"; | ||
return; | ||
} | ||
|
||
MemoryBlock& lastBlock = arena->mBlocks.back(); | ||
if (lastBlock.mSize - (reinterpret_cast<uint64_t>(lastBlock.mBase) - reinterpret_cast<uint64_t>(arena->mBlocks.back().mBase)) < pos) { | ||
LOG(LogError) << "Trying to set position past the end of the current block"; | ||
return; | ||
} | ||
|
||
lastBlock.mSize = pos; | ||
} | ||
|
||
void ArenaClear(Arena* arena) { | ||
for (MemoryBlock& block : arena->mBlocks) { | ||
free(block.mBase); | ||
} | ||
arena->mBlocks.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <map> | ||
#include <vector> | ||
#include <algorithm> // For lower_bound | ||
#include <typeinfo> | ||
#include <optional> | ||
#include <iterator> // For std::forward_iterator_tag | ||
#include <cstddef> // For std::ptrdiff_t | ||
|
||
#include "logging/Log.h" | ||
#include "meta/Reflection.h" | ||
|
||
namespace l::container { | ||
|
||
union LLData { | ||
struct Node { | ||
Node* next; | ||
Node* prev; | ||
}; | ||
struct LinkedList { | ||
Node* head; | ||
Node* tail; | ||
}; | ||
struct LinkedListNode { | ||
Node* next; | ||
Node* prev; | ||
int value; | ||
}; | ||
}; | ||
} |
File renamed without changes.