diff --git a/ECSMemoryPool/ecs_memorypool.h b/ECSMemoryPool/ecs_memorypool.h index ba43a97..ba03ed7 100644 --- a/ECSMemoryPool/ecs_memorypool.h +++ b/ECSMemoryPool/ecs_memorypool.h @@ -18,17 +18,17 @@ concept CompenentType = requires(T t, T other, const QJsonObject & in, QJsonObje /// 组件引用符号 /// /// -template class ComponentRefer : protected MemoryElement { +template class ComponentRefer : protected MemoryChunk { public: - ComponentRefer(ElementControlBlock* data) : MemoryElement(data) { referAdd(); } - ComponentRefer(const ComponentRefer &other) :MemoryElement(other.data_ptr) { referAdd(); } + ComponentRefer(ElementControlBlock* data) : MemoryChunk(data) { referAdd(); } + ComponentRefer(const ComponentRefer &other) :MemoryChunk(other.data_ptr) { referAdd(); } virtual ~ComponentRefer() { referSub(); } T* dataLock() { - return (T*)(MemoryElement::dataLock()); + return (T*)(MemoryChunk::dataLock()); } void unlock() { - MemoryElement::release(); + MemoryChunk::release(); } }; @@ -82,7 +82,7 @@ public: // 迭代内存页集合 for (auto page_ptr : page_list) { - auto max_element_cnt = page_ptr->maxChunkCapacity(); + auto max_element_cnt = page_ptr->maxCapacity(); auto active_cnt = page_ptr->getActiveCount(); // 初级校核可用数量 if (active_cnt < max_element_cnt) { @@ -109,7 +109,7 @@ public: } { // 生成新的内存页 - auto new_page = new MemoryPage(MemoryElement::rawSize(T::typeSize()), type_code); + auto new_page = new MemoryPage(MemoryChunk::rawSize(T::typeSize()), type_code); _storage_pages.insert(type_code, new_page); refer_ptr = new_page->getChunkPtr(0); refer_ptr->page_refer->getActiveCount(1); diff --git a/ECSMemoryPool/memory_pages.cpp b/ECSMemoryPool/memory_pages.cpp index b12ef19..4089810 100644 --- a/ECSMemoryPool/memory_pages.cpp +++ b/ECSMemoryPool/memory_pages.cpp @@ -9,7 +9,7 @@ MemoryPage::MemoryPage(uint16_t element_size, uint64_t typecode) uint16_t MemoryPage::chunkSize() const { - return pcb.byte_count_per_element - MemoryElement::data_buffer_offset; + return pcb.byte_count_per_element - MemoryChunk::data_buffer_offset; } uint16_t MemoryPage::getActiveCount(uint16_t inc) const @@ -40,7 +40,7 @@ std::pair MemoryPage::getRecords() const { } ElementControlBlock* MemoryPage::getChunkPtr(int index) { - auto member_cnt = maxChunkCapacity(); + auto member_cnt = maxCapacity(); auto element_size = chunkRawSize(); ElementControlBlock* element_ptr = nullptr; @@ -85,22 +85,22 @@ uint16_t MemoryPage::chunkRawSize() const { return pcb.byte_count_per_element; } -uint16_t MemoryPage::maxChunkCapacity() const { +uint16_t MemoryPage::maxCapacity() const { uint16_t cnt = sizeof(data_buffer); return cnt / this->chunkRawSize(); } // element ================================================================= -const uint32_t MemoryElement::data_buffer_offset = MemoryElement::validOffset(); +const uint32_t MemoryChunk::data_buffer_offset = MemoryChunk::validOffset(); -uint32_t MemoryElement::validOffset() +uint32_t MemoryChunk::validOffset() { uint32_t remains = sizeof(ElementControlBlock) % 16; uint32_t times = sizeof(ElementControlBlock) / 16; return (remains ? times + 1 : times) * 16; } -uint32_t MemoryElement::rawSize(uint32_t data_type_size) +uint32_t MemoryChunk::rawSize(uint32_t data_type_size) { uint32_t minimal_size = validOffset() + data_type_size; auto remains = minimal_size % 16; @@ -108,21 +108,21 @@ uint32_t MemoryElement::rawSize(uint32_t data_type_size) return (remains ? times + 1 : times) * 16; } -MemoryElement::MemoryElement(ElementControlBlock* access_bind) +MemoryChunk::MemoryChunk(ElementControlBlock* access_bind) : data_ptr((ElementControlBlock*)access_bind) {} -uint16_t MemoryElement::typeSize() const +uint16_t MemoryChunk::typeSize() const { auto page_ptr = data_ptr->page_refer; return page_ptr->chunkRawSize(); } -void MemoryElement::accessUpdate(uint64_t time_usec) +void MemoryChunk::accessUpdate(uint64_t time_usec) { data_ptr->page_refer->accessRecord(time_usec); } -bool MemoryElement::isActived(ElementControlBlock* refer) +bool MemoryChunk::isActived(ElementControlBlock* refer) { refer->page_refer->lock(); auto mark = refer->refer_count; @@ -130,27 +130,27 @@ bool MemoryElement::isActived(ElementControlBlock* refer) return mark; } -void MemoryElement::referAdd() +void MemoryChunk::referAdd() { data_ptr->page_refer->lock(); data_ptr->refer_count++; data_ptr->page_refer->release(); } -void MemoryElement::referSub() +void MemoryChunk::referSub() { data_ptr->page_refer->lock(); data_ptr->refer_count--; data_ptr->page_refer->release(); } -unsigned char* MemoryElement::dataLock() +unsigned char* MemoryChunk::dataLock() { data_ptr->page_refer->lock(); return ((unsigned char*)data_ptr) + data_buffer_offset; } -void MemoryElement::release() +void MemoryChunk::release() { data_ptr->page_refer->release(); } diff --git a/ECSMemoryPool/memory_pages.h b/ECSMemoryPool/memory_pages.h index 153e276..1eea16a 100644 --- a/ECSMemoryPool/memory_pages.h +++ b/ECSMemoryPool/memory_pages.h @@ -110,13 +110,13 @@ public: /// 获取元素数量 /// /// - uint16_t maxChunkCapacity() const; + uint16_t maxCapacity() const; }; /// /// 内存元素访问接口 /// -class ECSMEMORYPOOL_EXPORT MemoryElement { +class ECSMEMORYPOOL_EXPORT MemoryChunk { protected: ElementControlBlock* const data_ptr; @@ -134,7 +134,7 @@ public: /// 构建内存元素访问接口 /// /// - MemoryElement(ElementControlBlock* access_bind); + MemoryChunk(ElementControlBlock* access_bind); /// /// 提取关联元素尺寸