Fix "runtime error: shift exponent 32 is too large for 32-bit type 'int'" error
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / abort-handler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/system/common/abort-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace Adaptor
29 {
30
31 AbortHandler* AbortHandler::gInstance(NULL);
32
33 AbortHandler::AbortHandler( CallbackBase* callback )
34 : mSignalMask( 0 ),
35   mCallback( callback )
36 {
37   DALI_ASSERT_ALWAYS( gInstance == NULL && "Only one instance of abort handler allowed" );
38   gInstance = this;
39
40   memset( mSignalOldHandlers, 0, sizeof(SignalHandlerFuncPtr) * (_NSIG-1));
41 }
42
43 AbortHandler::~AbortHandler()
44 {
45   delete mCallback;
46
47   int signum;
48   for ( signum = 1; signum < _NSIG; signum++ )
49   {
50     if ( mSignalMask & (1ULL << (signum-1) ) )
51     {
52       // set signals back to default handling
53       signal( signum, mSignalOldHandlers[signum-1] );
54     }
55   }
56   gInstance = NULL;
57 }
58
59 bool AbortHandler::AbortOnSignal( int signum )
60 {
61   bool status = false;
62
63   if ( signum < _NSIG )
64   {
65     SignalHandlerFuncPtr signalHandlerPrevious = signal( signum, &AbortHandler::SignalHandler );
66
67 // SIG_ERR is a macro with C cast
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wold-style-cast"
70   if ( SIG_ERR != signalHandlerPrevious )
71     {
72       mSignalOldHandlers[signum-1] = signalHandlerPrevious;
73       mSignalMask |= ( 1ULL << (signum-1) );
74       status = true;
75     }
76   }
77 #pragma GCC diagnostic pop
78   return status;
79 }
80
81 void AbortHandler::SignalHandler( int signum )
82 {
83   if( gInstance )
84   {
85     if( gInstance->mCallback )
86     {
87       CallbackBase::Execute( *gInstance->mCallback );
88     }
89   }
90 }
91
92 } // Adaptor
93 } // Internal
94 } // Dali