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
.....
}There are 3 predefined memory allocator policies and 1 custom memory allocator in ogre.
#define OGRE_MEMORY_ALLOCATOR_STD 1#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.
#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>{};
}
#elifAs yo can see, the policy it self is a template, it takes a nontype argument MemoryCategory which is defined asenum 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 allowtypedef 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.
OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T))) TJust a placement new.
原文地址:http://blog.csdn.net/peanutandchestnut/article/details/42607683