PCL
StdAlloc.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/StdAlloc.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_StdAlloc_h
53 #define __PCL_StdAlloc_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 ), m_canShrink( canShrink )
99  {
100  }
101 
105  StandardAllocator( const StandardAllocator& ) = default;
106 
117  {
118  return ~size_type( 0 );
119  }
120 
134  {
135  // Check for null allocation attempts.
136  if ( n == 0 )
137  return 0;
138 
139  // Take into account a reasonable upper limit for the size in bytes of an
140  // allocation block header.
141  const size_type blockHeaderSize = 4*sizeof( void* );
142  n += blockHeaderSize;
143 
144  // Grow linearly by 8-byte chunks for n < 64 bytes.
145  if ( n < 64 )
146  return ((n >> 3) << 3) + 8 - blockHeaderSize;
147 
148  // Grow exponentially by doubling container capacity if fast growing is
149  // enabled or n < 64 KiB.
150  if ( IsFastGrowthEnabled() || n < 65536 )
151  {
152  size_type nn = 64;
153  while ( nn < n )
154  nn <<= 1;
155  return nn - blockHeaderSize;
156  }
157 
158  // If fast growing is disabled or n >= 64 KiB, grow linearly by 4 KiB
159  // chunks.
160  return ((n >> 12) << 12) + 4096 - blockHeaderSize;
161  }
162 
178  size_type ReallocatedBlockSize( size_type currentSize, size_type newSize ) const
179  {
180  return (currentSize < newSize || m_canShrink) ? BlockSize( newSize ) : currentSize;
181  }
182 
197  {
198  PCL_PRECONDITION( sz != 0 )
199  return ::operator new( sz );
200  }
201 
214  void DeallocateBlock( void* p )
215  {
216  PCL_PRECONDITION( p != nullptr )
217  ::operator delete( p );
218  }
219 
241  bool IsFastGrowthEnabled() const
242  {
243  return m_fastGrowth;
244  }
245 
254  void EnableFastGrowth( bool enable = true )
255  {
256  m_fastGrowth = enable;
257  }
258 
267  void DisableFastGrowth( bool disable = true )
268  {
269  EnableFastGrowth( !disable );
270  }
271 
285  bool IsShrinkingEnabled() const
286  {
287  return m_canShrink;
288  }
289 
298  void EnableShrinking( bool enable = true )
299  {
300  m_canShrink = enable;
301  }
302 
311  void DisableShrinking( bool disable = true )
312  {
313  EnableShrinking( !disable );
314  }
315 
316 private:
317 
318  bool m_fastGrowth : 1;
319  bool m_canShrink : 1;
320 };
321 
322 } // pcl
323 
324 // ----------------------------------------------------------------------------
325 
326 /* ** !
327  Block allocation operator for class StandardAllocator. Allocates a
328  contiguous block of memory of length \a sz in bytes, using the
329  standard operator new( size_t ).
330  */
331 /*
332 inline void* operator new( pcl::size_type sz, pcl::StandardAllocator& )
333 {
334  PCL_PRECONDITION( sz != nullptr )
335  return ::operator new( sz );
336 }
337 
338 #ifdef _MSC_VER
339 
340 inline void operator delete( void* p, pcl::StandardAllocator& )
341 {
342  PCL_PRECONDITION( p != nullptr )
343  ::operator delete( p );
344 }
345 
346 #endif
347  */
348 
353 inline void* operator new( pcl::size_type, void* p, pcl::StandardAllocator& )
354 {
355  PCL_PRECONDITION( p != nullptr )
356  return p;
357 }
358 
359 #ifdef _MSC_VER
360 
361 #pragma warning( push )
362 #pragma warning( disable: 4100 ) // 'unreferenced formal parameter'
363 
364 inline void operator delete( void* p, void*, pcl::StandardAllocator& )
365 {
366  PCL_PRECONDITION( p != nullptr )
367 }
368 
369 #pragma warning( pop )
370 
371 #endif
372 
373 // ----------------------------------------------------------------------------
374 
375 #endif // __PCL_StdAlloc_h
376 
377 // ----------------------------------------------------------------------------
378 // EOF pcl/StdAlloc.h - Released 2019-11-07T10:59:34Z
PCL root namespace.
Definition: AbstractImage.h:76
void EnableShrinking(bool enable=true)
Definition: StdAlloc.h:298
void DeallocateBlock(void *p)
Definition: StdAlloc.h:214
size_type ReallocatedBlockSize(size_type currentSize, size_type newSize) const
Definition: StdAlloc.h:178
bool IsShrinkingEnabled() const
Definition: StdAlloc.h:285
size_t size_type
Definition: Defs.h:543
size_type MaxSize() const
Definition: StdAlloc.h:116
StandardAllocator(bool fastGrowth=true, bool canShrink=true)
Definition: StdAlloc.h:97
void DisableFastGrowth(bool disable=true)
Definition: StdAlloc.h:267
void * AllocateBlock(size_type sz)
Definition: StdAlloc.h:196
size_type BlockSize(size_type n) const
Definition: StdAlloc.h:133
A block allocator class that uses the standard new and delete operators.
Definition: StdAlloc.h:81
void DisableShrinking(bool disable=true)
Definition: StdAlloc.h:311
void EnableFastGrowth(bool enable=true)
Definition: StdAlloc.h:254
bool IsFastGrowthEnabled() const
Definition: StdAlloc.h:241