aGrUM 3.1.1
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.
std::mutex _mutex_
Idx nbAllocation
Idx nbDeallocation

Constructors / Destructors

 SmallObjectAllocator ()
 Constructor.
 SmallObjectAllocator (const SmallObjectAllocator &)=delete
 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 159 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 73 of file smallObjectAllocator_inl.h.

73 :
75 _pool_.setKeyUniquenessPolicy(false);
76 GUM_CONSTRUCTOR(SmallObjectAllocator);
77 nbAllocation = 0;
79
80 // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
81 // a HashTable that will not be deleted ...
82 // so we inform our leak detector not to count those 2 objects
83 GUM_DESTRUCTOR(SmallObjectAllocator);
84 GUM_DESTRUCTOR(HashTable);
85 }
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 & )
privatedelete

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

References SmallObjectAllocator().

Here is the call graph for this function:

◆ ~SmallObjectAllocator()

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

Destructor.

Definition at line 90 of file smallObjectAllocator_inl.h.

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

References SmallObjectAllocator(), and _pool_.

Here is the call graph for this function:

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 72 of file smallObjectAllocator.cpp.

72 {
73 // Small Object Allocator called for an object of size equals to 0
74 GUM_ASSERT(objectSize > 0);
75
76 std::lock_guard< std::mutex > lock(_mutex_);
77
78 // If objectSize is greater than maxObjectSize, normal new is called
79 if (objectSize > _maxObjectSize_) return new unsigned char[objectSize];
80
81 //
82 if (!_pool_.exists(Size(objectSize))) {
83 // Calcul du nombre de block par chunk pour des objets de cette taille
84 std::size_t nb = _chunkSize_ / Size(objectSize);
85 if (nb > UCHAR_MAX) nb = UCHAR_MAX;
86 auto numBlocks = static_cast< unsigned char >(nb);
87
88 auto* newFa = new FixedAllocator(Size(objectSize), numBlocks);
89 _pool_.set(Size(objectSize), newFa);
90 }
92
93 return _pool_[Size(objectSize)]->allocate();
94 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _chunkSize_, _maxObjectSize_, _mutex_, _pool_, and nbAllocation.

Referenced by gum::ContingencyTable< Idx, GUM_ELEMENT >::attrAEndSafe(), gum::FusionContext< false >::leaf(), gum::AbstractLeaf::operator new(), gum::ActionSet::operator new(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator new(), gum::Chi2TestPolicy< GUM_ELEMENT >::operator new(), gum::ComposedLeaf::operator new(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator new(), gum::GTestPolicy< GUM_ELEMENT >::operator new(), gum::InternalNode::operator new(), gum::ITestPolicy< GUM_ELEMENT >::operator new(), gum::LeafPair::operator new(), gum::LeastSquareTestPolicy< GUM_ELEMENT >::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 116 of file smallObjectAllocator_inl.h.

116 {
117 // Small Object Allocator called for an object of size equals to 0
118 GUM_ASSERT(objectSize > 0);
119
120 std::lock_guard< std::mutex > lock(_mutex_);
121
122 // If objectSize is greater than maxObjectSize, normal new is called
123 if (objectSize > _maxObjectSize_) {
124 delete[] (unsigned char*)pDeallocatedObject;
125 return;
126 }
127
128 // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
129 _pool_[Size(objectSize)]->deallocate(pDeallocatedObject);
131 }

References _maxObjectSize_, _mutex_, _pool_, and nbDeallocation.

Referenced by gum::Link< NodeId >::Link(), gum::FusionContext< false >::leafAssociatedPair(), gum::AbstractLeaf::operator delete(), gum::ActionSet::operator delete(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator delete(), gum::Chi2TestPolicy< GUM_ELEMENT >::operator delete(), gum::ComposedLeaf::operator delete(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator delete(), gum::GTestPolicy< GUM_ELEMENT >::operator delete(), gum::InternalNode::operator delete(), gum::ITestPolicy< GUM_ELEMENT >::operator delete(), gum::LeafPair::operator delete(), gum::LeastSquareTestPolicy< GUM_ELEMENT >::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()

INLINE void gum::SmallObjectAllocator::displayStats ( )

Displays the number of allocation and deallocation made so far.

Definition at line 137 of file smallObjectAllocator_inl.h.

137 {
138 GUM_TRACE("Nb Small Allocation : " << nbAllocation
139 << " - Nb Small Deallocation : " << nbDeallocation);
140 }

References nbAllocation, and nbDeallocation.

◆ instance()

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

Definition at line 96 of file smallObjectAllocator_inl.h.

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

References SmallObjectAllocator().

Referenced by gum::Link< NodeId >::Link(), gum::ContingencyTable< Idx, GUM_ELEMENT >::attrAEndSafe(), gum::FusionContext< false >::leaf(), gum::FusionContext< false >::leafAssociatedPair(), gum::AbstractLeaf::operator delete(), gum::ActionSet::operator delete(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator delete(), gum::Chi2TestPolicy< GUM_ELEMENT >::operator delete(), gum::ComposedLeaf::operator delete(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator delete(), gum::GTestPolicy< GUM_ELEMENT >::operator delete(), gum::InternalNode::operator delete(), gum::ITestPolicy< GUM_ELEMENT >::operator delete(), gum::LeafPair::operator delete(), gum::LeastSquareTestPolicy< GUM_ELEMENT >::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_ELEMENT >::operator new(), gum::ComposedLeaf::operator new(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator new(), gum::GTestPolicy< GUM_ELEMENT >::operator new(), gum::InternalNode::operator new(), gum::ITestPolicy< GUM_ELEMENT >::operator new(), gum::LeafPair::operator new(), gum::LeastSquareTestPolicy< GUM_ELEMENT >::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()

INLINE Idx gum::SmallObjectAllocator::nbAlloc ( )

Definition at line 142 of file smallObjectAllocator_inl.h.

142{ return nbAllocation; }

References nbAllocation.

◆ nbDealloc()

INLINE Idx gum::SmallObjectAllocator::nbDealloc ( )

Definition at line 144 of file smallObjectAllocator_inl.h.

144{ return nbDeallocation; }

References nbDeallocation.

◆ operator=()

INLINE SmallObjectAllocator & gum::SmallObjectAllocator::operator= ( const SmallObjectAllocator & )
private

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

Definition at line 133 of file smallObjectAllocator_inl.h.

133 {
134 return instance();
135 }
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 166 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 171 of file smallObjectAllocator.h.

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

◆ _mutex_

std::mutex gum::SmallObjectAllocator::_mutex_
private

Definition at line 174 of file smallObjectAllocator.h.

Referenced by 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: