aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
51// ============================================================================
54
55// ============================================================================
56
57
58namespace gum {
59
60 // ############################################################################
61 // @name Constructors / Destructors
62 // ############################################################################
63
64 // ============================================================================
65 /*
66 * Constructor.
67 * @param chunkSize is the size of a chunk in bytes.
68 * @param maxObjectSize is the max size of object to be considered small
69 * Greater object than maxObjectSize will be forwarded to op new.
70 */
71 // ============================================================================
74 _pool_.setKeyUniquenessPolicy(false);
75 GUM_CONSTRUCTOR(SmallObjectAllocator);
76 nbAllocation = 0;
78
79 // SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
80 // a HashTable that will not be deleted ...
81 // so we inform our leak detector not to count those 2 objects
82 GUM_DESTRUCTOR(SmallObjectAllocator);
83 GUM_DESTRUCTOR(HashTable);
84 }
85
86 // ============================================================================
87 // Destructor.
88 // ============================================================================
90 GUM_DESTRUCTOR(SmallObjectAllocator);
91 for (_Pool_::iterator pit = _pool_.begin(); pit != _pool_.end(); ++pit)
92 delete pit.val();
93 }
94
96 static SmallObjectAllocator soa;
97
98 return soa;
99 }
100
101 // ############################################################################
102 // @name Allocator / Deallocator
103 // ############################################################################
104
105 // ============================================================================
106 // Allocates an object
107 // ============================================================================
108 INLINE void* SmallObjectAllocator::allocate(const size_t& objectSize) {
109 // Small Object Allocator called for an object of size equals to 0
110 GUM_ASSERT(objectSize > 0);
111
112 // If objectSize is greater than maxObjectSize, normal new is called
113 if (objectSize > _maxObjectSize_) return new unsigned char[objectSize];
114
115 void* ret;
116#pragma omp critical(soa)
117 {
118 //
119 if (!_pool_.exists(Size(objectSize))) {
120 // Calcul du nombre de block par chunk pour des objets de cette taille
121 std::size_t nb = _chunkSize_ / Size(objectSize);
122 if (nb > UCHAR_MAX) nb = UCHAR_MAX;
123 unsigned char numBlocks = static_cast< unsigned char >(nb);
124
125 FixedAllocator* newFa = new FixedAllocator(Size(objectSize), numBlocks);
126 _pool_.set(Size(objectSize), newFa);
127 }
128 nbAllocation++;
129
130 ret = _pool_[Size(objectSize)]->allocate();
131 }
132 return ret;
133 }
134
135 // ============================================================================
136 // Deallocates an object
137 // @param pDeallocatedObject is the object to be deallocated
138 // @param objectSize is the size of that object (useful for faster
139 // deallocation)
140 // ============================================================================
141 INLINE void SmallObjectAllocator::deallocate(void* pDeallocatedObject, const size_t& objectSize) {
142 // Small Object Allocator called for an object of size equals to 0
143 GUM_ASSERT(objectSize > 0);
144
145 // If objectSize is greater than maxObjectSize, normal new is called
146 if (objectSize > _maxObjectSize_) {
147 delete[] (unsigned char*)pDeallocatedObject;
148 return;
149 }
150
151#pragma omp critical(soa)
152 {
153 // std::cout << "Deallocating " << pDeallocatedObject << std::endl;
154 _pool_[Size(objectSize)]->deallocate(pDeallocatedObject);
156 }
157 }
158
159} // namespace gum
Allocates objects of one given size.
The class for generic Hash Tables.
Definition hashTable.h:637
HashTableIterator< Size, FixedAllocator * > iterator
Definition hashTable.h:650
static SmallObjectAllocator & instance()
void * allocate(const size_t &objectSize)
Allocates a block.
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
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of gum::SmallObjectAllocator.