Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreAlignedAllocator.h @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 6.6 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#ifndef __AlignedAllocator_H__
30#define __AlignedAllocator_H__
31
32#include "OgrePrerequisites.h"
33
34namespace Ogre {
35
36    /** Class to provide aligned memory allocate functionality.
37    @remarks
38        All SIMD processing are friendly with aligned memory, and some SIMD routines
39        are designed for working with aligned memory only. If the data are intented to
40        use SIMD processing, it's need to be aligned for better performance boost.
41        In additional, most time cache boundary aligned data also lead to better
42        performance even if didn't used SIMD processing. So this class provides a couple
43        of functions for allocate aligned memory.
44    @par
45        Anyways, in general, you don't need to use this class directly, Ogre internally
46        will take care with most SIMD and cache friendly optimisation if possible.
47    @par
48        This isn't a "one-step" optimisation, there are a lot of underlying work to
49        achieve performance boost. If you didn't know what are you doing or what there
50        are going, just ignore this class.
51    @note
52        This class intented to use by advanced user only.
53    */
54        class _OgreExport AlignedMemory
55        {
56        public:
57        /** Allocate memory with given alignment.
58            @param
59                size The size of memory need to allocate.
60            @param
61                alignment The alignment of result pointer, must be power of two
62                and in range [1, 128].
63            @returns
64                The allocated memory pointer.
65            @par
66                On failiure, exception will be throw.
67        */
68        static void* allocate(size_t size, size_t alignment);
69
70        /** Allocate memory with default platform dependent alignment.
71            @remarks
72                The default alignment depend on target machine, this function
73                guarantee aligned memory according with SIMD processing and
74                cache boundary friendly.
75            @param
76                size The size of memory need to allocate.
77            @returns
78                The allocated memory pointer.
79            @par
80                On failiure, exception will be throw.
81        */
82        static void* allocate(size_t size);
83
84        /** Deallocate memory that allocated by this class.
85            @param
86                p Pointer to the memory allocated by this class or <b>NULL</b> pointer.
87            @par
88                On <b>NULL</b> pointer, nothing happen.
89        */
90        static void deallocate(void* p);
91        };
92
93    /** STL compatible allocator with aligned memory allocate, used for tweak
94        STL containers work with aligned memory.
95    @remarks
96        This class designed for work with STL containers, by use this
97        class instead of std::allocator, the STL containers can work
98        with aligned memory allocate seamless.
99    @note
100        template parameter Alignment equal to zero means use default
101        platform dependent alignment.
102    */
103    template <typename T, unsigned Alignment = 0>
104    class AlignedAllocator
105    {
106        // compile-time check alignment is available.
107        typedef int IsValidAlignment
108            [Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1];
109
110    public:
111        //--- typedefs for STL compatible
112
113        typedef T value_type;
114
115        typedef value_type * pointer;
116        typedef const value_type * const_pointer;
117        typedef value_type & reference;
118        typedef const value_type & const_reference;
119        typedef std::size_t size_type;
120        typedef std::ptrdiff_t difference_type;
121
122        template <typename U>
123        struct rebind
124        {
125            typedef AlignedAllocator<U, Alignment> other;
126        };
127
128    public:
129        AlignedAllocator() { /* nothing to do */ }
130
131        // default copy constructor
132
133        // default assignment operator
134
135        // not explicit, mimicking std::allocator [20.4.1]
136        template <typename U, unsigned A>
137        AlignedAllocator(const AlignedAllocator<U, A> &) { /* nothing to do */ }
138
139        // default destructor
140
141        //--- functions for STL compatible
142
143        static pointer address(reference r)
144        { return &r; }
145        static const_pointer address(const_reference s)
146        { return &s; }
147        static size_type max_size()
148        { return (std::numeric_limits<size_type>::max)(); }
149        static void construct(const pointer ptr, const value_type & t)
150        { new (ptr) T(t); }
151        static void destroy(const pointer ptr)
152        {
153            ptr->~T();
154            (void) ptr; // avoid unused variable warning
155        }
156
157        bool operator==(const AlignedAllocator &) const
158        { return true; }
159        bool operator!=(const AlignedAllocator &) const
160        { return false; }
161
162        static pointer allocate(const size_type n)
163        {
164            // use default platform dependent alignment if 'Alignment' equal to zero.
165            const pointer ret = static_cast<pointer>(Alignment ?
166                AlignedMemory::allocate(sizeof(T) * n, Alignment) :
167                AlignedMemory::allocate(sizeof(T) * n));
168            return ret;
169        }
170        static pointer allocate(const size_type n, const void * const)
171        {
172            return allocate(n);
173        }
174        static void deallocate(const pointer ptr, const size_type)
175        {
176            AlignedMemory::deallocate(ptr);
177        }
178    };
179}
180
181#endif  // __AlignedAllocator_H__
Note: See TracBrowser for help on using the repository browser.