]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/memory/allocator.h
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / libs / memory / allocator.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_MEMORY_ALLOCATOR_H )
23 #define INCLUDED_MEMORY_ALLOCATOR_H
24
25 #include <memory>
26 #include <stddef.h>
27
28 #if 0
29
30 #define DefaultAllocator std::allocator
31
32 #else
33
34 /// \brief An allocator that uses c++ new/delete.
35 /// Compliant with the std::allocator interface.
36 template<typename Type>
37 class DefaultAllocator
38 {
39 public:
40
41 typedef Type value_type;
42 typedef value_type* pointer;
43 typedef const Type* const_pointer;
44 typedef Type& reference;
45 typedef const Type& const_reference;
46 typedef size_t size_type;
47 typedef ptrdiff_t difference_type;
48
49 template<typename Other>
50 struct rebind
51 {
52         typedef DefaultAllocator<Other> other;
53 };
54
55 DefaultAllocator(){
56 }
57 DefaultAllocator( const DefaultAllocator<Type>& ){
58 }
59 template<typename Other> DefaultAllocator( const DefaultAllocator<Other>& ){
60 }
61 ~DefaultAllocator(){
62 }
63
64 pointer address( reference instance ) const {
65         return &instance;
66 }
67 const_pointer address( const_reference instance ) const {
68         return &instance;
69 }
70 Type* allocate( size_type size, const void* = 0 ){
71         return static_cast<Type*>( ::operator new( size * sizeof( Type ) ) );
72 }
73 void deallocate( pointer p, size_type ){
74         ::operator delete( p );
75 }
76 size_type max_size() const {
77         return std::size_t( -1 ) / sizeof( Type );
78 }
79 void construct( pointer p, const Type& value ){
80         new(p) Type( value );
81 }
82 void destroy( pointer p ){
83         p->~Type();
84 }
85 };
86
87 template<typename Type, typename Other>
88 inline bool operator==( const DefaultAllocator<Type>&, const DefaultAllocator<Other>& ){
89         return true;
90 }
91 template<typename Type, typename OtherAllocator>
92 inline bool operator==( const DefaultAllocator<Type>&, const OtherAllocator& ){
93         return false;
94 }
95
96 #endif
97
98
99 template<typename Type>
100 class NamedAllocator : public DefaultAllocator<Type>
101 {
102 typedef DefaultAllocator<Type> allocator_type;
103
104 const char* m_name;
105 public:
106
107 typedef Type value_type;
108 typedef value_type* pointer;
109 typedef const Type* const_pointer;
110 typedef Type& reference;
111 typedef const Type& const_reference;
112 typedef size_t size_type;
113 typedef ptrdiff_t difference_type;
114
115 template<typename Other>
116 struct rebind
117 {
118         typedef NamedAllocator<Other> other;
119 };
120
121 explicit NamedAllocator( const char* name ) : m_name( name ){
122 }
123 NamedAllocator( const NamedAllocator<Type>& other ) : m_name( other.m_name ){
124 }
125 template<typename Other> NamedAllocator( const NamedAllocator<Other>& other ) : m_name( other.m_name ){
126 }
127 ~NamedAllocator(){
128 }
129
130 pointer address( reference instance ) const {
131         return allocator_type::address( instance );
132 }
133 const_pointer address( const_reference instance ) const {
134         return allocator_type::address( instance );
135 }
136 Type* allocate( size_type size, const void* = 0 ){
137         return allocator_type::allocate( size );
138 }
139 void deallocate( pointer p, size_type size ){
140         allocator_type::deallocate( p, size );
141 }
142 size_type max_size() const {
143         return allocator_type::max_size();
144 }
145 void construct( pointer p, const Type& value ){
146         allocator_type::construct( p, value );
147 }
148 void destroy( pointer p ){
149         allocator_type::destroy( p );
150 }
151
152 template<typename Other>
153 bool operator==( const NamedAllocator<Other>& other ){
154         return true;
155 }
156
157 // returns true if the allocators are not interchangeable
158 template<typename Other>
159 bool operator!=( const NamedAllocator<Other>& other ){
160         return false;
161 }
162 };
163
164
165
166 #include <algorithm>
167 #include "generic/object.h"
168
169
170
171 template<typename Type>
172 class DefaultConstruct
173 {
174 public:
175 void operator()( Type& t ){
176         constructor( t );
177 }
178 };
179
180 template<typename Type, typename T1>
181 class Construct
182 {
183 const T1& other;
184 public:
185 Construct( const T1& other_ ) : other( other_ ){
186 }
187 void operator()( Type& t ){
188         constructor( t, other );
189 }
190 };
191
192 template<typename Type>
193 class Destroy
194 {
195 public:
196 void operator()( Type& t ){
197         destructor( t );
198 }
199 };
200
201 template<typename Type, typename Allocator = DefaultAllocator<Type> >
202 class New : public Allocator
203 {
204 public:
205 New(){
206 }
207 explicit New( const Allocator& allocator ) : Allocator( allocator ){
208 }
209
210 Type* scalar(){
211         return new( Allocator::allocate( 1 ) )Type();
212 }
213 template<typename T1>
214 Type* scalar( const T1& t1 ){
215         return new( Allocator::allocate( 1 ) )Type( t1 );
216 }
217 template<typename T1, typename T2>
218 Type* scalar( const T1& t1, const T2& t2 ){
219         return new( Allocator::allocate( 1 ) )Type( t1, t2 );
220 }
221 template<typename T1, typename T2, typename T3>
222 Type* scalar( const T1& t1, const T2& t2, const T3& t3 ){
223         return new( Allocator::allocate( 1 ) )Type( t1, t2, t3 );
224 }
225 template<typename T1, typename T2, typename T3, typename T4>
226 Type* scalar( const T1& t1, const T2& t2, const T3& t3, const T4& t4 ){
227         return new( Allocator::allocate( 1 ) )Type( t1, t2, t3, t4 );
228 }
229 template<typename T1, typename T2, typename T3, typename T4, typename T5>
230 Type* scalar( const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5 ){
231         return new( Allocator::allocate( 1 ) )Type( t1, t2, t3, t4, t5 );
232 }
233 Type* vector( std::size_t size ){
234 #if 1
235         Type* p = Allocator::allocate( size );
236         std::for_each( p, p + size, DefaultConstruct<Type>() );
237         return p;
238 #else
239         // this does not work with msvc71 runtime
240         return new( Allocator::allocate( size ) )Type[size];
241 #endif
242 }
243 template<typename T1>
244 Type* vector( std::size_t size, const T1& t1 ){
245         Type* p = Allocator::allocate( size );
246         std::for_each( p, p + size, Construct<Type, T1>( t1 ) );
247         return p;
248 }
249 };
250
251 template<typename Type, typename Allocator = DefaultAllocator<Type> >
252 class Delete : public Allocator
253 {
254 public:
255 Delete(){
256 }
257 explicit Delete( const Allocator& allocator ) : Allocator( allocator ){
258 }
259
260 void scalar( Type* p ){
261         if ( p != 0 ) {
262                 p->~Type();
263                 Allocator::deallocate( p, 1 );
264         }
265 }
266 void vector( Type* p, std::size_t size ){
267         // '::operator delete' handles null
268         // 'std::allocator::deallocate' requires non-null
269         if ( p != 0 ) {
270                 std::for_each( p, p + size, Destroy<Type>() );
271                 Allocator::deallocate( p, size );
272         }
273 }
274 };
275
276
277 template<typename Type>
278 class NamedNew
279 {
280 public:
281 typedef New<Type, NamedAllocator<Type> > type;
282 };
283
284 template<typename Type>
285 class NamedDelete
286 {
287 public:
288 typedef Delete<Type, NamedAllocator<Type> > type;
289 };
290
291 #endif