FreeFOAM The Cross-Platform CFD Toolkit
sigFpeImpl.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) 1991-2010 OpenCFD Ltd.
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::sigFpeImpl
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  This is the platform specific opaque implementation for class sigFpe.
39 
40 SourceFiles
41  sigFpeImpl.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef sigFpeImpl_H
46 #define sigFpeImpl_H
47 
48 #include <OpenFOAM/OSspecific.H>
49 #include "FOAMConfig.h"
50 #include <signal.h>
51 
52 #if (defined(FOAM_HAVE_FENV_H) && defined(FOAM_HAVE_FEENABLEEXCEPT)) || \
53  (defined(FOAM_HAVE_SIGFPE_H) && defined(FOAM_HAVE_HANDLE_SIGFPES))
54 # define FOAM_USE_FPE_HANDLING
55 #endif
56 
57 #if defined(FOAM_HAVE_MALLOC_HOOK) && defined(FOAM_USE_FPE_HANDLING)
58 # define FOAM_USE_MALLOC_HOOK
59 #endif
60 
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 
63 namespace Foam
64 {
65 
66 /*---------------------------------------------------------------------------*\
67  Class sigFpeImpl Declaration
68 \*---------------------------------------------------------------------------*/
69 
71 {
72  // Private data
73 
74  //- Saved old signal trapping setting
75  static struct sigaction oldAction_;
76 
77 # ifdef FOAM_USE_MALLOC_HOOK
78 
79  //- Saved old malloc
80  static void *(*old_malloc_hook)(size_t, const void *);
81 
82  //- nan malloc function. From malloc_hook manpage.
83  static void* my_malloc_hook(size_t size, const void *caller);
84 
85 # endif
86 
87 
88  // Static data members
89 
90 # ifdef FOAM_USE_FPE_HANDLING
91 
92  //- Handler for caught signals
93  static void sigFpeHandler(int);
94 
95 # endif
96 
97 
98 public:
99 
100 
101  // Constructors
102 
103  sigFpeImpl();
104 
105 
106  // Destructor
107 
108  ~sigFpeImpl();
109 
110 
111  // Member functions
112 
113  void set(const bool verbose);
114 };
115 
116 
117 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
118 
119 } // End namespace Foam
120 
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 
123 #endif
124 
125 // ************************ vim: set sw=4 sts=4 et: ************************ //