1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 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 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // O S C L _ E R R O R _ I M P _ J U M P S 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 /*! \addtogroup osclerror OSCL Error 26 * 27 * @{ 28 */ 29 30 31 /** \file oscl_error_imp_jumps.h 32 \brief Implemenation of using Setjmp / Longjmp. 33 */ 34 35 #ifndef OSCL_ERROR_IMP_JUMPS_H_INCLUDED 36 #define OSCL_ERROR_IMP_JUMPS_H_INCLUDED 37 38 #ifndef OSCL_ERROR_TRAPCLEANUP_H_INCLUDED 39 #include "oscl_error_trapcleanup.h" 40 #endif 41 #ifndef OSCL_ASSERT_H_INCLUDED 42 #include "oscl_assert.h" 43 #endif 44 45 // Implemenation of Leave using Setjmp / Longjmp. 46 47 //ANSI setjmp/longjmp implementation. This is needed on any OS 48 //that does not support C++ exceptions. This is a complete implementation. 49 50 #ifndef OSCLCONFIG_ERROR_H_INCLUDED 51 #include "osclconfig_error.h" 52 #endif 53 54 #ifndef OSCL_ERROR_TRAPCLEANUP_H_INCLUDED 55 #include "oscl_error_trapcleanup.h" 56 #endif 57 #ifndef OSCL_DEFALLOC_H_INCLUDED 58 #include "oscl_defalloc.h" 59 #endif 60 #ifndef OSCL_ERROR_H_INCLUDED 61 #include "oscl_error.h" 62 #endif 63 64 class Oscl_DefAlloc; 65 66 //this defines the maximum depth of the jump mark stack. 67 #define OSCL_JUMP_MAX_JUMP_MARKS OSCL_MAX_TRAP_LEVELS 68 69 70 //OsclJump class 71 class OsclJump 72 { 73 public: 74 //for use in macros only. 75 76 OSCL_IMPORT_REF static void StaticJump(int a); 77 Jump(int a)78 void Jump(int a) 79 { 80 if (!Top()) 81 { 82 //Note: you can't leave here, since leave would 83 //invoke this routine again. It is not safe to return 84 //either, because calling code is expecting an execution 85 //end. 86 OSCL_ASSERT(false); 87 _OSCL_Abort(); 88 } 89 longjmp(*Top(), a); 90 } 91 Top()92 jmp_buf *Top() 93 { 94 OSCL_ASSERT(iJumpIndex >= 0); 95 return &iJumpArray[iJumpIndex]; 96 } 97 ~OsclJump()98 ~OsclJump() 99 { 100 //jump mark stack should be empty at this point. 101 OSCL_ASSERT(iJumpIndex == (-1)); 102 } 103 104 private: OsclJump()105 OsclJump(): iJumpIndex(-1) {} 106 PushMark()107 void PushMark() 108 { 109 OSCL_ASSERT(iJumpIndex < (OSCL_JUMP_MAX_JUMP_MARKS - 1));//jump stack is full! 110 iJumpIndex++; 111 } 112 PopMark()113 void PopMark() 114 { 115 OSCL_ASSERT(iJumpIndex >= 0);//jump stack is empty! 116 iJumpIndex--; 117 } 118 119 jmp_buf iJumpArray[OSCL_JUMP_MAX_JUMP_MARKS]; 120 121 //index to top of stack, or (-1) when stack is empty 122 int32 iJumpIndex; 123 124 friend class OsclErrorTrapImp; 125 }; 126 127 128 //internal jump type codes. 129 #define internalLeave (-1) 130 131 //Leave uses the OsclJump methods 132 #define PVError_DoLeave() OsclJump::StaticJump(internalLeave) 133 134 //_PV_TRAP macro catches leaves. 135 //_r is leave code, _s is statements to execute. 136 #define _PV_TRAP(__r,__s)\ 137 __r=OsclErrNone;\ 138 {\ 139 OsclErrorTrapImp* __trap=OsclErrorTrapImp::Trap();\ 140 if(!__trap){__s;}else{\ 141 int __tr=setjmp(*(__trap->iJumpData->Top()));\ 142 if (__tr==0)\ 143 {__s;}\ 144 else if (__tr==internalLeave)\ 145 {__r=__trap->iLeave;}\ 146 __trap->UnTrap();}\ 147 } 148 149 //Same as _PV_TRAP but avoids a TLS lookup. 150 // __trapimp is the OsclErrorTrapImp* for the calling thread. 151 #define _PV_TRAP_NO_TLS(__trapimp,__r,__s)\ 152 __r=OsclErrNone;\ 153 {\ 154 OsclErrorTrapImp* __trap=OsclErrorTrapImp::TrapNoTls(__trapimp);\ 155 if(!__trap){__s;}else{\ 156 int __tr=setjmp(*(__trap->iJumpData->Top()));\ 157 if (__tr==0)\ 158 {__s;}\ 159 else if (__tr==internalLeave)\ 160 {__r=__trap->iLeave;}\ 161 __trap->UnTrap();}\ 162 } 163 164 165 #endif // OSCL_ERROR_IMP_JUMPS_H_INCLUDED 166 167 /*! @} */ 168