Aqeel Tapia
Aqeel Tapia
  • Home
  • My Games
    • Experimental Games
    • Miscellaneous
  • Resume
  • Code Samples
  • Artwork
  • Contact
  • Home
  • My Games
    • Experimental Games
    • Miscellaneous
  • Resume
  • Code Samples
  • Artwork
  • Contact

Code Samples

Memory Manager

MemoryManager.h


#ifndef MEMORYMANAGER_H
#define MEMORYMANAGER_H

#include "BlockDescriptor.h"
#include stdint.h

class MemoryManager {

public:
	static MemoryManager * CreateMemoryManager();
	static void DestroyMemoryManager();

	static void * allocBlock(size_t i_size);
	static void freeBlock(void * i_ptr);

private:

	struct  Block
	{
		void* startAddress;
		size_t blockSize;
		Block * next = NULL;
		Block * prev = NULL;

	};

	static MemoryManager * m_pMyMemoryManager;
	void * m_pMyMemory;
	const size_t m_sizeMemory = 1024 * 1024;
	Block * allocStartPointer;
	Block * allocEndPointer;
	Block * freeStartPointer;
	Block * freeEndPointer;

	Block * findFreeBlock(size_t i_size);
	Block * findUsedBlock(void * i_ptr);

	void garbageCollector(Block * i_block);

	MemoryManager();
	~MemoryManager();

};

#endif // !MEMORYMANAGER_H




MemoryManager.cpp



#include "MemoryManager.h"
#include 
#include "DebuggingCode\Assert.h"
#include 

MemoryManager * MemoryManager::m_pMyMemoryManager = NULL;

MemoryManager::MemoryManager() :
allocEndPointer(NULL),
allocStartPointer(NULL),
freeStartPointer(NULL),
freeEndPointer(NULL)
{

	m_pMyMemory = _aligned_malloc(m_sizeMemory, 4);

	Block * startBlock = new Block();
	
	startBlock->next = NULL;
	startBlock->prev = NULL;
	startBlock->startAddress = m_pMyMemory;
	startBlock->blockSize = m_sizeMemory;

	freeStartPointer = freeEndPointer = startBlock;
}

void* MemoryManager::allocBlock(size_t i_size) {

	//find free block
	Block * freeBlock = m_pMyMemoryManager->findFreeBlock(i_size);

	if (freeBlock == NULL)
		return NULL;

	Block * newBlock = new Block();

	//add block to list
	newBlock->blockSize = i_size;
	newBlock->next = NULL;
	newBlock->prev = m_pMyMemoryManager->allocEndPointer;
	newBlock->startAddress = static_cast  (freeBlock->startAddress) + sizeof(Block);

	if (m_pMyMemoryManager->allocEndPointer != NULL)
		m_pMyMemoryManager->allocEndPointer->next = newBlock;

	m_pMyMemoryManager->allocEndPointer = newBlock;

	//reduce current freeBlock size by i_size
	freeBlock->blockSize = freeBlock->blockSize - i_size;

	//modify block for remainder of memory
	freeBlock->startAddress = static_cast (freeBlock->startAddress) + i_size;

	if (m_pMyMemoryManager->allocStartPointer == NULL)
		m_pMyMemoryManager->allocStartPointer = newBlock = m_pMyMemoryManager->allocEndPointer;

	return m_pMyMemoryManager->allocEndPointer->startAddress;
}


void MemoryManager::freeBlock(void * i_ptr) {

	

	//find block with pointer
	Block * usedBlock = m_pMyMemoryManager->findUsedBlock(i_ptr);

	if (usedBlock == NULL)
		return;
	//check if can be combined (garbage collection)
	m_pMyMemoryManager-> garbageCollector(usedBlock);
	//travel through free blocks and add new block
	//set prev and next to delete block)

	if (usedBlock->next != NULL)
		usedBlock->next->prev = usedBlock->prev;
	if (usedBlock->prev != NULL)
		usedBlock->prev->next = usedBlock->next;

	if (m_pMyMemoryManager->allocStartPointer == usedBlock)
		m_pMyMemoryManager->allocStartPointer = usedBlock->next;

	usedBlock->next = NULL;
	usedBlock->prev = NULL;

	if (m_pMyMemoryManager->freeEndPointer != NULL)
		m_pMyMemoryManager->freeEndPointer->next = usedBlock;

	m_pMyMemoryManager->freeEndPointer = usedBlock;
}

void MemoryManager::garbageCollector(Block * i_block) {

	Block * block = i_block;
	while (i_block->prev != NULL){
		if (block->startAddress == (static_cast  (i_block->prev->startAddress) + i_block->prev->blockSize))
			i_block->prev->blockSize += block->blockSize;
		else
			i_block = i_block->prev;
	}

	while (i_block->next != NULL){
		if (block->startAddress == (static_cast  (i_block->next->startAddress) - i_block->next->blockSize))
			i_block->next->blockSize += block->blockSize;
		else
			i_block = i_block->next;
	}
}

MemoryManager::Block * MemoryManager::findFreeBlock(size_t i_size) {

	Block * block = freeStartPointer;
	if (block->next == NULL)
		return block;

	while (block->next != NULL)
	{
		if (block->blockSize >= i_size)
			return block;
		else
			block = block->next;
	}
	return NULL;
	
}

MemoryManager::Block * MemoryManager::findUsedBlock(void * i_ptr) {

	Block * block = allocStartPointer;

	while (block != NULL)
	{
		if (block->startAddress == i_ptr)
			return block;
		else
			block = block->next;
	}
	return NULL;
}

MemoryManager* MemoryManager::CreateMemoryManager(){

	if (m_pMyMemoryManager == NULL) {
		m_pMyMemoryManager = new MemoryManager();
	}
	return m_pMyMemoryManager;
}

void MemoryManager::DestroyMemoryManager() {
	delete m_pMyMemoryManager;
	m_pMyMemoryManager = NULL;
}


MemoryManager::~MemoryManager(){

	while (allocStartPointer != NULL)
	{
		Block *p = allocStartPointer;
		allocStartPointer = allocStartPointer->next;
		delete p;
	}
	while (freeStartPointer != NULL)
	{
		Block *p = freeStartPointer;
		freeStartPointer = freeStartPointer->next;
		delete p;
	}
	_aligned_free(m_pMyMemory);
}



My Vector 3 class

Vector3.h

//A custom Vector 3 class with overloaded +,=, += operators.

#ifndef _MATH_VECTOR3_H
#define _MATH_VECTOR3_H

class Vector3 {

	float m_X, m_Y, m_Z;

public:
	Vector3() :m_X(0), m_Y(0), m_Z(0){}
	Vector3(float i_X, float i_Y, float i_Z) : m_X(i_X), m_Y(i_Y), m_Z(i_Z){}


	//setters
	void X(float i_X) { m_X = i_X; }
	void Y(float i_Y) { m_Y = i_Y; }
	void Z(float i_Z) { m_Z = i_Z; }

	//getters
	float getX() const { return m_X; }
	float getY() const { return m_Y; }
	float getZ() const { return m_Z; }

	void operator= (const Vector3 &i_input)
	{
		m_X = i_input.getX();
		m_Y = i_input.getY();
		m_Z = i_input.getZ();
	}

	void operator+= (const Vector3 & i_rhs)
	{
		*this = *this + i_rhs;
	}
	inline Vector3 operator+ (const Vector3 & i_rhs)
	{
		Vector3 result;

		result.X(this->getX() + i_rhs.getX());
		result.Y(getY() + i_rhs.getY());
		result.Z(getZ() + i_rhs.getZ());

		return result;
	}


};

#endif // _MATH_VECTOR3_H



Templated Ring buffer class

RingBuffer.h


#ifndef _TEMPLATERINGBUFFER_H
#define _TEMPLATERINGBUFFER_H

template
class RingBuffer {
	
	size_t startPtr = 0;
	size_t endPtr = 0;
	int size = 21;
	T ringBuffer[21];



public:
	RingBuffer() {

	}

	//return recently used element
	T &operator[](size_t i_index)
	{
		size_t i = endPtr - i_index - 1;
		if (i < 0)
			i = size + i;

		return ringBuffer[i];
	}

	void addElement(T * i_data){

			ringBuffer[endPtr] = * i_data;
			endPtr = (endPtr + 1) % size;
			if (endPtr == startPtr)
				startPtr = (startPtr + 1) % size;
	}


	bool isFull(){
		if (startPtr == endPtr+1%size)
			return true;
		return false;
	}

	bool isEmpty() {
		if (startPtr == endPtr)
			return true;
		return false;
	}
	
	//no of total elements
	size_t sizeOfBuffer() {
		return size-1;
	}

	//no if elements in buffer
	int count() {
		if (isEmpty())
			return 0;
		else if (isFull())
			return 20;
		else {
			if (endPtr > startPtr)
				return endPtr - startPtr;
			else
				return 20 - (endPtr + startPtr);
		}
	}

};


#endif // _TEMPLATERINGBUFFER_H


Powered by Create your own unique website with customizable templates.