2016-01-12 19:08:26 +01:00
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
class Memory
|
|
|
|
{
|
|
|
|
public:
|
2016-01-22 16:11:47 +01:00
|
|
|
class Allocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef void(*FreeCallback)(void*);
|
|
|
|
|
|
|
|
Allocator()
|
|
|
|
{
|
|
|
|
this->Pool.clear();
|
|
|
|
this->RefMemory.clear();
|
|
|
|
}
|
|
|
|
~Allocator()
|
|
|
|
{
|
|
|
|
for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); i++)
|
|
|
|
{
|
|
|
|
if (i->first && i->second)
|
|
|
|
{
|
|
|
|
i->second(i->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->RefMemory.clear();
|
|
|
|
|
|
|
|
for (auto data : this->Pool)
|
|
|
|
{
|
|
|
|
Memory::Free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->Pool.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reference(void* memory, FreeCallback callback)
|
|
|
|
{
|
|
|
|
this->RefMemory[memory] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Allocate(size_t length)
|
|
|
|
{
|
|
|
|
void* data = Memory::Allocate(length);
|
|
|
|
this->Pool.push_back(data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
template <typename T> T* AllocateArray(size_t count = 1)
|
|
|
|
{
|
|
|
|
return (T*)this->Allocate(count * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
char* DuplicateString(std::string string)
|
|
|
|
{
|
|
|
|
char* data = Memory::DuplicateString(string);
|
|
|
|
this->Pool.push_back(data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<void*> Pool;
|
|
|
|
std::map<void*, FreeCallback> RefMemory;
|
|
|
|
};
|
|
|
|
|
2016-01-12 19:08:26 +01:00
|
|
|
static void* Allocate(size_t length);
|
2016-01-22 16:11:47 +01:00
|
|
|
template <typename T> static T* AllocateArray(size_t count = 1)
|
2016-01-12 19:08:26 +01:00
|
|
|
{
|
|
|
|
return (T*)Allocate(count * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* DuplicateString(std::string string);
|
|
|
|
|
|
|
|
static void Free(void* data);
|
|
|
|
static void Free(const void* data);
|
|
|
|
};
|
|
|
|
}
|