aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::SmallObjectAllocator Class Reference

<agrum/base/core/smallObjectAllocator.h> More...

#include <smallObjectAllocator.h>

Collaboration diagram for gum::SmallObjectAllocator:

Public Member Functions

void displayStats ()
 Displays the number of allocation and deallocation made so far.
Idx nbAlloc ()
Idx nbDealloc ()
Allocator / Deallocator
void * allocate (const size_t &objectSize)
 Allocates a block.
void deallocate (void *pDeallocatedObject, const size_t &objectSize)
 Deallocates an object.

Static Public Member Functions

static SmallObjectAllocatorinstance ()

Static Public Attributes

static const size_t GUM_DEFAULT_CHUNK_SIZE = 8096
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE = 512

Private Types

using _Pool_ = HashTable< Size, FixedAllocator* >
 The pool containing FixedAllocator.

Private Attributes

_Pool_ _pool_
std::size_t _chunkSize_
 The memory that a chunk allocates.
std::size_t _maxObjectSize_
 The maximal size of an object befor new is called.
Idx nbAllocation
Idx nbDeallocation

Constructors / Destructors

 SmallObjectAllocator ()
 Constructor.
 SmallObjectAllocator (const SmallObjectAllocator &)
 Copy Constructor (does nothing since we use a Singleton).
SmallObjectAllocatoroperator= (const SmallObjectAllocator &)
 Operator = (does nothing since we use a Singleton).
virtual ~SmallObjectAllocator ()
 Destructor.

Detailed Description

<agrum/base/core/smallObjectAllocator.h>

Allocates objects of any size

SmallObjectAllocator does so by aggregating several FixedAllocator objects. When SmallObjectAllocator receives an allocation request, it either forwards it to the best matching FixedAllocator object or passes it to the default operator new

Definition at line 76 of file smallObjectAllocator.h.

Member Typedef Documentation

◆ _Pool_

The pool containing FixedAllocator.

Definition at line 162 of file smallObjectAllocator.h.

Constructor & Destructor Documentation

◆ SmallObjectAllocator() [1/2]

INLINE gum::SmallObjectAllocator::SmallObjectAllocator ( )
private

Constructor.

Greater object than maxObjectSize will be forwarded to op new.

Definition at line 72 of file smallObjectAllocator_inl.h.

72 :
74 _pool_.setKeyUniquenessPolicy(false);
75 GUM_CONSTRUCTOR(SmallObjectAllocator);
76 nbAllocation = 0;
78
79 // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
80 // a HashTable that will not be deleted ...
81 // so we inform our leak detector not to count those 2 objects
82 GUM_DESTRUCTOR(SmallObjectAllocator);
83 GUM_DESTRUCTOR(HashTable);
84 }
static const size_t GUM_DEFAULT_CHUNK_SIZE
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE
std::size_t _maxObjectSize_
The maximal size of an object befor new is called.
std::size_t _chunkSize_
The memory that a chunk allocates.

References SmallObjectAllocator(), _chunkSize_, _maxObjectSize_, _pool_, GUM_DEFAULT_CHUNK_SIZE, GUM_DEFAULT_MAX_OBJECT_SIZE, nbAllocation, and nbDeallocation.

Referenced by SmallObjectAllocator(), SmallObjectAllocator(), ~SmallObjectAllocator(), instance(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SmallObjectAllocator() [2/2]

gum::SmallObjectAllocator::SmallObjectAllocator ( const SmallObjectAllocator & )
inlineprivate

Copy Constructor (does nothing since we use a Singleton).

Definition at line 110 of file smallObjectAllocator.h.

110{};

References SmallObjectAllocator().

Here is the call graph for this function:

◆ ~SmallObjectAllocator()

INLINE gum::SmallObjectAllocator::~SmallObjectAllocator ( )
virtual

Destructor.

Definition at line 89 of file smallObjectAllocator_inl.h.

89 {
90 GUM_DESTRUCTOR(SmallObjectAllocator);
91 for (_Pool_::iterator pit = _pool_.begin(); pit != _pool_.end(); ++pit)
92 delete pit.val();
93 }
HashTableIterator< Size, FixedAllocator * > iterator
Definition hashTable.h:650

References SmallObjectAllocator(), and _pool_.

Here is the call graph for this function:

Member Function Documentation

◆ allocate()

INLINE void * gum::SmallObjectAllocator::allocate ( const size_t & objectSize)

Allocates a block.

Definition at line 108 of file smallObjectAllocator_inl.h.

108 {
109 // Small Object Allocator called for an object of size equals to 0
110 GUM_ASSERT(objectSize > 0);
111
112 // If objectSize is greater than maxObjectSize, normal new is called
113 if (objectSize > _maxObjectSize_) return new unsigned char[objectSize];
114
115 void* ret;
116#pragma omp critical(soa)
117 {
118 //
119 if (!_pool_.exists(Size(objectSize))) {
120 // Calcul du nombre de block par chunk pour des objets de cette taille
121 std::size_t nb = _chunkSize_ / Size(objectSize);
122 if (nb > UCHAR_MAX) nb = UCHAR_MAX;
123 unsigned char numBlocks = static_cast< unsigned char >(nb);
124
125 FixedAllocator* newFa = new FixedAllocator(Size(objectSize), numBlocks);
126 _pool_.set(Size(objectSize), newFa);
127 }
128 nbAllocation++;
129
130 ret = _pool_[Size(objectSize)]->allocate();
131 }
132 return ret;
133 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _chunkSize_, _maxObjectSize_, _pool_, and nbAllocation.

Referenced by gum::AbstractLeaf::operator new(), gum::ActionSet::operator new(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator new(), gum::Chi2TestPolicy< GUM_SCALAR >::operator new(), gum::ComposedLeaf::operator new(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator new(), gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator new(), gum::FusionContext< isInitial >::operator new(), gum::GTestPolicy< GUM_SCALAR >::operator new(), gum::InternalNode::operator new(), gum::ITestPolicy< GUM_SCALAR >::operator new(), gum::LeafPair::operator new(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator new(), gum::Link< T >::operator new(), gum::LinkedList< T >::operator new(), gum::NodeDatabase< AttributeSelection, isScalar >::operator new(), gum::O4DGContext::operator new(), gum::Observation::operator new(), and gum::Parent::operator new().

Here is the caller graph for this function:

◆ deallocate()

INLINE void gum::SmallObjectAllocator::deallocate ( void * pDeallocatedObject,
const size_t & objectSize )

Deallocates an object.

Parameters
pDeallocatedObjectis the object to be deallocated
objectSizeis the size of that object (useful for faster deallocation)

Definition at line 141 of file smallObjectAllocator_inl.h.

141 {
142 // Small Object Allocator called for an object of size equals to 0
143 GUM_ASSERT(objectSize > 0);
144
145 // If objectSize is greater than maxObjectSize, normal new is called
146 if (objectSize > _maxObjectSize_) {
147 delete[] (unsigned char*)pDeallocatedObject;
148 return;
149 }
150
151#pragma omp critical(soa)
152 {
153 // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
154 _pool_[Size(objectSize)]->deallocate(pDeallocatedObject);
156 }
157 }

References _maxObjectSize_, _pool_, and nbDeallocation.

Referenced by gum::Link< NodeId >::Link(), gum::AbstractLeaf::operator delete(), gum::ActionSet::operator delete(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator delete(), gum::Chi2TestPolicy< GUM_SCALAR >::operator delete(), gum::ComposedLeaf::operator delete(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator delete(), gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator delete(), gum::FusionContext< isInitial >::operator delete(), gum::GTestPolicy< GUM_SCALAR >::operator delete(), gum::InternalNode::operator delete(), gum::ITestPolicy< GUM_SCALAR >::operator delete(), gum::LeafPair::operator delete(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete(), gum::LinkedList< T >::operator delete(), gum::NodeDatabase< AttributeSelection, isScalar >::operator delete(), gum::O4DGContext::operator delete(), gum::Observation::operator delete(), and gum::Parent::operator delete().

Here is the caller graph for this function:

◆ displayStats()

void gum::SmallObjectAllocator::displayStats ( )
inline

Displays the number of allocation and deallocation made so far.

Definition at line 149 of file smallObjectAllocator.h.

149 {
150 GUM_TRACE("Nb Small Allocation : " << nbAllocation
151 << " - Nb Small Deallocation : " << nbDeallocation);
152 }

References nbAllocation, and nbDeallocation.

◆ instance()

INLINE SmallObjectAllocator & gum::SmallObjectAllocator::instance ( )
static

Definition at line 95 of file smallObjectAllocator_inl.h.

95 {
96 static SmallObjectAllocator soa;
97
98 return soa;
99 }

References SmallObjectAllocator().

Referenced by gum::Link< NodeId >::Link(), gum::AbstractLeaf::operator delete(), gum::ActionSet::operator delete(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator delete(), gum::Chi2TestPolicy< GUM_SCALAR >::operator delete(), gum::ComposedLeaf::operator delete(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator delete(), gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator delete(), gum::FusionContext< isInitial >::operator delete(), gum::GTestPolicy< GUM_SCALAR >::operator delete(), gum::InternalNode::operator delete(), gum::ITestPolicy< GUM_SCALAR >::operator delete(), gum::LeafPair::operator delete(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete(), gum::LinkedList< T >::operator delete(), gum::NodeDatabase< AttributeSelection, isScalar >::operator delete(), gum::O4DGContext::operator delete(), gum::Observation::operator delete(), gum::Parent::operator delete(), gum::AbstractLeaf::operator new(), gum::ActionSet::operator new(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator new(), gum::Chi2TestPolicy< GUM_SCALAR >::operator new(), gum::ComposedLeaf::operator new(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator new(), gum::ContingencyTable< GUM_SCALAR_A, GUM_SCALAR_B >::operator new(), gum::FusionContext< isInitial >::operator new(), gum::GTestPolicy< GUM_SCALAR >::operator new(), gum::InternalNode::operator new(), gum::ITestPolicy< GUM_SCALAR >::operator new(), gum::LeafPair::operator new(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator new(), gum::Link< T >::operator new(), gum::LinkedList< T >::operator new(), gum::NodeDatabase< AttributeSelection, isScalar >::operator new(), gum::O4DGContext::operator new(), gum::Observation::operator new(), gum::Parent::operator new(), and operator=().

Here is the call graph for this function:

◆ nbAlloc()

Idx gum::SmallObjectAllocator::nbAlloc ( )
inline

Definition at line 154 of file smallObjectAllocator.h.

154{ return nbAllocation; }

References nbAllocation.

◆ nbDealloc()

Idx gum::SmallObjectAllocator::nbDealloc ( )
inline

Definition at line 156 of file smallObjectAllocator.h.

156{ return nbDeallocation; }

References nbDeallocation.

◆ operator=()

SmallObjectAllocator & gum::SmallObjectAllocator::operator= ( const SmallObjectAllocator & )
inlineprivate

Operator = (does nothing since we use a Singleton).

Definition at line 115 of file smallObjectAllocator.h.

115{ return instance(); }
static SmallObjectAllocator & instance()

References SmallObjectAllocator(), and instance().

Here is the call graph for this function:

Member Data Documentation

◆ _chunkSize_

std::size_t gum::SmallObjectAllocator::_chunkSize_
private

The memory that a chunk allocates.

Definition at line 169 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator(), and allocate().

◆ _maxObjectSize_

std::size_t gum::SmallObjectAllocator::_maxObjectSize_
private

The maximal size of an object befor new is called.

Definition at line 174 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator(), allocate(), and deallocate().

◆ _pool_

_Pool_ gum::SmallObjectAllocator::_pool_
private

◆ GUM_DEFAULT_CHUNK_SIZE

const size_t gum::SmallObjectAllocator::GUM_DEFAULT_CHUNK_SIZE = 8096
static
Parameters
Thedefault size of chunck of memory. These chuncks are pre-allocated memory space which are then split in small memory space of the size of a small object

Definition at line 83 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator().

◆ GUM_DEFAULT_MAX_OBJECT_SIZE

const size_t gum::SmallObjectAllocator::GUM_DEFAULT_MAX_OBJECT_SIZE = 512
static
Parameters
Thedefault maximal size under which an object is considered small. If an object size is over this limit, the normal new allocator is called.

Definition at line 90 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator().

◆ nbAllocation

Idx gum::SmallObjectAllocator::nbAllocation
private

Definition at line 176 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator(), allocate(), displayStats(), and nbAlloc().

◆ nbDeallocation

Idx gum::SmallObjectAllocator::nbDeallocation
private

Definition at line 177 of file smallObjectAllocator.h.

Referenced by SmallObjectAllocator(), deallocate(), displayStats(), and nbDealloc().


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