Memory allocate in Ogre

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 primitive type 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.

时间: 2024-10-06 16:34:53

Memory allocate in Ogre的相关文章

Reducing and Profiling GPU Memory Usage in Keras with TensorFlow Backend

keras 自适应分配显存 & 清理不用的变量释放 GPU 显存 Intro Are you running out of GPU memory when using keras or tensorflow deep learning models, but only some of the time? Are you curious about exactly how much GPU memory your tensorflow model uses during training? Are

单例模式与多线程

概述 关于一般单例模式的创建和分析在我的另一篇博客<Java设计模式--单件模式>中有详细说明.只是在上篇博客中的单例是针对于单线程的操作,而对于多线程却并不适用,本文就从单例模式与多线程安全的角度出发,讲解单例模式在多线程中应该如何被使用. 版权说明 著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 本文作者:Coding-Naga 发表日期: 2016年4月6日 本文链接:http://blog.csdn.net/lemon_tree12138/article/det

操作系统思考 第三章 虚拟内存

第三章 虚拟内存 作者:Allen B. Downey 原文:Chapter 3 Virtual memory 译者:飞龙 协议:CC BY-NC-SA 4.0 3.1 简明信息理论 比特是二进制的数字,也是信息的单位.一个比特有两种可能的情况,写为0或者1.如果是两个比特,那就有四种可能的组合,00.01.10和11.通常,如果你有b个比特,你就可以表示2 ** b个值之一.一个字节是8个比特,所以它可以储存256个值之一. 从其它方面来讲,假设你想要储存字母表中的字母.字母共有26个,所以你

彻头彻尾理解单例模式与多线程

摘要: 本文首先概述了单例模式产生动机,揭示了单例模式的本质和应用场景.紧接着,我们给出了单例模式在单线程环境下的两种经典实现:饿汉式 和 懒汉式,但是饿汉式是线程安全的,而懒汉式是非线程安全的.在多线程环境下,我们特别介绍了五种方式来在多线程环境下创建线程安全的单例,使用 synchronized方法.synchronized块.静态内部类.双重检查模式 和 ThreadLocal 实现懒汉式单例,并总结出实现效率高且线程安全的单例所需要注意的事项. 版权声明: 本文原创作者:书呆子Rico

各种软核处理器二进制文件FPGA初始化文件生成程序

不管是MIPS, Nios II, MicroBlaze, MSP430, 8051, OpenRISC, OpenSPARC, LEON2/LEON3等等软核处理器,在FPGA上实现的时候我们通常需要一部分片上RAM存储bootloader,可以使用gcc的objcopy把bootloader的text, exception vector, rodata, data or bss等你需要的内容copy出来,可以通过-O binary生成二进制文件,可以通过-j .section提取你要的sec

重载operator new实现检测内存泄漏是否可行

行与不行,就凭我这水平,说出来未免显示太过自大.不还,我还想根据自己的代码来讨论这个问题. 重载operator new来检测内存只的办法,那就是在new的时候记录指针地址及文件名.行号,在delete的时候取消记录.到最后程序结束,还有哪些指针未释放,则为泄漏. 第一步,你得重载operator new,或者也可以重写.在http://www.cplusplus.com/reference/new/operator%20new/中指明new有三种形式,因为我们还分配数组,故还有new[]这个函

mina 之 java.lang.OutOfMemoryError

前段时间在测试过程中发现了mina框架的问题:当mina一次传输的文件超过一定值(如55m)或者连续传输文件的次数过于频繁,就会内存溢出: org.apache.mina.filter.codec.ProtocolEncoderException:java.lang.OutOfMemoryError: Java heap space atorg.apache.mina.filter.codec.ProtocolCodecFilter.filterWrite(ProtocolCodecFilter

双重检查锁定与延迟初始化

在Java程序中,有时候可能需要推迟一些高开销的对象初始化操作,并且只有在使用这些对象时才进行初始化.此时程序员可能会采用延迟初始化.但要正确实现线程安全的延迟初始化需要一些技巧,否则很容易出现问题.比如,下面是非线程安全的延迟初始化对象的示例代码: public class UnsafeLazyInitialization { private static Instance instance; public static Instance getInstance() { if (instanc

从一个简单的Java单例示例谈谈并发

一个简单的单例示例 单例模式可能是大家经常接触和使用的一个设计模式,你可能会这么写 public class UnsafeLazyInitiallization { private static UnsafeLazyInitiallization instance; private UnsafeLazyInitiallization() { } public static UnsafeLazyInitiallization getInstance(){ if(instance==null){ /