aGrUM  0.14.1
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 53 of file smallObjectAllocator.h.

Member Typedef Documentation

◆ __Pool

The pool containing FixedAllocator.

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

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

47  :
51  GUM_CONSTRUCTOR(SmallObjectAllocator);
52  nbAllocation = 0;
53  nbDeallocation = 0;
54 
55  // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
56  // a HashTable that will not be deleted ...
57  // so we inform our leak detector not to count those 2 objects
58  GUM_DESTRUCTOR(SmallObjectAllocator);
59  GUM_DESTRUCTOR(HashTable);
60  }
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 86 of file smallObjectAllocator.h.

86 {};

◆ ~SmallObjectAllocator()

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

Destructor.

Definition at line 65 of file smallObjectAllocator_inl.h.

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

Referenced by operator=().

65  {
66  GUM_DESTRUCTOR(SmallObjectAllocator);
67  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
68  delete pit.val();
69  }
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:690
+ 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 84 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::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=().

84  {
85  // Small Object Allocator called for an object of size equals to 0
86  GUM_ASSERT(objectSize > 0);
87 
88  // If objectSize is greater than maxObjectSize, normal new is called
89  if (objectSize > __maxObjectSize) return new unsigned char[objectSize];
90 
91  void* ret;
92 #pragma omp critical(soa)
93  {
94  //
95  if (!__pool.exists(Size(objectSize))) {
96  // Calcul du nombre de block par chunk pour des objets de cette taille
97  std::size_t nb = __chunkSize / Size(objectSize);
98  if (nb > UCHAR_MAX) nb = UCHAR_MAX;
99  unsigned char numBlocks = static_cast< unsigned char >(nb);
100 
101  FixedAllocator* newFa = new FixedAllocator(Size(objectSize), numBlocks);
102  __pool.set(Size(objectSize), newFa);
103  }
104  nbAllocation++;
105 
106  ret = __pool[Size(objectSize)]->allocate();
107  }
108  return ret;
109  }
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:45
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 117 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=().

118  {
119  // Small Object Allocator called for an object of size equals to 0
120  GUM_ASSERT(objectSize > 0);
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 #pragma omp critical(soa)
129  {
130  // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
131  __pool[Size(objectSize)]->deallocate(pDeallocatedObject);
132  nbDeallocation++;
133  }
134  }
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:45
+ 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 127 of file smallObjectAllocator.h.

References nbAllocation, and nbDeallocation.

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

◆ instance()

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

Definition at line 71 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::AbstractLeaf::operator delete(), gum::Parent::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::LeafPair::operator new(), gum::Observation::operator new(), gum::Parent::operator new(), gum::AbstractLeaf::operator new(), gum::GTestPolicy< GUM_SCALAR >::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=().

71  {
72  static SmallObjectAllocator soa;
73 
74  return soa;
75  }

◆ nbAlloc()

Idx gum::SmallObjectAllocator::nbAlloc ( )
inline

Definition at line 133 of file smallObjectAllocator.h.

References nbAllocation.

◆ nbDealloc()

Idx gum::SmallObjectAllocator::nbDealloc ( )
inline

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

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

91  {
92  return instance();
93  }
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 146 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 151 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 60 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 67 of file smallObjectAllocator.h.

◆ nbAllocation

Idx gum::SmallObjectAllocator::nbAllocation
private

Definition at line 153 of file smallObjectAllocator.h.

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

◆ nbDeallocation

Idx gum::SmallObjectAllocator::nbDeallocation
private

Definition at line 154 of file smallObjectAllocator.h.

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


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