From 6e4122e551eeb41d3e950b363dd837d5586fe560 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 31 Jul 2015 16:49:10 +0200 Subject: Unified the doxy-comment format. --- src/AllocationPool.h | 129 ++++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 58 deletions(-) (limited to 'src/AllocationPool.h') diff --git a/src/AllocationPool.h b/src/AllocationPool.h index e82f9807e..4815ab414 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -3,6 +3,10 @@ #include + + + + template class cAllocationPool { @@ -12,100 +16,109 @@ public: public: virtual ~cStarvationCallbacks() {} - /** Is called when the reserve buffer starts to be used **/ + /** Is called when the reserve buffer starts to be used */ virtual void OnStartUsingReserve() = 0; - /** Is called once the reserve buffer has returned to normal size **/ + /** Is called once the reserve buffer has returned to normal size */ virtual void OnEndUsingReserve() = 0; /** Is called when the allocation pool is unable to allocate memory. Will be repeatedly - called if it does not free sufficient memory **/ + called if it does not free sufficient memory */ virtual void OnOutOfReserve() = 0; }; virtual ~cAllocationPool() {} - /** Allocates a pointer to T **/ + /** Allocates a pointer to T */ virtual T * Allocate() = 0; - /** Frees the pointer passed in a_ptr, invalidating it **/ + /** Frees the pointer passed in a_ptr, invalidating it */ virtual void Free(T * a_ptr) = 0; }; + + + + /** Allocates memory storing unused elements in a linked list. Keeps at least NumElementsInReserve -elements in the list unless malloc fails so that the program has a reserve to handle OOM.**/ +elements in the list unless malloc fails so that the program has a reserve to handle OOM. */ template -class cListAllocationPool : public cAllocationPool +class cListAllocationPool: + public cAllocationPool { - public: +public: - cListAllocationPool(std::unique_ptr::cStarvationCallbacks> a_Callbacks) : - m_Callbacks(std::move(a_Callbacks)) + cListAllocationPool(std::unique_ptr::cStarvationCallbacks> a_Callbacks): + m_Callbacks(std::move(a_Callbacks)) + { + for (size_t i = 0; i < NumElementsInReserve; i++) { - for (size_t i = 0; i < NumElementsInReserve; i++) + void * space = malloc(sizeof(T)); + if (space == nullptr) { - void * space = malloc(sizeof(T)); - if (space == nullptr) - { - m_Callbacks->OnStartUsingReserve(); - break; - } - m_FreeList.push_front(space); + m_Callbacks->OnStartUsingReserve(); + break; } + m_FreeList.push_front(space); } + } - virtual ~cListAllocationPool() + + virtual ~cListAllocationPool() + { + while (!m_FreeList.empty()) { - while (!m_FreeList.empty()) - { - free (m_FreeList.front()); - m_FreeList.pop_front(); - } + free (m_FreeList.front()); + m_FreeList.pop_front(); } + } - virtual T * Allocate() override + + virtual T * Allocate() override + { + if (m_FreeList.size() <= NumElementsInReserve) { - if (m_FreeList.size() <= NumElementsInReserve) + void * space = malloc(sizeof(T)); + if (space != nullptr) { - void * space = malloc(sizeof(T)); - if (space != nullptr) - { - return new(space) T; - } - else if (m_FreeList.size() == NumElementsInReserve) - { - m_Callbacks->OnStartUsingReserve(); - } - else if (m_FreeList.empty()) - { - m_Callbacks->OnOutOfReserve(); - // Try again until the memory is avalable - return Allocate(); - } + return new(space) T; } - // placement new, used to initalize the object - T * ret = new (m_FreeList.front()) T; - m_FreeList.pop_front(); - return ret; - } - virtual void Free(T * a_ptr) override - { - if (a_ptr == nullptr) + else if (m_FreeList.size() == NumElementsInReserve) { - return; + m_Callbacks->OnStartUsingReserve(); } - // placement destruct. - a_ptr->~T(); - m_FreeList.push_front(a_ptr); - if (m_FreeList.size() == NumElementsInReserve) + else if (m_FreeList.empty()) { - m_Callbacks->OnEndUsingReserve(); + m_Callbacks->OnOutOfReserve(); + // Try again until the memory is avalable + return Allocate(); } } + // placement new, used to initalize the object + T * ret = new (m_FreeList.front()) T; + m_FreeList.pop_front(); + return ret; + } + + + virtual void Free(T * a_ptr) override + { + if (a_ptr == nullptr) + { + return; + } + // placement destruct. + a_ptr->~T(); + m_FreeList.push_front(a_ptr); + if (m_FreeList.size() == NumElementsInReserve) + { + m_Callbacks->OnEndUsingReserve(); + } + } - private: - std::list m_FreeList; - std::unique_ptr::cStarvationCallbacks> m_Callbacks; +private: + std::list m_FreeList; + std::unique_ptr::cStarvationCallbacks> m_Callbacks; }; -- cgit v1.2.3