PCL
Allocator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/Allocator.h - Released 2019-11-07T10:59:34Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2019 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (http://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Allocator_h
53 #define __PCL_Allocator_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Relational.h>
61 #include <pcl/Utility.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
130 template <class T, class A>
131 class PCL_CLASS Allocator : public A
132 {
133 public:
134 
138  Allocator() = default;
139 
143  Allocator( const Allocator<T,A>& ) = default;
144 
148  Allocator( const A& a ) : A( a )
149  {
150  }
151 
162  T* Allocate( size_type n, size_type extra = 0 )
163  {
164  PCL_PRECONDITION( n+extra > 0 )
165  return (T*)this->AllocateBlock( n*sizeof( T )+extra );
166  //return (T*)new ( *this ) uint8[ n*sizeof( T )+extra ];
167  }
168 
176  void Deallocate( T* p )
177  {
178  PCL_PRECONDITION( p != nullptr )
179  this->DeallocateBlock( (void*)p );
180  //this->operator delete( (void*)p );
181  }
182 
188  {
189  return A::MaxSize()/sizeof( T );
190  }
191 
206  {
207  return A::BlockSize( n*sizeof( T ) )/sizeof( T );
208  }
209 
226  size_type ReallocatedLength( size_type currentLength, size_type newLength ) const
227  {
228  return A::ReallocatedBlockSize( currentLength*sizeof( T ), newLength*sizeof( T ) )/sizeof( T );
229  }
230 };
231 
232 // ----------------------------------------------------------------------------
233 
246 template <class T, class A> inline void Construct( T* p, A& a )
247 {
248  PCL_PRECONDITION( p != nullptr )
249  new( (void*)p, a )T();
250 }
251 
259 template <class T, class T1, class A> inline void Construct( T* p, const T1& v, A& a )
260 {
261  PCL_PRECONDITION( p != nullptr )
262  new( (void*)p, a )T( v );
263 }
264 
265 #ifdef _MSC_VER
266 # pragma warning( push )
267 # pragma warning( disable : 4100 ) // unreferenced formal parameter
268 #endif // (VC++ limitation !?)
269 
276 template <class T> inline void Destroy( T* p )
277 {
278  PCL_PRECONDITION( p != nullptr )
279  p->~T();
280 }
281 
288 template <class T> inline void Destroy( T* p, T* q )
289 {
290  PCL_PRECONDITION( p != nullptr && q != nullptr )
291  for ( ; p < q; ++p )
292  p->~T();
293 }
294 
295 #ifdef _MSC_VER
296 # pragma warning( pop )
297 #endif
298 
299 inline void Destroy( void* ) {}
300 inline void Destroy( void*, void* ) {}
301 
302 inline void Destroy( bool* ) {}
303 inline void Destroy( bool*, bool* ) {}
304 
305 inline void Destroy( signed char* ) {}
306 inline void Destroy( signed char*, signed char* ) {}
307 
308 inline void Destroy( unsigned char* ) {}
309 inline void Destroy( unsigned char*, unsigned char* ) {}
310 
311 inline void Destroy( wchar_t* ) {}
312 inline void Destroy( wchar_t*, wchar_t* ) {}
313 
314 inline void Destroy( char16_t* ) {}
315 inline void Destroy( char16_t*, char16_t* ) {}
316 
317 inline void Destroy( char32_t* ) {}
318 inline void Destroy( char32_t*, char32_t* ) {}
319 
320 inline void Destroy( signed int* ) {}
321 inline void Destroy( signed int*, signed int* ) {}
322 
323 inline void Destroy( unsigned int* ) {}
324 inline void Destroy( unsigned int*, unsigned int* ) {}
325 
326 inline void Destroy( signed short* ) {}
327 inline void Destroy( signed short*, signed short* ) {}
328 
329 inline void Destroy( unsigned short* ) {}
330 inline void Destroy( unsigned short*, unsigned short* ) {}
331 
332 inline void Destroy( signed long* ) {}
333 inline void Destroy( signed long*, signed long* ) {}
334 
335 inline void Destroy( unsigned long* ) {}
336 inline void Destroy( unsigned long*, unsigned long* ) {}
337 
338 inline void Destroy( signed long long* ) {}
339 inline void Destroy( signed long long*, signed long long* ) {}
340 
341 inline void Destroy( unsigned long long* ) {}
342 inline void Destroy( unsigned long long*, unsigned long long* ) {}
343 
344 inline void Destroy( float* ) {}
345 inline void Destroy( float*, float* ) {}
346 
347 inline void Destroy( double* ) {}
348 inline void Destroy( double*, double* ) {}
349 
350 inline void Destroy( long double* ) {}
351 inline void Destroy( long double*, long double* ) {}
352 
353 inline void Destroy( void** ) {}
354 inline void Destroy( void**, void** ) {}
355 
356 inline void Destroy( bool** ) {}
357 inline void Destroy( bool**, bool** ) {}
358 
359 inline void Destroy( signed char** ) {}
360 inline void Destroy( signed char**, signed char** ) {}
361 
362 inline void Destroy( unsigned char** ) {}
363 inline void Destroy( unsigned char**, unsigned char** ) {}
364 
365 inline void Destroy( wchar_t** ) {}
366 inline void Destroy( wchar_t**, wchar_t** ) {}
367 
368 inline void Destroy( signed int** ) {}
369 inline void Destroy( signed int**, signed int** ) {}
370 
371 inline void Destroy( unsigned int** ) {}
372 inline void Destroy( unsigned int**, unsigned int** ) {}
373 
374 inline void Destroy( signed short** ) {}
375 inline void Destroy( signed short**, signed short** ) {}
376 
377 inline void Destroy( unsigned short** ) {}
378 inline void Destroy( unsigned short**, unsigned short** ) {}
379 
380 inline void Destroy( signed long** ) {}
381 inline void Destroy( signed long**, signed long** ) {}
382 
383 inline void Destroy( unsigned long** ) {}
384 inline void Destroy( unsigned long**, unsigned long** ) {}
385 
386 inline void Destroy( signed long long** ) {}
387 inline void Destroy( signed long long**, signed long long** ) {}
388 
389 inline void Destroy( unsigned long long** ) {}
390 inline void Destroy( unsigned long long**, unsigned long long** ) {}
391 
392 inline void Destroy( float** ) {}
393 inline void Destroy( float**, float** ) {}
394 
395 inline void Destroy( double** ) {}
396 inline void Destroy( double**, double** ) {}
397 
398 inline void Destroy( long double** ) {}
399 inline void Destroy( long double**, long double** ) {}
400 
401 // ----------------------------------------------------------------------------
402 
403 } // pcl
404 
405 #endif // __PCL_Allocator_h
406 
407 // ----------------------------------------------------------------------------
408 // EOF pcl/Allocator.h - Released 2019-11-07T10:59:34Z
void Deallocate(T *p)
Definition: Allocator.h:176
PCL root namespace.
Definition: AbstractImage.h:76
void Construct(T *p, A &a)
Definition: Allocator.h:246
size_t size_type
Definition: Defs.h:543
Provides memory allocation for PCL containers.
Definition: Allocator.h:131
size_type ReallocatedLength(size_type currentLength, size_type newLength) const
Definition: Allocator.h:226
void Destroy(T *p)
Definition: Allocator.h:276
size_type MaxLength() const
Definition: Allocator.h:187
size_type PagedLength(size_type n) const
Definition: Allocator.h:205
Allocator(const A &a)
Definition: Allocator.h:148