diff --git a/ECSMemoryPool/ecs_memorypool.cpp b/ECSMemoryPool/ecs_memorypool.cpp index be14c36..04ca6f2 100644 --- a/ECSMemoryPool/ecs_memorypool.cpp +++ b/ECSMemoryPool/ecs_memorypool.cpp @@ -3,3 +3,57 @@ ECSMemoryPool::ECSMemoryPool() { } + +ChunkAccessIndex::ChunkAccessIndex(ElementControlBlock* ecb, uint64_t chunk_type_code) +:_element_bind(MemoryElement(ecb)), _chunk_type_code(chunk_type_code) +{ + +} + +ChunkAccessIndex::ChunkAccessIndex(const ChunkAccessIndex& other) :_element_bind(other._element_bind), +_chunk_type_code(other._chunk_type_code), +_member_offset(other._member_offset) +{ + +} + +uint64_t ChunkAccessIndex::chunkType() const +{ + return _chunk_type_code; +} + +uint16_t ChunkAccessIndex::chunkSize() const +{ + return _element_bind.typeSize(); +} + +bool ChunkAccessIndex::contains(uint64_t type_code) +{ + return _member_offset.find(type_code) != _member_offset.cend(); +} + +int8_t ChunkAccessIndex::setMember(uint64_t type_code, uint16_t offset, uint16_t size) +{ + if (_element_bind.typeSize() <= offset + size) { + return -1; + } + + _member_offset[type_code] = std::make_pair(offset, size); + return 0; +} + +unsigned char* ChunkAccessIndex::getLockedDataPtr(uint64_t type_code) +{ + auto index_offset = _member_offset.find(type_code); + if (index_offset == _member_offset.cend()) + return nullptr; + + auto raw_ptr = _element_bind.dataLock(); + auto data_target_ptr = raw_ptr + index_offset->first; + return data_target_ptr; +} + +void ChunkAccessIndex::releaseData() +{ + _element_bind.release(); +} diff --git a/ECSMemoryPool/ecs_memorypool.h b/ECSMemoryPool/ecs_memorypool.h index 59e0fe2..017a793 100644 --- a/ECSMemoryPool/ecs_memorypool.h +++ b/ECSMemoryPool/ecs_memorypool.h @@ -4,6 +4,7 @@ #include #include #include +#include #include template @@ -21,7 +22,7 @@ concept CompenentType = requires(T t, T other, const QJsonObject & in, QJsonObje template class ComponentRefer : protected MemoryElement { public: ComponentRefer(ElementControlBlock* data) : MemoryElement(data) { referAdd(); } - ComponentRefer(const ComponentRefer &other) :MemoryElement(other.data_ptr) { referAdd(); } + ComponentRefer(const ComponentRefer& other) :MemoryElement(other.data_ptr) { referAdd(); } virtual ~ComponentRefer() { referSub(); } T* dataLock() { @@ -34,6 +35,26 @@ public: }; +class ECSMEMORYPOOL_EXPORT ChunkAccessIndex { +private: + MemoryElement _element_bind; + uint64_t _chunk_type_code; + std::map> _member_offset; + +public: + ChunkAccessIndex(ElementControlBlock* ecb, uint64_t chunk_type_code); + ChunkAccessIndex(const ChunkAccessIndex& other); + + uint64_t chunkType() const; + uint16_t chunkSize() const; + bool contains(uint64_t type_code); + + int8_t setMember(uint64_t type_code, uint16_t offset, uint16_t size); + unsigned char* getLockedDataPtr(uint64_t type_code); + void releaseData(); +}; + + class ECSMEMORYPOOL_EXPORT ECSMemoryPool { private: std::mutex _pool_protected_; @@ -55,7 +76,7 @@ public: /// /// /// - template void registerComponent(const T &one) { + template void registerComponent(const T& one) { std::lock_guard lockx(_pool_protected_); auto ptr = new char[T::typeSize()];