Open CASCADE Technology 7.8.2.dev
NCollection_IncAllocator Class Reference

#include <NCollection_IncAllocator.hxx>

Inheritance diagram for NCollection_IncAllocator:

Data Structures

struct  IBlock
 Forward list to keep multi-time allocated pointers. On Reset operation objects will be reused. More...
 

Public Types

enum class  IBlockSizeLevel : unsigned short {
  Min = 0 , Small , Medium , Large ,
  Max
}
 Description ability to next growing size each 5-th new block. More...
 
- Public Types inherited from Standard_Transient
typedef void base_type
 Returns a type descriptor about this object.
 

Public Member Functions

 NCollection_IncAllocator (const size_t theBlockSize=THE_DEFAULT_BLOCK_SIZE)
 Constructor. Note that this constructor does NOT setup mutex for using allocator concurrently from different threads, see SetThreadSafe() method.
 
void SetThreadSafe (const bool theIsThreadSafe=true)
 Setup mutex for thread-safe allocations.
 
void * Allocate (const size_t size) override
 Allocate memory with given size. Returns NULL on failure.
 
void * AllocateOptimal (const size_t size) override
 Allocate memory with given size. Returns NULL on failure.
 
void Free (void *) override
 Free a previously allocated memory. Does nothing.
 
 ~NCollection_IncAllocator ()
 Destructor (calls Clean() internally)
 
void Reset (const bool theReleaseMemory=false)
 Re-initialize the allocator so that the next Allocate call should start allocating in the very beginning as though the allocator is just constructed. Warning: make sure that all previously allocated data are no more used in your code!
 
- Public Member Functions inherited from NCollection_BaseAllocator
- Public Member Functions inherited from Standard_Transient
 Standard_Transient ()
 Empty constructor.
 
 Standard_Transient (const Standard_Transient &)
 Copy constructor – does nothing.
 
Standard_Transientoperator= (const Standard_Transient &)
 Assignment operator, needed to avoid copying reference counter.
 
virtual ~Standard_Transient ()
 Destructor must be virtual.
 
virtual const opencascade::handle< Standard_Type > & DynamicType () const
 Returns a type descriptor about this object.
 
Standard_Boolean IsInstance (const opencascade::handle< Standard_Type > &theType) const
 Returns a true value if this is an instance of Type.
 
Standard_Boolean IsInstance (const Standard_CString theTypeName) const
 Returns a true value if this is an instance of TypeName.
 
Standard_Boolean IsKind (const opencascade::handle< Standard_Type > &theType) const
 Returns true if this is an instance of Type or an instance of any class that inherits from Type. Note that multiple inheritance is not supported by OCCT RTTI mechanism.
 
Standard_Boolean IsKind (const Standard_CString theTypeName) const
 Returns true if this is an instance of TypeName or an instance of any class that inherits from TypeName. Note that multiple inheritance is not supported by OCCT RTTI mechanism.
 
Standard_TransientThis () const
 Returns non-const pointer to this object (like const_cast). For protection against creating handle to objects allocated in stack or call from constructor, it will raise exception Standard_ProgramError if reference counter is zero.
 
Standard_Integer GetRefCount () const noexcept
 Get the reference counter of this object.
 
void IncrementRefCounter () noexcept
 Increments the reference counter of this object.
 
Standard_Integer DecrementRefCounter () noexcept
 Decrements the reference counter of this object; returns the decremented value.
 
virtual void Delete () const
 Memory deallocator for transient classes.
 

Static Public Attributes

static constexpr size_t THE_DEFAULT_BLOCK_SIZE = 1024 * 12
 
static constexpr size_t THE_MINIMUM_BLOCK_SIZE = 1024 * 2
 

Protected Member Functions

void increaseBlockSize ()
 Increases size according current block size level.
 
void resetBlock (IBlock *theBlock) const
 Resets available size and CurPointer field.
 
void clean ()
 Flush all previously allocated data. All pointers returned by Allocate() become invalid – be very careful with this.
 
- Protected Member Functions inherited from NCollection_BaseAllocator
 NCollection_BaseAllocator ()
 Constructor - prohibited.
 

Additional Inherited Members

- Static Public Member Functions inherited from NCollection_BaseAllocator
static const Handle< NCollection_BaseAllocator > & CommonBaseAllocator (void)
 CommonBaseAllocator This method is designed to have the only one BaseAllocator (to avoid useless copying of collections). However one can use operator new to create more BaseAllocators, but it is injurious.
 
- Static Public Member Functions inherited from Standard_Transient
static constexpr const char * get_type_name ()
 Returns a type descriptor about this object.
 
static const opencascade::handle< Standard_Type > & get_type_descriptor ()
 Returns type descriptor of Standard_Transient class.
 

Detailed Description

Class NCollection_IncAllocator - incremental memory allocator. This class allocates memory on request returning the pointer to an allocated block. This memory is never returned to the system until the allocator is destroyed.

By comparison with the standard new() and malloc() calls, this method is faster and consumes very small additional memory to maintain the heap.

All pointers returned by Allocate() are aligned to the size of the data type "aligned_t". To modify the size of memory blocks requested from the OS, use the parameter of the constructor (measured in bytes); if this parameter is smaller than 25 bytes on 32bit or 49 bytes on 64bit, the block size will be the default 12 kbytes.

It is not recommended to use memory blocks larger than 16KB on Windows platform for the repeated operations because Low Fragmentation Heap is not going to be used for these allocations which may lead to memory fragmentation and the general performance slow down.

Note that this allocator is most suitable for single-threaded algorithms (consider creating dedicated allocators per working thread), and thread-safety of allocations is DISABLED by default (see SetThreadSafe()).

Member Enumeration Documentation

◆ IBlockSizeLevel

enum class NCollection_IncAllocator::IBlockSizeLevel : unsigned short
strong

Description ability to next growing size each 5-th new block.

Enumerator
Min 
Small 
Medium 
Large 
Max 

Constructor & Destructor Documentation

◆ NCollection_IncAllocator()

NCollection_IncAllocator::NCollection_IncAllocator ( const size_t theBlockSize = THE_DEFAULT_BLOCK_SIZE)

Constructor. Note that this constructor does NOT setup mutex for using allocator concurrently from different threads, see SetThreadSafe() method.

The default size of the memory blocks is 12KB. It is not recommended to use memory blocks larger than 16KB on Windows platform for the repeated operations (and thus multiple allocations) because Low Fragmentation Heap is not going to be used for these allocations, leading to memory fragmentation and eventual performance slow down.

◆ ~NCollection_IncAllocator()

NCollection_IncAllocator::~NCollection_IncAllocator ( )

Destructor (calls Clean() internally)

Member Function Documentation

◆ Allocate()

void * NCollection_IncAllocator::Allocate ( const size_t size)
overridevirtual

Allocate memory with given size. Returns NULL on failure.

Reimplemented from NCollection_BaseAllocator.

◆ AllocateOptimal()

void * NCollection_IncAllocator::AllocateOptimal ( const size_t size)
overridevirtual

Allocate memory with given size. Returns NULL on failure.

Reimplemented from NCollection_BaseAllocator.

◆ clean()

void NCollection_IncAllocator::clean ( )
protected

Flush all previously allocated data. All pointers returned by Allocate() become invalid – be very careful with this.

◆ Free()

void NCollection_IncAllocator::Free ( void * )
inlineoverridevirtual

Free a previously allocated memory. Does nothing.

Reimplemented from NCollection_BaseAllocator.

◆ increaseBlockSize()

void NCollection_IncAllocator::increaseBlockSize ( )
protected

Increases size according current block size level.

◆ Reset()

void NCollection_IncAllocator::Reset ( const bool theReleaseMemory = false)

Re-initialize the allocator so that the next Allocate call should start allocating in the very beginning as though the allocator is just constructed. Warning: make sure that all previously allocated data are no more used in your code!

Parameters
theReleaseMemoryTrue - release all previously allocated memory, False - preserve it for future allocations.

◆ resetBlock()

void NCollection_IncAllocator::resetBlock ( IBlock * theBlock) const
protected

Resets available size and CurPointer field.

◆ SetThreadSafe()

void NCollection_IncAllocator::SetThreadSafe ( const bool theIsThreadSafe = true)

Setup mutex for thread-safe allocations.

Field Documentation

◆ THE_DEFAULT_BLOCK_SIZE

size_t NCollection_IncAllocator::THE_DEFAULT_BLOCK_SIZE = 1024 * 12
staticconstexpr

◆ THE_MINIMUM_BLOCK_SIZE

size_t NCollection_IncAllocator::THE_MINIMUM_BLOCK_SIZE = 1024 * 2
staticconstexpr

The documentation for this class was generated from the following file: