My Project
Valgrind.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM 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 General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef OPM_VALGRIND_HPP
28 #define OPM_VALGRIND_HPP
29 
30 #if HAVE_VALGRIND
31 #include <valgrind/memcheck.h>
32 #endif
33 
34 namespace Opm {
35 namespace Valgrind {
40 inline bool IsRunning()
41 {
42 #if !defined NDEBUG && HAVE_VALGRIND
43  return RUNNING_ON_VALGRIND;
44 #else
45  return false;
46 #endif
47 }
48 
73 template <class T>
74 inline bool CheckDefined([[maybe_unused]] const T& value)
75 {
76 #if !defined NDEBUG && HAVE_VALGRIND
77  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(&value, sizeof(T));
78  return tmp == 0;
79 #else
80  return true;
81 #endif
82 }
83 
84 
85 
105 template <class T>
106 inline bool CheckAddressable([[maybe_unused]] const T& value)
107 {
108 #if !defined NDEBUG && HAVE_VALGRIND
109  auto tmp = VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&value, sizeof(T));
110  return tmp == 0;
111 #else
112  return true;
113 #endif
114 }
115 
141 template <class T>
142 inline bool CheckDefined([[maybe_unused]] const T* value,
143  [[maybe_unused]] int size)
144 {
145 #if !defined NDEBUG && HAVE_VALGRIND
146  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(value, size*sizeof(T));
147  return tmp == 0;
148 #else
149  return true;
150 #endif
151 }
152 
170 template <class T>
171 inline void SetUndefined([[maybe_unused]] const T& value)
172 {
173 #if !defined NDEBUG && HAVE_VALGRIND
174  VALGRIND_MAKE_MEM_UNDEFINED(&value, sizeof(T));
175 #endif
176 }
177 
196 template <class T>
197 inline void SetUndefined([[maybe_unused]] const T* value,
198  [[maybe_unused]] int size)
199 {
200 #if !defined NDEBUG && HAVE_VALGRIND
201  VALGRIND_MAKE_MEM_UNDEFINED(value, size*sizeof(T));
202 #endif
203 }
204 
221 template <class T>
222 inline void SetDefined([[maybe_unused]] const T& value)
223 {
224 #if !defined NDEBUG && HAVE_VALGRIND
225  VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(T));
226 #endif
227 }
228 
247 template <class T>
248 inline void SetDefined([[maybe_unused]] const T* value,
249  [[maybe_unused]] int n)
250 {
251 #if !defined NDEBUG && HAVE_VALGRIND
252  VALGRIND_MAKE_MEM_DEFINED(value, n*sizeof(T));
253 #endif
254 }
255 
272 template <class T>
273 inline void SetNoAccess([[maybe_unused]] const T& value)
274 {
275 #if !defined NDEBUG && HAVE_VALGRIND
276  VALGRIND_MAKE_MEM_NOACCESS(&value, sizeof(T));
277 #endif
278 }
279 
296 template <class T>
297 inline void SetNoAccess([[maybe_unused]] const T* value,
298  [[maybe_unused]] int size)
299 {
300 #if !defined NDEBUG && HAVE_VALGRIND
301  VALGRIND_MAKE_MEM_NOACCESS(value, size*sizeof(T));
302 #endif
303 }
304 
305 }} // namespace Valgrind, Opm
306 
307 #endif
void SetDefined([[maybe_unused]] const T &value)
Make the memory on which an object resides defined.
Definition: Valgrind.hpp:222
bool IsRunning()
Returns whether the program is running under Valgrind or not.
Definition: Valgrind.hpp:40
bool CheckAddressable([[maybe_unused]] const T &value)
Make valgrind complain if any of the memory occupied by an object is not addressable.
Definition: Valgrind.hpp:106
void SetUndefined([[maybe_unused]] const T &value)
Make the memory on which an object resides undefined in valgrind runs.
Definition: Valgrind.hpp:171
bool CheckDefined([[maybe_unused]] const T &value)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition: Valgrind.hpp:74
void SetNoAccess([[maybe_unused]] const T &value)
Make valgrind complain if an object's memory is accessed.
Definition: Valgrind.hpp:273