mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 11:12:29 +00:00
Compare commits
2 commits
c57f882da2
...
739adccafd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
739adccafd | ||
|
|
e19808b918 |
4 changed files with 133 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ file(GLOB COMMON_SOURCES
|
||||||
"*.cpp"
|
"*.cpp"
|
||||||
"datamgr/*.cpp"
|
"datamgr/*.cpp"
|
||||||
"datastore/*.cpp"
|
"datastore/*.cpp"
|
||||||
|
"memory/*.cpp"
|
||||||
"mempool/*.cpp"
|
"mempool/*.cpp"
|
||||||
"objectalloc/*.cpp"
|
"objectalloc/*.cpp"
|
||||||
"processor/*.cpp"
|
"processor/*.cpp"
|
||||||
|
|
|
||||||
6
common/DataAllocator.hpp
Normal file
6
common/DataAllocator.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef COMMON_DATA_ALLOCATOR_HPP
|
||||||
|
#define COMMON_DATA_ALLOCATOR_HPP
|
||||||
|
|
||||||
|
#include "common/memory/CDataAllocator.hpp"
|
||||||
|
|
||||||
|
#endif
|
||||||
94
common/memory/CDataAllocator.cpp
Normal file
94
common/memory/CDataAllocator.cpp
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
#include "common/memory/CDataAllocator.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <storm/Error.hpp>
|
||||||
|
#include <bc/Memory.hpp>
|
||||||
|
|
||||||
|
CDataAllocator::CDataAllocator(uint32_t bytesPerData, uint32_t dataPerBlock) {
|
||||||
|
this->m_bytesPerData = std::max(bytesPerData, uint32_t(sizeof(Data)));
|
||||||
|
this->m_dataPerBlock = std::max(dataPerBlock, uint32_t(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
CDataAllocator::~CDataAllocator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void CDataAllocator::Clear(const char* fileName, int32_t lineNumber) {
|
||||||
|
if (this->m_dataUsed) {
|
||||||
|
if (!fileName) {
|
||||||
|
fileName == __FILE__;
|
||||||
|
lineNumber = __LINE__;
|
||||||
|
}
|
||||||
|
|
||||||
|
SErrDisplayErrorFmt(
|
||||||
|
0x8510007E,
|
||||||
|
fileName,
|
||||||
|
lineNumber,
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
"CDataAllocator@0x%08x: leaked %u: %s(%d) [%u][%u]",
|
||||||
|
this,
|
||||||
|
this->m_dataUsed,
|
||||||
|
fileName,
|
||||||
|
lineNumber,
|
||||||
|
this->m_dataPerBlock,
|
||||||
|
this->m_bytesPerData);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (this->m_blockList) {
|
||||||
|
auto block = this->m_blockList;
|
||||||
|
this->m_blockList = block->m_next;
|
||||||
|
SMemFree(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->m_dataUsed = 0;
|
||||||
|
this->m_dataList = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CDataAllocator::Data* CDataAllocator::GetData(int32_t zero, const char* fileName, int32_t lineNumber) {
|
||||||
|
if (this->m_dataPerBlock == 1) {
|
||||||
|
fileName = nullptr;
|
||||||
|
lineNumber = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this->m_dataList) {
|
||||||
|
if (!fileName) {
|
||||||
|
fileName = __FILE__;
|
||||||
|
lineNumber = __LINE__;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto memory = static_cast<char*>(SMemAlloc(
|
||||||
|
this->m_dataPerBlock * this->m_bytesPerData + sizeof(Block),
|
||||||
|
fileName,
|
||||||
|
lineNumber,
|
||||||
|
0));
|
||||||
|
Block* block = reinterpret_cast<Block*>(memory);
|
||||||
|
Data* data = reinterpret_cast<Data*>(memory + sizeof(Block*));
|
||||||
|
|
||||||
|
this->m_dataList = data;
|
||||||
|
for (uint32_t i = 0; i < this->m_dataPerBlock - 1; ++i) {
|
||||||
|
auto next = reinterpret_cast<char*>(data) + this->m_bytesPerData;
|
||||||
|
data->m_next = reinterpret_cast<Data*>(next);
|
||||||
|
data = data->m_next;
|
||||||
|
}
|
||||||
|
data->m_next = 0;
|
||||||
|
block->m_next = this->m_blockList;
|
||||||
|
this->m_blockList = block;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto data = this->m_dataList;
|
||||||
|
this->m_dataList = data->m_next;
|
||||||
|
if (zero) {
|
||||||
|
memset(data, 0, this->m_bytesPerData);
|
||||||
|
}
|
||||||
|
this->m_dataUsed++;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CDataAllocator::PutData(void* data, const char* fileName, int32_t lineNumber) {
|
||||||
|
STORM_ASSERT(this->m_dataUsed > 0);
|
||||||
|
auto d = reinterpret_cast<Data*>(data);
|
||||||
|
d->m_next = this->m_dataList;
|
||||||
|
this->m_dataUsed--;
|
||||||
|
this->m_dataList = d;
|
||||||
|
}
|
||||||
32
common/memory/CDataAllocator.hpp
Normal file
32
common/memory/CDataAllocator.hpp
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#ifndef COMMON_MEMORY_C_DATA_ALLOCATOR_HPP
|
||||||
|
#define COMMON_MEMORY_C_DATA_ALLOCATOR_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
class CDataAllocator {
|
||||||
|
public:
|
||||||
|
// Sub-types
|
||||||
|
struct Block {
|
||||||
|
Block* m_next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Data {
|
||||||
|
Data* m_next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Member functions
|
||||||
|
CDataAllocator(uint32_t bytesPerData, uint32_t dataPerBlock);
|
||||||
|
~CDataAllocator();
|
||||||
|
void Clear(const char* fileName, int32_t lineNumber);
|
||||||
|
Data* GetData(int32_t zero, const char* fileName, int32_t lineNumber);
|
||||||
|
void PutData(void* data, const char* fileName, int32_t lineNumber);
|
||||||
|
|
||||||
|
// Member variables
|
||||||
|
uint32_t m_bytesPerData = sizeof(Data);
|
||||||
|
uint32_t m_dataPerBlock = 1;
|
||||||
|
uint32_t m_dataUsed = 0;
|
||||||
|
Block* m_blockList = nullptr;
|
||||||
|
Data* m_dataList = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue