#ifndef SLANG_CONTAINER_POOL_H #define SLANG_CONTAINER_POOL_H #include "../core/slang-dictionary.h" #include "../core/slang-list.h" #include "../core/slang-virtual-object-pool.h" // A pool to allow reuse of common types of containers to avoid // frequent resizing and rehashing. namespace Slang { static const int kContainerPoolSize = 1024; template struct ObjectPool { ObjectPool(int maxElementCount) { m_pool.initPool(maxElementCount); m_objects.setCount(maxElementCount); } T* getObject() { auto id = m_pool.alloc(1); if (id == -1) SLANG_UNEXPECTED("container pool allocation failure."); return &m_objects[id]; } void freeObject(T* object) { auto id = (int)(object - m_objects.getBuffer()); m_pool.free(id, 1); } VirtualObjectPool m_pool; List m_objects; }; struct ContainerPool { ObjectPool> m_listPool; ObjectPool> m_dictionaryPool; ObjectPool> m_hashSetPool; ContainerPool() : m_listPool(kContainerPoolSize) , m_dictionaryPool(kContainerPoolSize) , m_hashSetPool(kContainerPoolSize) { } template List* getList() { return (List*)m_listPool.getObject(); } template Dictionary* getDictionary() { return (Dictionary*)m_dictionaryPool.getObject(); } template HashSet* getHashSet() { return (HashSet*)m_hashSetPool.getObject(); } template void free(List* list) { list->clear(); m_listPool.freeObject((List*)list); } template void free(Dictionary* dict) { dict->clear(); m_dictionaryPool.freeObject((Dictionary*)dict); } template void free(HashSet* set) { set->clear(); m_hashSetPool.freeObject((HashSet*)set); } }; } // namespace Slang #endif