Nektar++
Loading...
Searching...
No Matches
library
UnitTests
Memory
TestNekMemoryManager.cpp
Go to the documentation of this file.
1
///////////////////////////////////////////////////////////////////////////////
2
//
3
// File: TestNekMemoryManager.cpp
4
//
5
// For more information, please see: http://www.nektar.info
6
//
7
// The MIT License
8
//
9
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10
// Department of Aeronautics, Imperial College London (UK), and Scientific
11
// Computing and Imaging Institute, University of Utah (USA).
12
//
13
// Permission is hereby granted, free of charge, to any person obtaining a
14
// copy of this software and associated documentation files (the "Software"),
15
// to deal in the Software without restriction, including without limitation
16
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
// and/or sell copies of the Software, and to permit persons to whom the
18
// Software is furnished to do so, subject to the following conditions:
19
//
20
// The above copyright notice and this permission notice shall be included
21
// in all copies or substantial portions of the Software.
22
//
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
30
//
31
// Description: Tests the boost utility functions.
32
//
33
///////////////////////////////////////////////////////////////////////////////
34
35
#define NEKTAR_MAX_MEMORY_MANAGER_CONSTRUCTOR_ARGS 4
36
37
#include <
UnitTests/CountedObject.h
>
38
39
#include <
LibUtilities/Memory/NekMemoryManager.hpp
>
40
41
namespace
Nektar::MemManagerUnitTests
42
{
43
BOOST_AUTO_TEST_CASE
(testParameterizedConstructors)
44
{
45
CountedObject<int>::ClearCounters
();
46
47
CountedObject<int>
*ob1 =
MemoryManager<CountedObject<int>
>::Allocate();
48
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
49
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 0u);
50
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 0u);
51
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 0u);
52
53
unsigned
int
one = 1;
54
CountedObject<int>
*ob2 =
MemoryManager<CountedObject<int>
>::Allocate(one);
55
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
56
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 1u);
57
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 0u);
58
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 0u);
59
60
unsigned
int
two = 2;
61
CountedObject<int>
*ob3 =
62
MemoryManager<CountedObject<int>
>::Allocate(one, two);
63
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
64
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 1u);
65
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 1u);
66
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 0u);
67
68
unsigned
int
three = 3;
69
CountedObject<int>
*ob4 =
70
MemoryManager<CountedObject<int>
>::Allocate(one, two, three);
71
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
72
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 1u);
73
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 1u);
74
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 1u);
75
BOOST_CHECK_EQUAL(ob4->
value
, 6u);
76
77
MemoryManager<CountedObject<int>
>::Deallocate(ob1);
78
MemoryManager<CountedObject<int>
>::Deallocate(ob2);
79
MemoryManager<CountedObject<int>
>::Deallocate(ob3);
80
MemoryManager<CountedObject<int>
>::Deallocate(ob4);
81
82
BOOST_CHECK(ob1 ==
nullptr
);
83
BOOST_CHECK(ob2 ==
nullptr
);
84
BOOST_CHECK(ob3 ==
nullptr
);
85
BOOST_CHECK(ob4 ==
nullptr
);
86
87
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
88
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 1u);
89
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 1u);
90
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 1u);
91
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDestroyed
, 4u);
92
}
93
94
BOOST_AUTO_TEST_CASE
(testSmartPointerAllocation)
95
{
96
CountedObject<int>::ClearCounters
();
97
98
{
99
std::shared_ptr<CountedObject<int>> ob1 =
100
MemoryManager<CountedObject<int>
>::AllocateSharedPtr();
101
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
102
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
,
103
0u);
104
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
,
105
0u);
106
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
,
107
0u);
108
109
int
one = 1;
110
std::shared_ptr<CountedObject<int>> ob2 =
111
MemoryManager<CountedObject<int>
>::AllocateSharedPtr(one);
112
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
113
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
,
114
1u);
115
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
,
116
0u);
117
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
,
118
0u);
119
120
int
two = 2;
121
std::shared_ptr<CountedObject<int>> ob3 =
122
MemoryManager<CountedObject<int>
>::AllocateSharedPtr(one, two);
123
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
124
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
,
125
1u);
126
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
,
127
1u);
128
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
,
129
0u);
130
131
int
three = 3;
132
std::shared_ptr<CountedObject<int>> ob4 =
133
MemoryManager<CountedObject<int>
>::AllocateSharedPtr(one, two,
134
three);
135
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
136
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
,
137
1u);
138
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
,
139
1u);
140
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
,
141
1u);
142
BOOST_CHECK_EQUAL(ob4->value, 6u);
143
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDestroyed
, 0u);
144
}
145
146
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDestroyed
, 4u);
147
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDefaultConstructed
, 1u);
148
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf1ParameterConstructions
, 1u);
149
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf2ParameterConstructions
, 1u);
150
BOOST_CHECK_EQUAL(
CountedObject<int>::numberOf3ParameterConstructions
, 1u);
151
BOOST_CHECK_EQUAL(
CountedObject<int>::numberDestroyed
, 4u);
152
}
153
154
BOOST_AUTO_TEST_CASE
(ReproduceMemoryErrors)
155
{
156
std::shared_ptr<int> m =
MemoryManager<int>::AllocateSharedPtr
();
157
std::shared_ptr<int> m1 =
MemoryManager<int>::AllocateSharedPtr
();
158
m1 =
MemoryManager<int>::AllocateSharedPtr
();
159
}
160
}
// namespace Nektar::MemManagerUnitTests
CountedObject.h
NekMemoryManager.hpp
Nektar::CountedObject
Definition
CountedObject.h:45
Nektar::CountedObject::ClearCounters
static void ClearCounters()
Definition
CountedObject.h:102
Nektar::CountedObject::value
unsigned int value
Definition
CountedObject.h:139
Nektar::MemoryManager
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
Definition
NekMemoryManager.hpp:81
Nektar::MemoryManager::AllocateSharedPtr
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Definition
NekMemoryManager.hpp:166
Nektar::MemManagerUnitTests
Definition
TestNekMemoryManager.cpp:42
Nektar::MemManagerUnitTests::BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testParameterizedConstructors)
Definition
TestNekMemoryManager.cpp:43
Generated on Sun Nov 9 2025 20:17:00 for Nektar++ by
1.9.8