aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
smallObjectAllocator_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
52// ============================================================================
55
56// ============================================================================
57
58
59namespace gum {
60
61 // ############################################################################
62 // @name Constructors / Destructors
63 // ############################################################################
64
65 // ============================================================================
66 /*
67 * Constructor.
68 * @param chunkSize is the size of a chunk in bytes.
69 * @param maxObjectSize is the max size of object to be considered small
70 * Greater object than maxObjectSize will be forwarded to op new.
71 */
72 // ============================================================================
76 GUM_CONSTRUCTOR(SmallObjectAllocator);
77 nbAllocation = 0;
79
80 // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
81 // a HashTable that will not be deleted ...
82 // so we inform our leak detector not to count those 2 objects
83 GUM_DESTRUCTOR(SmallObjectAllocator);
84 GUM_DESTRUCTOR(HashTable);
85 }
86
87 // ============================================================================
88 // Destructor.
89 // ============================================================================
91 GUM_DESTRUCTOR(SmallObjectAllocator);
92 for (_Pool_::iterator pit = _pool_.begin(); pit != _pool_.end(); ++pit)
93 delete pit.val();
94 }
95
97 static SmallObjectAllocator soa;
98
99 return soa;
100 }
101
102 // ############################################################################
103 // @name Allocator / Deallocator
104 // ############################################################################
105
106 // ============================================================================
107 // Allocates an object
108 // ============================================================================
109
110 // ============================================================================
111 // Deallocates an object
112 // @param pDeallocatedObject is the object to be deallocated
113 // @param objectSize is the size of that object (useful for faster
114 // deallocation)
115 // ============================================================================
116 INLINE void SmallObjectAllocator::deallocate(void* pDeallocatedObject, const size_t& objectSize) {
117 // Small Object Allocator called for an object of size equals to 0
118 GUM_ASSERT(objectSize > 0);
119
120 std::lock_guard< std::mutex > lock(_mutex_);
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 // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
129 _pool_[Size(objectSize)]->deallocate(pDeallocatedObject);
131 }
132
136
138 GUM_TRACE("Nb Small Allocation : " << nbAllocation
139 << " - Nb Small Deallocation : " << nbDeallocation);
140 }
141
143
145
146} // namespace gum
The class for generic Hash Tables.
Definition hashTable.h:640
HashTableIterator< Size, FixedAllocator * > iterator
Definition hashTable.h:653
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
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.
static SmallObjectAllocator & instance()
void displayStats()
Displays the number of allocation and deallocation made so far.
SmallObjectAllocator & operator=(const SmallObjectAllocator &)
Operator = (does nothing since we use a Singleton).
static const size_t GUM_DEFAULT_CHUNK_SIZE
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE
std::size_t _maxObjectSize_
The maximal size of an object befor new is called.
virtual ~SmallObjectAllocator()
Destructor.
std::size_t _chunkSize_
The memory that a chunk allocates.
Headers of gum::FixedAllocator.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of gum::SmallObjectAllocator.