aGrUM  0.16.0
gum::SmallObjectAllocator Class Reference

<agrum/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. More...
 
Idx nbAlloc ()
 
Idx nbDealloc ()
 
Allocator / Deallocator
void * allocate (const size_t &objectSize)
 Allocates a block. More...
 
void deallocate (void *pDeallocatedObject, const size_t &objectSize)
 Deallocates an object. More...
 

Static Public Attributes

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

Static Public Member Functions

static SmallObjectAllocatorinstance ()
 

Constructors / Destructors

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

Detailed Description

<agrum/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 55 of file smallObjectAllocator.h.

Member Typedef Documentation

◆ __Pool

The pool containing FixedAllocator.

Definition at line 142 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 50 of file smallObjectAllocator_inl.h.

References __pool, nbAllocation, nbDeallocation, and gum::HashTable< Key, Val, Alloc >::setKeyUniquenessPolicy().

50  :
54  GUM_CONSTRUCTOR(SmallObjectAllocator);
55  nbAllocation = 0;
56  nbDeallocation = 0;
57 
58  // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
59  // a HashTable that will not be deleted ...
60  // so we inform our leak detector not to count those 2 objects
61  GUM_DESTRUCTOR(SmallObjectAllocator);
62  GUM_DESTRUCTOR(HashTable);
63  }
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE
static const size_t GUM_DEFAULT_CHUNK_SIZE
std::size_t __maxObjectSize
The maximal size of an object befor new is called.
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
std::size_t __chunkSize
The memory that a chunk allocates.
+ Here is the call 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 88 of file smallObjectAllocator.h.

88 {};

◆ ~SmallObjectAllocator()

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

Destructor.

Definition at line 68 of file smallObjectAllocator_inl.h.

References __pool, gum::HashTable< Key, Val, Alloc >::begin(), and gum::HashTable< Key, Val, Alloc >::end().

Referenced by operator=().

68  {
69  GUM_DESTRUCTOR(SmallObjectAllocator);
70  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
71  delete pit.val();
72  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
HashTableIterator< Key, Val > iterator
Types for STL compliance.
Definition: hashTable.h:693
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 87 of file smallObjectAllocator_inl.h.

References __chunkSize, __maxObjectSize, __pool, gum::HashTable< Key, Val, Alloc >::exists(), nbAllocation, and gum::HashTable< Key, Val, Alloc >::set().

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::operator new(), gum::O4DGContext::operator new(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator new(), gum::ComposedLeaf::operator new(), gum::Parent::operator new(), gum::ITestPolicy< GUM_SCALAR >::operator new(), gum::LeafPair::operator new(), gum::Observation::operator new(), gum::GTestPolicy< GUM_SCALAR >::operator new(), gum::AbstractLeaf::operator new(), gum::ContingencyTable< Idx, GUM_SCALAR >::operator new(), gum::FusionContext< true >::operator new(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator new(), gum::NodeDatabase< AttributeSelection, isScalar >::operator new(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator new(), gum::ActionSet::operator new(), gum::InternalNode::operator new(), and operator=().

87  {
88  // Small Object Allocator called for an object of size equals to 0
89  GUM_ASSERT(objectSize > 0);
90 
91  // If objectSize is greater than maxObjectSize, normal new is called
92  if (objectSize > __maxObjectSize) return new unsigned char[objectSize];
93 
94  void* ret;
95 #pragma omp critical(soa)
96  {
97  //
98  if (!__pool.exists(Size(objectSize))) {
99  // Calcul du nombre de block par chunk pour des objets de cette taille
100  std::size_t nb = __chunkSize / Size(objectSize);
101  if (nb > UCHAR_MAX) nb = UCHAR_MAX;
102  unsigned char numBlocks = static_cast< unsigned char >(nb);
103 
104  FixedAllocator* newFa = new FixedAllocator(Size(objectSize), numBlocks);
105  __pool.set(Size(objectSize), newFa);
106  }
107  nbAllocation++;
108 
109  ret = __pool[Size(objectSize)]->allocate();
110  }
111  return ret;
112  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
std::size_t __maxObjectSize
The maximal size of an object befor new is called.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
std::size_t __chunkSize
The memory that a chunk allocates.
+ Here is the call graph for this function:
+ 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 120 of file smallObjectAllocator_inl.h.

References __maxObjectSize, __pool, and nbDeallocation.

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::operator delete(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete(), gum::ComposedLeaf::operator delete(), gum::O4DGContext::operator delete(), gum::ITestPolicy< GUM_SCALAR >::operator delete(), gum::LeafPair::operator delete(), gum::Observation::operator delete(), gum::Parent::operator delete(), gum::AbstractLeaf::operator delete(), gum::GTestPolicy< GUM_SCALAR >::operator delete(), gum::ContingencyTable< Idx, GUM_SCALAR >::operator delete(), gum::FusionContext< true >::operator delete(), gum::ConcreteLeaf< AttributeSelection, isScalar >::operator delete(), gum::NodeDatabase< AttributeSelection, isScalar >::operator delete(), gum::ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >::operator delete(), gum::ActionSet::operator delete(), gum::InternalNode::operator delete(), and operator=().

121  {
122  // Small Object Allocator called for an object of size equals to 0
123  GUM_ASSERT(objectSize > 0);
124 
125  // If objectSize is greater than maxObjectSize, normal new is called
126  if (objectSize > __maxObjectSize) {
127  delete[](unsigned char*) pDeallocatedObject;
128  return;
129  }
130 
131 #pragma omp critical(soa)
132  {
133  // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
134  __pool[Size(objectSize)]->deallocate(pDeallocatedObject);
135  nbDeallocation++;
136  }
137  }
std::size_t __maxObjectSize
The maximal size of an object befor new is called.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ 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 129 of file smallObjectAllocator.h.

References nbAllocation, and nbDeallocation.

129  {
130  GUM_TRACE("Nb Small Allocation : " << nbAllocation
131  << " - Nb Small Deallocation : "
132  << nbDeallocation);
133  }

◆ instance()

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

Definition at line 74 of file smallObjectAllocator_inl.h.

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

74  {
75  static SmallObjectAllocator soa;
76 
77  return soa;
78  }

◆ nbAlloc()

Idx gum::SmallObjectAllocator::nbAlloc ( )
inline

Definition at line 135 of file smallObjectAllocator.h.

References nbAllocation.

◆ nbDealloc()

Idx gum::SmallObjectAllocator::nbDealloc ( )
inline

Definition at line 136 of file smallObjectAllocator.h.

References nbDeallocation.

◆ operator=()

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

Operator = (does nothing since we use a Singleton)

Definition at line 93 of file smallObjectAllocator.h.

References allocate(), deallocate(), instance(), and ~SmallObjectAllocator().

93  {
94  return instance();
95  }
static SmallObjectAllocator & 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 148 of file smallObjectAllocator.h.

Referenced by allocate().

◆ __maxObjectSize

std::size_t gum::SmallObjectAllocator::__maxObjectSize
private

The maximal size of an object befor new is called.

Definition at line 153 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 62 of file smallObjectAllocator.h.

◆ 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 69 of file smallObjectAllocator.h.

◆ nbAllocation

Idx gum::SmallObjectAllocator::nbAllocation
private

Definition at line 155 of file smallObjectAllocator.h.

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

◆ nbDeallocation

Idx gum::SmallObjectAllocator::nbDeallocation
private

Definition at line 156 of file smallObjectAllocator.h.

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


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