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      ProfilerAlloc;
00266     typedef GeometryAllocatedObject     ProgMeshAlloc;
00267     typedef SceneCtlAllocatedObject     RenderQueueAlloc;
00268     typedef RenderSysAllocatedObject    RenderSysAlloc;
00269     typedef GeneralAllocatedObject      RootAlloc;
00270     typedef ResourceAllocatedObject     ResourceAlloc;
00271     typedef GeneralAllocatedObject      SerializerAlloc;
00272     typedef SceneCtlAllocatedObject     SceneMgtAlloc;
00273     typedef ScriptingAllocatedObject    ScriptCompilerAlloc;
00274     typedef ScriptingAllocatedObject    ScriptTranslatorAlloc;
00275     typedef SceneCtlAllocatedObject     ShadowDataAlloc;
00276     typedef GeneralAllocatedObject      StreamAlloc;
00277     typedef SceneObjAllocatedObject     SubEntityAlloc;
00278     typedef ResourceAllocatedObject     SubMeshAlloc;
00279     typedef ResourceAllocatedObject     TechniqueAlloc;
00280     typedef GeneralAllocatedObject      TimerAlloc;
00281     typedef ResourceAllocatedObject     TextureUnitStateAlloc;
00282     typedef GeneralAllocatedObject      UtilityAlloc;
00283     typedef GeometryAllocatedObject     VertexDataAlloc;
00284     typedef RenderSysAllocatedObject    ViewportAlloc;
00285 
00286     // Containers (by-value only)
00287     // Will  be of the form:
00288     // typedef STLAllocator<T, DefaultAllocPolicy, Category> TAlloc;
00289     // for use in std::vector<T, TAlloc> 
00290     
00291 
00292 
00293 }
00294 
00295 // Util functions
00296 namespace Ogre
00297 {
00302     template<typename T>
00303     T* constructN(T* basePtr, size_t count)
00304     {
00305         for (size_t i = 0; i < count; ++i)
00306         {
00307             new ((void*)(basePtr+i)) T();
00308         }
00309         return basePtr;
00310     }
00311 }
00312 // define macros 
00313 
00314 #if OGRE_DEBUG_MODE
00315 
00317 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
00319 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
00321 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes(ptr)
00322 
00324 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
00326 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count) 
00328 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes(ptr);}
00330 #   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(ptr);}
00331 
00332 // aligned allocation
00334 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
00336 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
00338 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
00340 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
00342 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr)
00344 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr)
00345 
00347 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
00349 #   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) 
00351 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr);}
00353 #   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);}
00355 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
00357 #   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) 
00359 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr);}
00361 #   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);}
00362 
00363 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
00364 // Also hooks up the file/line/function params
00365 // Can only be used with classes that derive from AllocatedObject since customised new/delete needed
00366 #   define OGRE_NEW new (__FILE__, __LINE__, __FUNCTION__)
00367 #   define OGRE_DELETE delete
00368 
00369 
00370 #else // !OGRE_DEBUG_MODE
00371 
00373 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes)
00375 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
00377 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes(ptr)
00378 
00380 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T))) T
00382 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count) 
00384 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes(ptr);}
00386 #   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(ptr);}
00387 
00388 // aligned allocation
00390 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes)
00392 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes)
00394 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
00396 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count)))
00398 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr)
00400 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr)
00401 
00403 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T))) T
00405 #   define OGRE_NEW_ARRAY_T_SIMD(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count) 
00407 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr);}
00409 #   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);}
00411 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T))) T
00413 #   define OGRE_NEW_ARRAY_T_ALIGN(T, count, category, align) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count))), count) 
00415 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr);}
00417 #   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);}
00418 
00419 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
00420 #   define OGRE_NEW new 
00421 #   define OGRE_DELETE delete
00422 
00423 #endif // OGRE_DEBUG_MODE
00424 
00425 
00426 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Aug 28 20:53:48 2008