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  Copyright (C) 2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef OPM_VALGRIND_HPP
26 #define OPM_VALGRIND_HPP
27 
29 
30 #if HAVE_VALGRIND
31 #include <valgrind/memcheck.h>
32 #endif
33 
34 namespace Valgrind
35 {
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(const T& value OPM_UNUSED)
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 
109 template <class T>
110 inline bool CheckDefined(const T* value OPM_UNUSED, int size OPM_UNUSED)
111 {
112 #if !defined NDEBUG && HAVE_VALGRIND
113  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(value, size*sizeof(T));
114  return tmp == 0;
115 #else
116  return true;
117 #endif
118 }
119 
137 template <class T>
138 inline void SetUndefined(const T &value OPM_UNUSED)
139 {
140 #if !defined NDEBUG && HAVE_VALGRIND
141  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_UNDEFINED(&value, sizeof(T));
142 #endif
143 }
144 
163 template <class T>
164 inline void SetUndefined(const T* value OPM_UNUSED, int size OPM_UNUSED)
165 {
166 #if !defined NDEBUG && HAVE_VALGRIND
167  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_UNDEFINED(value, size*sizeof(T));
168 #endif
169 }
170 
187 template <class T>
188 inline void SetDefined(const T& value OPM_UNUSED)
189 {
190 #if !defined NDEBUG && HAVE_VALGRIND
191  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(T));
192 #endif
193 }
194 
213 template <class T>
214 inline void SetDefined(const T *value OPM_UNUSED, int n OPM_UNUSED)
215 {
216 #if !defined NDEBUG && HAVE_VALGRIND
217  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_DEFINED(value, n*sizeof(T));
218 #endif
219 }
220 
237 template <class T>
238 inline void SetNoAccess(const T &value OPM_UNUSED)
239 {
240 #if !defined NDEBUG && HAVE_VALGRIND
241  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_NOACCESS(&value, sizeof(T));
242 #endif
243 }
244 
261 template <class T>
262 inline void SetNoAccess(const T *value OPM_UNUSED, int size OPM_UNUSED)
263 {
264 #if !defined NDEBUG && HAVE_VALGRIND
265  auto OPM_UNUSED result = VALGRIND_MAKE_MEM_NOACCESS(value, size*sizeof(T));
266 #endif
267 }
268 
269 } // namespace Valgrind
270 
271 #endif
void SetUndefined(const T &value OPM_UNUSED)
Make the memory on which an object resides undefined in valgrind runs.
Definition: Valgrind.hpp:138
bool CheckDefined(const T &value OPM_UNUSED)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition: Valgrind.hpp:74
void SetNoAccess(const T &value OPM_UNUSED)
Make valgrind complain if an object's memory is accessed.
Definition: Valgrind.hpp:238
Definition: Valgrind.hpp:34
bool IsRunning()
Returns whether the program is running under Valgrind or not.
Definition: Valgrind.hpp:40
#define OPM_UNUSED
Definition: Unused.hpp:32
void SetDefined(const T &value OPM_UNUSED)
Make the memory on which an object resides defined.
Definition: Valgrind.hpp:188
Provides the OPM_UNUSED macro.