MUP1+Øvelse+12+Buffer

toc =Fixed size buffer pattern=

MemPool.h
code format="cpp" class MemPool { public: MemPool(size_t, size_t); virtual ~MemPool; void* allocate; bool free(void*); bool anyFree; void* info; size_t getElementSize; private: class BufferDescriptor {		public: BufferDescriptor : ptr(NULL), isFree(false) {} void* ptr; bool isFree; };	void* rawMem; BufferDescriptor* bufDescPtr; size_t nElements; size_t size; size_t nElementsFree; }; code

MemPool.cpp
code format="cpp" MemPool::MemPool(size_t nElements, size_t size) {	this->nElements = nElements; this->size = size;

rawMem = malloc(nElements * size); bufDescPtr = (BufferDescriptor*) malloc(nElements			* sizeof(BufferDescriptor));

for (size_t i = 0; i < nElements; i++) {		bufDescPtr[i].ptr = (void*) ((size_t) rawMem + (i * size)); bufDescPtr[i].isFree = true; } }

MemPool::~MemPool {	::free(bufDescPtr); ::free(rawMem); }

void* MemPool::allocate {	for (size_t i = 0; i < nElements; i++) {		if (bufDescPtr[i].isFree) {			bufDescPtr[i].isFree = false; --nElementsFree; return bufDescPtr[i].ptr; }	}	return NULL; }

bool MemPool::free(void* p) { for (size_t i = 0; i < nElements; i++) {		if (bufDescPtr[i].ptr == p) 		{ bufDescPtr[i].isFree = true; ++nElementsFree; return true; }	}	return false; }

bool MemPool::anyFree {	return nElementsFree > 0; }

void* MemPool::info { cout << "[" << nElements << "*" << size << "," << nElementsFree << "]\t"; for(size_t i = 0; i < nElements; i++) cout << (bufDescPtr[i].isFree ? "1" : "0"); cout << endl; } code

HeapManager.h
code format="cpp" class HeapManager { public: static HeapManager* getInstance; virtual ~HeapManager; void* allocate(size_t); void free(void*); void info;

protected: HeapManager(size_t, size_t = 5, size_t = 8);

private: static HeapManager* instance; MemPool* memPool;

size_t nPools; size_t nElements; size_t smallestPool; }; code

HeapManager.cpp
code format="cpp" HeapManager* HeapManager::instance = NULL;

HeapManager* HeapManager::getInstance {	if (instance == NULL) { instance = (HeapManager*) malloc(sizeof(HeapManager)); ::new (instance) HeapManager; }	return instance; }

HeapManager::HeapManager(size_t nPools, size_t nElements,		size_t smallestPool) {	this->nPools = nPools; this->nElements = nElements; this->smallestPool = smallestPool;

memPool = (MemPool*) malloc(nPools * sizeof(MemPool)); for (size_t i = 0, size = smallestPool; i < nPools; i++, size *= 2) {		::new (memPool + i) MemPool(nElements, size); } }

HeapManager::~HeapManager {	for (size_t i = 0; i < nPools; i++) {		memPool[i].~MemPool; ::free(memPool[i]); }	::free(instance); }

void* HeapManager::allocate(size_t size) {	for (size_t i = 0; i < nPools; i++) if (size <= memPool[i].getElementSize) return memPool[i].allocate; return NULL; }

void HeapManager::free(void* ptr) {	for (size_t i = 0; i < nPools; i++) if (memPool[i].free(ptr)) break; }

void HeapManager::info {	std::cout << "HeapManager info" << endl << "" << std::endl; for (size_t i = 0; i < nPools; i++) memPools[i].info; std::cout << std::endl; } code

Exercise 4
code format="cpp" void* operator new(size_t size) { return HeapManager::getInstance->allocate(size); }

void operator delete(void* ptr) { HeapManager::getInstance->free(ptr); } code