From 277ac7c741583afbd5970ce0aec98371615b22db Mon Sep 17 00:00:00 2001 From: Terraneo Federico <fede.tft@hotmail.it> Date: Wed, 18 Apr 2012 20:45:54 +0200 Subject: [PATCH] Starting the implementation of the pool allocator --- Makefile | 2 +- pool_allocator.cpp | 26 ++++++++++++++++++++++++++ pool_allocator.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 pool_allocator.cpp create mode 100644 pool_allocator.h diff --git a/Makefile b/Makefile index 6c8092c3..4722f612 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ SUBDIRS := miosix ## List here your source files (both .s, .c and .cpp) ## SRC := \ -main.cpp +main.cpp pool_allocator.cpp ## ## List here additional static libraries with relative path diff --git a/pool_allocator.cpp b/pool_allocator.cpp new file mode 100644 index 00000000..4e682cfc --- /dev/null +++ b/pool_allocator.cpp @@ -0,0 +1,26 @@ + +#include "pool_allocator.h" + +PoolAllocator::PoolAllocator(unsigned int *poolBase, unsigned int poolSize) + : poolBase(poolBase), poolSize(poolSize) +{ + bitmap=new unsigned int[poolSize/blockSize/sizeof(unsigned int)]; +} + +unsigned int *PoolAllocator::allocate(int size) +{ + //TODO : align this, and validate size to prevent wraparound + unsigned int offset=0; + if(poolBase % size) offset=size-(poolBase % size); + int startBit=offset/blockSize; +} + +void PoolAllocator::deallocate(unsigned int *ptr) +{ + +} + +PoolAllocator::~PoolAllocator() +{ + delete[] bitmap; +} diff --git a/pool_allocator.h b/pool_allocator.h new file mode 100644 index 00000000..40b42f99 --- /dev/null +++ b/pool_allocator.h @@ -0,0 +1,30 @@ + +#include <map> + +#ifndef POOL_ALLOCATOR +#define POOL_ALLOCATOR + +class PoolAllocator +{ +public: + PoolAllocator(unsigned int *poolBase, unsigned int poolSize); + + unsigned int *allocate(int size); + + void deallocate(unsigned int *ptr); + + ~PoolAllocator(); + + static const int blockSize=1024; + +private: + PoolAllocator(const PoolAllocator&); + PoolAllocator& operator= (const PoolAllocator&); + + unsigned int *bitmap; + unsigned int *poolBase; + unsigned int poolSize; + std::map<unsigned int*,unsigned int> allocatedBlocks; +}; + +#endif //POOL_ALLOCATOR -- GitLab