aGrUM  0.16.0
smallObjectAllocator_inl.h
Go to the documentation of this file.
1 
30 // ============================================================================
33 // ============================================================================
34 
35 
36 namespace gum {
37 
38  // ############################################################################
39  // @name Constructors / Destructors
40  // ############################################################################
41 
42  // ============================================================================
43  /*
44  * Constructor.
45  * @param chunkSize is the size of a chunk in bytes.
46  * @param maxObjectSize is the max size of object to be considered small
47  * Greater object than maxObjectSize will be forwarded to op new.
48  */
49  // ============================================================================
51  __chunkSize(GUM_DEFAULT_CHUNK_SIZE),
52  __maxObjectSize(GUM_DEFAULT_MAX_OBJECT_SIZE) {
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  }
64 
65  // ============================================================================
66  // Destructor.
67  // ============================================================================
69  GUM_DESTRUCTOR(SmallObjectAllocator);
70  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
71  delete pit.val();
72  }
73 
75  static SmallObjectAllocator soa;
76 
77  return soa;
78  }
79 
80  // ############################################################################
81  // @name Allocator / Deallocator
82  // ############################################################################
83 
84  // ============================================================================
85  // Allocates an object
86  // ============================================================================
87  INLINE void* SmallObjectAllocator::allocate(const size_t& objectSize) {
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  }
113 
114  // ============================================================================
115  // Deallocates an object
116  // @param pDeallocatedObject is the object to be deallocated
117  // @param objectSize is the size of that object (useful for faster
118  // deallocation)
119  // ============================================================================
120  INLINE void SmallObjectAllocator::deallocate(void* pDeallocatedObject,
121  const size_t& objectSize) {
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  }
138 
139 } // namespace gum
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Unsafe Iterators for hashtablesHashTableIterator provides a fast but unsafe way to parse HashTables...
Definition: hashTable.h:2750
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:679
virtual ~SmallObjectAllocator()
Destructor.
<agrum/core/smallObjectAllocator.h>
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
std::size_t __maxObjectSize
The maximal size of an object befor new is called.
Allocates objects of one given size.
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
void * allocate(const size_t &objectSize)
Allocates a block.
static SmallObjectAllocator & instance()
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.