PCL
StandardAllocator.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/StandardAllocator.h - Released 2024-01-13T15:47:58Z
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_StandardAllocator_h
53 #define __PCL_StandardAllocator_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <new>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
81 class PCL_CLASS StandardAllocator
82 {
83 public:
84 
97  StandardAllocator( bool fastGrowth = true, bool canShrink = true )
98  : m_fastGrowth( fastGrowth )
99  , m_canShrink( canShrink )
100  {
101  }
102 
106  StandardAllocator( const StandardAllocator& ) = default;
107 
119  {
120  return ~size_type( 0 );
121  }
122 
136  {
137  // Check for null allocation attempts.
138  if ( n == 0 )
139  return 0;
140 
141  // Take into account a reasonable upper limit for the size in bytes of an
142  // allocation block header.
143  const size_type blockHeaderSize = 4*sizeof( void* );
144  n += blockHeaderSize;
145 
146  // Grow linearly by 8-byte chunks for n < 64 bytes.
147  if ( n < 64 )
148  return ((n >> 3) << 3) + 8 - blockHeaderSize;
149 
150  // Grow exponentially by doubling container capacity if fast growing is
151  // enabled or n < 64 KiB.
152  if ( IsFastGrowthEnabled() || n < 65536 )
153  {
154  size_type nn = 64;
155  while ( nn < n )
156  nn <<= 1;
157  return nn - blockHeaderSize;
158  }
159 
160  // If fast growing is disabled or n >= 64 KiB, grow linearly by 4 KiB
161  // chunks.
162  return ((n >> 12) << 12) + 4096 - blockHeaderSize;
163  }
164 
180  size_type ReallocatedBlockSize( size_type currentSize, size_type newSize ) const
181  {
182  return (currentSize < newSize || m_canShrink) ? BlockSize( newSize ) : currentSize;
183  }
184 
195  void* AllocateBlock( size_type size )
196  {
197  PCL_PRECONDITION( size != 0 )
198  return ::operator new( size );
199  }
200 
210  void DeallocateBlock( void* p )
211  {
212  PCL_PRECONDITION( p != nullptr )
213  ::operator delete( p );
214  }
215 
237  bool IsFastGrowthEnabled() const
238  {
239  return m_fastGrowth;
240  }
241 
250  void EnableFastGrowth( bool enable = true )
251  {
252  m_fastGrowth = enable;
253  }
254 
263  void DisableFastGrowth( bool disable = true )
264  {
265  EnableFastGrowth( !disable );
266  }
267 
281  bool IsShrinkingEnabled() const
282  {
283  return m_canShrink;
284  }
285 
294  void EnableShrinking( bool enable = true )
295  {
296  m_canShrink = enable;
297  }
298 
307  void DisableShrinking( bool disable = true )
308  {
309  EnableShrinking( !disable );
310  }
311 
312 private:
313 
314  bool m_fastGrowth : 1;
315  bool m_canShrink : 1;
316 };
317 
318 } // pcl
319 
320 // ----------------------------------------------------------------------------
321 
326 inline void* operator new( pcl::size_type, void* p, pcl::StandardAllocator& )
327 {
328  PCL_PRECONDITION( p != nullptr )
329  return p;
330 }
331 
332 #ifdef _MSC_VER
333 #pragma warning( push )
334 #pragma warning( disable: 4100 ) // 'unreferenced formal parameter'
335 
336 inline void operator delete( void* p, void*, pcl::StandardAllocator& )
337 {
338  PCL_PRECONDITION( p != nullptr )
339 }
340 
341 #pragma warning( pop )
342 #endif // _MSC_VER
343 
344 // ----------------------------------------------------------------------------
345 
346 #endif // __PCL_StandardAllocator_h
347 
348 // ----------------------------------------------------------------------------
349 // EOF pcl/StandardAllocator.h - Released 2024-01-13T15:47:58Z
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::StandardAllocator::DisableFastGrowth
void DisableFastGrowth(bool disable=true)
Definition: StandardAllocator.h:263
pcl::StandardAllocator::IsFastGrowthEnabled
bool IsFastGrowthEnabled() const
Definition: StandardAllocator.h:237
pcl::StandardAllocator::MaxSize
size_type MaxSize() const
Definition: StandardAllocator.h:118
pcl::StandardAllocator::StandardAllocator
StandardAllocator(bool fastGrowth=true, bool canShrink=true)
Definition: StandardAllocator.h:97
pcl::size_type
size_t size_type
Definition: Defs.h:612
pcl::StandardAllocator::DisableShrinking
void DisableShrinking(bool disable=true)
Definition: StandardAllocator.h:307
pcl::StandardAllocator::BlockSize
size_type BlockSize(size_type n) const
Definition: StandardAllocator.h:135
pcl::StandardAllocator
A block allocator class that uses the standard new and delete operators.
Definition: StandardAllocator.h:81
pcl::StandardAllocator::AllocateBlock
void * AllocateBlock(size_type size)
Definition: StandardAllocator.h:195
pcl::StandardAllocator::EnableFastGrowth
void EnableFastGrowth(bool enable=true)
Definition: StandardAllocator.h:250
pcl::StandardAllocator::ReallocatedBlockSize
size_type ReallocatedBlockSize(size_type currentSize, size_type newSize) const
Definition: StandardAllocator.h:180
pcl::StandardAllocator::EnableShrinking
void EnableShrinking(bool enable=true)
Definition: StandardAllocator.h:294
Defs.h
pcl::StandardAllocator::DeallocateBlock
void DeallocateBlock(void *p)
Definition: StandardAllocator.h:210
pcl::StandardAllocator::IsShrinkingEnabled
bool IsShrinkingEnabled() const
Definition: StandardAllocator.h:281