diff --git a/Makefile b/Makefile index 6c8092c395ce60ea16c806f71d2a1011ae067d4a..4722f6121aa1fa0de8ea460521bcde7c78543460 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 0000000000000000000000000000000000000000..4e682cfce38717176acc294b926296715b97a265 --- /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 0000000000000000000000000000000000000000..40b42f99a4c09a77ce6b4c064a8b6457ac55c155 --- /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