diff --git a/common/memorypool.cpp b/common/memorypool.cpp new file mode 100644 index 00000000000..8bd7f802ca0 --- /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 bd3e9f21a0a..a7c0f4eb903 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 \