aGrUM  0.13.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 // ============================================================================
28 #include <assert.h>
29 // ============================================================================
32 // ============================================================================
33 
34 
35 namespace gum {
36 
37  // ############################################################################
38  // @name Constructors / Destructors
39  // ############################################################################
40 
41  // ============================================================================
42  /*
43  * Constructor.
44  * @param chunkSize is the size of a chunk in bytes.
45  * @param maxObjectSize is the max size of object to be considered small
46  * Greater object than maxObjectSize will be forwarded to op new.
47  */
48  // ============================================================================
50  __chunkSize(GUM_DEFAULT_CHUNK_SIZE),
51  __maxObjectSize(GUM_DEFAULT_MAX_OBJECT_SIZE) {
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  }
63 
64  // ============================================================================
65  // Destructor.
66  // ============================================================================
68  GUM_DESTRUCTOR(SmallObjectAllocator)
69  for (__Pool::iterator pit = __pool.begin(); pit != __pool.end(); ++pit)
70  delete pit.val();
71  }
72 
74  static SmallObjectAllocator soa;
75 
76  return soa;
77  }
78 
79  // ############################################################################
80  // @name Allocator / Deallocator
81  // ############################################################################
82 
83  // ============================================================================
84  // Allocates an object
85  // ============================================================================
86  INLINE void* SmallObjectAllocator::allocate(const size_t& objectSize) {
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  }
112 
113  // ============================================================================
114  // Deallocates an object
115  // @param pDeallocatedObject is the object to be deallocated
116  // @param objectSize is the size of that object (useful for faster
117  // deallocation)
118  // ============================================================================
119  INLINE void SmallObjectAllocator::deallocate(void* pDeallocatedObject,
120  const size_t& objectSize) {
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  }
137 
138 } // namespace gum
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
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 __chunkSize
The memory that a chunk allocates.
Headers of gum::FixedAllocator.