[148] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _MemorySTLAllocator_H__ |
---|
| 30 | #define _MemorySTLAllocator_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | #include "OgreHeaderPrefix.h" |
---|
| 34 | |
---|
| 35 | namespace Ogre |
---|
| 36 | { |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | /** \addtogroup Core |
---|
| 40 | * @{ |
---|
| 41 | */ |
---|
| 42 | /** \addtogroup Memory |
---|
| 43 | * @{ |
---|
| 44 | */ |
---|
| 45 | /** |
---|
| 46 | Wrapper class for operating as an STL container allocator. |
---|
| 47 | This class acts as the host for a configured allocation policy. |
---|
| 48 | |
---|
| 49 | It takes the following template parameters:- |
---|
| 50 | <ul> |
---|
| 51 | <li>T : the type we will be allocating (see rebind later)</li> |
---|
| 52 | <li>AllocPolicy : a memory allocator policy </li> |
---|
| 53 | </ul> |
---|
| 54 | |
---|
| 55 | See StdAllocPolicy for an example of implementing a Policy. The reason for |
---|
| 56 | implementing the allocation in a policy is that it makes implementing |
---|
| 57 | a custom allocator easier - you just have to provide an alternative |
---|
| 58 | policy with just allocate/deallocate implementations; this class does all the |
---|
| 59 | housekeeping associated with keeping the STL happy. |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | // Base STL allocator class. |
---|
| 63 | template<typename T> |
---|
| 64 | struct STLAllocatorBase |
---|
| 65 | { // base class for generic allocators |
---|
| 66 | typedef T value_type; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | // Base STL allocator class. (const T version). |
---|
| 70 | template<typename T> |
---|
| 71 | struct STLAllocatorBase<const T> |
---|
| 72 | { // base class for generic allocators for const T |
---|
| 73 | typedef T value_type; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | template |
---|
| 77 | < |
---|
| 78 | typename T, |
---|
| 79 | typename AllocPolicy |
---|
| 80 | > |
---|
| 81 | class STLAllocator : public STLAllocatorBase<T> |
---|
| 82 | { |
---|
| 83 | public : |
---|
| 84 | /// define our types, as per ISO C++ |
---|
| 85 | typedef STLAllocatorBase<T> Base; |
---|
| 86 | typedef typename Base::value_type value_type; |
---|
| 87 | typedef value_type* pointer; |
---|
| 88 | typedef const value_type* const_pointer; |
---|
| 89 | typedef value_type& reference; |
---|
| 90 | typedef const value_type& const_reference; |
---|
| 91 | typedef std::size_t size_type; |
---|
| 92 | typedef std::ptrdiff_t difference_type; |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | /// the standard rebind mechanism |
---|
| 96 | template<typename U> |
---|
| 97 | struct rebind |
---|
| 98 | { |
---|
| 99 | typedef STLAllocator<U, AllocPolicy> other; |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | /// ctor |
---|
| 103 | inline explicit STLAllocator() |
---|
| 104 | { } |
---|
| 105 | |
---|
| 106 | /// dtor |
---|
| 107 | virtual ~STLAllocator() |
---|
| 108 | { } |
---|
| 109 | |
---|
| 110 | /// copy ctor - done component wise |
---|
| 111 | inline STLAllocator( STLAllocator const& ) |
---|
| 112 | { } |
---|
| 113 | |
---|
| 114 | /// cast |
---|
| 115 | template <typename U> |
---|
| 116 | inline STLAllocator( STLAllocator<U, AllocPolicy> const& ) |
---|
| 117 | { } |
---|
| 118 | |
---|
| 119 | /// cast |
---|
| 120 | template <typename U, typename P> |
---|
| 121 | inline STLAllocator( STLAllocator<U, P> const& ) |
---|
| 122 | { } |
---|
| 123 | |
---|
| 124 | /// memory allocation (elements, used by STL) |
---|
| 125 | inline pointer allocate( size_type count, |
---|
| 126 | typename std::allocator<void>::const_pointer ptr = 0 ) |
---|
| 127 | { |
---|
| 128 | (void)ptr; |
---|
| 129 | // convert request to bytes |
---|
| 130 | register size_type sz = count*sizeof( T ); |
---|
| 131 | pointer p = static_cast<pointer>(AllocPolicy::allocateBytes(sz)); |
---|
| 132 | return p; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /// memory deallocation (elements, used by STL) |
---|
| 136 | inline void deallocate( pointer ptr, size_type ) |
---|
| 137 | { |
---|
| 138 | // convert request to bytes, but we can't use this? |
---|
| 139 | // register size_type sz = count*sizeof( T ); |
---|
| 140 | AllocPolicy::deallocateBytes(ptr); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | pointer address(reference x) const |
---|
| 144 | { |
---|
| 145 | return &x; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | const_pointer address(const_reference x) const |
---|
| 149 | { |
---|
| 150 | return &x; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | size_type max_size() const throw() |
---|
| 154 | { |
---|
| 155 | // maximum size this can handle, delegate |
---|
| 156 | return AllocPolicy::getMaxAllocationSize(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | #if __cplusplus < 201103L |
---|
| 160 | void construct(pointer p) |
---|
| 161 | { |
---|
| 162 | // call placement new |
---|
| 163 | new(static_cast<void*>(p)) T(); |
---|
| 164 | } |
---|
| 165 | #endif |
---|
| 166 | |
---|
| 167 | void construct(pointer p, const T& val) |
---|
| 168 | { |
---|
| 169 | // call placement new |
---|
| 170 | new(static_cast<void*>(p)) T(val); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void destroy(pointer p) |
---|
| 174 | { |
---|
| 175 | // do we have to protect against non-classes here? |
---|
| 176 | // some articles suggest yes, some no |
---|
| 177 | p->~T(); |
---|
| 178 | } |
---|
| 179 | }; |
---|
| 180 | |
---|
| 181 | /// determine equality, can memory from another allocator |
---|
| 182 | /// be released by this allocator, (ISO C++) |
---|
| 183 | template<typename T, typename T2, typename P> |
---|
| 184 | inline bool operator==(STLAllocator<T,P> const&, |
---|
| 185 | STLAllocator<T2,P> const&) |
---|
| 186 | { |
---|
| 187 | // same alloc policy (P), memory can be freed |
---|
| 188 | return true; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /// determine equality, can memory from another allocator |
---|
| 192 | /// be released by this allocator, (ISO C++) |
---|
| 193 | template<typename T, typename P, typename OtherAllocator> |
---|
| 194 | inline bool operator==(STLAllocator<T,P> const&, |
---|
| 195 | OtherAllocator const&) |
---|
| 196 | { |
---|
| 197 | return false; |
---|
| 198 | } |
---|
| 199 | /// determine equality, can memory from another allocator |
---|
| 200 | /// be released by this allocator, (ISO C++) |
---|
| 201 | template<typename T, typename T2, typename P> |
---|
| 202 | inline bool operator!=(STLAllocator<T,P> const&, |
---|
| 203 | STLAllocator<T2,P> const&) |
---|
| 204 | { |
---|
| 205 | // same alloc policy (P), memory can be freed |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /// determine equality, can memory from another allocator |
---|
| 210 | /// be released by this allocator, (ISO C++) |
---|
| 211 | template<typename T, typename P, typename OtherAllocator> |
---|
| 212 | inline bool operator!=(STLAllocator<T,P> const&, |
---|
| 213 | OtherAllocator const&) |
---|
| 214 | { |
---|
| 215 | return true; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /** @} */ |
---|
| 220 | /** @} */ |
---|
| 221 | |
---|
| 222 | }// namespace Ogre |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | #include "OgreHeaderSuffix.h" |
---|
| 226 | |
---|
| 227 | #endif // _MemorySTLAllocator_H__ |
---|
| 228 | |
---|