Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_CPPU_SOURCE_UNO_DESTR_HXX
20 : #define INCLUDED_CPPU_SOURCE_UNO_DESTR_HXX
21 :
22 : #include <sal/config.h>
23 :
24 : #include <cassert>
25 :
26 : #include "prim.hxx"
27 :
28 :
29 : namespace cppu
30 : {
31 :
32 :
33 : //#### destruction #################################################################################
34 :
35 :
36 :
37 : void destructStruct(
38 : void * pValue,
39 : typelib_CompoundTypeDescription * pTypeDescr,
40 : uno_ReleaseFunc release );
41 :
42 72300416 : inline void _destructStruct(
43 : void * pValue,
44 : typelib_CompoundTypeDescription * pTypeDescr,
45 : uno_ReleaseFunc release )
46 : {
47 72300416 : if (pTypeDescr->pBaseTypeDescription)
48 : {
49 2522773 : destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
50 : }
51 :
52 72300412 : typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
53 72300412 : sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
54 72300412 : sal_Int32 nDescr = pTypeDescr->nMembers;
55 414511384 : while (nDescr--)
56 : {
57 : ::uno_type_destructData(
58 539821112 : (char *)pValue + pMemberOffsets[nDescr],
59 809731668 : ppTypeRefs[nDescr], release );
60 : }
61 72300416 : }
62 :
63 :
64 : void destructSequence(
65 : uno_Sequence * pSequence,
66 : typelib_TypeDescriptionReference * pType,
67 : typelib_TypeDescription * pTypeDescr,
68 : uno_ReleaseFunc release );
69 :
70 :
71 476962516 : inline void _destructAny(
72 : uno_Any * pAny,
73 : uno_ReleaseFunc release )
74 : {
75 476962516 : typelib_TypeDescriptionReference * pType = pAny->pType;
76 :
77 476962516 : switch (pType->eTypeClass)
78 : {
79 : case typelib_TypeClass_HYPER:
80 : case typelib_TypeClass_UNSIGNED_HYPER:
81 : if (sizeof(void *) < sizeof(sal_Int64))
82 : {
83 : ::rtl_freeMemory( pAny->pData );
84 : }
85 736085 : break;
86 : case typelib_TypeClass_FLOAT:
87 : if (sizeof(void *) < sizeof(float))
88 : {
89 : ::rtl_freeMemory( pAny->pData );
90 : }
91 1558879 : break;
92 : case typelib_TypeClass_DOUBLE:
93 : if (sizeof(void *) < sizeof(double))
94 : {
95 : ::rtl_freeMemory( pAny->pData );
96 : }
97 6519311 : break;
98 : case typelib_TypeClass_STRING:
99 78883864 : ::rtl_uString_release( (rtl_uString *)pAny->pReserved );
100 78883877 : break;
101 : case typelib_TypeClass_TYPE:
102 : ::typelib_typedescriptionreference_release(
103 138043 : (typelib_TypeDescriptionReference *)pAny->pReserved );
104 138043 : break;
105 : case typelib_TypeClass_ANY:
106 : OSL_FAIL( "### unexpected nested any!" );
107 0 : ::uno_any_destruct( (uno_Any *)pAny->pData, release );
108 0 : ::rtl_freeMemory( pAny->pData );
109 0 : break;
110 : case typelib_TypeClass_TYPEDEF:
111 : OSL_FAIL( "### unexpected typedef!" );
112 0 : break;
113 : case typelib_TypeClass_STRUCT:
114 : case typelib_TypeClass_EXCEPTION:
115 : {
116 8386333 : typelib_TypeDescription * pTypeDescr = 0;
117 8386333 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
118 8386333 : _destructStruct( pAny->pData, (typelib_CompoundTypeDescription *)pTypeDescr, release );
119 8386333 : TYPELIB_DANGER_RELEASE( pTypeDescr );
120 8386333 : ::rtl_freeMemory( pAny->pData );
121 8386333 : break;
122 : }
123 : case typelib_TypeClass_SEQUENCE:
124 : {
125 : destructSequence(
126 21037958 : static_cast<uno_Sequence *>(pAny->pReserved), pType, 0, release );
127 21037966 : break;
128 : }
129 : case typelib_TypeClass_INTERFACE:
130 117446521 : _release( pAny->pReserved, release );
131 117446536 : break;
132 : default:
133 242255522 : break;
134 : }
135 : #if OSL_DEBUG_LEVEL > 0
136 : pAny->pData = reinterpret_cast<void *>(0xdeadbeef);
137 : #endif
138 :
139 476962552 : ::typelib_typedescriptionreference_release( pType );
140 476962806 : }
141 :
142 37701190 : inline sal_Int32 idestructElements(
143 : void * pElements, typelib_TypeDescriptionReference * pElementType,
144 : sal_Int32 nStartIndex, sal_Int32 nStopIndex,
145 : uno_ReleaseFunc release )
146 : {
147 37701190 : switch (pElementType->eTypeClass)
148 : {
149 : case typelib_TypeClass_CHAR:
150 0 : return (sal_Int32)(sizeof(sal_Unicode));
151 : case typelib_TypeClass_BOOLEAN:
152 102848 : return (sal_Int32)(sizeof(sal_Bool));
153 : case typelib_TypeClass_BYTE:
154 4992317 : return (sal_Int32)(sizeof(sal_Int8));
155 : case typelib_TypeClass_SHORT:
156 : case typelib_TypeClass_UNSIGNED_SHORT:
157 2877 : return (sal_Int32)(sizeof(sal_Int16));
158 : case typelib_TypeClass_LONG:
159 : case typelib_TypeClass_UNSIGNED_LONG:
160 166377 : return (sal_Int32)(sizeof(sal_Int32));
161 : case typelib_TypeClass_HYPER:
162 : case typelib_TypeClass_UNSIGNED_HYPER:
163 2939 : return (sal_Int32)(sizeof(sal_Int64));
164 : case typelib_TypeClass_FLOAT:
165 12 : return (sal_Int32)(sizeof(float));
166 : case typelib_TypeClass_DOUBLE:
167 418619 : return (sal_Int32)(sizeof(double));
168 :
169 : case typelib_TypeClass_STRING:
170 : {
171 3761695 : rtl_uString ** pDest = (rtl_uString **)pElements;
172 19801195 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
173 : {
174 16039500 : ::rtl_uString_release( pDest[nPos] );
175 : }
176 3761695 : return (sal_Int32)(sizeof(rtl_uString *));
177 : }
178 : case typelib_TypeClass_TYPE:
179 : {
180 80212 : typelib_TypeDescriptionReference ** pDest = (typelib_TypeDescriptionReference **)pElements;
181 2412838 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
182 : {
183 2332626 : ::typelib_typedescriptionreference_release( pDest[nPos] );
184 : }
185 80212 : return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
186 : }
187 : case typelib_TypeClass_ANY:
188 : {
189 2936812 : uno_Any * pDest = (uno_Any *)pElements;
190 12793293 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
191 : {
192 9856481 : _destructAny( &pDest[nPos], release );
193 : }
194 2936812 : return (sal_Int32)(sizeof(uno_Any));
195 : }
196 : case typelib_TypeClass_ENUM:
197 80814 : return (sal_Int32)(sizeof(sal_Int32));
198 : case typelib_TypeClass_STRUCT:
199 : case typelib_TypeClass_EXCEPTION:
200 : {
201 9446664 : typelib_TypeDescription * pElementTypeDescr = 0;
202 9446664 : TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
203 9446662 : sal_Int32 nElementSize = pElementTypeDescr->nSize;
204 69591075 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
205 : {
206 : _destructStruct(
207 120288824 : (char *)pElements + (nElementSize * nPos),
208 : (typelib_CompoundTypeDescription *)pElementTypeDescr,
209 180433236 : release );
210 : }
211 9446663 : sal_Int32 nSize = pElementTypeDescr->nSize;
212 9446663 : TYPELIB_DANGER_RELEASE( pElementTypeDescr );
213 9446663 : return nSize;
214 : }
215 : case typelib_TypeClass_SEQUENCE:
216 : {
217 281730 : typelib_TypeDescription * pElementTypeDescr = 0;
218 281730 : TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
219 281730 : uno_Sequence ** pDest = (uno_Sequence **)pElements;
220 10964599 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
221 : {
222 : destructSequence(
223 10682869 : pDest[nPos],
224 : pElementTypeDescr->pWeakRef, pElementTypeDescr,
225 21365738 : release );
226 : }
227 281730 : TYPELIB_DANGER_RELEASE( pElementTypeDescr );
228 281730 : return (sal_Int32)(sizeof(uno_Sequence *));
229 : }
230 : case typelib_TypeClass_INTERFACE:
231 : {
232 15427274 : if (release)
233 : {
234 57519444 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
235 : {
236 42092307 : void * p = ((void **)pElements)[nPos];
237 42092307 : if (p)
238 : {
239 41446114 : (*release)( p );
240 : }
241 : }
242 : }
243 : else
244 : {
245 380 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
246 : {
247 242 : uno_Interface * p = ((uno_Interface **)pElements)[nPos];
248 242 : if (p)
249 : {
250 220 : (*p->release)( p );
251 : }
252 : }
253 : }
254 15427275 : return (sal_Int32)(sizeof(void *));
255 : }
256 : default:
257 : OSL_ASSERT(false);
258 0 : return 0;
259 : }
260 : }
261 :
262 35159023 : inline void idestroySequence(
263 : uno_Sequence * pSeq,
264 : typelib_TypeDescriptionReference * pType,
265 : typelib_TypeDescription * pTypeDescr,
266 : uno_ReleaseFunc release )
267 : {
268 : assert(pSeq != nullptr);
269 : assert(pSeq->nRefCount == 0);
270 35159023 : if (pSeq->nElements > 0)
271 : {
272 33467533 : if (pTypeDescr)
273 : {
274 : idestructElements(
275 : pSeq->elements,
276 : ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
277 768774 : pSeq->nElements, release );
278 : }
279 : else
280 : {
281 32698759 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
282 : idestructElements(
283 : pSeq->elements,
284 : ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
285 32698759 : pSeq->nElements, release );
286 32698737 : TYPELIB_DANGER_RELEASE( pTypeDescr );
287 : }
288 : }
289 35158997 : ::rtl_freeMemory( pSeq );
290 35159082 : }
291 :
292 51296629 : inline void idestructSequence(
293 : uno_Sequence * pSeq,
294 : typelib_TypeDescriptionReference * pType,
295 : typelib_TypeDescription * pTypeDescr,
296 : uno_ReleaseFunc release )
297 : {
298 51296629 : if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
299 : {
300 11375314 : idestroySequence(pSeq, pType, pTypeDescr, release);
301 : }
302 51296629 : }
303 :
304 270612680 : inline void _destructData(
305 : void * pValue,
306 : typelib_TypeDescriptionReference * pType,
307 : typelib_TypeDescription * pTypeDescr,
308 : uno_ReleaseFunc release )
309 : {
310 270612680 : switch (pType->eTypeClass)
311 : {
312 : case typelib_TypeClass_STRING:
313 91663570 : ::rtl_uString_release( *(rtl_uString **)pValue );
314 91663570 : break;
315 : case typelib_TypeClass_TYPE:
316 5181433 : ::typelib_typedescriptionreference_release( *(typelib_TypeDescriptionReference **)pValue );
317 5181433 : break;
318 : case typelib_TypeClass_ANY:
319 46195447 : _destructAny( (uno_Any *)pValue, release );
320 46195454 : break;
321 : case typelib_TypeClass_TYPEDEF:
322 : OSL_FAIL( "### unexpected typedef!" );
323 0 : break;
324 : case typelib_TypeClass_STRUCT:
325 : case typelib_TypeClass_EXCEPTION:
326 1246899 : if (pTypeDescr)
327 : {
328 48593 : _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
329 : }
330 : else
331 : {
332 1198306 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
333 1198306 : _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
334 1198306 : TYPELIB_DANGER_RELEASE( pTypeDescr );
335 : }
336 1246899 : break;
337 : case typelib_TypeClass_SEQUENCE:
338 : {
339 : idestructSequence(
340 979387 : *(uno_Sequence **)pValue, pType, pTypeDescr, release );
341 979387 : break;
342 : }
343 : case typelib_TypeClass_INTERFACE:
344 1066374 : _release( *(void **)pValue, release );
345 1066374 : break;
346 : default:
347 124279570 : break;
348 : }
349 270612687 : }
350 :
351 : }
352 :
353 : #endif
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|