1 /* Siconos is a program dedicated to modeling, simulation and control
2  * of non smooth dynamical systems.
3  *
4  * Copyright 2021 INRIA.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17 */
18 
19 /*!\file sn_error_handling.h
20  * \brief error handling functions and data structures*/
21 
22 #ifndef ERROR_HANDLING_H
23 #define ERROR_HANDLING_H
24 
25 #include <setjmp.h>  // for jmp_buf
26 #include "SiconosConfig.h" // for BUILD_AS_CPP // IWYU pragma: keep
27 
28 #define SN_SETJMP_INTERNAL_START setjmp(*sn_get_internal_jmp_buf())
29 #define SN_SETJMP_INTERNAL_STOP sn_release_internal_jmp_buf();
30 
31 #define SN_SETJMP_EXTERNAL_START setjmp(*sn_get_jmp_buf())
32 #define SN_SETJMP_EXTERNAL_STOP sn_release_jmp_buf();
33 
34 typedef enum { SN_NO_ERROR, SN_MEMORY_ALLOC_ERROR, SN_UNSUPPORTED_LINALG_OP, SN_PROBLEM_NOT_PROCESSABLE, SN_UNKOWN_ERROR, SN_NOT_COMPILED_ERROR } SN_ERROR_T;
35 
36 #if defined(__cplusplus) && !defined (BUILD_AS_CPP)
37 extern "C"
38 {
39 #endif
40 
41 
42   /* Get the external jmp buffer and mark it as used
43    * \return the external jmp buffer
44    */
45   jmp_buf* sn_get_jmp_buf(void);
46 
47   /** Release the internal jmp buffer: this indicates that it is no longer in
48    * use and that there should be no longjmp() call to this part of the stack.
49    * The user should call this function whenever the call to a numerics
50    * function has been successful*/
51   void sn_release_jmp_buf(void);
52 
53   /* Get the internal jmp buffer and mark it as used
54    * \warning this function is ment to be called inside the numerics library.
55    * To use the exception handler from an external library/executable, use
56    * sn_get_jmp_buf()
57    * \return the internal jmp buffer
58    */
59   jmp_buf* sn_get_internal_jmp_buf(void);
60 
61   /** Release the internal jmp buffer: this indicates that it is no longer in
62    * use and that there should be no longjmp() call to this part of the stack.
63    * The user should call this function whenever the call to a numerics
64    * function has been successful.
65    * \warning This should not be called outside the numerics library. Use
66    * sn_release_jmp_buf() instead when calling from another library/executable
67    */
68   void sn_release_internal_jmp_buf(void);
69 
70   /* Function to call whenever a fatal error occured. This function either call
71    * longjmp if setjmp has been called previously and is still active. If not,
72    * it calls abort().
73    * \param code error code
74    * \param msn error message
75    */
76   void sn_fatal_error(SN_ERROR_T code, const char* msg);
77 
78   /* Get the last error message
79    * \return the error message
80    */
81   const char* sn_fatal_error_msg(void);
82 
83 #if defined(__cplusplus) && !defined (BUILD_AS_CPP)
84 }
85 #endif
86 
87 #endif /* ERROR_HANDLING_H  */
88