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 
31 
32 #if HAVE_VALGRIND
33 #include <valgrind/memcheck.h>
34 #endif
35 
36 #if HAVE_VALGRIND
37 #define OPM_VALGRIND_OPTIM_UNUSED OPM_OPTIM_UNUSED
38 #else
39 #define OPM_VALGRIND_OPTIM_UNUSED OPM_UNUSED
40 #endif
41 
42 namespace Opm {
43 namespace Valgrind {
48 inline bool IsRunning()
49 {
50 #if !defined NDEBUG && HAVE_VALGRIND
51  return RUNNING_ON_VALGRIND;
52 #else
53  return false;
54 #endif
55 }
56 
81 template <class T>
82 inline bool CheckDefined(const T& value OPM_VALGRIND_OPTIM_UNUSED)
83 {
84 #if !defined NDEBUG && HAVE_VALGRIND
85  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(&value, sizeof(T));
86  return tmp == 0;
87 #else
88  return true;
89 #endif
90 }
91 
92 
93 
113 template <class T>
114 inline bool CheckAddressable(const T& value OPM_VALGRIND_OPTIM_UNUSED)
115 {
116 #if !defined NDEBUG && HAVE_VALGRIND
117  auto tmp = VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&value, sizeof(T));
118  return tmp == 0;
119 #else
120  return true;
121 #endif
122 }
123 
149 template <class T>
150 inline bool CheckDefined(const T* value OPM_VALGRIND_OPTIM_UNUSED, int size OPM_VALGRIND_OPTIM_UNUSED)
151 {
152 #if !defined NDEBUG && HAVE_VALGRIND
153  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(value, size*sizeof(T));
154  return tmp == 0;
155 #else
156  return true;
157 #endif
158 }
159 
177 template <class T>
178 inline void SetUndefined(const T &value OPM_VALGRIND_OPTIM_UNUSED)
179 {
180 #if !defined NDEBUG && HAVE_VALGRIND
181  VALGRIND_MAKE_MEM_UNDEFINED(&value, sizeof(T));
182 #endif
183 }
184 
203 template <class T>
204 inline void SetUndefined(const T* value OPM_VALGRIND_OPTIM_UNUSED, int size OPM_VALGRIND_OPTIM_UNUSED)
205 {
206 #if !defined NDEBUG && HAVE_VALGRIND
207  VALGRIND_MAKE_MEM_UNDEFINED(value, size*sizeof(T));
208 #endif
209 }
210 
227 template <class T>
228 inline void SetDefined(const T& value OPM_VALGRIND_OPTIM_UNUSED)
229 {
230 #if !defined NDEBUG && HAVE_VALGRIND
231  VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(T));
232 #endif
233 }
234 
253 template <class T>
254 inline void SetDefined(const T *value OPM_VALGRIND_OPTIM_UNUSED, int n OPM_VALGRIND_OPTIM_UNUSED)
255 {
256 #if !defined NDEBUG && HAVE_VALGRIND
257  VALGRIND_MAKE_MEM_DEFINED(value, n*sizeof(T));
258 #endif
259 }
260 
277 template <class T>
278 inline void SetNoAccess(const T &value OPM_VALGRIND_OPTIM_UNUSED)
279 {
280 #if !defined NDEBUG && HAVE_VALGRIND
281  VALGRIND_MAKE_MEM_NOACCESS(&value, sizeof(T));
282 #endif
283 }
284 
301 template <class T>
302 inline void SetNoAccess(const T *value OPM_VALGRIND_OPTIM_UNUSED, int size OPM_VALGRIND_OPTIM_UNUSED)
303 {
304 #if !defined NDEBUG && HAVE_VALGRIND
305  VALGRIND_MAKE_MEM_NOACCESS(value, size*sizeof(T));
306 #endif
307 }
308 
309 }} // namespace Valgrind, Opm
310 
311 #endif
Provides the OPM_UNUSED macro.
void SetNoAccess(const T &value OPM_VALGRIND_OPTIM_UNUSED)
Make valgrind complain if an object's memory is accessed.
Definition: Valgrind.hpp:278
bool IsRunning()
Returns whether the program is running under Valgrind or not.
Definition: Valgrind.hpp:48
bool CheckDefined(const T &value OPM_VALGRIND_OPTIM_UNUSED)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition: Valgrind.hpp:82
void SetDefined(const T &value OPM_VALGRIND_OPTIM_UNUSED)
Make the memory on which an object resides defined.
Definition: Valgrind.hpp:228
void SetUndefined(const T &value OPM_VALGRIND_OPTIM_UNUSED)
Make the memory on which an object resides undefined in valgrind runs.
Definition: Valgrind.hpp:178
bool CheckAddressable(const T &value OPM_VALGRIND_OPTIM_UNUSED)
Make valgrind complain if any of the memory occupied by an object is not addressable.
Definition: Valgrind.hpp:114