Ogre - Memory , Introduction

1. OgreConfig中的关于内存的定义
Ogre使用的默认内存分配策略是 OGRE_MEMORY_ALLOCATOR_NEDPOOLING

// define the memory allocator configuration to use
#defineOGRE_MEMORY_ALLOCATOR_STD1
#defineOGRE_MEMORY_ALLOCATOR_NED2
#defineOGRE_MEMORY_ALLOCATOR_USER3
#defineOGRE_MEMORY_ALLOCATOR_NEDPOOLING4

#ifndefOGRE_MEMORY_ALLOCATOR
#  define OGRE_MEMORY_ALLOCATOR OGRE_MEMORY_ALLOCATOR_NEDPOOLING
#endif

2.

    mRoot = new Ogre::Root(mPluginsCfg); 
    Ogre::WxMaterialBinManager* mat = OGRE_NEW Ogre::WxMaterialBinManager; 
    setupResources();

    ResourceAlloc中重载了new操作运算符
    在Debug模式下 OGRE_NEW被定义为
   #    define OGRE_NEWnew(__FILE__,__LINE__,__FUNCTION__)
   会调用AllocatedObject的函数void* operator new(size_t sz, const char* file, intline, const char*func),
   而Debug下的new仍然调用 void* operator new(size_t sz)


template<class Alloc>
class_OgreExport AllocatedObject
{
public:
    explicitAllocatedObject()
    { }

    ~AllocatedObject()
    { }

    /// operator new, with debug line info
  
void* operator new(size_t sz,const char*file,intline,const char* func)
    {
        return Alloc::allocateBytes(sz,file,line,func);
    }

    void* operator new(size_t sz)
    {
        returnAlloc::allocateBytes(sz);
    }
…………
}

   一个继承自ResourceAlloc的类
   class /*_OgreExport*/ WxMaterialBinManager : public ScriptLoader, public ResourceAlloc
   {    
        public: 

        WxMaterialBinManager();
        ~WxMaterialBinManager(); 
        virtual const StringVector& getScriptPatterns(void) const { return mScriptPatterns; } 

        virtual void parseScript(DataStreamPtr& stream, const String& groupName);
        virtual Real getLoadingOrder(void) const { return mLoadOrder; } 


        StringVector mScriptPatterns; 
        Real mLoadOrder; 
    };

下面是ResourceAlloc的定义:
typedef ResourceAllocatedObject        ResourceAlloc;
typedef AllocatedObject ResourceAllocatedObject;
typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_RESOURCE>ResourceAllocPolicy;

Ogre::MEMCATEGORY_RESOURCE是个内存的类型
namespace Ogre
{
    /** /addtogroup Core
    *  @{
    */
    /** /addtogroup Memory
    *  @{
    */

    /** A set of categories that indicate the purpose of a chunk of memory
    being allocated. 
    These categories will be provided at allocation time in order to allow
    the allocation policy to vary its behaviour if it wishes. This allows you
    to use a single policy but still have variant behaviour. The level of 
    control it gives you is at a higher level than assigning different 
    policies to different classes, but is the only control you have over
    general allocations that are primitive types.
    */
    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
    };
    /** @} */
    /** @} */

}


使用的内存分配策略

#if OGRE_MEMORY_ALLOCATOR==OGRE_MEMORY_ALLOCATOR_NEDPOOLING

#  include"OgreMemoryNedPooling.h"
namespace Ogre
{
    // configure default allocators based on the options above
    // notice how we're not using the memory categories here but still roughing them out
    // in your allocators you might choose to create different policies per category

    // configurable category, for general malloc
    // notice how we ignore the category here, you could specialise
  
template<MemoryCategory Cat>classCategorisedAllocPolicy:publicNedPoolingPolicy{};
    template<MemoryCategory Cat,size_t align= 0>classCategorisedAlignAllocPolicy:public NedPoolingAlignedPolicy<align>{};
}

#elif OGRE_MEMORY_ALLOCATOR==OGRE_MEMORY_ALLOCATOR_NED
……其它策略

3.CategorisedAllocPolicy 继承自 NedPoolingPolicy;

/**    An allocation policy for use with AllocatedObject and 
STLAllocator. This is the class that actually does the allocation
and deallocation of physical memory, and is what you will want to 
provide a custom version of if you wish to change how memory is allocated.
@par
This allocation policy uses nedmalloc 
    (http://nedprod.com/programs/portable/nedmalloc/index.html). 
*/
class _OgreExport NedPoolingPolicy
{
public:
    static inline void* allocateBytes(size_t count, 
        const char* file = 0, int line = 0, const char* func = 0)
    {
        return NedPoolingImpl::allocBytes(count, file, line, func);
    }
    static inline void deallocateBytes(void* ptr)
    {
        NedPoolingImpl::deallocBytes(ptr);
    }
    /// Get the maximum size of a single allocation
    static inline size_t getMaxAllocationSize()
    {
        return std::numeric_limits<size_t>::max();
    }

private:
    // No instantiation
    NedPoolingPolicy()
    { }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值