Memory allocate in Ogre

4 篇文章 0 订阅

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值