OgreMemoryStdAlloc.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 __MemoryStdAlloc_H__
00031 #define __MemoryStdAlloc_H__
00032 
00033 #include <memory>
00034 #include <limits>
00035 
00036 #include "OgreAlignedAllocator.h"
00037 #include "OgreMemoryTracker.h"
00038 
00039 namespace Ogre
00040 {
00041 
00049     class _OgreExport StdAllocPolicy
00050     {
00051     public:
00052         static inline void* allocateBytes(size_t count, 
00053             const char* file = 0, int line = 0, const char* func = 0)
00054         {
00055             void* ptr = malloc(count);
00056 #if OGRE_MEMORY_TRACKER
00057             // this alloc policy doesn't do pools
00058             MemoryTracker::get()._recordAlloc(ptr, count, 0, file, line, func);
00059 #else
00060             // avoid unused params warning
00061             count=file+line-func;
00062 #endif
00063             return ptr;
00064         }
00065 
00066         static inline void deallocateBytes(void* ptr)
00067         {
00068 #if OGRE_MEMORY_TRACKER
00069             MemoryTracker::get()._recordDealloc(ptr);
00070 #endif
00071             free(ptr);
00072         }
00073 
00075         static inline size_t getMaxAllocationSize()
00076         {
00077             return std::numeric_limits<size_t>::max();
00078         }
00079     private:
00080         // no instantiation
00081         StdAllocPolicy()
00082         { }
00083     };
00084 
00097     template <size_t Alignment = 0>
00098     class StdAlignedAllocPolicy
00099     {
00100     public:
00101         // compile-time check alignment is available.
00102         typedef int IsValidAlignment
00103             [Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1];
00104 
00105         static inline void* allocateBytes(size_t count, 
00106             const char* file = 0, int line = 0, const char* func = 0)
00107         {
00108             void* ptr = Alignment ? AlignedMemory::allocate(count, Alignment)
00109                 : AlignedMemory::allocate(count);
00110 #if OGRE_MEMORY_TRACKER
00111             // this alloc policy doesn't do pools
00112             MemoryTracker::get()._recordAlloc(ptr, count, 0, file, line, func);
00113 #else
00114             // avoid unused params warning
00115             file;line;func;
00116 #endif
00117             return ptr;
00118         }
00119 
00120         static inline void deallocateBytes(void* ptr)
00121         {
00122 #if OGRE_MEMORY_TRACKER
00123             MemoryTracker::get()._recordDealloc(ptr);
00124 #endif
00125             AlignedMemory::deallocate(ptr);
00126         }
00127 
00129         static inline size_t getMaxAllocationSize()
00130         {
00131             return std::numeric_limits<size_t>::max();
00132         }
00133     private:
00134         // No instantiation
00135         StdAlignedAllocPolicy()
00136         { }
00137     };
00138 
00139 
00140 
00141 }// namespace Ogre
00142 #endif // __MemoryStdAlloc_H__

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:49 2008