1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
31 #include "OSspecific.H"
32 #include "IOstreams.H"
43 #elif defined(sgiN32) || defined(sgiN32Gcc)
50 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
52 struct sigaction Foam::sigFpe::oldAction_;
57 void *(*Foam::sigFpe::old_malloc_hook)(size_t, const void *) = NULL;
59 void* Foam::sigFpe::my_malloc_hook(size_t size, const void *caller)
63 // Restore all old hooks
64 __malloc_hook = old_malloc_hook;
67 result = malloc (size);
69 // initialize to signalling nan
72 const uint32_t sNAN = 0x7ff7fffflu;
74 int nScalars = size / sizeof(scalar);
76 uint32_t* dPtr = reinterpret_cast<uint32_t*>(result);
78 for (int i = 0; i < nScalars; i++)
85 const uint64_t sNAN = 0x7ff7ffffffffffffllu;
87 int nScalars = size/sizeof(scalar);
89 uint64_t* dPtr = reinterpret_cast<uint64_t*>(result);
91 for (int i = 0; i < nScalars; i++)
98 // Restore our own hooks
99 __malloc_hook = my_malloc_hook;
109 void Foam::sigFpe::sigFpeHandler(int)
111 // Reset old handling
112 if (sigaction(SIGFPE, &oldAction_, NULL) < 0)
116 "Foam::sigSegv::sigFpeHandler()"
117 ) << "Cannot reset SIGFPE trapping"
118 << abort(FatalError);
121 // Update jobInfo file
124 error::printStack(Perr);
126 // Throw signal (to old handler)
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 Foam::sigFpe::sigFpe()
137 oldAction_.sa_handler = NULL;
141 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
143 Foam::sigFpe::~sigFpe()
145 if (env("FOAM_SIGFPE"))
150 if (oldAction_.sa_handler && sigaction(SIGFPE, &oldAction_, NULL) < 0)
154 "Foam::sigFpe::~sigFpe()"
155 ) << "Cannot reset SIGFPE trapping"
156 << abort(FatalError);
162 if (env("FOAM_SETNAN"))
166 // Reset to standard malloc
167 if (oldAction_.sa_handler)
169 __malloc_hook = old_malloc_hook;
177 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
179 void Foam::sigFpe::set()
181 if (oldAction_.sa_handler)
185 "Foam::sigFpe::set()"
186 ) << "Cannot call sigFpe::set() more than once"
187 << abort(FatalError);
190 if (env("FOAM_SIGFPE"))
201 struct sigaction newAction;
202 newAction.sa_handler = sigFpeHandler;
203 newAction.sa_flags = SA_NODEFER;
204 sigemptyset(&newAction.sa_mask);
205 if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
209 "Foam::sigFpe::set()"
210 ) << "Cannot set SIGFPE trapping"
211 << abort(FatalError);
215 # elif defined(sgiN32) || defined(sgiN32Gcc)
217 sigfpe_[_DIVZERO].abort=1;
218 sigfpe_[_OVERFL].abort=1;
219 sigfpe_[_INVALID].abort=1;
221 sigfpe_[_DIVZERO].trace=1;
222 sigfpe_[_OVERFL].trace=1;
223 sigfpe_[_INVALID].trace=1;
240 if (env("FOAM_SETNAN"))
245 __malloc_hook = Foam::sigFpe::my_malloc_hook;
252 // ************************************************************************* //