aGrUM  0.14.2
smallObjectAllocator_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // ============================================================================
30 // ============================================================================
31 
32 
33 namespace gum {
34 
35  // ############################################################################
36  // @name Constructors / Destructors
37  // ############################################################################
38 
39  // ============================================================================
40  /*
41  * Constructor.
42  * @param chunkSize is the size of a chunk in bytes.
43  * @param maxObjectSize is the max size of object to be considered small
44  * Greater object than maxObjectSize will be forwarded to op new.
45  */
46  // ============================================================================
48  __chunkSize(GUM_DEFAULT_CHUNK_SIZE),
49  __maxObjectSize(GUM_DEFAULT_MAX_OBJECT_SIZE) {
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  }
61 
62  // ============================================================================
63  // Destructor.
64  // ============================================================================
66  GUM_DESTRUCTOR(SmallObjectAllocator);
67  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
68  delete pit.val();
69  }
70 
72  static SmallObjectAllocator soa;
73 
74  return soa;
75  }
76 
77  // ############################################################################
78  // @name Allocator / Deallocator
79  // ############################################################################
80 
81  // ============================================================================
82  // Allocates an object
83  // ============================================================================
84  INLINE void* SmallObjectAllocator::allocate(const size_t& objectSize) {
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  }
110 
111  // ============================================================================
112  // Deallocates an object
113  // @param pDeallocatedObject is the object to be deallocated
114  // @param objectSize is the size of that object (useful for faster
115  // deallocation)
116  // ============================================================================
117  INLINE void SmallObjectAllocator::deallocate(void* pDeallocatedObject,
118  const size_t& objectSize) {
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  }
135 
136 } // 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.
Headers of gum::SmallObjectAllocator.
Unsafe Iterators for hashtablesHashTableIterator provides a fast but unsafe way to parse HashTables...
Definition: hashTable.h:2747
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
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:45
std::size_t __chunkSize
The memory that a chunk allocates.
Headers of gum::FixedAllocator.