Forward compatibility: flex
[foam-extend-3.2.git] / src / OSspecific / POSIX / signals / sigFpe.H
blob8d4e3dc67003bf59df6f6533a73aaa6904191c40
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Class
25     Foam::sigFpe
27 Description
28     Set up trapping for floating point exceptions (signal FPE).
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.
38 SourceFiles
39     sigFpe.C
41 \*---------------------------------------------------------------------------*/
43 #ifndef sigFpe_H
44 #define sigFpe_H
46 #include "OSspecific.H"
47 #include <signal.h>
49 #if defined(linux) || defined(linuxAMD64) || defined(linuxIA64) || defined(linuxARM7)
50 #    define LINUX
51 #endif
53 #if defined(LINUX) && defined(__GNUC__)
54 #    define LINUX_GNUC
55 #endif
57 #ifdef __APPLE__
58 #include <malloc/malloc.h>
60 // these are defined by the mach-headers and would break compilation of Switch.H
61 #undef TRUE
62 #undef FALSE
64 #endif
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 namespace Foam
71 /*---------------------------------------------------------------------------*\
72                            Class sigFpe Declaration
73 \*---------------------------------------------------------------------------*/
75 class sigFpe
77     // Private data
79         //- Saved old signal trapping setting
80         static struct sigaction oldAction_;
82 #       ifdef LINUX
84             //- Saved old malloc
85             static void *(*old_malloc_hook)(size_t, const void *);
87             //- nan malloc function. From malloc_hook manpage.
88             static void* my_malloc_hook(size_t size, const void *caller);
90 #elif defined (__APPLE__)
92     //- pointer to the original malloc that is overrided
93     static void *(*system_malloc_)(malloc_zone_t *zone, size_t size);
95     //- the overriding handler
96     static void* nan_malloc_(malloc_zone_t *zone, size_t size);
98 #       endif
101     // Static data members
103 #   if defined(LINUX_GNUC) || defined(__APPLE__)
105         //- Handler for caught signals
106         static void sigFpeHandler(int);
108 #   endif
111 public:
114     // Constructors
116         sigFpe();
119     // Destructor
121         ~sigFpe();
124     // Member functions
126         void set(const bool verbose);
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 } // End namespace Foam
134 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
136 #endif
138 // ************************************************************************* //