WsComponentPool/ECSMemoryPool/memory_pages.cpp

157 lines
3.9 KiB
C++
Raw Normal View History

2025-09-11 10:36:49 +00:00
#include "memory_pages.h"
2025-09-27 15:41:15 +00:00
MemoryPage::MemoryPage(uint16_t chunk_size, uint64_t typecode)
2025-09-11 10:36:49 +00:00
{
2025-09-27 15:41:15 +00:00
pcb.byte_count_per_chunk = chunk_size;
2025-09-11 10:36:49 +00:00
pcb.total_buffer_size = sizeof(data_buffer);
2025-09-27 15:41:15 +00:00
pcb.typecode_of_chunk = typecode;
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:45:04 +00:00
uint16_t MemoryPage::chunkSize() const
2025-09-11 14:01:42 +00:00
{
2025-09-27 15:41:15 +00:00
return pcb.byte_count_per_chunk - MemoryChunk::data_buffer_offset;
2025-09-11 14:01:42 +00:00
}
uint16_t MemoryPage::getActiveCount(uint16_t inc) const
{
auto data_ptr = const_cast<MemoryPage*>(this);
std::lock_guard<std::mutex> g(data_ptr->pcb.access_protected);
2025-09-27 15:41:15 +00:00
data_ptr->pcb.active_chunks_count += inc;
return data_ptr->pcb.active_chunks_count;
2025-09-11 14:01:42 +00:00
}
2025-09-11 10:36:49 +00:00
void MemoryPage::accessRecord(uint64_t timepoint_usec) {
std::lock_guard<std::mutex> g(pcb.access_protected);
pcb.curr_access_usec = timepoint_usec;
pcb.acc_count_per_cycle++;
}
uint32_t MemoryPage::timesClear() {
std::lock_guard<std::mutex> g(pcb.access_protected);
auto value = pcb.acc_count_per_cycle;
pcb.acc_count_per_cycle = 0;
return value;
}
std::pair<uint64_t, uint32_t> MemoryPage::getRecords() const {
auto data_ptr = const_cast<MemoryPage*>(this);
std::lock_guard<std::mutex> g(data_ptr->pcb.access_protected);
return std::make_pair(pcb.curr_access_usec, pcb.acc_count_per_cycle);
}
2025-09-27 15:41:15 +00:00
ChunkControlBlock* MemoryPage::getChunkPtr(int index) {
2025-09-26 16:46:46 +00:00
auto member_cnt = maxCapacity();
2025-09-27 15:41:15 +00:00
auto chunk_size = chunkRawSize();
2025-09-11 10:36:49 +00:00
2025-09-27 15:41:15 +00:00
ChunkControlBlock* chunk_ptr = nullptr;
2025-09-11 10:36:49 +00:00
if (index >= 0 && index < member_cnt) {
2025-09-27 15:41:15 +00:00
chunk_ptr = (ChunkControlBlock*)(data_buffer + index * chunk_size);
2025-09-11 10:36:49 +00:00
}
else if (index < 0 && index >= -member_cnt) {
2025-09-27 15:41:15 +00:00
chunk_ptr = (ChunkControlBlock*)(data_buffer + (member_cnt + index) * chunk_size);
2025-09-11 10:36:49 +00:00
}
lock();
2025-09-27 15:41:15 +00:00
if (chunk_ptr) {
chunk_ptr->page_refer = this;
chunk_ptr->chunk_index = index;
2025-09-11 10:36:49 +00:00
}
release();
2025-09-27 15:41:15 +00:00
return chunk_ptr;
2025-09-11 10:36:49 +00:00
}
void MemoryPage::lock()
{
pcb.access_protected.lock();
}
void MemoryPage::release() {
#pragma warning(push)
#pragma warning(disable : 26110)
pcb.access_protected.unlock();
#pragma warning(pop)
}
2025-09-27 15:41:15 +00:00
uint64_t MemoryPage::chunkTypeCode() const
2025-09-11 10:36:49 +00:00
{
auto data_ptr = const_cast<MemoryPage*>(this);
std::lock_guard<std::mutex> g(data_ptr->pcb.access_protected);
2025-09-27 15:41:15 +00:00
return pcb.typecode_of_chunk;
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:45:04 +00:00
uint16_t MemoryPage::chunkRawSize() const {
2025-09-11 10:36:49 +00:00
auto data_ptr = const_cast<MemoryPage*>(this);
std::lock_guard<std::mutex> g(data_ptr->pcb.access_protected);
2025-09-27 15:41:15 +00:00
return pcb.byte_count_per_chunk;
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:46:46 +00:00
uint16_t MemoryPage::maxCapacity() const {
2025-09-11 10:36:49 +00:00
uint16_t cnt = sizeof(data_buffer);
2025-09-26 16:45:04 +00:00
return cnt / this->chunkRawSize();
2025-09-11 10:36:49 +00:00
}
2025-09-27 15:41:15 +00:00
// chunk =================================================================
2025-09-26 16:46:46 +00:00
const uint32_t MemoryChunk::data_buffer_offset = MemoryChunk::validOffset();
2025-09-26 16:46:46 +00:00
uint32_t MemoryChunk::validOffset()
2025-09-11 10:36:49 +00:00
{
2025-09-27 15:41:15 +00:00
uint32_t remains = sizeof(ChunkControlBlock) % 16;
uint32_t times = sizeof(ChunkControlBlock) / 16;
return (remains ? times + 1 : times) * 16;
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:46:46 +00:00
uint32_t MemoryChunk::rawSize(uint32_t data_type_size)
2025-09-11 10:36:49 +00:00
{
uint32_t minimal_size = validOffset() + data_type_size;
auto remains = minimal_size % 16;
auto times = minimal_size / 16;
return (remains ? times + 1 : times) * 16;
}
2025-09-27 15:41:15 +00:00
MemoryChunk::MemoryChunk(ChunkControlBlock* access_bind)
: data_ptr((ChunkControlBlock*)access_bind) {}
2025-09-26 16:46:46 +00:00
uint16_t MemoryChunk::typeSize() const
{
auto page_ptr = data_ptr->page_refer;
2025-09-26 16:45:04 +00:00
return page_ptr->chunkRawSize();
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:46:46 +00:00
void MemoryChunk::accessUpdate(uint64_t time_usec)
2025-09-11 10:36:49 +00:00
{
data_ptr->page_refer->accessRecord(time_usec);
}
2025-09-27 15:41:15 +00:00
bool MemoryChunk::isActived(ChunkControlBlock* refer)
2025-09-11 14:01:42 +00:00
{
refer->page_refer->lock();
auto mark = refer->refer_count;
refer->page_refer->release();
return mark;
}
2025-09-26 16:46:46 +00:00
void MemoryChunk::referAdd()
2025-09-11 10:36:49 +00:00
{
data_ptr->page_refer->lock();
2025-09-11 14:01:42 +00:00
data_ptr->refer_count++;
2025-09-11 10:36:49 +00:00
data_ptr->page_refer->release();
}
2025-09-26 16:46:46 +00:00
void MemoryChunk::referSub()
2025-09-11 10:36:49 +00:00
{
data_ptr->page_refer->lock();
2025-09-11 14:01:42 +00:00
data_ptr->refer_count--;
2025-09-11 10:36:49 +00:00
data_ptr->page_refer->release();
}
2025-09-26 16:46:46 +00:00
unsigned char* MemoryChunk::dataLock()
2025-09-11 10:36:49 +00:00
{
data_ptr->page_refer->lock();
2025-09-11 14:01:42 +00:00
return ((unsigned char*)data_ptr) + data_buffer_offset;
2025-09-11 10:36:49 +00:00
}
2025-09-26 16:46:46 +00:00
void MemoryChunk::release()
2025-09-11 10:36:49 +00:00
{
data_ptr->page_refer->release();
}