Ogre - Memory , Introduction

来源:互联网 发布:微pe 知乎 编辑:程序博客网 时间:2024/06/05 23:52

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:publicNedPoolingAlignedPolicy<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 allocationand 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.@parThis 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()    { }};

原创粉丝点击