aGrUM  0.13.2
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

The pool containing FixedAllocator.

Definition at line 140 of file smallObjectAllocator.h.

Constructor & Destructor Documentation

INLINE gum::SmallObjectAllocator::SmallObjectAllocator ( )
private

Constructor.

Greater object than maxObjectSize will be forwarded to op new.

Definition at line 49 of file smallObjectAllocator_inl.h.

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

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

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

Copy Constructor (does nothing since we use a Singleton)

Definition at line 86 of file smallObjectAllocator.h.

86 {};
INLINE gum::SmallObjectAllocator::~SmallObjectAllocator ( )
virtual

Destructor.

Definition at line 67 of file smallObjectAllocator_inl.h.

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

Referenced by operator=().

67  {
68  GUM_DESTRUCTOR(SmallObjectAllocator)
69  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
70  delete pit.val();
71  }
HashTable< Size, FixedAllocator * > __Pool
The pool containing FixedAllocator.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Function Documentation

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

Allocates a block.

Definition at line 86 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::Link< T >::operator new(), gum::FusionContext< true >::operator new(), gum::ContingencyTable< Idx, GUM_SCALAR >::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::LinkedList< T >::operator new(), gum::InternalNode::operator new(), and operator=().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 119 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::AbstractLeaf::operator delete(), gum::Parent::operator delete(), gum::GTestPolicy< GUM_SCALAR >::operator delete(), gum::FusionContext< true >::operator delete(), gum::Link< T >::operator delete(), gum::ContingencyTable< Idx, GUM_SCALAR >::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::LinkedList< T >::operator delete(), gum::InternalNode::operator delete(), and operator=().

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

+ Here is the caller graph for this function:

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  }
INLINE SmallObjectAllocator & gum::SmallObjectAllocator::instance ( )
static

Definition at line 73 of file smallObjectAllocator_inl.h.

Referenced by gum::Chi2TestPolicy< GUM_SCALAR >::operator delete(), gum::LeastSquareTestPolicy< GUM_SCALAR >::operator delete(), gum::ComposedLeaf::operator delete(), gum::ITestPolicy< GUM_SCALAR >::operator delete(), gum::O4DGContext::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::Link< T >::operator delete(), gum::FusionContext< true >::operator delete(), gum::ContingencyTable< Idx, GUM_SCALAR >::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::LinkedList< T >::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::Parent::operator new(), gum::Observation::operator new(), gum::GTestPolicy< GUM_SCALAR >::operator new(), gum::AbstractLeaf::operator new(), gum::Link< T >::operator new(), gum::FusionContext< true >::operator new(), gum::ContingencyTable< Idx, GUM_SCALAR >::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::LinkedList< T >::operator new(), gum::InternalNode::operator new(), and operator=().

73  {
74  static SmallObjectAllocator soa;
75 
76  return soa;
77  }
Idx gum::SmallObjectAllocator::nbAlloc ( )
inline

Definition at line 133 of file smallObjectAllocator.h.

References nbAllocation.

Idx gum::SmallObjectAllocator::nbDealloc ( )
inline

Definition at line 134 of file smallObjectAllocator.h.

References nbDeallocation.

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

std::size_t gum::SmallObjectAllocator::__chunkSize
private

The memory that a chunk allocates.

Definition at line 146 of file smallObjectAllocator.h.

Referenced by allocate().

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 gum::SmallObjectAllocator::__pool
private
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.

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.

Idx gum::SmallObjectAllocator::nbAllocation
private

Definition at line 153 of file smallObjectAllocator.h.

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

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: