From: Mikko Rasa Date: Sun, 7 May 2023 17:32:02 +0000 (+0300) Subject: Chain certain pool operations to pools of derived objects X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=bf1bc4671b84ead1491ba38fe8faa79a63994218;p=libs%2Fgame.git Chain certain pool operations to pools of derived objects --- diff --git a/source/game/pool.cpp b/source/game/pool.cpp index 174a939..a986302 100644 --- a/source/game/pool.cpp +++ b/source/game/pool.cpp @@ -138,6 +138,13 @@ void PoolBase::add_block() } void PoolBase::destroy(void *obj) +{ + if(_destroy(obj)) + throw invalid_argument("PoolBase::destroy"); +} + + +int PoolBase::_destroy(void *obj) { unsigned block_index = 0; unsigned object_index = BLOCK_SIZE; @@ -156,17 +163,27 @@ void PoolBase::destroy(void *obj) } if(object_index>=BLOCK_SIZE) - throw invalid_argument("PoolBase::destroy"); + { + for(PoolBase *p: derived_pools) + { + int result = p->_destroy(obj); + if(result!=1) + return result; + } + return 1; + } FlagType *flags = reinterpret_cast(blocks[block_index]+BLOCK_SIZE*object_size); FlagType bit = 1<<(object_index%FLAG_BITS); if(!(flags[object_index/FLAG_BITS]&bit)) - throw invalid_argument("PoolBase::destroy"); + return 2; deleter(obj); flags[object_index/FLAG_BITS] &= ~bit; --object_count; free_list[object_count] = block_index*BLOCK_SIZE+object_index; + + return 0; } } // namespace Msp::Game diff --git a/source/game/pool.h b/source/game/pool.h index dde179d..6357b86 100644 --- a/source/game/pool.h +++ b/source/game/pool.h @@ -85,6 +85,9 @@ private: public: std::uint32_t get_capacity() const { return capacity; } void destroy(void *); + +private: + int _destroy(void *); }; @@ -153,6 +156,9 @@ inline void PoolBase::iterate_objects(const F &func) ++flags; } } + + for(PoolBase *p: derived_pools) + p->iterate_objects(func); }