PCL
Allocator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Allocator.h - Released 2024-06-18T15:48:54Z
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-2024 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 (https://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 )
149  : A( a )
150  {
151  }
152 
163  T* Allocate( size_type n, size_type extra = 0 )
164  {
165  PCL_PRECONDITION( n+extra > 0 )
166  return (T*)this->AllocateBlock( n*sizeof( T )+extra );
167  //return (T*)new ( *this ) uint8[ n*sizeof( T )+extra ];
168  }
169 
177  void Deallocate( T* p )
178  {
179  PCL_PRECONDITION( p != nullptr )
180  this->DeallocateBlock( (void*)p );
181  //this->operator delete( (void*)p );
182  }
183 
189  {
190  return A::MaxSize()/sizeof( T );
191  }
192 
207  {
208  return A::BlockSize( n*sizeof( T ) )/sizeof( T );
209  }
210 
227  size_type ReallocatedLength( size_type currentLength, size_type newLength ) const
228  {
229  return A::ReallocatedBlockSize( currentLength*sizeof( T ), newLength*sizeof( T ) )/sizeof( T );
230  }
231 };
232 
233 // ----------------------------------------------------------------------------
234 
247 template <class T, class A> inline void Construct( T* p, A& a )
248 {
249  PCL_PRECONDITION( p != nullptr )
250  new( (void*)p, a )T();
251 }
252 
260 template <class T, class T1, class A> inline void Construct( T* p, const T1& v, A& a )
261 {
262  PCL_PRECONDITION( p != nullptr )
263  new( (void*)p, a )T( v );
264 }
265 
266 #ifdef _MSC_VER
267 # pragma warning( push )
268 # pragma warning( disable : 4100 ) // unreferenced formal parameter
269 #endif // (VC++ limitation !?)
270 
277 template <class T> inline void Destroy( T* p )
278 {
279  PCL_PRECONDITION( p != nullptr )
280  p->~T();
281 }
282 
289 template <class T> inline void Destroy( T* p, T* q )
290 {
291  PCL_PRECONDITION( p != nullptr && q != nullptr )
292  for ( ; p < q; ++p )
293  p->~T();
294 }
295 
296 #ifdef _MSC_VER
297 # pragma warning( pop )
298 #endif
299 
300 inline void Destroy( void* ) {}
301 inline void Destroy( void*, void* ) {}
302 
303 inline void Destroy( bool* ) {}
304 inline void Destroy( bool*, bool* ) {}
305 
306 inline void Destroy( signed char* ) {}
307 inline void Destroy( signed char*, signed char* ) {}
308 
309 inline void Destroy( unsigned char* ) {}
310 inline void Destroy( unsigned char*, unsigned char* ) {}
311 
312 inline void Destroy( wchar_t* ) {}
313 inline void Destroy( wchar_t*, wchar_t* ) {}
314 
315 inline void Destroy( char16_t* ) {}
316 inline void Destroy( char16_t*, char16_t* ) {}
317 
318 inline void Destroy( char32_t* ) {}
319 inline void Destroy( char32_t*, char32_t* ) {}
320 
321 inline void Destroy( signed int* ) {}
322 inline void Destroy( signed int*, signed int* ) {}
323 
324 inline void Destroy( unsigned int* ) {}
325 inline void Destroy( unsigned int*, unsigned int* ) {}
326 
327 inline void Destroy( signed short* ) {}
328 inline void Destroy( signed short*, signed short* ) {}
329 
330 inline void Destroy( unsigned short* ) {}
331 inline void Destroy( unsigned short*, unsigned short* ) {}
332 
333 inline void Destroy( signed long* ) {}
334 inline void Destroy( signed long*, signed long* ) {}
335 
336 inline void Destroy( unsigned long* ) {}
337 inline void Destroy( unsigned long*, unsigned long* ) {}
338 
339 inline void Destroy( signed long long* ) {}
340 inline void Destroy( signed long long*, signed long long* ) {}
341 
342 inline void Destroy( unsigned long long* ) {}
343 inline void Destroy( unsigned long long*, unsigned long long* ) {}
344 
345 inline void Destroy( float* ) {}
346 inline void Destroy( float*, float* ) {}
347 
348 inline void Destroy( double* ) {}
349 inline void Destroy( double*, double* ) {}
350 
351 inline void Destroy( long double* ) {}
352 inline void Destroy( long double*, long double* ) {}
353 
354 inline void Destroy( void** ) {}
355 inline void Destroy( void**, void** ) {}
356 
357 inline void Destroy( bool** ) {}
358 inline void Destroy( bool**, bool** ) {}
359 
360 inline void Destroy( signed char** ) {}
361 inline void Destroy( signed char**, signed char** ) {}
362 
363 inline void Destroy( unsigned char** ) {}
364 inline void Destroy( unsigned char**, unsigned char** ) {}
365 
366 inline void Destroy( wchar_t** ) {}
367 inline void Destroy( wchar_t**, wchar_t** ) {}
368 
369 inline void Destroy( signed int** ) {}
370 inline void Destroy( signed int**, signed int** ) {}
371 
372 inline void Destroy( unsigned int** ) {}
373 inline void Destroy( unsigned int**, unsigned int** ) {}
374 
375 inline void Destroy( signed short** ) {}
376 inline void Destroy( signed short**, signed short** ) {}
377 
378 inline void Destroy( unsigned short** ) {}
379 inline void Destroy( unsigned short**, unsigned short** ) {}
380 
381 inline void Destroy( signed long** ) {}
382 inline void Destroy( signed long**, signed long** ) {}
383 
384 inline void Destroy( unsigned long** ) {}
385 inline void Destroy( unsigned long**, unsigned long** ) {}
386 
387 inline void Destroy( signed long long** ) {}
388 inline void Destroy( signed long long**, signed long long** ) {}
389 
390 inline void Destroy( unsigned long long** ) {}
391 inline void Destroy( unsigned long long**, unsigned long long** ) {}
392 
393 inline void Destroy( float** ) {}
394 inline void Destroy( float**, float** ) {}
395 
396 inline void Destroy( double** ) {}
397 inline void Destroy( double**, double** ) {}
398 
399 inline void Destroy( long double** ) {}
400 inline void Destroy( long double**, long double** ) {}
401 
402 // ----------------------------------------------------------------------------
403 
404 } // pcl
405 
406 #endif // __PCL_Allocator_h
407 
408 // ----------------------------------------------------------------------------
409 // EOF pcl/Allocator.h - Released 2024-06-18T15:48:54Z
Provides memory allocation for PCL containers.
Definition: Allocator.h:132
Allocator(const A &a)
Definition: Allocator.h:148
size_type MaxLength() const
Definition: Allocator.h:188
Allocator(const Allocator< T, A > &)=default
size_type ReallocatedLength(size_type currentLength, size_type newLength) const
Definition: Allocator.h:227
void Deallocate(T *p)
Definition: Allocator.h:177
size_type PagedLength(size_type n) const
Definition: Allocator.h:206
Allocator()=default
void Construct(T *p, A &a)
Definition: Allocator.h:247
void Destroy(T *p)
Definition: Allocator.h:277
size_t size_type
Definition: Defs.h:609
PCL root namespace.
Definition: AbstractImage.h:77