# 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-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028
    00029 #ifndef __MemoryAllocatorConfig_H__
    00030 #define __MemoryAllocatorConfig_H__
    00031
    00032 #include "OgreMemoryAllocatedObject.h"
    00033
    00114
    00119
    00124
    00127
    00130
    00140 namespace Ogre
    00141 {
    00158     enum MemoryCategory
    00159     {
    00161         MEMCATEGORY_GENERAL = 0,
    00163         MEMCATEGORY_GEOMETRY = 1,
    00165         MEMCATEGORY_ANIMATION = 2,
    00167         MEMCATEGORY_SCENE_CONTROL = 3,
    00169         MEMCATEGORY_SCENE_OBJECTS = 4,
    00171         MEMCATEGORY_RESOURCE = 5,
    00173         MEMCATEGORY_SCRIPTING = 6,
    00175         MEMCATEGORY_RENDERSYS = 7,
    00176
    00177
    00178         // sentinel value, do not use
    00179         MEMCATEGORY_COUNT = 8
    00180     };
    00184 }
    00185
    00186 #include "OgreMemoryAllocatedObject.h"
    00187 #include "OgreMemorySTLAllocator.h"
    00188
    00189 #if OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_NEDPOOLING
    00190
    00191 #  include "OgreMemoryNedPooling.h"
    00192 namespace Ogre
    00193 {
    00194     // configure default allocators based on the options above
    00195     // notice how we're not using the memory categories here but still roughing them out
    00196     // in your allocators you might choose to create different policies per category
    00197
    00198     // configurable category, for general malloc
    00199     // notice how we ignore the category here, you could specialise
    00200     template <MemoryCategory Cat> class CategorisedAllocPolicy : public NedPoolingPolicy{};
    00201     template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public NedPoolingAlignedPolicy<align>{};
    00202 }
    00203
    00204 #elif OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_NED
    00205
    00206 #  include "OgreMemoryNedAlloc.h"
    00207 namespace Ogre
    00208 {
    00209     // configure default allocators based on the options above
    00210     // notice how we're not using the memory categories here but still roughing them out
    00211     // in your allocators you might choose to create different policies per category
    00212
    00213     // configurable category, for general malloc
    00214     // notice how we ignore the category here, you could specialise
    00215     template <MemoryCategory Cat> class CategorisedAllocPolicy : public NedAllocPolicy{};
    00216     template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public NedAlignedAllocPolicy<align>{};
    00217 }
    00218
    00219 #elif OGRE_MEMORY_ALLOCATOR == OGRE_MEMORY_ALLOCATOR_STD
    00220
    00221 #  include "OgreMemoryStdAlloc.h"
    00222 namespace Ogre
    00223 {
    00224     // configure default allocators based on the options above
    00225     // notice how we're not using the memory categories here but still roughing them out
    00226     // in your allocators you might choose to create different policies per category
    00227
    00228     // configurable category, for general malloc
    00229     // notice how we ignore the category here
    00230     template <MemoryCategory Cat> class CategorisedAllocPolicy : public StdAllocPolicy{};
    00231     template <MemoryCategory Cat, size_t align = 0> class CategorisedAlignAllocPolicy : public StdAlignedAllocPolicy<align>{};
    00232
    00233     // if you wanted to specialise the allocation per category, here's how it might work:
    00234     // template <> class CategorisedAllocPolicy<MEMCATEGORY_SCENE_OBJECTS> : public YourSceneObjectAllocPolicy{};
    00235     // template <size_t align> class CategorisedAlignAllocPolicy<MEMCATEGORY_SCENE_OBJECTS, align> : public YourSceneObjectAllocPolicy<align>{};
    00236
    00237
    00238 }
    00239
    00240 #else
    00241
    00242 // your allocators here?
    00243
    00244 #endif
    00245
    00246 namespace Ogre
    00247 {
    00248     // Useful shortcuts
    00249     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_GENERAL> GeneralAllocPolicy;
    00250     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_GEOMETRY> GeometryAllocPolicy;
    00251     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_ANIMATION> AnimationAllocPolicy;
    00252     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_SCENE_CONTROL> SceneCtlAllocPolicy;
    00253     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_SCENE_OBJECTS> SceneObjAllocPolicy;
    00254     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_RESOURCE> ResourceAllocPolicy;
    00255     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_SCRIPTING> ScriptingAllocPolicy;
    00256     typedef CategorisedAllocPolicy<Ogre::MEMCATEGORY_RENDERSYS> RenderSysAllocPolicy;
    00257
    00258     // Now define all the base classes for each allocation
    00259     typedef AllocatedObject<GeneralAllocPolicy> GeneralAllocatedObject;
    00260     typedef AllocatedObject<GeometryAllocPolicy> GeometryAllocatedObject;
    00261     typedef AllocatedObject<AnimationAllocPolicy> AnimationAllocatedObject;
    00262     typedef AllocatedObject<SceneCtlAllocPolicy> SceneCtlAllocatedObject;
    00263     typedef AllocatedObject<SceneObjAllocPolicy> SceneObjAllocatedObject;
    00264     typedef AllocatedObject<ResourceAllocPolicy> ResourceAllocatedObject;
    00265     typedef AllocatedObject<ScriptingAllocPolicy> ScriptingAllocatedObject;
    00266     typedef AllocatedObject<RenderSysAllocPolicy> RenderSysAllocatedObject;
    00267
    00268
    00269     // Per-class allocators defined here
    00270     // NOTE: small, non-virtual classes should not subclass an allocator
    00271     // the virtual function table could double their size and make them less efficient
    00272     // use primitive or STL allocators / deallocators for those
    00273     typedef ScriptingAllocatedObject    AbstractNodeAlloc;
    00274     typedef AnimationAllocatedObject    AnimableAlloc;
    00275     typedef AnimationAllocatedObject    AnimationAlloc;
    00276     typedef GeneralAllocatedObject      ArchiveAlloc;
    00277     typedef GeometryAllocatedObject     BatchedGeometryAlloc;
    00278     typedef RenderSysAllocatedObject    BufferAlloc;
    00279     typedef GeneralAllocatedObject      CodecAlloc;
    00280     typedef ResourceAllocatedObject     CompositorInstAlloc;
    00281     typedef GeneralAllocatedObject      ConfigAlloc;
    00282     typedef GeneralAllocatedObject      ControllerAlloc;
    00283     typedef GeometryAllocatedObject     DebugGeomAlloc;
    00284     typedef GeneralAllocatedObject      DynLibAlloc;
    00285     typedef GeometryAllocatedObject     EdgeDataAlloc;
    00286     typedef GeneralAllocatedObject      FactoryAlloc;
    00287     typedef SceneObjAllocatedObject     FXAlloc;
    00288     typedef GeneralAllocatedObject      ImageAlloc;
    00289     typedef GeometryAllocatedObject     IndexDataAlloc;
    00290     typedef GeneralAllocatedObject      LogAlloc;
    00291     typedef SceneObjAllocatedObject     MovableAlloc;
    00292     typedef SceneCtlAllocatedObject     NodeAlloc;
    00293     typedef SceneObjAllocatedObject     OverlayAlloc;
    00294     typedef RenderSysAllocatedObject    GpuParamsAlloc;
    00295     typedef ResourceAllocatedObject     PassAlloc;
    00296     typedef GeometryAllocatedObject     PatchAlloc;
    00297     typedef GeneralAllocatedObject      PluginAlloc;
    00298     typedef GeneralAllocatedObject      ProfilerAlloc;
    00299     typedef GeometryAllocatedObject     ProgMeshAlloc;
    00300     typedef SceneCtlAllocatedObject     RenderQueueAlloc;
    00301     typedef RenderSysAllocatedObject    RenderSysAlloc;
    00302     typedef GeneralAllocatedObject      RootAlloc;
    00303     typedef ResourceAllocatedObject     ResourceAlloc;
    00304     typedef GeneralAllocatedObject      SerializerAlloc;
    00305     typedef SceneCtlAllocatedObject     SceneMgtAlloc;
    00306     typedef ScriptingAllocatedObject    ScriptCompilerAlloc;
    00307     typedef ScriptingAllocatedObject    ScriptTranslatorAlloc;
    00308     typedef SceneCtlAllocatedObject     ShadowDataAlloc;
    00309     typedef GeneralAllocatedObject      StreamAlloc;
    00310     typedef SceneObjAllocatedObject     SubEntityAlloc;
    00311     typedef ResourceAllocatedObject     SubMeshAlloc;
    00312     typedef ResourceAllocatedObject     TechniqueAlloc;
    00313     typedef GeneralAllocatedObject      TimerAlloc;
    00314     typedef ResourceAllocatedObject     TextureUnitStateAlloc;
    00315     typedef GeneralAllocatedObject      UtilityAlloc;
    00316     typedef GeometryAllocatedObject     VertexDataAlloc;
    00317     typedef RenderSysAllocatedObject    ViewportAlloc;
    00318     typedef SceneCtlAllocatedObject     LodAlloc;
    00319
    00320     // Containers (by-value only)
    00321     // Will  be of the form:
    00322     // typedef STLAllocator<T, DefaultAllocPolicy, Category> TAlloc;
    00323     // for use in vector<T, TAlloc>::type
    00324
    00325
    00326
    00327 }
    00328
    00329 // Util functions
    00330 namespace Ogre
    00331 {
    00343     template<typename T>
    00344     T* constructN(T* basePtr, size_t count)
    00345     {
    00346         for (size_t i = 0; i < count; ++i)
    00347         {
    00348             new ((void*)(basePtr+i)) T();
    00349         }
    00350         return basePtr;
    00351     }
    00355 }
    00356 // define macros
    00357
    00365 #if OGRE_DEBUG_MODE
    00366
    00368 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00369
    00370 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00371
    00372 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr)
    00373
    00375 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00376
    00377 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__)), count)
    00378
    00379 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00380
    00381 #   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);}
    00382
    00383 // aligned allocation
    00385 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00386
    00387 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes, __FILE__, __LINE__, __FUNCTION__)
    00388
    00389 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00390
    00391 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count), __FILE__, __LINE__, __FUNCTION__))
    00392
    00393 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr)
    00394
    00395 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr)
    00396
    00398 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00399
    00400 #   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)
    00401
    00402 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes(ptr);}
    00403
    00404 #   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);}
    00405
    00406 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T), __FILE__, __LINE__, __FUNCTION__)) T
    00407
    00408 #   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)
    00409
    00410 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes(ptr);}
    00411
    00412 #   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);}
    00413
    00414 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
    00415 // Also hooks up the file/line/function params
    00416 // Can only be used with classes that derive from AllocatedObject since customised new/delete needed
    00417 #   define OGRE_NEW new (__FILE__, __LINE__, __FUNCTION__)
    00418 #   define OGRE_DELETE delete
    00419
    00420
    00421 #else // !OGRE_DEBUG_MODE
    00422
    00424 #   define OGRE_MALLOC(bytes, category) ::Ogre::CategorisedAllocPolicy<category>::allocateBytes(bytes)
    00425
    00426 #   define OGRE_ALLOC_T(T, count, category) static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
    00427
    00428 #   define OGRE_FREE(ptr, category) ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr)
    00429
    00431 #   define OGRE_NEW_T(T, category) new (::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T))) T
    00432
    00433 #   define OGRE_NEW_ARRAY_T(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count)
    00434
    00435 #   define OGRE_DELETE_T(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00436
    00437 #   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);}
    00438
    00439 // aligned allocation
    00441 #   define OGRE_MALLOC_SIMD(bytes, category) ::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(bytes)
    00442
    00443 #   define OGRE_MALLOC_ALIGN(bytes, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(bytes)
    00444
    00445 #   define OGRE_ALLOC_T_SIMD(T, count, category) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count)))
    00446
    00447 #   define OGRE_ALLOC_T_ALIGN(T, count, category, align) static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count)))
    00448
    00449 #   define OGRE_FREE_SIMD(ptr, category) ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes((void*)ptr)
    00450
    00451 #   define OGRE_FREE_ALIGN(ptr, category, align) ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes((void*)ptr)
    00452
    00454 #   define OGRE_NEW_T_SIMD(T, category) new (::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T))) T
    00455
    00456 #   define OGRE_NEW_ARRAY_T_SIMD(T, count, category) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category>::allocateBytes(sizeof(T)*(count))), count)
    00457
    00458 #   define OGRE_DELETE_T_SIMD(ptr, T, category) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category>::deallocateBytes((void*)ptr);}
    00459
    00460 #   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);}
    00461
    00462 #   define OGRE_NEW_T_ALIGN(T, category, align) new (::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T))) T
    00463
    00464 #   define OGRE_NEW_ARRAY_T_ALIGN(T, count, category, align) ::Ogre::constructN(static_cast<T*>(::Ogre::CategorisedAlignAllocPolicy<category, align>::allocateBytes(sizeof(T)*(count))), count)
    00465
    00466 #   define OGRE_DELETE_T_ALIGN(ptr, T, category, align) if(ptr){(ptr)->~T(); ::Ogre::CategorisedAlignAllocPolicy<category, align>::deallocateBytes((void*)ptr);}
    00467
    00468 #   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);}
    00469
    00470 // new / delete for classes deriving from AllocatedObject (alignment determined by per-class policy)
    00471 #   define OGRE_NEW new
    00472 #   define OGRE_DELETE delete
    00473
    00474 #endif // OGRE_DEBUG_MODE
    00475
    00476
    00477 namespace Ogre
    00478 {
    00483     template<typename T>
    00484     void deletePtr(T* ptr)
    00485     {
    00486         OGRE_DELETE ptr;
    00487     }
    00488 }
    00489
    00493 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52