Hello all
I want to have some basic control over memory allocation in my game engine. I use the classes IAllocatable and MemoryManager.
All classes in the engine derive from IAllocatable. Whenever objects of these classes are instantiated, they automatically call the MemoryManager which in turns calls the standard C/C++ memory functions. Does this make sense? Am I doing something wrong? Any pitfalls I should watch out for?
#ifndef IAllocatable_H
#define IAllocatable_H
class IAllocatable
{
public:
void* operator new(std::size_t sz);
void operator delete(void* ptr);
};
#endif
#include "IAllocatable.h"
void* IAllocatable::operator new( std::size_t sz )
{
return MemoryManager::malloc(sz);
}
void IAllocatable::operator delete( void* ptr )
{
MemoryManager::free(ptr);
}
#ifndef MemoryManager_H
#define MemoryManager_H
class MemoryManager
{
public:
void* malloc( std::size_t sz );
void free( void* ptr );
};
#endif
#include "MemoryManager.h"
void* MemoryManager::malloc( std::size_t sz )
{
return std::malloc(sz);
}
void MemoryManager::free( void* ptr )
{
std::free(ptr);
}