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
19
20 #include "oscl_error.h"
21 #include "oscl_assert.h"
22 #include "oscl_error_trapcleanup.h"
23 #include "oscl_error_imp.h"
24 #include "oscl_heapbase.h"
25 #include "pvlogger.h"
26
27 //
28 // OsclErrorTrap
29 //
Init(Oscl_DefAlloc * aAlloc)30 OSCL_EXPORT_REF int32 OsclErrorTrap::Init(Oscl_DefAlloc *aAlloc)
31 //Init the error trap for this thread.
32 {
33
34 //make sure OsclError is only initialized once per thread
35 int32 error;
36 if (OsclErrorTrapImp::GetErrorTrap(error))
37 return OsclErrAlreadyInstalled;
38 if (error)
39 return error;//from GetErrorTrap
40
41 _OsclBasicAllocator defalloc;
42 OsclAny *ptr = (aAlloc) ? aAlloc->ALLOCATE(sizeof(OsclErrorTrapImp))
43 : defalloc.ALLOCATE(sizeof(OsclErrorTrapImp));
44 if (!ptr)
45 return OsclErrNoMemory;
46
47 OsclErrorTrapImp* self = new(ptr) OsclErrorTrapImp(aAlloc, error);
48 if (error)
49 {//not successfully constructed.
50 self->~OsclErrorTrapImp();
51 (aAlloc) ? aAlloc->deallocate(ptr) : defalloc.deallocate(ptr);
52 return error;
53 }
54
55 OsclErrorTrapImp::SetErrorTrap(self, error);
56
57 return error;
58 }
59
Cleanup()60 OSCL_EXPORT_REF int32 OsclErrorTrap::Cleanup()
61 //Cleanup the error trap for this thread.
62 {
63 int32 error;
64 OsclErrorTrapImp *errortrap = OsclErrorTrapImp::GetErrorTrap(error);
65 if (errortrap)
66 {
67 Oscl_DefAlloc *alloc = errortrap->iAlloc;
68 errortrap->~OsclErrorTrapImp();
69 alloc->deallocate(errortrap);
70 OsclErrorTrapImp::SetErrorTrap(NULL, error);
71 }
72 else if (!error)
73 error = OsclErrNotInstalled;//no errortrap.
74
75 return error;
76 }
77
GetErrorTrapImp()78 OSCL_EXPORT_REF OsclErrorTrapImp* OsclErrorTrap::GetErrorTrapImp()
79 {
80 return OsclErrorTrapImp::GetErrorTrap();
81 }
82
83 //
84 // OsclError
85 //
PushL(_OsclHeapBase * aPtr)86 OSCL_EXPORT_REF void OsclError::PushL(_OsclHeapBase * aPtr)
87 {
88 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
89 if (!trap)
90 {
91 OsclError::Leave(OsclErrNotInstalled);
92 return;
93 }
94 trap->iTrapStack->PushL(aPtr);
95 }
96
PushL(OsclAny * aPtr)97 OSCL_EXPORT_REF void OsclError::PushL(OsclAny* aPtr)
98 {
99 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
100 if (!trap)
101 {
102 OsclError::Leave(OsclErrNotInstalled);
103 return;
104 }
105 //convert from TAny to TCleanupItem so the correct
106 //deallocator will get used.
107 OsclTrapItem item(OsclErrorTrapImp::TrapOperation, aPtr);
108 trap->iTrapStack->PushL(item);
109 }
110
PushL(OsclTrapItem anItem)111 OSCL_EXPORT_REF void OsclError::PushL(OsclTrapItem anItem)
112 {
113 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
114 if (!trap)
115 {
116 OsclError::Leave(OsclErrNotInstalled);
117 return;
118 }
119 trap->iTrapStack->PushL(anItem);
120 }
121
Pop()122 OSCL_EXPORT_REF void OsclError::Pop()
123 {
124 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
125 if (!trap)
126 {
127 OsclError::Leave(OsclErrNotInstalled);
128 return;
129 }
130 trap->iTrapStack->Pop();
131 }
132
Pop(int32 aCount)133 OSCL_EXPORT_REF void OsclError::Pop(int32 aCount)
134 {
135 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
136 if (!trap)
137 {
138 OsclError::Leave(OsclErrNotInstalled);
139 return;
140 }
141 trap->iTrapStack->Pop(aCount);
142 }
143
PopDealloc()144 OSCL_EXPORT_REF void OsclError::PopDealloc()
145 {
146 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
147 if (!trap)
148 {
149 OsclError::Leave(OsclErrNotInstalled);
150 return;
151 }
152 trap->iTrapStack->PopDealloc();
153 }
154
PopDealloc(int32 aCount)155 OSCL_EXPORT_REF void OsclError::PopDealloc(int32 aCount)
156 {
157 OsclErrorTrapImp *trap = OsclErrorTrapImp::GetErrorTrap();
158 if (!trap)
159 {
160 OsclError::Leave(OsclErrNotInstalled);
161 return;
162 }
163 trap->iTrapStack->PopDealloc(aCount);
164 }
165
Leave(int32 aReason)166 OSCL_EXPORT_REF void OsclError::Leave(int32 aReason)
167 {
168 //log the leave
169 PVLOGGER_LOGMSG(PVLOGMSG_INST_HLDBG, PVLogger::GetLoggerObject("OsclError"), PVLOGMSG_ERR
170 , (0, "OsclError::Leave! reason %d", aReason));
171
172
173 //set the global leave code if errortrap is installed.
174 OsclErrorTrapImp *errortrap = OsclErrorTrapImp::GetErrorTrap();
175 if (errortrap)
176 {
177 OSCL_ASSERT(!errortrap->iLeave);//to avoid infinite recursion
178 errortrap->iLeave = aReason;
179 }
180
181 //Process the cleanup stack.
182 if (errortrap)
183 errortrap->iTrapStack->Leaving();
184
185 PVError_DoLeave();
186 }
187
LeaveIfNull(OsclAny * p)188 OSCL_EXPORT_REF void OsclError::LeaveIfNull(OsclAny* p)
189 {
190 if (!p)
191 Leave(OsclErrNoMemory);
192 }
193
LeaveIfError(int32 aReason)194 OSCL_EXPORT_REF void OsclError::LeaveIfError(int32 aReason)
195 {
196 if (aReason != OsclErrNone)
197 Leave(aReason);
198 }
199
200
201
202
203
204
205