sigFpe.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::sigFpe
26 
27 Description
28  Set up trapping for floating point exceptions (signal FPE).
29 
30  Controlled by two env vars:
31  \param FOAM_SIGFPE \n
32  exception trapping
33  \param FOAM_SETNAN \n
34  initialization of all malloced memory to NaN. If FOAM_SIGFPE
35  also set, this will cause usage of uninitialized scalars to trigger
36  an abort.
37 
38  Can be used either directly through the static member functions or
39  through the scope of the object (constructor sets trapping; destructor
40  restores original).
41 
42 SourceFiles
43  sigFpe.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef sigFpe_H
48 #define sigFpe_H
49 
50 #include <signal.h>
51 
52 #if defined(linux) || defined(linux64) || defined(linuxIA64) || \
53  defined(linuxARM7) || defined(linuxPPC64) || defined(linuxPPC64le)
54  #define LINUX
55 #endif
56 
57 #if defined(LINUX) && defined(__GNUC__)
58  #define LINUX_GNUC
59 #endif
60 
61 #include "UList.H"
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 /*---------------------------------------------------------------------------*\
69  Class sigFpe Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 class sigFpe
73 {
74  // Private data
75 
76  //- Saved old signal trapping setting
77  static struct sigaction oldAction_;
78 
79  //- Flag to indicate floating point trapping is enabled
80  static bool sigFpeActive_;
81 
82 
83  // Static data members
84 
85  #ifdef LINUX_GNUC
86  //- Handler for caught signals
87  static void sigHandler(int);
88  #endif
89 
90 
91 public:
92 
93 
94  // Constructors
95 
96  //- Construct null
97  sigFpe();
98 
99 
100  //- Destructor
101  ~sigFpe();
102 
103 
104  // Member functions
105 
106  //- Activate SIGFPE signal handler when FOAM_SIGFPE is %set
107  // Fill memory with NaN when FOAM_SETNAN is %set
108  static void set(const bool verbose);
109 
110  //- Deactivate SIGFPE signal handler and NaN memory initialisation
111  static void unset(const bool verbose);
112 
113  //- Flag to indicate mallocNan is enabled
114  static bool mallocNanActive_;
115 
116  #ifdef LINUX
117  //- Malloc function which initializes to NaN
118  static void* mallocNan(size_t size);
119  #endif
120 
121  //- Fill block of data with NaN
122  static void fillNan(UList<scalar>&);
123 };
124 
125 
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
127 
128 } // End namespace Foam
129 
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
131 
132 #endif
133 
134 // ************************************************************************* //
Foam::sigFpe::set
static void set(const bool verbose)
Activate SIGFPE signal handler when FOAM_SIGFPE is set.
Definition: sigFpe.C:131
Foam::sigFpe::oldAction_
static struct sigaction oldAction_
Saved old signal trapping setting.
Definition: sigFpe.H:76
Foam::sigFpe::mallocNanActive_
static bool mallocNanActive_
Flag to indicate mallocNan is enabled.
Definition: sigFpe.H:113
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::sigFpe
Set up trapping for floating point exceptions (signal FPE).
Definition: sigFpe.H:71
Foam::sigFpe::sigFpeActive_
static bool sigFpeActive_
Flag to indicate floating point trapping is enabled.
Definition: sigFpe.H:79
Foam::sigFpe::fillNan
static void fillNan(UList< scalar > &)
Fill block of data with NaN.
Definition: sigFpe.C:50
UList.H
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
Foam::sigFpe::sigFpe
sigFpe()
Construct null.
Definition: sigFpe.C:115
Foam::sigFpe::unset
static void unset(const bool verbose)
Deactivate SIGFPE signal handler and NaN memory initialisation.
Definition: sigFpe.C:226
Foam::sigFpe::~sigFpe
~sigFpe()
Destructor.
Definition: sigFpe.C:123