aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
smallObjectAllocator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of gum::SmallObjectAllocator
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  *
29  */
30 #ifndef GUM_SMALL_OBJECT_ALLOCATOR_H
31 #define GUM_SMALL_OBJECT_ALLOCATOR_H
32 
33 // ============================================================================
34 #include <agrum/agrum.h>
35 // ============================================================================
36 #include <agrum/tools/core/hashTable.h>
37 #include <agrum/tools/core/smallobjectallocator/fixedAllocator.h>
38 // ============================================================================
39 
40 
41 namespace gum {
42  /**
43  * @class SmallObjectAllocator smallObjectAllocator.h
44  * <agrum/tools/core/smallObjectAllocator.h>
45  *
46  * @brief Allocates objects of any size
47  *
48  * SmallObjectAllocator does so by aggregating several FixedAllocator objects.
49  * When SmallObjectAllocator receives an allocation request, it either forwards
50  * it to the best matching FixedAllocator object or passes it to the default
51  * operator new
52  *
53  * @ingroup core
54  */
56  public:
57  /**
58  * @param The default size of chunck of memory.
59  * These chuncks are pre-allocated memory space which are
60  * then split in small memory space of the size of a small object
61  */
63 
64  /**
65  * @param The default maximal size under which an object is considered
66  * small.
67  * If an object size is over this limit, the normal new allocator is called.
68  */
70 
71 
72  // ############################################################################
73  /// @name Constructors / Destructors
74  // ############################################################################
75  /// @{
76  private:
77  // ============================================================================
78  /**
79  * Constructor.
80  * Greater object than maxObjectSize will be forwarded to op new.
81  */
82  // ============================================================================
84 
85  // ============================================================================
86  /// Copy Constructor (does nothing since we use a Singleton)
87  // ============================================================================
89 
90  // ============================================================================
91  /// Operator = (does nothing since we use a Singleton)
92  // ============================================================================
94 
95  public:
96  // ============================================================================
97  /// Destructor.
98  // ============================================================================
99  virtual ~SmallObjectAllocator();
100 
101  /// @}
102 
103  static SmallObjectAllocator& instance();
104 
105  // ############################################################################
106  /// @name Allocator / Deallocator
107  // ############################################################################
108  /// @{
109  // ============================================================================
110  /// Allocates a block
111  // ============================================================================
112  void* allocate(const size_t& objectSize);
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  void deallocate(void* pDeallocatedObject, const size_t& objectSize);
121 
122  /// @}
123 
124  // ============================================================================
125  /// Displays the number of allocation and deallocation made so far
126  // ============================================================================
127  void displayStats() {
128  GUM_TRACE("Nb Small Allocation : " << nbAllocation
129  << " - Nb Small Deallocation : " << nbDeallocation);
130  }
131 
132  Idx nbAlloc() { return nbAllocation; }
133  Idx nbDealloc() { return nbDeallocation; }
134 
135  private:
136  // ============================================================================
137  /// The pool containing FixedAllocator
138  // ============================================================================
141 
142  // ============================================================================
143  /// The memory that a chunk allocates
144  // ============================================================================
146 
147  // ============================================================================
148  /// The maximal size of an object befor new is called
149  // ============================================================================
151 
154  };
155 } // namespace gum
156 
157 // Macro used to shorten code in classes using SmallObjectAllocator
158 #define SOA_ALLOCATE(x) SmallObjectAllocator::instance().allocate(x)
159 #define SOA_DEALLOCATE(x, y) SmallObjectAllocator::instance().deallocate(x, y)
160 
161 #ifndef GUM_NO_INLINE
162 # include <agrum/tools/core/smallobjectallocator/smallObjectAllocator_inl.h>
163 #endif
164 
165 #endif // GUM_SMALL_OBJECT_ALLOCATOR_H
SmallObjectAllocator & operator=(const SmallObjectAllocator &)
Operator = (does nothing since we use a Singleton)
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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.
virtual ~SmallObjectAllocator()
Destructor.
SmallObjectAllocator(const SmallObjectAllocator &)
Copy Constructor (does nothing since we use a Singleton)
<agrum/tools/core/smallObjectAllocator.h>
void displayStats()
Displays the number of allocation and deallocation made so far.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
HashTable< Size, FixedAllocator *> _Pool_
The pool containing FixedAllocator.
void * allocate(const size_t &objectSize)
Allocates a block.
static SmallObjectAllocator & instance()