51 lines
962 B
C++
51 lines
962 B
C++
/*
|
|
* MemoryManager.cpp
|
|
*
|
|
* Created on: Mar 16, 2014
|
|
* Author: robert
|
|
*/
|
|
|
|
#include "MemoryManager.h"
|
|
#include "../common/DebugManager.h"
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
MemoryManager::MemoryManager(): numberOfAllocations(0), numberOfFrees(0)
|
|
{
|
|
|
|
}
|
|
|
|
MemoryManager::~MemoryManager()
|
|
{
|
|
for(gp* payload: freePayloads)
|
|
{
|
|
delete payload;
|
|
numberOfFrees++;
|
|
}
|
|
DebugManager::getInstance().printDebugMessage("MemomryManager","Number of allocated payloads: " + to_string(numberOfAllocations));
|
|
DebugManager::getInstance().printDebugMessage("MemomryManager","Number of freed payloads: " + to_string(numberOfFrees));
|
|
}
|
|
|
|
gp* MemoryManager::allocate()
|
|
{
|
|
if(freePayloads.empty())
|
|
{
|
|
numberOfAllocations++;
|
|
return new gp(this);
|
|
}
|
|
else
|
|
{
|
|
gp* result = freePayloads.back();
|
|
freePayloads.pop_back();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
void MemoryManager::free(gp* payload)
|
|
{
|
|
payload->reset(); //clears all extensions
|
|
freePayloads.push_back(payload);
|
|
}
|
|
|