aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
smallObjectAllocator_inl.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 Inlines of gum::SmallObjectAllocator
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*
29
*/
30
// ============================================================================
31
#
include
<
agrum
/
tools
/
core
/
smallobjectallocator
/
fixedAllocator
.
h
>
32
#
include
<
agrum
/
tools
/
core
/
smallobjectallocator
/
smallObjectAllocator
.
h
>
33
// ============================================================================
34
35
36
namespace
gum
{
37
38
// ############################################################################
39
// @name Constructors / Destructors
40
// ############################################################################
41
42
// ============================================================================
43
/*
44
* Constructor.
45
* @param chunkSize is the size of a chunk in bytes.
46
* @param maxObjectSize is the max size of object to be considered small
47
* Greater object than maxObjectSize will be forwarded to op new.
48
*/
49
// ============================================================================
50
INLINE
SmallObjectAllocator
::
SmallObjectAllocator
() :
51
_chunkSize_
(
GUM_DEFAULT_CHUNK_SIZE
),
_maxObjectSize_
(
GUM_DEFAULT_MAX_OBJECT_SIZE
) {
52
_pool_
.
setKeyUniquenessPolicy
(
false
);
53
GUM_CONSTRUCTOR
(
SmallObjectAllocator
);
54
nbAllocation
= 0;
55
nbDeallocation
= 0;
56
57
// SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
58
// a HashTable that will not be deleted ...
59
// so we inform our leak detector not to count those 2 objects
60
GUM_DESTRUCTOR
(
SmallObjectAllocator
);
61
GUM_DESTRUCTOR
(
HashTable
);
62
}
63
64
// ============================================================================
65
// Destructor.
66
// ============================================================================
67
INLINE
SmallObjectAllocator
::~
SmallObjectAllocator
() {
68
GUM_DESTRUCTOR
(
SmallObjectAllocator
);
69
for
(
_Pool_
::
iterator
pit
=
_pool_
.
begin
();
pit
!=
_pool_
.
end
(); ++
pit
)
70
delete
pit
.
val
();
71
}
72
73
INLINE SmallObjectAllocator&
SmallObjectAllocator
::
instance
() {
74
static
SmallObjectAllocator
soa
;
75
76
return
soa
;
77
}
78
79
// ############################################################################
80
// @name Allocator / Deallocator
81
// ############################################################################
82
83
// ============================================================================
84
// Allocates an object
85
// ============================================================================
86
INLINE
void
*
SmallObjectAllocator
::
allocate
(
const
size_t
&
objectSize
) {
87
// Small Object Allocator called for an object of size equals to 0
88
GUM_ASSERT
(
objectSize
> 0);
89
90
// If objectSize is greater than maxObjectSize, normal new is called
91
if
(
objectSize
>
_maxObjectSize_
)
return
new
unsigned
char
[
objectSize
];
92
93
void
*
ret
;
94
#
pragma
omp
critical
(
soa
)
95
{
96
//
97
if
(!
_pool_
.
exists
(
Size
(
objectSize
))) {
98
// Calcul du nombre de block par chunk pour des objets de cette taille
99
std
::
size_t
nb
=
_chunkSize_
/
Size
(
objectSize
);
100
if
(
nb
>
UCHAR_MAX
)
nb
=
UCHAR_MAX
;
101
unsigned
char
numBlocks
=
static_cast
<
unsigned
char
>(
nb
);
102
103
FixedAllocator
*
newFa
=
new
FixedAllocator
(
Size
(
objectSize
),
numBlocks
);
104
_pool_
.
set
(
Size
(
objectSize
),
newFa
);
105
}
106
nbAllocation
++;
107
108
ret
=
_pool_
[
Size
(
objectSize
)]->
allocate
();
109
}
110
return
ret
;
111
}
112
113
// ============================================================================
114
// Deallocates an object
115
// @param pDeallocatedObject is the object to be deallocated
116
// @param objectSize is the size of that object (useful for faster
117
// deallocation)
118
// ============================================================================
119
INLINE
void
SmallObjectAllocator
::
deallocate
(
void
*
pDeallocatedObject
,
const
size_t
&
objectSize
) {
120
// Small Object Allocator called for an object of size equals to 0
121
GUM_ASSERT
(
objectSize
> 0);
122
123
// If objectSize is greater than maxObjectSize, normal new is called
124
if
(
objectSize
>
_maxObjectSize_
) {
125
delete
[](
unsigned
char
*)
pDeallocatedObject
;
126
return
;
127
}
128
129
#
pragma
omp
critical
(
soa
)
130
{
131
// std::cout << "Deallocating " << pDeallocatedObject << std::endl;
132
_pool_
[
Size
(
objectSize
)]->
deallocate
(
pDeallocatedObject
);
133
nbDeallocation
++;
134
}
135
}
136
137
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643