Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OgreAlignedAllocator.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-2006 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 #ifndef __AlignedAllocator_H__
00030 #define __AlignedAllocator_H__
00031 
00032 #include "OgrePrerequisites.h"
00033 
00034 namespace Ogre {
00035 
00054     class _OgreExport AlignedMemory
00055     {
00056     public:
00068         static void* allocate(size_t size, size_t alignment);
00069 
00082         static void* allocate(size_t size);
00083 
00090         static void deallocate(void* p);
00091     };
00092 
00103     template <typename T, unsigned Alignment = 0>
00104     class AlignedAllocator
00105     {
00106         // compile-time check alignment is available.
00107         typedef int IsValidAlignment
00108             [Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1];
00109 
00110     public:
00111         //--- typedefs for STL compatible
00112 
00113         typedef T value_type;
00114 
00115         typedef value_type * pointer;
00116         typedef const value_type * const_pointer;
00117         typedef value_type & reference;
00118         typedef const value_type & const_reference;
00119         typedef std::size_t size_type;
00120         typedef std::ptrdiff_t difference_type;
00121 
00122         template <typename U>
00123         struct rebind
00124         {
00125             typedef AlignedAllocator<U, Alignment> other;
00126         };
00127 
00128     public:
00129         AlignedAllocator() { /* nothing to do */ }
00130 
00131         // default copy constructor
00132 
00133         // default assignment operator
00134 
00135         // not explicit, mimicking std::allocator [20.4.1]
00136         template <typename U, unsigned A>
00137         AlignedAllocator(const AlignedAllocator<U, A> &) { /* nothing to do */ }
00138 
00139         // default destructor
00140 
00141         //--- functions for STL compatible
00142 
00143         static pointer address(reference r)
00144         { return &r; }
00145         static const_pointer address(const_reference s)
00146         { return &s; }
00147         static size_type max_size()
00148         { return (std::numeric_limits<size_type>::max)(); }
00149         static void construct(const pointer ptr, const value_type & t)
00150         { new (ptr) T(t); }
00151         static void destroy(const pointer ptr)
00152         {
00153             ptr->~T();
00154             (void) ptr; // avoid unused variable warning
00155         }
00156 
00157         bool operator==(const AlignedAllocator &) const
00158         { return true; }
00159         bool operator!=(const AlignedAllocator &) const
00160         { return false; }
00161 
00162         static pointer allocate(const size_type n)
00163         {
00164             // use default platform dependent alignment if 'Alignment' equal to zero.
00165             const pointer ret = static_cast<pointer>(Alignment ?
00166                 AlignedMemory::allocate(sizeof(T) * n, Alignment) :
00167                 AlignedMemory::allocate(sizeof(T) * n));
00168             return ret;
00169         }
00170         static pointer allocate(const size_type n, const void * const)
00171         {
00172             return allocate(n);
00173         }
00174         static void deallocate(const pointer ptr, const size_type)
00175         {
00176             AlignedMemory::deallocate(ptr);
00177         }
00178     };
00179 }
00180 
00181 #endif  // __AlignedAllocator_H__

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 25 13:03:13 2007