From 411a588850604d67e8a606c0496999f5065d35d5 Mon Sep 17 00:00:00 2001 From: Bertrand Augereau Date: Sun, 30 Mar 2008 05:42:39 +0000 Subject: Introduction of a fixed size memory pool with a typical free list implementation + : amortized O(1) allocation, O(1) deallocation, less overhead per allocation - : unused memory is not reclaimed until death or manual invocation of a function svn-id: r31320 --- common/memorypool.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ common/memorypool.h | 34 +++++++++++++++++++ common/module.mk | 1 + 3 files changed, 129 insertions(+) create mode 100644 common/memorypool.cpp create mode 100644 common/memorypool.h diff --git a/common/memorypool.cpp b/common/memorypool.cpp new file mode 100644 index 0000000000..8bd7f802ca --- /dev/null +++ b/common/memorypool.cpp @@ -0,0 +1,94 @@ +#include "common/memorypool.h" +#include + +namespace Common +{ + +static const size_t CHUNK_PAGE_SIZE = 32; + +void* MemoryPool::allocPage() { + void* result = ::malloc(CHUNK_PAGE_SIZE * _chunkSize); + _pages.push_back(result); + void* current = result; + for(size_t i=1; i= page) && (ptr < (char*)page + CHUNK_PAGE_SIZE * _chunkSize); +} + +void MemoryPool::freeUnusedPages() { + //std::sort(_pages.begin(), _pages.end()); + Array numberOfFreeChunksPerPage; + numberOfFreeChunksPerPage.resize(_pages.size()); + for(size_t i=0; i +#include "common/array.h" + +namespace Common +{ + +class MemoryPool +{ + private: + MemoryPool(const MemoryPool&); + MemoryPool& operator=(const MemoryPool&); + + size_t _chunkSize; + Array _pages; + void* _next; + + void* allocPage(); + bool isPointerInPage(void* ptr, void* page); + public: + MemoryPool(size_t chunkSize); + ~MemoryPool(); + + void* malloc(); + void free(void* ptr); + + void freeUnusedPages(); +}; + +} + +#endif diff --git a/common/module.mk b/common/module.mk index bd3e9f21a0..a7c0f4eb90 100644 --- a/common/module.mk +++ b/common/module.mk @@ -7,6 +7,7 @@ MODULE_OBJS := \ file.o \ fs.o \ hashmap.o \ + memorypool.o \ md5.o \ mutex.o \ str.o \ -- cgit v1.2.3