chore: initial commit

This commit is contained in:
fallenoak 2022-12-26 16:26:06 -06:00
commit 69a95ae405
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
28 changed files with 18574 additions and 0 deletions

19
common/CMakeLists.txt Normal file
View file

@ -0,0 +1,19 @@
file(GLOB COMMON_SOURCES
"*.cpp"
"ref/*.cpp"
"string/*.cpp"
)
add_library(common STATIC
${COMMON_SOURCES}
)
target_include_directories(common
PUBLIC
${PROJECT_SOURCE_DIR}
)
target_link_libraries(common
PUBLIC
storm
)

6
common/Ref.hpp Normal file
View file

@ -0,0 +1,6 @@
#ifndef COMMON_REF_HPP
#define COMMON_REF_HPP
#include "common/ref/TRefCnt.hpp"
#endif

6
common/String.hpp Normal file
View file

@ -0,0 +1,6 @@
#ifndef COMMON_STRING_HPP
#define COMMON_STRING_HPP
#include "common/string/RCString.hpp"
#endif

19
common/ref/TRefCnt.hpp Normal file
View file

@ -0,0 +1,19 @@
#ifndef COMMON_T_REF_CNT_HPP
#define COMMON_T_REF_CNT_HPP
#include <cstdint>
class TRefCnt {
public:
// Member variables
uint32_t m_refcnt;
};
template <class T>
class TRefCntPtr {
public:
// Member variables
T* m_ptr;
};
#endif

View file

@ -0,0 +1,49 @@
#include "common/string/CStringManager.hpp"
#include <climits>
#include <cstdlib>
#include <cstring>
#include <storm/Memory.hpp>
#include <storm/String.hpp>
CStringManager* CStringManager::s_stringManager;
CStringManager* CStringManager::Get() {
// If the string manager isn't instantiated yet, do so now
if (!CStringManager::s_stringManager) {
void* m = SMemAlloc(sizeof(CStringManager), __FILE__, __LINE__, 0x0);
CStringManager::s_stringManager = m ? new (m) CStringManager() : nullptr;
}
return CStringManager::s_stringManager;
}
CStringManager::CStringManager() {
this->m_memory.AllocateBlock(0);
memset(this->m_buckets, 0, sizeof(this->m_buckets));
}
char* CStringManager::Add(const char* str) {
uint32_t hashval = SStrHashHT(str);
uint32_t bucketIdx = hashval % 0x209;
auto bucket = this->m_buckets[bucketIdx];
if (bucket) {
auto ele = bucket;
while (ele) {
if (!SStrCmp(str, &ele->str, INT_MAX)) {
return &ele->str;
}
ele = ele->next;
}
}
auto newEle = this->m_memory.New(str);
// Insert new element at start
newEle->next = this->m_buckets[bucketIdx];
this->m_buckets[bucketIdx] = newEle;
return &newEle->str;
}

View file

@ -0,0 +1,25 @@
#ifndef COMMON_STRING_C_STRING_MANAGER_HPP
#define COMMON_STRING_C_STRING_MANAGER_HPP
#include "common/string/CStringMemory.hpp"
struct CStringElement;
class CStringManager {
public:
// Static variables
static CStringManager* s_stringManager;
// Static functions
static CStringManager* Get();
// Member variables
CStringElement* m_buckets[521];
CStringMemory m_memory;
// Member functions
CStringManager();
char* Add(const char* str);
};
#endif

View file

@ -0,0 +1,44 @@
#include "common/string/CStringMemory.hpp"
#include <climits>
#include <storm/Memory.hpp>
#include <storm/String.hpp>
void CStringMemory::AllocateBlock(size_t blockSize) {
// Ensure the block is of a minimum size
if (blockSize <= 0x10000) {
blockSize = 0x10000;
}
uint32_t index = this->Count();
this->GrowToFit(index, 0);
// Allocate the block
void* block = SMemAlloc(blockSize, __FILE__, __LINE__, 0x0);
// Store the block in the array
this->operator[](index) = block;
this->m_free = reinterpret_cast<uintptr_t>(block);
this->m_end = reinterpret_cast<uintptr_t>(block) + blockSize;
}
CStringElement* CStringMemory::New(const char* str) {
size_t strLen = SStrLen(str);
// Aligned to pointer: sizeof(pointer + string)
size_t eleSize = (strLen + (sizeof(void*) * 2)) & static_cast<uintptr_t>(-sizeof(void*));
// Allocation would exceed block end, so allocate a new block
if (this->m_free + eleSize > this->m_end) {
this->AllocateBlock(eleSize);
}
auto ele = reinterpret_cast<CStringElement*>(this->m_free);
// Copy the provided string into string memory
SStrCopy(&ele->str, str, INT_MAX);
this->m_free += eleSize;
return ele;
}

View file

@ -0,0 +1,22 @@
#ifndef COMMON_STRING_C_STRING_MEMORY_HPP
#define COMMON_STRING_C_STRING_MEMORY_HPP
#include <storm/Array.hpp>
struct CStringElement {
CStringElement* next;
char str;
};
class CStringMemory : public TSGrowableArray<void*> {
public:
// Member variables
uintptr_t m_free;
uintptr_t m_end;
// Member functions
void AllocateBlock(size_t blockSize);
CStringElement* New(const char* str);
};
#endif

View file

@ -0,0 +1,34 @@
#include "common/string/RCString.hpp"
#include "common/string/CStringManager.hpp"
#include <storm/String.hpp>
RCString::RCString()
: TRefCnt() {
this->Copy(nullptr);
}
void RCString::Copy(const char* source) {
if (source) {
this->m_str = CStringManager::Get()->Add(source);
} else {
this->m_str = nullptr;
}
}
void RCString::Get(char* buf, size_t bufSize) const {
const char* str = this->GetString();
if (str) {
SStrCopy(buf, str, bufSize);
} else {
*buf = 0;
}
}
const char* RCString::GetString() const {
if (this->m_str) {
return this->m_str;
} else {
return nullptr;
}
}

View file

@ -0,0 +1,19 @@
#ifndef COMMON_STRING_RC_STRING_HPP
#define COMMON_STRING_RC_STRING_HPP
#include "common/ref/TRefCnt.hpp"
#include <cstdlib>
class RCString : public TRefCnt {
public:
// Member variables
char* m_str;
// Member functions
RCString();
void Copy(const char* source);
void Get(char* buf, size_t bufSize) const;
const char* GetString() const;
};
#endif