Memory allocate in Oger
Memory allocate
Every memory in ogre will be allocated from AllocatedObject, it use Policy to switch from different allocators.
It looks like this:
template <class Alloc> class _OgreExport AllocatedObject
{
public:
void* operator new(size_t sz, const char* file, int line, const char* func)
{
return Alloc::allocateBytes(sz, file, line, func);
}
void* operator new(size_t sz)
{
return Alloc::allocateBytes(sz);
}
/// placement operator new
void* operator new(size_t sz, void* ptr)
{
(void) sz;
return ptr;
}
/// array operator new, with debug line info
void* operator new[] ( size_t sz, const char* file, int line, const char* func )
{
return Alloc::allocateBytes(sz, file, line, func);
}
void* operator new[] ( size_t sz )
{
return Alloc::allocateBytes(sz);
}
.....
..override delete
.....
}
Underlying memory allocation is done by policy.
There are 3 predefined memory allocator policies and 1 custom memory allocator in ogre.
#define OGRE_MEMORY_ALLOCATOR_STD 1#define OGRE_MEMORY_ALLOCATOR_NED 2
#define OGRE_MEMORY_ALLOCATOR_USER 3
#define OGRE_MEMORY_ALLOCATOR_NEDPOOLING 4
By default ogre use OGRE_MEMORY_ALLOCATOR_NEDPOOLING, STD is legacy,
If you are interested, you can read the ned allocation policy here.
For most user, you just need to know how to use it.
It use macro to choose policy, which looks like this
#elif OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_NED
# include "OgreMemoryNedAlloc.h"
namespace Ogre
{
template <MemoryCategory Cat> class CategorisedAllocPolicy : public NedAllocPolicy{};
template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public NedAlignedAllocPolicy<align>{};
}
#elif
As yo can see, the policy it self is a template, it takes a nontype argument MemoryCategory which is defined as
enum MemoryCategory
{
/// General purpose
MEMCATEGORY_GENERAL = 0,
/// Geometry held in main memory
MEMCATEGORY_GEOMETRY = 1,
/// Animation data like tracks, bone matrices
MEMCATEGORY_ANIMATION = 2,
/// Nodes, control data
MEMCATEGORY_SCENE_CONTROL = 3,
/// Scene object instances
MEMCATEGORY_SCENE_OBJECTS = 4,
/// Other resources
MEMCATEGORY_RESOURCE = 5,
/// Scripting
MEMCATEGORY_SCRIPTING = 6,
/// Rendersystem structures
MEMCATEGORY_RENDERSYS = 7,
// sentinel value, do not use
MEMCATEGORY_COUNT = 8
};
This is used to indicate the purse of a chunk of memory being used.These categories will be provided at allocation time in order to allow
the allocation policy to vary its behaviour if it wishes.
Then ogre define specific class allocator which looks like this:
typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_SCENE_OBJECTS> SceneObjAllocPolicy;
typedef AllocatedObject<SceneObjAllocPolicy> SceneObjAllocatedObject;
typedef SceneObjAllocatedObject MovableAlloc;
class MovableObject : <span style="font-family: Arial, Helvetica, sans-serif;">MovableAlloc</span>
Then you can use OGRE_NEW to allocate memory.
For class not inherited from Alloc, you can use OGRE_NEW_T, It's defined as
OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T))) T
Just a placement new.
For prmitive type, use OGRE_ALLOC_T or OGRE_MALLOC