merge the formfield patch from ooo-build
[ooovba.git] / tools / source / solar / solar.c
blobe1124fa69db26094458d967f72fcd7c1fa705fcf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solar.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /* POSIX defines that a program is undefined after a SIG_SEGV. The
32 * code stopped working on Linux Kernel 2.6 so I have moved this back to
33 * use FORK.
34 * If at a later time the signals work correctly with the Linux Kernel 2.6
35 * then this change may be reverted although not strictly posix safe. */
36 #define USE_FORK_TO_CHECK 1
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <string.h>
43 #include <unistd.h>
44 #include <sys/types.h>
46 #define I_STDARG
47 #ifdef I_STDARG
48 #include <stdarg.h>
49 #else
50 #include <varargs.h>
51 #endif
53 #define NO_USE_FORK_TO_CHECK
54 #ifdef USE_FORK_TO_CHECK
55 #include <sys/wait.h>
56 #else
57 #include <signal.h>
58 #include <setjmp.h>
59 #endif
61 #define printTypeSize(Type,Name) printf( "sizeof(%s)\t= %d\n", Name, sizeof (Type) )
63 #define isSignedType(Type) (((Type)-1) < 0)
64 #define printTypeSign(Type,Name) printf( "%s\t= %s %s\n", Name, ( isSignedType(Type) ? "signed" : "unsigned" ), Name )
67 /*************************************************************************
69 |* IsBigEndian()
71 |* Beschreibung True, wenn CPU BigEndian ist
73 |* Ersterstellung EG 26.06.96
74 |* Letzte Aenderung
76 *************************************************************************/
77 int IsBigEndian()
79 long l = 1;
80 return ! *(char*)&l;
83 /*************************************************************************
85 |* IsStackGrowingDown()
87 |* Beschreibung True, wenn der Stack nach unten waechst
89 |* Ersterstellung EG 26.06.96
90 |* Letzte Aenderung
92 *************************************************************************/
93 int IsStackGrowingDown_2( int * pI )
95 int i = 1;
96 return ((unsigned long)&i) < (unsigned long)pI;
99 int IsStackGrowingDown()
101 int i = 1;
102 return IsStackGrowingDown_2(&i);
105 /*************************************************************************
107 |* GetStackAlignment()
109 |* Beschreibung Alignment von char Parametern, die (hoffentlich)
110 |* ueber den Stack uebergeben werden
112 |* Ersterstellung EG 26.06.96
113 |* Letzte Aenderung
115 *************************************************************************/
116 int GetStackAlignment_3( char*p, long l, int i, short s, char b, char c, ... )
118 if ( IsStackGrowingDown() )
119 return &c - &b;
120 else
121 return &b - &c;
124 int GetStackAlignment_2( char*p, long l, int i, short s, char b, char c )
126 if ( IsStackGrowingDown() )
127 return &c - &b;
128 else
129 return &b - &c;
132 int GetStackAlignment()
134 int nStackAlignment = GetStackAlignment_3(0,1,2,3,4,5);
135 if ( nStackAlignment != GetStackAlignment_2(0,1,2,3,4,5) )
136 printf( "Pascal calling convention\n" );
137 return nStackAlignment;
141 /*************************************************************************
143 |* Typdeclarations for memory access test functions
145 *************************************************************************/
146 typedef enum { t_char, t_short, t_int, t_long, t_double } Type;
147 typedef int (*TestFunc)( Type, void* );
150 /*************************************************************************
152 |* PrintArgs()
154 |* Beschreibung Testfunktion fuer variable Parameter
156 |* Ersterstellung EG 26.06.96
157 |* Letzte Aenderung
159 *************************************************************************/
160 #ifdef I_STDARG
161 void PrintArgs( int p, ... )
162 #else
163 void PrintArgs( p, va_alist )
164 int p;
165 va_dcl
166 #endif
168 int value;
169 va_list ap;
171 #ifdef I_STDARG
172 va_start( ap, p );
173 #else
174 va_start( ap );
175 #endif
177 printf( "value = %d", p );
179 while ( ( value = va_arg(ap, int) ) != 0 )
180 printf( " %d", value );
182 printf( "\n" );
183 va_end(ap);
186 #ifndef USE_FORK_TO_CHECK
187 /*************************************************************************
189 |* SignalHdl()
191 |* Beschreibung faengt SIGBUS und SIGSEGV in check() ab
193 |* Ersterstellung EG 26.06.96
194 |* Letzte Aenderung
196 *************************************************************************/
197 static jmp_buf check_env;
198 static int bSignal;
199 void SignalHdl( int sig )
201 bSignal = 1;
203 fprintf( stderr, "Signal %d caught\n", sig );
204 signal( SIGSEGV, SIG_DFL );
205 signal( SIGBUS, SIG_DFL );
206 siglongjmp( check_env, sig );
208 #endif
210 /*************************************************************************
212 |* check()
214 |* Beschreibung Testet MemoryZugriff (read/write)
216 |* Ersterstellung EG 26.06.96
217 |* Letzte Aenderung
219 *************************************************************************/
220 int check( TestFunc func, Type eT, void* p )
222 #ifdef USE_FORK_TO_CHECK
223 pid_t nChild = fork();
224 if ( nChild )
226 int exitVal;
227 wait( &exitVal );
228 if ( exitVal & 0xff )
229 return -1;
230 else
231 return exitVal >> 8;
233 else
235 exit( func( eT, p ) );
237 #else
238 int result;
240 bSignal = 0;
242 if ( !sigsetjmp( check_env, 1 ) )
244 signal( SIGSEGV, SignalHdl );
245 signal( SIGBUS, SignalHdl );
246 result = func( eT, p );
247 signal( SIGSEGV, SIG_DFL );
248 signal( SIGBUS, SIG_DFL );
251 if ( bSignal )
252 return -1;
253 else
254 return 0;
255 #endif
258 /*************************************************************************
260 |* GetAtAddress()
262 |* Beschreibung memory read access
264 |* Ersterstellung EG 26.06.96
265 |* Letzte Aenderung
267 *************************************************************************/
268 int GetAtAddress( Type eT, void* p )
270 switch ( eT )
272 case t_char: return *((char*)p);
273 case t_short: return *((short*)p);
274 case t_int: return *((int*)p);
275 case t_long: return *((long*)p);
276 case t_double: return *((double*)p);
278 abort();
281 /*************************************************************************
283 |* SetAtAddress()
285 |* Beschreibung memory write access
287 |* Ersterstellung EG 26.06.96
288 |* Letzte Aenderung
290 *************************************************************************/
291 int SetAtAddress( Type eT, void* p )
293 switch ( eT )
295 case t_char: return *((char*)p) = 0;
296 case t_short: return *((short*)p) = 0;
297 case t_int: return *((int*)p) = 0;
298 case t_long: return *((long*)p) = 0;
299 case t_double: return *((double*)p)= 0;
301 abort();
304 char* TypeName( Type eT )
306 switch ( eT )
308 case t_char: return "char";
309 case t_short: return "short";
310 case t_int: return "int";
311 case t_long: return "long";
312 case t_double: return "double";
314 abort();
317 /*************************************************************************
319 |* Check(Get|Set)Access()
321 |* Beschreibung Testet MemoryZugriff (read/write)
322 |* Zugriffsverletzungen werden abgefangen
324 |* Ersterstellung EG 26.06.96
325 |* Letzte Aenderung
327 *************************************************************************/
328 int CheckGetAccess( Type eT, void* p )
330 int b;
331 b = -1 != check( (TestFunc)GetAtAddress, eT, p );
332 #if OSL_DEBUG_LEVEL > 1
333 fprintf( stderr,
334 "%s read %s at %p\n",
335 (b? "can" : "can not" ), TypeName(eT), p );
336 #endif
337 return b;
339 int CheckSetAccess( Type eT, void* p )
341 int b;
343 b = -1 != check( (TestFunc)SetAtAddress, eT, p );
344 #if OSL_DEBUG_LEVEL > 1
345 fprintf( stderr,
346 "%s write %s at %p\n",
347 (b? "can" : "can not" ), TypeName(eT), p );
348 #endif
349 return b;
352 /*************************************************************************
354 |* GetAlignment()
356 |* Beschreibung Bestimmt das Alignment verschiedener Typen
358 |* Ersterstellung EG 26.06.96
359 |* Letzte Aenderung
361 *************************************************************************/
362 int GetAlignment( Type eT )
364 char a[ 16*8 ];
365 long p = (long)(void*)a;
366 int i;
368 /* clear a[...] to set legal value for double access */
369 for ( i = 0; i < 16*8; i++ )
370 a[i] = 0;
372 p = ( p + 0xF ) & ~0xF;
373 for ( i = 1; i < 16; i++ )
374 if ( CheckGetAccess( eT, (void*)(p+i) ) )
375 return i;
376 return 0;
379 /*************************************************************************
381 |* struct Description
383 |* Beschreibung Beschreibt die Parameter der Architektur
385 |* Ersterstellung EG 26.06.96
386 |* Letzte Aenderung
388 *************************************************************************/
389 struct Description
391 int bBigEndian;
392 int bStackGrowsDown;
393 int nStackAlignment;
394 int nAlignment[3]; /* 2,4,8 */
397 /*************************************************************************
399 |* Description_Ctor()
401 |* Beschreibung Bestimmt die Parameter der Architektur
403 |* Ersterstellung EG 26.06.96
404 |* Letzte Aenderung
406 *************************************************************************/
407 void Description_Ctor( struct Description* pThis )
409 pThis->bBigEndian = IsBigEndian();
410 pThis->bStackGrowsDown = IsStackGrowingDown();
411 pThis->nStackAlignment = GetStackAlignment();
413 if ( sizeof(short) != 2 )
414 abort();
415 pThis->nAlignment[0] = GetAlignment( t_short );
416 if ( sizeof(int) != 4 )
417 abort();
418 pThis->nAlignment[1] = GetAlignment( t_int );
420 if ( sizeof(long) == 8 )
421 pThis->nAlignment[2] = GetAlignment( t_long );
422 else if ( sizeof(double) == 8 )
423 pThis->nAlignment[2] = GetAlignment( t_double );
424 else
425 abort();
428 /*************************************************************************
430 |* Description_Print()
432 |* Beschreibung Schreibt die Parameter der Architektur als Header
434 |* Ersterstellung EG 26.06.96
435 |* Letzte Aenderung
437 *************************************************************************/
438 void Description_Print( struct Description* pThis, char* name )
440 int i;
441 FILE* f = fopen( name, "w" );
442 if( ! f ) {
443 fprintf( stderr, "Unable to open file %s: %s\n", name, strerror( errno ) );
444 exit( 99 );
446 fprintf( f, "#define __%s\n",
447 pThis->bBigEndian ? "BIGENDIAN" : "LITTLEENDIAN" );
448 for ( i = 0; i < 3; i++ )
449 fprintf( f, "#define __ALIGNMENT%d\t%d\n",
450 1 << (i+1), pThis->nAlignment[i] );
451 fprintf( f, "/* Stack alignment is not used... */\n" );
452 fprintf( f, "#define __STACKALIGNMENT\t%d\n", pThis->nStackAlignment );
453 fprintf( f, "#define __STACKDIRECTION\t%d\n",
454 pThis->bStackGrowsDown ? -1 : 1 );
455 fprintf( f, "#define __SIZEOFCHAR\t%d\n", sizeof( char ) );
456 fprintf( f, "#define __SIZEOFSHORT\t%d\n", sizeof( short ) );
457 fprintf( f, "#define __SIZEOFINT\t%d\n", sizeof( int ) );
458 fprintf( f, "#define __SIZEOFLONG\t%d\n", sizeof( long ) );
459 fprintf( f, "#define __SIZEOFPOINTER\t%d\n", sizeof( void* ) );
460 fprintf( f, "#define __SIZEOFDOUBLE\t%d\n", sizeof( double ) );
461 fprintf( f, "#define __IEEEDOUBLE\n" );
462 fclose( f );
465 /*************************************************************************
467 |* InfoMemoryAccess()
469 |* Beschreibung Informeller Bytezugriffstest
471 |* Ersterstellung EG 26.06.96
472 |* Letzte Aenderung
474 *************************************************************************/
475 void InfoMemoryAccess( char* p )
477 if ( CheckGetAccess( t_char, p ) )
478 printf( "can read address %p\n", p );
479 else
480 printf( "can not read address %p\n", p );
482 if ( CheckSetAccess( t_char, p ) )
483 printf( "can write address %p\n", p );
484 else
485 printf( "can not write address %p\n", p );
488 /*************************************************************************
490 |* InfoMemoryTypeAccess()
492 |* Beschreibung Informeller Zugriffstest verschiedener Typen
494 |* Ersterstellung EG 15.08.96
495 |* Letzte Aenderung
497 *************************************************************************/
498 void InfoMemoryTypeAccess( Type eT )
500 char a[64];
501 int i;
503 /* clear a[...] to set legal value for double access */
504 for ( i = 0; i < 64; i++ )
505 a[i] = 0;
507 for ( i = 56; i >= 7; i >>= 1 )
509 printf( "Zugriff %s auf %i-Aligned Adresse : ", TypeName( eT ), i / 7 );
510 printf( ( CheckGetAccess( eT, (long*)&a[i] ) ? "OK\n" : "ERROR\n" ) );
513 /************************************************************************
515 * Use C code to determine the characteristics of the building platform.
517 ************************************************************************/
518 int main( int argc, char* argv[] )
520 printTypeSign( char, "char" );
521 printTypeSign( short, "short" );
522 printTypeSign( int, "int" );
523 printTypeSign( long, "long" );
525 printTypeSize( char, "char" );
526 printTypeSize( short, "short" );
527 printTypeSize( int, "int" );
528 printTypeSize( long, "long" );
529 printTypeSize( float, "float" );
530 printTypeSize( double, "double" );
531 printTypeSize( void *, "void *" );
533 if ( IsBigEndian() )
534 printf( "BIGENDIAN (Sparc, MC680x0, RS6000, IP22, IP32, g3)\n" );
535 else
536 printf( "LITTLEENDIAN (Intel, VAX, PowerPC)\n" );
538 if( IsStackGrowingDown() )
539 printf( "Stack waechst nach unten\n" );
540 else
541 printf( "Stack waechst nach oben\n" );
543 printf( "STACKALIGNMENT : %d\n", GetStackAlignment() );
545 /* PrintArgs( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ); */
547 if ( argc > 1 )
549 struct Description description;
550 Description_Ctor( &description );
551 Description_Print( &description, argv[1] );
554 char* p = NULL;
555 InfoMemoryAccess( p );
556 p = (char*)&p;
557 InfoMemoryAccess( p );
558 InfoMemoryTypeAccess( t_short );
559 InfoMemoryTypeAccess( t_int );
560 InfoMemoryTypeAccess( t_long );
561 InfoMemoryTypeAccess( t_double );
564 exit( 0 );