aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
smallObjectAllocator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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  return instance();
95  }
96 
97  public:
98  // ============================================================================
99  /// Destructor.
100  // ============================================================================
101  virtual ~SmallObjectAllocator();
102 
103  /// @}
104 
105  static SmallObjectAllocator& instance();
106 
107  // ############################################################################
108  /// @name Allocator / Deallocator
109  // ############################################################################
110  /// @{
111  // ============================================================================
112  /// Allocates a block
113  // ============================================================================
114  void* allocate(const size_t& objectSize);
115 
116  // ============================================================================
117  /// Deallocates an object
118  /// @param pDeallocatedObject is the object to be deallocated
119  /// @param objectSize is the size of that object (useful for faster
120  /// deallocation)
121  // ============================================================================
122  void deallocate(void* pDeallocatedObject, const size_t& objectSize);
123 
124  /// @}
125 
126  // ============================================================================
127  /// Displays the number of allocation and deallocation made so far
128  // ============================================================================
129  void displayStats() {
130  GUM_TRACE("Nb Small Allocation : " << nbAllocation
131  << " - Nb Small Deallocation : "
132  << nbDeallocation);
133  }
134 
135  Idx nbAlloc() { return nbAllocation; }
136  Idx nbDealloc() { return nbDeallocation; }
137 
138  private:
139  // ============================================================================
140  /// The pool containing FixedAllocator
141  // ============================================================================
144 
145  // ============================================================================
146  /// The memory that a chunk allocates
147  // ============================================================================
149 
150  // ============================================================================
151  /// The maximal size of an object befor new is called
152  // ============================================================================
154 
157  };
158 } // namespace gum
159 
160 // Macro used to shorten code in classes using SmallObjectAllocator
161 #define SOA_ALLOCATE(x) SmallObjectAllocator::instance().allocate(x)
162 #define SOA_DEALLOCATE(x, y) SmallObjectAllocator::instance().deallocate(x, y)
163 
164 #ifndef GUM_NO_INLINE
165 # include <agrum/tools/core/smallobjectallocator/smallObjectAllocator_inl.h>
166 #endif
167 
168 #endif // GUM_SMALL_OBJECT_ALLOCATOR_H
HashTable< Size, FixedAllocator *> Pool__
The pool containing FixedAllocator.
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:669
static const size_t GUM_DEFAULT_CHUNK_SIZE
virtual ~SmallObjectAllocator()
Destructor.
std::size_t chunkSize__
The memory that a chunk allocates.
SmallObjectAllocator(const SmallObjectAllocator &)
Copy Constructor (does nothing since we use a Singleton)
<agrum/tools/core/smallObjectAllocator.h>
std::size_t maxObjectSize__
The maximal size of an object befor new is called.
void displayStats()
Displays the number of allocation and deallocation made so far.
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
void * allocate(const size_t &objectSize)
Allocates a block.
static SmallObjectAllocator & instance()