Ogreでのメモリ管理

8404 ワード

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_と定義されているNEW new(__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)templateclass_が呼び出されます.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 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 class CategorisedAllocPolicy: public NedPoolingPolicy{};     template class CategorisedAlignAllocPolicy: public NedPoolingAlignedPolicy{}; } #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()
    { }
};