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

<agrum/tools/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/tools/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 139 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 gum::Set< Key, Alloc >::emplace().

50  :
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 _chunkSize_
The memory that a chunk allocates.
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...
+ 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 67 of file smallObjectAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

67  {
68  GUM_DESTRUCTOR(SmallObjectAllocator);
69  for (_Pool_::iterator pit = _pool_.begin(); pit != _pool_.end(); ++pit)
70  delete pit.val();
71  }
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:680
+ 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 86 of file smallObjectAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

86  {
87  // Small Object Allocator called for an object of size equals to 0
88  GUM_ASSERT(objectSize > 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  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
std::size_t _chunkSize_
The memory that a chunk allocates.
std::size_t _maxObjectSize_
The maximal size of an object befor new is called.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call 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 119 of file smallObjectAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

119  {
120  // Small Object Allocator called for an object of size equals to 0
121  GUM_ASSERT(objectSize > 0);
122 
123  // If objectSize is greater than maxObjectSize, normal new is called
124  if (objectSize > _maxObjectSize_) {
125  delete[](unsigned char*) pDeallocatedObject;
126  return;
127  }
128 
129 #pragma omp critical(soa)
130  {
131  // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
132  _pool_[Size(objectSize)]->deallocate(pDeallocatedObject);
133  nbDeallocation++;
134  }
135  }
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:47
+ Here is the call 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.

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

◆ instance()

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

Definition at line 73 of file smallObjectAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

Referenced by operator=().

73  {
74  static SmallObjectAllocator soa;
75 
76  return soa;
77  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nbAlloc()

Idx gum::SmallObjectAllocator::nbAlloc ( )
inline

Definition at line 132 of file smallObjectAllocator.h.

◆ nbDealloc()

Idx gum::SmallObjectAllocator::nbDealloc ( )
inline

Definition at line 133 of file smallObjectAllocator.h.

◆ 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 instance().

93 { return instance(); }
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 145 of file smallObjectAllocator.h.

◆ _maxObjectSize_

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

The maximal size of an object befor new is called.

Definition at line 150 of file smallObjectAllocator.h.

◆ _pool_

_Pool_ gum::SmallObjectAllocator::_pool_
private

Definition at line 140 of file smallObjectAllocator.h.

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

◆ nbDeallocation

Idx gum::SmallObjectAllocator::nbDeallocation
private

Definition at line 153 of file smallObjectAllocator.h.


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