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
This commit is contained in:
Bertrand Augereau 2008-03-30 05:42:39 +00:00
parent dc813c1c20
commit 411a588850
3 changed files with 129 additions and 0 deletions

34
common/memorypool.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef COMMON_MEMORYPOOL_H
#define COMMON_MEMORYPOOL_H
#include <cstring>
#include "common/array.h"
namespace Common
{
class MemoryPool
{
private:
MemoryPool(const MemoryPool&);
MemoryPool& operator=(const MemoryPool&);
size_t _chunkSize;
Array<void*> _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