1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
29 #include "OSspecific.H"
30 #include "IOstreams.H"
41 #elif defined(sgiN32) || defined(sgiN32Gcc)
49 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
51 struct sigaction Foam::sigFpe::oldAction_;
56 void *(*Foam::sigFpe::oldMallocHook_)(size_t, const void *) = NULL;
58 void* Foam::sigFpe::nanMallocHook_(size_t size, const void *caller)
62 // Restore all old hooks
63 __malloc_hook = oldMallocHook_;
66 result = malloc(size);
68 // initialize to signalling NaN
71 const uint32_t sNAN = 0x7ff7fffflu;
72 uint32_t* dPtr = reinterpret_cast<uint32_t*>(result);
76 const uint64_t sNAN = 0x7ff7ffffffffffffllu;
77 uint64_t* dPtr = reinterpret_cast<uint64_t*>(result);
81 const size_t nScalars = size/sizeof(scalar);
82 for (size_t i = 0; i < nScalars; ++i)
88 // Restore our own hooks
89 __malloc_hook = nanMallocHook_;
99 void Foam::sigFpe::sigHandler(int)
101 // Reset old handling
102 if (sigaction(SIGFPE, &oldAction_, NULL) < 0)
106 "Foam::sigSegv::sigHandler()"
107 ) << "Cannot reset SIGFPE trapping"
108 << abort(FatalError);
111 // Update jobInfo file
114 error::printStack(Perr);
116 // Throw signal (to old handler)
123 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
125 Foam::sigFpe::sigFpe()
127 oldAction_.sa_handler = NULL;
131 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
133 Foam::sigFpe::~sigFpe()
135 if (env("FOAM_SIGFPE"))
140 if (oldAction_.sa_handler && sigaction(SIGFPE, &oldAction_, NULL) < 0)
144 "Foam::sigFpe::~sigFpe()"
145 ) << "Cannot reset SIGFPE trapping"
146 << abort(FatalError);
152 if (env("FOAM_SETNAN"))
156 // Reset to standard malloc
157 if (oldAction_.sa_handler)
159 __malloc_hook = oldMallocHook_;
167 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 void Foam::sigFpe::set(const bool verbose)
171 if (oldAction_.sa_handler)
175 "Foam::sigFpe::set()"
176 ) << "Cannot call sigFpe::set() more than once"
177 << abort(FatalError);
180 if (env("FOAM_SIGFPE"))
182 bool supported = false;
194 struct sigaction newAction;
195 newAction.sa_handler = sigHandler;
196 newAction.sa_flags = SA_NODEFER;
197 sigemptyset(&newAction.sa_mask);
198 if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
202 "Foam::sigFpe::set()"
203 ) << "Cannot set SIGFPE trapping"
204 << abort(FatalError);
208 # elif defined(sgiN32) || defined(sgiN32Gcc)
211 sigfpe_[_DIVZERO].abort=1;
212 sigfpe_[_OVERFL].abort=1;
213 sigfpe_[_INVALID].abort=1;
215 sigfpe_[_DIVZERO].trace=1;
216 sigfpe_[_OVERFL].trace=1;
217 sigfpe_[_INVALID].trace=1;
237 Info<< "sigFpe : Enabling floating point exception trapping"
238 << " (FOAM_SIGFPE)." << endl;
242 Info<< "sigFpe : Floating point exception trapping"
243 << " - not supported on this platform" << endl;
249 if (env("FOAM_SETNAN"))
251 bool supported = false;
257 __malloc_hook = Foam::sigFpe::nanMallocHook_;
266 Info<< "SetNaN : Initialising allocated memory to NaN"
267 << " (FOAM_SETNAN)." << endl;
271 Info<< "SetNaN : Initialise allocated memory to NaN"
272 << " - not supported on this platform" << endl;
279 // ************************************************************************* //