mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 11:12:29 +00:00
chore: initial commit
This commit is contained in:
commit
69a95ae405
28 changed files with 18574 additions and 0 deletions
49
common/string/CStringManager.cpp
Normal file
49
common/string/CStringManager.cpp
Normal 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;
|
||||
}
|
||||
25
common/string/CStringManager.hpp
Normal file
25
common/string/CStringManager.hpp
Normal 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
|
||||
44
common/string/CStringMemory.cpp
Normal file
44
common/string/CStringMemory.cpp
Normal 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;
|
||||
}
|
||||
22
common/string/CStringMemory.hpp
Normal file
22
common/string/CStringMemory.hpp
Normal 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
|
||||
34
common/string/RCString.cpp
Normal file
34
common/string/RCString.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
19
common/string/RCString.hpp
Normal file
19
common/string/RCString.hpp
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue