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 :
20 :
21 : #include <cstddef>
22 : #include <stdio.h>
23 :
24 : #include "cppu/macros.hxx"
25 : #include "osl/mutex.hxx"
26 : #include "sal/log.hxx"
27 : #include "uno/data.h"
28 :
29 : #include "constr.hxx"
30 : #include "destr.hxx"
31 : #include "copy.hxx"
32 : #include "assign.hxx"
33 : #include "eq.hxx"
34 :
35 : using namespace ::cppu;
36 : using namespace ::osl;
37 :
38 : namespace cppu
39 : {
40 :
41 : // Sequence<>() (default ctor) relies on this being static:
42 : uno_Sequence g_emptySeq = { 1, 0, { 0 } };
43 : typelib_TypeDescriptionReference * g_pVoidType = 0;
44 :
45 :
46 22923 : void * binuno_queryInterface( void * pUnoI, typelib_TypeDescriptionReference * pDestType )
47 : {
48 : // init queryInterface() td
49 : static typelib_TypeDescription * g_pQITD = 0;
50 22923 : if (0 == g_pQITD)
51 : {
52 195 : MutexGuard aGuard( Mutex::getGlobalMutex() );
53 195 : if (0 == g_pQITD)
54 : {
55 : typelib_TypeDescriptionReference * type_XInterface =
56 195 : * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE );
57 195 : typelib_InterfaceTypeDescription * pTXInterfaceDescr = 0;
58 195 : TYPELIB_DANGER_GET( reinterpret_cast<typelib_TypeDescription **>(&pTXInterfaceDescr), type_XInterface );
59 : assert(pTXInterfaceDescr->ppAllMembers);
60 : typelib_typedescriptionreference_getDescription(
61 195 : &g_pQITD, pTXInterfaceDescr->ppAllMembers[ 0 ] );
62 195 : TYPELIB_DANGER_RELEASE( &pTXInterfaceDescr->aBase );
63 195 : }
64 : }
65 :
66 : uno_Any aRet, aExc;
67 22923 : uno_Any * pExc = &aExc;
68 : void * aArgs[ 1 ];
69 22923 : aArgs[ 0 ] = &pDestType;
70 : (*static_cast<uno_Interface *>(pUnoI)->pDispatcher)(
71 22923 : static_cast<uno_Interface *>(pUnoI), g_pQITD, &aRet, aArgs, &pExc );
72 :
73 22923 : uno_Interface * ret = 0;
74 22923 : if (0 == pExc)
75 : {
76 22923 : typelib_TypeDescriptionReference * ret_type = aRet.pType;
77 22923 : switch (ret_type->eTypeClass)
78 : {
79 : case typelib_TypeClass_VOID: // common case
80 0 : typelib_typedescriptionreference_release( ret_type );
81 0 : break;
82 : case typelib_TypeClass_INTERFACE:
83 : // tweaky... avoiding acquire/ release pair
84 22923 : typelib_typedescriptionreference_release( ret_type );
85 22923 : ret = static_cast<uno_Interface *>(aRet.pReserved); // serving acquired interface
86 22923 : break;
87 : default:
88 0 : _destructAny( &aRet, 0 );
89 0 : break;
90 : }
91 : }
92 : else
93 : {
94 : SAL_WARN(
95 : "cppu",
96 : "exception occurred querying for interface "
97 : << OUString(pDestType->pTypeName) << ": ["
98 : << OUString(pExc->pType->pTypeName) << "] "
99 : << *static_cast<OUString const *>(pExc->pData));
100 : // Message is very first member
101 0 : uno_any_destruct( pExc, 0 );
102 : }
103 22923 : return ret;
104 : }
105 :
106 :
107 462763 : void defaultConstructStruct(
108 : void * pMem,
109 : typelib_CompoundTypeDescription * pCompType )
110 : {
111 462763 : _defaultConstructStruct( pMem, pCompType );
112 462763 : }
113 :
114 1209359 : void copyConstructStruct(
115 : void * pDest, void * pSource,
116 : typelib_CompoundTypeDescription * pTypeDescr,
117 : uno_AcquireFunc acquire, uno_Mapping * mapping )
118 : {
119 1209359 : _copyConstructStruct( pDest, pSource, pTypeDescr, acquire, mapping );
120 1209359 : }
121 :
122 1714031 : void destructStruct(
123 : void * pValue,
124 : typelib_CompoundTypeDescription * pTypeDescr,
125 : uno_ReleaseFunc release )
126 : {
127 1714031 : _destructStruct( pValue, pTypeDescr, release );
128 1714031 : }
129 :
130 345 : bool equalStruct(
131 : void * pDest, void *pSource,
132 : typelib_CompoundTypeDescription * pTypeDescr,
133 : uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
134 : {
135 345 : return _equalStruct( pDest, pSource, pTypeDescr, queryInterface, release );
136 : }
137 :
138 144759 : bool assignStruct(
139 : void * pDest, void * pSource,
140 : typelib_CompoundTypeDescription * pTypeDescr,
141 : uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
142 : {
143 144759 : return _assignStruct( pDest, pSource, pTypeDescr, queryInterface, acquire, release );
144 : }
145 :
146 :
147 12269733 : uno_Sequence * copyConstructSequence(
148 : uno_Sequence * pSource,
149 : typelib_TypeDescriptionReference * pElementType,
150 : uno_AcquireFunc acquire, uno_Mapping * mapping )
151 : {
152 12269733 : return icopyConstructSequence( pSource, pElementType, acquire, mapping );
153 : }
154 :
155 :
156 17799600 : void destructSequence(
157 : uno_Sequence * pSequence,
158 : typelib_TypeDescriptionReference * pType,
159 : typelib_TypeDescription * pTypeDescr,
160 : uno_ReleaseFunc release )
161 : {
162 17799600 : idestructSequence( pSequence, pType, pTypeDescr, release );
163 17799599 : }
164 :
165 :
166 1303 : bool equalSequence(
167 : uno_Sequence * pDest, uno_Sequence * pSource,
168 : typelib_TypeDescriptionReference * pElementType,
169 : uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
170 : {
171 1303 : return _equalSequence( pDest, pSource, pElementType, queryInterface, release );
172 : }
173 :
174 : }
175 :
176 : extern "C"
177 : {
178 :
179 54059154 : void SAL_CALL uno_type_constructData(
180 : void * pMem, typelib_TypeDescriptionReference * pType )
181 : SAL_THROW_EXTERN_C()
182 : {
183 54059154 : _defaultConstructData( pMem, pType, 0 );
184 54059155 : }
185 :
186 504 : void SAL_CALL uno_constructData(
187 : void * pMem, typelib_TypeDescription * pTypeDescr )
188 : SAL_THROW_EXTERN_C()
189 : {
190 504 : _defaultConstructData( pMem, pTypeDescr->pWeakRef, pTypeDescr );
191 504 : }
192 :
193 264186481 : void SAL_CALL uno_type_destructData(
194 : void * pValue, typelib_TypeDescriptionReference * pType,
195 : uno_ReleaseFunc release )
196 : SAL_THROW_EXTERN_C()
197 : {
198 264186481 : _destructData( pValue, pType, 0, release );
199 264186481 : }
200 :
201 637936 : void SAL_CALL uno_destructData(
202 : void * pValue,
203 : typelib_TypeDescription * pTypeDescr,
204 : uno_ReleaseFunc release )
205 : SAL_THROW_EXTERN_C()
206 : {
207 637936 : _destructData( pValue, pTypeDescr->pWeakRef, pTypeDescr, release );
208 637936 : }
209 :
210 209280820 : void SAL_CALL uno_type_copyData(
211 : void * pDest, void * pSource,
212 : typelib_TypeDescriptionReference * pType,
213 : uno_AcquireFunc acquire )
214 : SAL_THROW_EXTERN_C()
215 : {
216 209280820 : _copyConstructData( pDest, pSource, pType, 0, acquire, 0 );
217 209280820 : }
218 :
219 185328 : void SAL_CALL uno_copyData(
220 : void * pDest, void * pSource,
221 : typelib_TypeDescription * pTypeDescr,
222 : uno_AcquireFunc acquire )
223 : SAL_THROW_EXTERN_C()
224 : {
225 185328 : _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, acquire, 0 );
226 185328 : }
227 :
228 896044 : void SAL_CALL uno_type_copyAndConvertData(
229 : void * pDest, void * pSource,
230 : typelib_TypeDescriptionReference * pType,
231 : uno_Mapping * mapping )
232 : SAL_THROW_EXTERN_C()
233 : {
234 896044 : _copyConstructData( pDest, pSource, pType, 0, 0, mapping );
235 896044 : }
236 :
237 626994 : void SAL_CALL uno_copyAndConvertData(
238 : void * pDest, void * pSource,
239 : typelib_TypeDescription * pTypeDescr,
240 : uno_Mapping * mapping )
241 : SAL_THROW_EXTERN_C()
242 : {
243 626994 : _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, 0, mapping );
244 626994 : }
245 :
246 2530967 : sal_Bool SAL_CALL uno_type_equalData(
247 : void * pVal1, typelib_TypeDescriptionReference * pVal1Type,
248 : void * pVal2, typelib_TypeDescriptionReference * pVal2Type,
249 : uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
250 : SAL_THROW_EXTERN_C()
251 : {
252 : return _equalData(
253 : pVal1, pVal1Type, 0,
254 : pVal2, pVal2Type,
255 2530967 : queryInterface, release );
256 : }
257 :
258 0 : sal_Bool SAL_CALL uno_equalData(
259 : void * pVal1, typelib_TypeDescription * pVal1TD,
260 : void * pVal2, typelib_TypeDescription * pVal2TD,
261 : uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
262 : SAL_THROW_EXTERN_C()
263 : {
264 : return _equalData(
265 : pVal1, pVal1TD->pWeakRef, pVal1TD,
266 : pVal2, pVal2TD->pWeakRef,
267 0 : queryInterface, release );
268 : }
269 :
270 202189813 : sal_Bool SAL_CALL uno_type_assignData(
271 : void * pDest, typelib_TypeDescriptionReference * pDestType,
272 : void * pSource, typelib_TypeDescriptionReference * pSourceType,
273 : uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
274 : SAL_THROW_EXTERN_C()
275 : {
276 : return _assignData(
277 : pDest, pDestType, 0,
278 : pSource, pSourceType, 0,
279 202189813 : queryInterface, acquire, release );
280 : }
281 :
282 605 : sal_Bool SAL_CALL uno_assignData(
283 : void * pDest, typelib_TypeDescription * pDestTD,
284 : void * pSource, typelib_TypeDescription * pSourceTD,
285 : uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
286 : SAL_THROW_EXTERN_C()
287 : {
288 : return _assignData(
289 : pDest, pDestTD->pWeakRef, pDestTD,
290 : pSource, pSourceTD->pWeakRef, pSourceTD,
291 605 : queryInterface, acquire, release );
292 : }
293 :
294 98015 : sal_Bool SAL_CALL uno_type_isAssignableFromData(
295 : typelib_TypeDescriptionReference * pAssignable,
296 : void * pFrom, typelib_TypeDescriptionReference * pFromType,
297 : uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release )
298 : SAL_THROW_EXTERN_C()
299 : {
300 98015 : if (::typelib_typedescriptionreference_isAssignableFrom( pAssignable, pFromType ))
301 95027 : return sal_True;
302 2988 : if (typelib_TypeClass_INTERFACE != pFromType->eTypeClass ||
303 0 : typelib_TypeClass_INTERFACE != pAssignable->eTypeClass)
304 : {
305 2988 : return sal_False;
306 : }
307 :
308 : // query
309 0 : if (0 == pFrom)
310 0 : return sal_False;
311 0 : void * pInterface = *static_cast<void **>(pFrom);
312 0 : if (0 == pInterface)
313 0 : return sal_False;
314 :
315 0 : if (0 == queryInterface)
316 0 : queryInterface = binuno_queryInterface;
317 0 : void * p = (*queryInterface)( pInterface, pAssignable );
318 0 : _release( p, release );
319 0 : return (0 != p);
320 : }
321 :
322 : }
323 :
324 : #if OSL_DEBUG_LEVEL > 1
325 :
326 : namespace cppu {
327 :
328 : #if defined( SAL_W32)
329 : #pragma pack(push, 8)
330 : #endif
331 :
332 : // Why hardcode like this instead of using the (generated)
333 : // <sal/typesizes.h> ?
334 :
335 : #if (defined(INTEL) \
336 : && (defined(__GNUC__) && (defined(LINUX) || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD)) \
337 : || defined(MACOSX) || defined(DRAGONFLY))) \
338 : || defined(IOS)
339 : #define MAX_ALIGNMENT_4
340 : #endif
341 :
342 : #define OFFSET_OF( s, m ) reinterpret_cast< size_t >((char *)&((s *)16)->m -16)
343 :
344 : #define BINTEST_VERIFY( c ) \
345 : if (! (c)) \
346 : { \
347 : fprintf( stderr, "### binary compatibility test failed: %s [line %d]!!!\n", #c, __LINE__ ); \
348 : abort(); \
349 : }
350 :
351 : #define BINTEST_VERIFYOFFSET( s, m, n ) \
352 : if (OFFSET_OF(s, m) != static_cast<size_t>(n)) \
353 : { \
354 : fprintf(stderr, "### OFFSET_OF(" #s ", " #m ") = %" SAL_PRI_SIZET "u instead of expected %" SAL_PRI_SIZET "u!!!\n", \
355 : OFFSET_OF(s, m), static_cast<size_t>(n)); \
356 : abort(); \
357 : }
358 :
359 : #define BINTEST_VERIFYSIZE( s, n ) \
360 : if (sizeof(s) != static_cast<size_t>(n)) \
361 : { \
362 : fprintf(stderr, "### sizeof(" #s ") = %" SAL_PRI_SIZET "u instead of expected %" SAL_PRI_SIZET "u!!!\n", \
363 : sizeof(s), static_cast<size_t>(n)); \
364 : abort(); \
365 : }
366 :
367 : struct C1
368 : {
369 : sal_Int16 n1;
370 : };
371 : struct C2 : public C1
372 : {
373 : sal_Int32 n2 CPPU_GCC3_ALIGN( C1 );
374 : };
375 : struct C3 : public C2
376 : {
377 : double d3;
378 : sal_Int32 n3;
379 : };
380 : struct C4 : public C3
381 : {
382 : sal_Int32 n4 CPPU_GCC3_ALIGN( C3 );
383 : double d4;
384 : };
385 : struct C5 : public C4
386 : {
387 : sal_Int64 n5;
388 : sal_Bool b5;
389 : };
390 : struct C6 : public C1
391 : {
392 : C5 c6 CPPU_GCC3_ALIGN( C1 );
393 : sal_Bool b6;
394 : };
395 :
396 : struct D
397 : {
398 : sal_Int16 d;
399 : sal_Int32 e;
400 : };
401 : struct E
402 : {
403 : sal_Bool a;
404 : sal_Bool b;
405 : sal_Bool c;
406 : sal_Int16 d;
407 : sal_Int32 e;
408 : };
409 :
410 : struct M
411 : {
412 : sal_Int32 n;
413 : sal_Int16 o;
414 : };
415 :
416 : struct N : public M
417 : {
418 : sal_Int16 p CPPU_GCC3_ALIGN( M );
419 : };
420 : struct N2
421 : {
422 : M m;
423 : sal_Int16 p;
424 : };
425 :
426 : struct O : public M
427 : {
428 : double p;
429 : sal_Int16 q;
430 : };
431 : struct O2 : public O
432 : {
433 : sal_Int16 p2 CPPU_GCC3_ALIGN( O );
434 : };
435 :
436 : struct P : public N
437 : {
438 : double p2;
439 : };
440 :
441 : struct empty
442 : {
443 : };
444 : struct second : public empty
445 : {
446 : int a;
447 : };
448 :
449 : struct AlignSize_Impl
450 : {
451 : sal_Int16 nInt16;
452 : double dDouble;
453 : };
454 :
455 : struct Char1
456 : {
457 : char c1;
458 : };
459 : struct Char2 : public Char1
460 : {
461 : char c2 CPPU_GCC3_ALIGN( Char1 );
462 : };
463 : struct Char3 : public Char2
464 : {
465 : char c3 CPPU_GCC3_ALIGN( Char2 );
466 : };
467 : struct Char4
468 : {
469 : Char3 chars;
470 : char c;
471 : };
472 : class Ref
473 : {
474 : void * p;
475 : };
476 : enum Enum
477 : {
478 : v = SAL_MAX_ENUM
479 : };
480 :
481 :
482 : class BinaryCompatible_Impl
483 : {
484 : public:
485 : BinaryCompatible_Impl();
486 : };
487 : BinaryCompatible_Impl::BinaryCompatible_Impl()
488 : {
489 : static_assert( ((sal_Bool) true) == sal_True &&
490 : (1 != 0) == sal_True, "must be binary compatible" );
491 : static_assert( ((sal_Bool) false) == sal_False &&
492 : (1 == 0) == sal_False, "must be binary compatible" );
493 : #ifdef MAX_ALIGNMENT_4
494 : // max alignment is 4
495 : BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 4 );
496 : BINTEST_VERIFYSIZE( AlignSize_Impl, 12 );
497 : #else
498 : // max alignment is 8
499 : BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 8 );
500 : BINTEST_VERIFYSIZE( AlignSize_Impl, 16 );
501 : #endif
502 :
503 : // sequence
504 : BINTEST_VERIFY( (SAL_SEQUENCE_HEADER_SIZE % 8) == 0 );
505 : // enum
506 : BINTEST_VERIFY( sizeof( Enum ) == sizeof( sal_Int32 ) );
507 : // any
508 : BINTEST_VERIFY( sizeof(void *) >= sizeof(sal_Int32) );
509 : BINTEST_VERIFY( sizeof( uno_Any ) == sizeof( void * ) * 3 );
510 : BINTEST_VERIFYOFFSET( uno_Any, pType, 0 );
511 : BINTEST_VERIFYOFFSET( uno_Any, pData, 1 * sizeof (void *) );
512 : BINTEST_VERIFYOFFSET( uno_Any, pReserved, 2 * sizeof (void *) );
513 : // interface
514 : BINTEST_VERIFY( sizeof( Ref ) == sizeof( void * ) );
515 : // string
516 : BINTEST_VERIFY( sizeof( OUString ) == sizeof( rtl_uString * ) );
517 : // struct
518 : BINTEST_VERIFYSIZE( M, 8 );
519 : BINTEST_VERIFYOFFSET( M, o, 4 );
520 : BINTEST_VERIFYSIZE( N, 12 );
521 : BINTEST_VERIFYOFFSET( N, p, 8 );
522 : BINTEST_VERIFYSIZE( N2, 12 );
523 : BINTEST_VERIFYOFFSET( N2, p, 8 );
524 : #ifdef MAX_ALIGNMENT_4
525 : BINTEST_VERIFYSIZE( O, 20 );
526 : #else
527 : BINTEST_VERIFYSIZE( O, 24 );
528 : #endif
529 : BINTEST_VERIFYSIZE( D, 8 );
530 : BINTEST_VERIFYOFFSET( D, e, 4 );
531 : BINTEST_VERIFYOFFSET( E, d, 4 );
532 : BINTEST_VERIFYOFFSET( E, e, 8 );
533 :
534 : BINTEST_VERIFYSIZE( C1, 2 );
535 : BINTEST_VERIFYSIZE( C2, 8 );
536 : BINTEST_VERIFYOFFSET( C2, n2, 4 );
537 :
538 : #ifdef MAX_ALIGNMENT_4
539 : BINTEST_VERIFYSIZE( C3, 20 );
540 : BINTEST_VERIFYOFFSET( C3, d3, 8 );
541 : BINTEST_VERIFYOFFSET( C3, n3, 16 );
542 : BINTEST_VERIFYSIZE( C4, 32 );
543 : BINTEST_VERIFYOFFSET( C4, n4, 20 );
544 : BINTEST_VERIFYOFFSET( C4, d4, 24 );
545 : BINTEST_VERIFYSIZE( C5, 44 );
546 : BINTEST_VERIFYOFFSET( C5, n5, 32 );
547 : BINTEST_VERIFYOFFSET( C5, b5, 40 );
548 : BINTEST_VERIFYSIZE( C6, 52 );
549 : BINTEST_VERIFYOFFSET( C6, c6, 4 );
550 : BINTEST_VERIFYOFFSET( C6, b6, 48 );
551 :
552 : BINTEST_VERIFYSIZE( O2, 24 );
553 : BINTEST_VERIFYOFFSET( O2, p2, 20 );
554 : #else
555 : BINTEST_VERIFYSIZE( C3, 24 );
556 : BINTEST_VERIFYOFFSET( C3, d3, 8 );
557 : BINTEST_VERIFYOFFSET( C3, n3, 16 );
558 : BINTEST_VERIFYSIZE( C4, 40 );
559 : BINTEST_VERIFYOFFSET( C4, n4, 24 );
560 : BINTEST_VERIFYOFFSET( C4, d4, 32 );
561 : BINTEST_VERIFYSIZE( C5, 56 );
562 : BINTEST_VERIFYOFFSET( C5, n5, 40 );
563 : BINTEST_VERIFYOFFSET( C5, b5, 48 );
564 : BINTEST_VERIFYSIZE( C6, 72 );
565 : BINTEST_VERIFYOFFSET( C6, c6, 8 );
566 : BINTEST_VERIFYOFFSET( C6, b6, 64 );
567 :
568 : BINTEST_VERIFYSIZE( O2, 32 );
569 : BINTEST_VERIFYOFFSET( O2, p2, 24 );
570 : #endif
571 :
572 : BINTEST_VERIFYSIZE( Char3, 3 );
573 : BINTEST_VERIFYOFFSET( Char4, c, 3 );
574 :
575 : #ifdef MAX_ALIGNMENT_4
576 : // max alignment is 4
577 : BINTEST_VERIFYSIZE( P, 20 );
578 : #else
579 : // alignment of P is 8, because of P[] ...
580 : BINTEST_VERIFYSIZE( P, 24 );
581 : BINTEST_VERIFYSIZE( second, sizeof( int ) );
582 : #endif
583 : }
584 :
585 : #ifdef SAL_W32
586 : # pragma pack(pop)
587 : #endif
588 :
589 : static BinaryCompatible_Impl aTest;
590 :
591 : }
592 :
593 : #endif
594 :
595 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|