OGRE: OgreMemoryAllocatorConfig.h Source File - OGRE Documentation

# OgreMemoryAllocatorConfig.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2008 Torus Knot Software Ltd
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023
    00024 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd
    00027 ---------------------------------------------------------------------------
    00028 */
    00029
    00030 #ifndef __MemoryAllocatorConfig_H__
    00031 #define __MemoryAllocatorConfig_H__
    00032
    00033 #include "OgrePrerequisites.h"
    00034
    00109
    00114
    00119
    00122
    00125
    00133 namespace Ogre
    00134 {
    00144     enum MemoryCategory
    00145     {
    00147         MEMCATEGORY_GENERAL = 0,
    00149         MEMCATEGORY_GEOMETRY = 1,
    00151         MEMCATEGORY_ANIMATION = 2,
    00153         MEMCATEGORY_SCENE_CONTROL = 3,
    00155         MEMCATEGORY_SCENE_OBJECTS = 4,
    00157         MEMCATEGORY_RESOURCE = 5,
    00159         MEMCATEGORY_SCRIPTING = 6,
    00161         MEMCATEGORY_RENDERSYS = 7,
    00162
    00163
    00164         // sentinel value, do not use
    00165         MEMCATEGORY_COUNT = 8
    00166     };
    00167 }
    00168
    00169 #include "OgreMemoryAllocatedObject.h"
    00170 #include "OgreMemorySTLAllocator.h"
    00171
    00172 #if OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_NED
    00173
    00174 #  include "OgreMemoryNedAlloc.h"
    00175 namespace Ogre
    00176 {
    00177     // configure default allocators based on the options above
    00178     // notice how we're not using the memory categories here but still roughing them out
    00179     // in your allocators you might choose to create different policies per category
    00180
    00181     // configurable category, for general malloc
    00182     // notice how we ignore the category here, you could specialise
    00183     template <MemoryCategory Cat> class CategorisedAllocPolicy : public NedAllocPolicy{};
    00184     template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public NedAlignedAllocPolicy<align>{};
    00185 }
    00186
    00187 #elif OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_STD
    00188
    00189 #  include "OgreMemoryStdAlloc.h"
    00190 namespace Ogre
    00191 {
    00192     // configure default allocators based on the options above
    00193     // notice how we're not using the memory categories here but still roughing them out
    00194     // in your allocators you might choose to create different policies per category
    00195
    00196     // configurable category, for general malloc
    00197     // notice how we ignore the category here
    00198     template <MemoryCategory Cat> class CategorisedAllocPolicy : public StdAllocPolicy{};
    00199     template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public StdAlignedAllocPolicy<align>{};
    00200
    00201     // if you wanted to specialise the allocation per category, here's how it might work:
    00202     // template <> class CategorisedAllocPolicy<MEMCATEGORY_SCENE_OBJECTS> : public YourSceneObjectAllocPolicy{};
    00203     // template <size_t align> class CategorisedAlignAllocPolicy<MEMCATEGORY_SCENE_OBJECTS, align> : public YourSceneObjectAllocPolicy<align>{};
    00204
    00205
    00206 }
    00207
    00208 #else
    00209
    00210 // your allocators here?
    00211
    00212 #endif
    00213
    00214 namespace Ogre
    00215 {
    00216     // Useful shortcuts
    00217     typedef CategorisedAllocPolicy<MEMCATEGORY_GENERAL> GeneralAllocPolicy;
    00218     typedef CategorisedAllocPolicy<MEMCATEGORY_GEOMETRY> GeometryAllocPolicy;
    00219     typedef CategorisedAllocPolicy<MEMCATEGORY_ANIMATION> AnimationAllocPolicy;
    00220     typedef CategorisedAllocPolicy<MEMCATEGORY_SCENE_CONTROL> SceneCtlAllocPolicy;
    00221     typedef CategorisedAllocPolicy<MEMCATEGORY_SCENE_OBJECTS> SceneObjAllocPolicy;
    00222     typedef CategorisedAllocPolicy<MEMCATEGORY_RESOURCE> ResourceAllocPolicy;
    00223     typedef CategorisedAllocPolicy<MEMCATEGORY_SCRIPTING> ScriptingAllocPolicy;
    00224     typedef CategorisedAllocPolicy<MEMCATEGORY_RENDERSYS> RenderSysAllocPolicy;
    00225
    00226     // Now define all the base classes for each allocation
    00227     typedef AllocatedObject<GeneralAllocPolicy> GeneralAllocatedObject;
    00228     typedef AllocatedObject<GeometryAllocPolicy> GeometryAllocatedObject;
    00229     typedef AllocatedObject<AnimationAllocPolicy> AnimationAllocatedObject;
    00230     typedef AllocatedObject<SceneCtlAllocPolicy> SceneCtlAllocatedObject;
    00231     typedef AllocatedObject<SceneObjAllocPolicy> SceneObjAllocatedObject;
    00232     typedef AllocatedObject<ResourceAllocPolicy> ResourceAllocatedObject;
    00233     typedef AllocatedObject<ScriptingAllocPolicy> ScriptingAllocatedObject;
    00234     typedef AllocatedObject<RenderSysAllocPolicy> RenderSysAllocatedObject;
    00235
    00236
    00237     // Per-class allocators defined here
    00238     // NOTE: small, non-virtual classes should not subclass an allocator
    00239     // the virtual function table could double their size and make them less efficient
    00240     // use primitive or STL allocators / deallocators for those
    00241     typedef ScriptingAllocatedObject    AbstractNodeAlloc;
    00242     typedef AnimationAllocatedObject    AnimableAlloc;
    00243     typedef AnimationAllocatedObject    AnimationAlloc;
    00244     typedef GeneralAllocatedObject      ArchiveAlloc;
    00245     typedef GeometryAllocatedObject     BatchedGeometryAlloc;
    00246     typedef RenderSysAllocatedObject    BufferAlloc;
    00247     typedef GeneralAllocatedObject      CodecAlloc;
    00248     typedef ResourceAllocatedObject     CompositorInstAlloc;
    00249     typedef GeneralAllocatedObject      ConfigAlloc;
    00250     typedef GeneralAllocatedObject      ControllerAlloc;
    00251     typedef GeometryAllocatedObject     DebugGeomAlloc;
    00252     typedef GeneralAllocatedObject      DynLibAlloc;
    00253     typedef GeometryAllocatedObject     EdgeDataAlloc;
    00254     typedef GeneralAllocatedObject      FactoryAlloc;
    00255     typedef SceneObjAllocatedObject     FXAlloc;
    00256     typedef GeneralAllocatedObject      ImageAlloc;
    00257     typedef GeometryAllocatedObject     IndexDataAlloc;
    00258     typedef GeneralAllocatedObject      LogAlloc;
    00259     typedef SceneObjAllocatedObject     MovableAlloc;
    00260     typedef SceneCtlAllocatedObject     NodeAlloc;
    00261     typedef SceneObjAllocatedObject     OverlayAlloc;
    00262     typedef RenderSysAllocatedObject    GpuParamsAlloc;
    00263     typedef ResourceAllocatedObject     PassAlloc;
    00264     typedef GeometryAllocatedObject     PatchAlloc;
    00265     typedef GeneralAllocatedObject      PluginAlloc;
    00266     typedef GeneralAllocatedObject      ProfilerAlloc;
    00267     typedef GeometryAllocatedObject     ProgMeshAlloc;
    00268     typedef SceneCtlAllocatedObject     RenderQueueAlloc;
    00269     typedef RenderSysAllocatedObject    RenderSysAlloc;
    00270     typedef GeneralAllocatedObject      RootAlloc;
    00271     typedef ResourceAllocatedObject     ResourceAlloc;
    00272     typedef GeneralAllocatedObject      SerializerAlloc;
    00273     typedef SceneCtlAllocatedObject     SceneMgtAlloc;
    00274     typedef ScriptingAllocatedObject    ScriptCompilerAlloc;
    00275     typedef ScriptingAllocatedObject    ScriptTranslatorAlloc;
    00276     typedef SceneCtlAllocatedObject     ShadowDataAlloc;
    00277     typedef GeneralAllocatedObject      StreamAlloc;
    00278     typedef SceneObjAllocatedObject     SubEntityAlloc;
    00279     typedef ResourceAllocatedObject     SubMeshAlloc;
    00280     typedef ResourceAllocatedObject     TechniqueAlloc;
    00281     typedef GeneralAllocatedObject      TimerAlloc;
    00282     typedef ResourceAllocatedObject     TextureUnitStateAlloc;
    00283     typedef GeneralAllocatedObject      UtilityAlloc;
    00284     typedef GeometryAllocatedObject     VertexDataAlloc;
    00285     typedef RenderSysAllocatedObject    ViewportAlloc;
    00286
    00287     // Containers (by-value only)
    00288     // Will  be of the form:
    00289     // typedef STLAllocator<T, DefaultAllocPolicy, Category> TAlloc;
    00290     // for use in std::vector<T, TAlloc>
    00291
    00292
    00293
    00294 }
    00295
    00296 // Util functions
    00297 namespace Ogre
    00298 {
    00303     template<typename T>
    00304     T* constructN(T* basePtr, size_t count)
    00305     {
    00306         for (size_t i = 0; i < count; ++i)
    00307         {
    00308             new ((void*)(basePtr+i)) T();
    00309         }
    00310         return basePtr;
    00311     }
    00312 }
    00313 // define macros
    00314
    00315 #if OGRE_DEBUG_MODE
    00316
    00318 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00320 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00322 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr)
    00323
    00325 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00327 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count)
    00329 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00331 #   define OGRE_DELETE_ARRAY_T(ptr, T, count, category) if(ptr){for (size_t b = 0; b < count; ++b) { (ptr)[b].~T();} ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00332
    00333 // aligned allocation
    00335 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00337 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00339 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00341 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00343 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr)
    00345 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr)
    00346
    00348 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00350 #   define OGRE_NEW_ARRAY_T_SIMD(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count)
    00352 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr);}
    00354 #   define OGRE_DELETE_ARRAY_T_SIMD(ptr, T, count, category) if(ptr){for (size_t b = 0; b < count; ++b) { (ptr)[b].~T();} ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr);}
    00356 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00358 #   define OGRE_NEW_ARRAY_T_ALIGN(T, count, category, align) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count)
    00360 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr);}
    00362 #   define OGRE_DELETE_ARRAY_T_ALIGN(ptr, T, count, category, align) if(ptr){for (size_t _b = 0; _b < count; ++_b) { (ptr)[_b].~T();} ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr);}
    00363
    00364 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
    00365 // Also hooks up the file/line/function params
    00366 // Can only be used with classes that derive from AllocatedObject since customised new/delete needed
    00367 #   define OGRE_NEW new (__FILE__, __LINE__, __FUNCTION__)
    00368 #   define OGRE_DELETE delete
    00369
    00370
    00371 #else // !OGRE_DEBUG_MODE
    00372
    00374 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes)
    00376 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
    00378 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr)
    00379
    00381 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T))) T
    00383 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count)
    00385 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00387 #   define OGRE_DELETE_ARRAY_T(ptr, T, count, category) if(ptr){for (size_t b = 0; b < count; ++b) { (ptr)[b].~T();} ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00388
    00389 // aligned allocation
    00391 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes)
    00393 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes)
    00395 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
    00397 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count)))
    00399 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes((void*)ptr)
    00401 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes((void*)ptr)
    00402
    00404 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T))) T
    00406 #   define OGRE_NEW_ARRAY_T_SIMD(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count)
    00408 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00410 #   define OGRE_DELETE_ARRAY_T_SIMD(ptr, T, count, category) if(ptr){for (size_t b = 0; b < count; ++b) { (ptr)[b].~T();} ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00412 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T))) T
    00414 #   define OGRE_NEW_ARRAY_T_ALIGN(T, count, category, align) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count))), count)
    00416 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes((void*)ptr);}
    00418 #   define OGRE_DELETE_ARRAY_T_ALIGN(ptr, T, count, category, align) if(ptr){for (size_t _b = 0; _b < count; ++_b) { (ptr)[_b].~T();} ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes((void*)ptr);}
    00419
    00420 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
    00421 #   define OGRE_NEW new
    00422 #   define OGRE_DELETE delete
    00423
    00424 #endif // OGRE_DEBUG_MODE
    00425
    00426
    00427 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 26 11:32:18 2009