aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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
// ============================================================================
53
#include <
agrum/base/core/smallobjectallocator/fixedAllocator.h
>
54
#include <
agrum/base/core/smallobjectallocator/smallObjectAllocator.h
>
55
56
// ============================================================================
57
58
59
namespace
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
// ============================================================================
73
INLINE
SmallObjectAllocator::SmallObjectAllocator
() :
74
_chunkSize_
(
GUM_DEFAULT_CHUNK_SIZE
),
_maxObjectSize_
(
GUM_DEFAULT_MAX_OBJECT_SIZE
) {
75
_pool_
.
setKeyUniquenessPolicy
(
false
);
76
GUM_CONSTRUCTOR(
SmallObjectAllocator
);
77
nbAllocation
= 0;
78
nbDeallocation
= 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
// ============================================================================
90
INLINE
SmallObjectAllocator::~SmallObjectAllocator
() {
91
GUM_DESTRUCTOR(
SmallObjectAllocator
);
92
for
(
_Pool_::iterator
pit =
_pool_
.
begin
(); pit !=
_pool_
.
end
(); ++pit)
93
delete
pit.val();
94
}
95
96
INLINE
SmallObjectAllocator
&
SmallObjectAllocator::instance
() {
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);
130
nbDeallocation
++;
131
}
132
133
INLINE
SmallObjectAllocator
&
SmallObjectAllocator::operator=
(
const
SmallObjectAllocator
&) {
134
return
instance
();
135
}
136
137
INLINE
void
SmallObjectAllocator::displayStats
() {
138
GUM_TRACE(
"Nb Small Allocation : "
<<
nbAllocation
139
<<
" - Nb Small Deallocation : "
<<
nbDeallocation
);
140
}
141
142
INLINE
Idx
SmallObjectAllocator::nbAlloc
() {
return
nbAllocation
; }
143
144
INLINE
Idx
SmallObjectAllocator::nbDealloc
() {
return
nbDeallocation
; }
145
146
}
// namespace gum
gum::HashTable
The class for generic Hash Tables.
Definition
hashTable.h:640
gum::HashTable< Size, FixedAllocator * >::iterator
HashTableIterator< Size, FixedAllocator * > iterator
Definition
hashTable.h:653
gum::HashTable::setKeyUniquenessPolicy
void setKeyUniquenessPolicy(const bool new_policy) noexcept
Enables the user to change dynamically the policy for checking whether there can exist several elemen...
Definition
hashTable_tpl.h:651
gum::HashTable::begin
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
Definition
hashTable_tpl.h:490
gum::HashTable::end
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Definition
hashTable_tpl.h:473
gum::SmallObjectAllocator::instance
static SmallObjectAllocator & instance()
Definition
smallObjectAllocator_inl.h:96
gum::SmallObjectAllocator::_mutex_
std::mutex _mutex_
Definition
smallObjectAllocator.h:174
gum::SmallObjectAllocator::displayStats
void displayStats()
Displays the number of allocation and deallocation made so far.
Definition
smallObjectAllocator_inl.h:137
gum::SmallObjectAllocator::operator=
SmallObjectAllocator & operator=(const SmallObjectAllocator &)
Operator = (does nothing since we use a Singleton).
Definition
smallObjectAllocator_inl.h:133
gum::SmallObjectAllocator::nbAlloc
Idx nbAlloc()
Definition
smallObjectAllocator_inl.h:142
gum::SmallObjectAllocator::nbAllocation
Idx nbAllocation
Definition
smallObjectAllocator.h:176
gum::SmallObjectAllocator::GUM_DEFAULT_CHUNK_SIZE
static const size_t GUM_DEFAULT_CHUNK_SIZE
Definition
smallObjectAllocator.h:83
gum::SmallObjectAllocator::nbDealloc
Idx nbDealloc()
Definition
smallObjectAllocator_inl.h:144
gum::SmallObjectAllocator::SmallObjectAllocator
SmallObjectAllocator()
Constructor.
Definition
smallObjectAllocator_inl.h:73
gum::SmallObjectAllocator::_pool_
_Pool_ _pool_
Definition
smallObjectAllocator.h:161
gum::SmallObjectAllocator::nbDeallocation
Idx nbDeallocation
Definition
smallObjectAllocator.h:177
gum::SmallObjectAllocator::deallocate
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
Definition
smallObjectAllocator_inl.h:116
gum::SmallObjectAllocator::GUM_DEFAULT_MAX_OBJECT_SIZE
static const size_t GUM_DEFAULT_MAX_OBJECT_SIZE
Definition
smallObjectAllocator.h:90
gum::SmallObjectAllocator::_maxObjectSize_
std::size_t _maxObjectSize_
The maximal size of an object befor new is called.
Definition
smallObjectAllocator.h:171
gum::SmallObjectAllocator::~SmallObjectAllocator
virtual ~SmallObjectAllocator()
Destructor.
Definition
smallObjectAllocator_inl.h:90
gum::SmallObjectAllocator::_chunkSize_
std::size_t _chunkSize_
The memory that a chunk allocates.
Definition
smallObjectAllocator.h:166
fixedAllocator.h
Headers of gum::FixedAllocator.
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
smallObjectAllocator.h
Headers of gum::SmallObjectAllocator.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0