From a346a329b32cfa887cc0d4fa3f229aeccd33706d Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 18 Jan 2016 10:16:35 +0100 Subject: [PATCH] Added an option to disable the Pool allocation in Config.ini, in case there is a mess with the MemoryPool. --- include/Engine/Core/MemoryPool.h | 18 ++++++++++++++---- resources/DefaultConfig.ini | 4 +++- src/Engine/Core/MemoryPool.cpp | 5 +++++ src/Game/Game.cpp | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/Engine/Core/MemoryPool.cpp diff --git a/include/Engine/Core/MemoryPool.h b/include/Engine/Core/MemoryPool.h index ff24ac80..3a1cc069 100644 --- a/include/Engine/Core/MemoryPool.h +++ b/include/Engine/Core/MemoryPool.h @@ -5,6 +5,14 @@ template class MemoryPoolForwardIterator; +namespace DisableMemoryPool +{ +//if true -> Pool allocation is not used when calling Allocate/Free, just use regular dynamic allocation. +//if false -> Use pool allocation. +//Should default to false, unless the DisableMemoryPool is true in the Config.ini files. +extern bool Value; +} + //This is the class to use if you want to allocate blocks (slots) of raw memory, with a fixed maximum size (stride). //Additionally, if you know that every memory-block will contain one object of a specific type, (i.e. the stride for the slot //will the size of the object type) you should use ObjectPool instead, your life will become easier. @@ -80,8 +88,8 @@ public: //If element cannot be allocated in the pool, because the memory ran out, memory is allocated dynamically with malloc() "outside the pool". char* Allocate() { - for (; m_CurrentAllocSlot < m_NumSlots && m_SlotIsAllocated[m_CurrentAllocSlot]; ++m_CurrentAllocSlot); - if (m_CurrentAllocSlot < m_NumSlots) { + for (; m_CurrentAllocSlot < m_NumSlots && m_SlotIsAllocated[m_CurrentAllocSlot] && !DisableMemoryPool::Value; ++m_CurrentAllocSlot); + if (m_CurrentAllocSlot < m_NumSlots && !DisableMemoryPool::Value) { if (m_LowestAllocatedSlot > m_CurrentAllocSlot) m_LowestAllocatedSlot = m_CurrentAllocSlot; //Mark the slot as allocated. @@ -93,7 +101,9 @@ public: else { m_ExtraMemory.push_back((char*)malloc(m_Stride)); //We should preferably not enter here to avoid performance issues. Set more numMaxElements in constructor instead. - LOG_WARNING("Allocated slots exceed Pool size, extra memory allocated dynamically. Pool size: %u, dynamic size: %u.", m_NumSlots, m_ExtraMemory.size()); + if (!DisableMemoryPool::Value) { + LOG_WARNING("Allocated slots exceed Pool size, extra memory allocated dynamically. Pool size: %u, dynamic size: %u.", m_NumSlots, m_ExtraMemory.size()); + } return m_ExtraMemory.back(); } } @@ -108,7 +118,7 @@ public: //(i.e. IsAllocatedInPool may give false positives) //if it was malloc():ed //so, we may enter here even if we shouldn't. - if (IsAllocatedInPool(obj)) { + if (!DisableMemoryPool::Value && IsAllocatedInPool(obj)) { --m_NumAllocatedSlots; const size_t freeSlot = (obj - m_StartAddress) / m_Stride; m_SlotIsAllocated[freeSlot] = false; diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index d84b15bb..fb490ec8 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -2,7 +2,9 @@ LogLevel=1 LoadMap= EditorEnabled=false - +; if true -> Pool allocation is not used when calling Allocate/Free, just use regular dynamic allocation. +; if false -> Use pool allocation. +DisableMemoryPool=false [Video] Fullscreen=false diff --git a/src/Engine/Core/MemoryPool.cpp b/src/Engine/Core/MemoryPool.cpp new file mode 100644 index 00000000..8978641b --- /dev/null +++ b/src/Engine/Core/MemoryPool.cpp @@ -0,0 +1,5 @@ +#include "Core/MemoryPool.h" +namespace DisableMemoryPool +{ +bool Value = false; +} \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c0cad6d3..80b45eb8 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -16,6 +16,7 @@ Game::Game(int argc, char* argv[]) m_Config = ResourceManager::Load("Config.ini"); ResourceManager::UseThreading = m_Config->Get("Multithreading.ResourceLoading", true); + DisableMemoryPool::Value = m_Config->Get("Debug.DisableMemoryPool", false); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); // Create the core event broker