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