aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
smallObjectAllocator_inl.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 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
),
52
maxObjectSize__
(
GUM_DEFAULT_MAX_OBJECT_SIZE
) {
53
pool__
.
setKeyUniquenessPolicy
(
false
);
54
GUM_CONSTRUCTOR
(
SmallObjectAllocator
);
55
nbAllocation
= 0;
56
nbDeallocation
= 0;
57
58
// SmallObjectAllocator::Instance will create a static SmallObjectAllocator and
59
// a HashTable that will not be deleted ...
60
// so we inform our leak detector not to count those 2 objects
61
GUM_DESTRUCTOR
(
SmallObjectAllocator
);
62
GUM_DESTRUCTOR
(
HashTable
);
63
}
64
65
// ============================================================================
66
// Destructor.
67
// ============================================================================
68
INLINE
SmallObjectAllocator
::~
SmallObjectAllocator
() {
69
GUM_DESTRUCTOR
(
SmallObjectAllocator
);
70
for
(
Pool__
::
iterator
pit
=
pool__
.
begin
();
pit
!=
pool__
.
end
(); ++
pit
)
71
delete
pit
.
val
();
72
}
73
74
INLINE SmallObjectAllocator&
SmallObjectAllocator
::
instance
() {
75
static
SmallObjectAllocator
soa
;
76
77
return
soa
;
78
}
79
80
// ############################################################################
81
// @name Allocator / Deallocator
82
// ############################################################################
83
84
// ============================================================================
85
// Allocates an object
86
// ============================================================================
87
INLINE
void
*
SmallObjectAllocator
::
allocate
(
const
size_t
&
objectSize
) {
88
// Small Object Allocator called for an object of size equals to 0
89
GUM_ASSERT
(
objectSize
> 0);
90
91
// If objectSize is greater than maxObjectSize, normal new is called
92
if
(
objectSize
>
maxObjectSize__
)
return
new
unsigned
char
[
objectSize
];
93
94
void
*
ret
;
95
#
pragma
omp
critical
(
soa
)
96
{
97
//
98
if
(!
pool__
.
exists
(
Size
(
objectSize
))) {
99
// Calcul du nombre de block par chunk pour des objets de cette taille
100
std
::
size_t
nb
=
chunkSize__
/
Size
(
objectSize
);
101
if
(
nb
>
UCHAR_MAX
)
nb
=
UCHAR_MAX
;
102
unsigned
char
numBlocks
=
static_cast
<
unsigned
char
>(
nb
);
103
104
FixedAllocator
*
newFa
=
new
FixedAllocator
(
Size
(
objectSize
),
numBlocks
);
105
pool__
.
set
(
Size
(
objectSize
),
newFa
);
106
}
107
nbAllocation
++;
108
109
ret
=
pool__
[
Size
(
objectSize
)]->
allocate
();
110
}
111
return
ret
;
112
}
113
114
// ============================================================================
115
// Deallocates an object
116
// @param pDeallocatedObject is the object to be deallocated
117
// @param objectSize is the size of that object (useful for faster
118
// deallocation)
119
// ============================================================================
120
INLINE
void
SmallObjectAllocator
::
deallocate
(
void
*
pDeallocatedObject
,
121
const
size_t
&
objectSize
) {
122
// Small Object Allocator called for an object of size equals to 0
123
GUM_ASSERT
(
objectSize
> 0);
124
125
// If objectSize is greater than maxObjectSize, normal new is called
126
if
(
objectSize
>
maxObjectSize__
) {
127
delete
[](
unsigned
char
*)
pDeallocatedObject
;
128
return
;
129
}
130
131
#
pragma
omp
critical
(
soa
)
132
{
133
// std::cout << "Deallocating " << pDeallocatedObject << std::endl;
134
pool__
[
Size
(
objectSize
)]->
deallocate
(
pDeallocatedObject
);
135
nbDeallocation
++;
136
}
137
}
138
139
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669