diff --git a/src/Parser/Lexer/Lexer.hpp b/src/Parser/Lexer/Lexer.hpp index 85e0e9a..acb241a 100644 --- a/src/Parser/Lexer/Lexer.hpp +++ b/src/Parser/Lexer/Lexer.hpp @@ -22,7 +22,7 @@ namespace MalachScript::Parser { size_t _scriptLength; Diagnostics::Logger* _diagnostics; - Utils::MemoryPool<2048, 1024> _allocator; + Utils::MemoryPool<4096> _allocator; inline char8_t Consume() { if (++_position >= _scriptLength) { diff --git a/src/Utils/MemoryPool.hpp b/src/Utils/MemoryPool.hpp index f53c6dd..a6a6287 100644 --- a/src/Utils/MemoryPool.hpp +++ b/src/Utils/MemoryPool.hpp @@ -6,24 +6,23 @@ #include namespace MalachScript::Utils { - template class MemoryPool { + template class MemoryPool { public: - MemoryPool() { - auto* ptr = malloc(initialSize); - if (ptr == nullptr) { - throw std::logic_error("Out of memory."); - } - _head = static_cast(ptr); - _tail = _head; - _end = _head + initialSize; - } + MemoryPool() { AllocateBlock(); } + MemoryPool(MemoryPool const&) = delete; + MemoryPool& operator=(MemoryPool const&) = delete; - ~MemoryPool() { free(_head); } + ~MemoryPool() { + for (auto c : _chunks) { + free(c); + } + } template T* Create(parameters... args) { auto* p = _tail + sizeof(T); if (p >= _end) { - IncreaseStep(); + AllocateBlock(); + p = _tail + sizeof(T); } auto* element = new (_tail) T(args...); _tail = p; @@ -31,17 +30,20 @@ namespace MalachScript::Utils { } private: + std::vector _chunks; + uint8_t* _tail = nullptr; uint8_t* _end; uint8_t* _head = nullptr; - void IncreaseStep() { - _end += steps; - auto newPtr = realloc(_head, _end - _head); + void AllocateBlock() { + auto* newPtr = malloc(steps * sizeof(uint8_t*)); if (newPtr == nullptr) { throw std::logic_error("Out of memory."); } - _head = static_cast(newPtr); + _tail = _head = static_cast(newPtr); + _end = _head + (steps * sizeof(uint8_t*)); + _chunks.push_back(_head); } }; }