merge the formfield patch from ooo-build
[ooovba.git] / tools / workben / solar.c
blobbbfef17528bf7262c3bf8ea7a39016d1e6638bf5
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.7 $
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 #include <stdio.h>
32 #include <stdlib.h>
34 typedef enum { t_char, t_short, t_int, t_long, t_double } Type;
35 typedef int (*TestFunc)( Type, void* );
37 struct Description;
39 int IsBigEndian(void);
40 int IsStackGrowingDown_2( int * pI );
41 int IsStackGrowingDown(void);
42 int GetStackAlignment_3( char*p, long l, int i, short s, char b, char c, ... );
43 int GetStackAlignment_2( char*p, long l, int i, short s, char b, char c );
44 int GetStackAlignment(void);
45 void PrintArgs( int p, ... );
46 int check( TestFunc func, Type eT, void* p );
48 #if defined (UNX) || defined (WNT) || defined (OS2)
50 #ifdef UNX
51 #include <unistd.h>
52 #endif
53 #include <sys/types.h>
55 #define I_STDARG
56 #ifdef I_STDARG
57 #include <stdarg.h>
58 #else
59 #include <varargs.h>
60 #endif
62 #define NO_USE_FORK_TO_CHECK
63 #ifdef USE_FORK_TO_CHECK
64 #include <sys/wait.h>
65 #else
66 #include <signal.h>
67 #include <setjmp.h>
68 #endif
70 #else
71 #endif
73 #define printTypeSize(Type,Name) printf( "sizeof(%s)\t= %d\n", Name, \
74 sizeof (Type) )
76 #define isSignedType(Type) (((Type)-1) < 0)
77 #define printTypeSign(Type,Name) printf( "%s\t= %s %s\n", Name, \
78 ( isSignedType(Type) ? "unsigned" : "signed" ), Name )
81 int IsBigEndian()
83 long l = 1;
84 return ! *(char*)&l;
87 int IsStackGrowingDown_2( int * pI )
89 int i = 1;
90 return ((unsigned long)&i) < (unsigned long)pI;
93 int IsStackGrowingDown()
95 int i = 1;
96 return IsStackGrowingDown_2(&i);
99 int GetStackAlignment_3( char*p, long l, int i, short s, char b, char c, ... )
101 (void) p; (void) l; (void) i; (void) s; /* unused */
102 if ( IsStackGrowingDown() )
103 return &c - &b;
104 else
105 return &b - &c;
108 int GetStackAlignment_2( char*p, long l, int i, short s, char b, char c )
110 (void) p; (void) l; (void) i; (void) s; /* unused */
111 if ( IsStackGrowingDown() )
112 return &c - &b;
113 else
114 return &b - &c;
117 int GetStackAlignment()
119 int nStackAlignment = GetStackAlignment_3(0,1,2,3,4,5);
120 if ( nStackAlignment != GetStackAlignment_2(0,1,2,3,4,5) )
121 printf( "Pascal calling convention\n" );
122 return nStackAlignment;
128 #if defined (UNX) || defined (WNT) || defined (OS2)
130 #ifdef I_STDARG
131 void PrintArgs( int p, ... )
132 #else
133 void PrintArgs( p, va_alist )
134 int p;
135 va_dcl
136 #endif
138 int value;
139 va_list ap;
141 #ifdef I_STDARG
142 va_start( ap, p );
143 #else
144 va_start( ap );
145 #endif
147 printf( "value = %d", p );
149 while ( ( value = va_arg(ap, int) ) != 0 )
150 printf( " %d", value );
152 printf( "\n" );
153 va_end(ap);
156 #ifndef USE_FORK_TO_CHECK
157 static jmp_buf check_env;
158 static int bSignal;
159 #if defined (UNX) || defined (OS2)
160 void SignalHandler( int sig )
161 #else
162 void __cdecl SignalHandler( int sig )
163 #endif
165 bSignal = 1;
167 fprintf( stderr, "Signal %d caught\n", sig );
168 signal( sig, SignalHandler );
170 longjmp( check_env, sig );
172 #endif
174 int check( TestFunc func, Type eT, void* p )
176 #ifdef USE_FORK_TO_CHECK
177 pid_t nChild = fork();
178 if ( nChild )
180 int exitVal;
181 wait( &exitVal );
182 if ( exitVal & 0xff )
183 return -1;
184 else
185 return exitVal >> 8;
187 else
189 exit( func( eT, p ) );
191 #else
192 int result;
194 bSignal = 0;
196 if ( !setjmp( check_env ) )
198 signal( SIGSEGV, SignalHandler );
199 #ifdef UNX
200 signal( SIGBUS, SignalHandler );
201 #else
202 #endif
203 result = func( eT, p );
204 signal( SIGSEGV, SIG_DFL );
205 #ifdef UNX
206 signal( SIGBUS, SIG_DFL );
207 #else
208 #endif
211 if ( bSignal )
212 return -1;
213 else
214 return 0;
215 #endif
218 #endif
221 int GetAtAddress( Type eT, void* p )
223 switch ( eT )
225 case t_char: return *((char*)p);
226 case t_short: return *((short*)p);
227 case t_int: return *((int*)p);
228 case t_long: return *((long*)p);
229 case t_double: return *((double*)p);
231 abort();
234 int SetAtAddress( Type eT, void* p )
236 switch ( eT )
238 case t_char: return *((char*)p) = 0;
239 case t_short: return *((short*)p) = 0;
240 case t_int: return *((int*)p) = 0;
241 case t_long: return *((long*)p) = 0;
242 case t_double: return *((double*)p)= 0;
244 abort();
247 char* TypeName( Type eT )
249 switch ( eT )
251 case t_char: return "char";
252 case t_short: return "short";
253 case t_int: return "int";
254 case t_long: return "long";
255 case t_double: return "double";
257 abort();
260 int CheckGetAccess( Type eT, void* p )
262 int b;
263 b = -1 != check( (TestFunc)GetAtAddress, eT, p );
264 #if OSL_DEBUG_LEVEL > 1
265 fprintf( stderr,
266 "%s read %s at %p\n",
267 (b? "can" : "can not" ), TypeName(eT), p );
268 #endif
269 return b;
271 int CheckSetAccess( Type eT, void* p )
273 int b;
274 b = -1 != check( (TestFunc)SetAtAddress, eT, p );
275 #if OSL_DEBUG_LEVEL > 1
276 fprintf( stderr,
277 "%s write %s at %p\n",
278 (b? "can" : "can not" ), TypeName(eT), p );
279 #endif
280 return b;
283 int GetAlignment( Type eT )
285 char a[ 16*8 ];
286 int p = (int)(void*)&a;
287 int i;
288 p = ( p + 0xF ) & ~0xF;
289 for ( i = 1; i < 16; i++ )
290 if ( CheckGetAccess( eT, (void*)(p+i) ) )
291 return i;
292 return 0;
295 int CheckCharAccess( char* p )
297 if ( CheckGetAccess( t_char, p ) )
298 printf( "can read address %p\n", p );
299 else
300 printf( "can not read address %p\n", p );
302 if ( CheckSetAccess( t_char, p ) )
303 printf( "can write address %p\n", p );
304 else
305 printf( "can not write address %p\n", p );
307 return 0;
310 struct Description
312 int bBigEndian;
313 int bStackGrowsDown;
314 int nStackAlignment;
315 int nAlignment[3]; /* 2,4,8 */
318 void Description_Ctor( struct Description* pThis )
320 pThis->bBigEndian = IsBigEndian();
321 pThis->bStackGrowsDown = IsStackGrowingDown();
322 pThis->nStackAlignment = GetStackAlignment();
324 if ( sizeof(short) != 2 )
325 abort();
326 pThis->nAlignment[0] = GetAlignment( t_short );
327 if ( sizeof(int) != 4 )
328 abort();
329 pThis->nAlignment[1] = GetAlignment( t_int );
330 if ( sizeof(double) != 8 )
331 abort();
332 pThis->nAlignment[2] = GetAlignment( t_double );
335 void Description_Print( struct Description* pThis, char* name )
337 int i;
338 FILE* f = fopen( name, "w" );
339 fprintf( f, "#define __%s\n",
340 pThis->bBigEndian ? "BIGENDIAN" : "LITTLEENDIAN" );
341 for ( i = 0; i < 3; i++ )
342 fprintf( f, "#define __ALIGNMENT%d\t%d\n",
343 1 << (i+1), pThis->nAlignment[i] );
344 fprintf( f, "#define __STACKALIGNMENT wird nicht benutzt\t%d\n", pThis->nStackAlignment );
345 fprintf( f, "#define __STACKDIRECTION\t%d\n",
346 pThis->bStackGrowsDown ? -1 : 1 );
347 fprintf( f, "#define __SIZEOFCHAR\t%d\n", sizeof( char ) );
348 fprintf( f, "#define __SIZEOFSHORT\t%d\n", sizeof( short ) );
349 fprintf( f, "#define __SIZEOFINT\t%d\n", sizeof( int ) );
350 fprintf( f, "#define __SIZEOFLONG\t%d\n", sizeof( long ) );
351 fprintf( f, "#define __SIZEOFPOINTER\t%d\n", sizeof( void* ) );
352 fprintf( f, "#define __SIZEOFDOUBLE\t%d\n", sizeof( double ) );
353 fprintf( f, "#define __IEEEDOUBLE\n" );
354 fprintf( f, "#define _SOLAR_NODESCRIPTION\n" );
356 fclose(f);
360 #ifdef WNT
361 __cdecl
362 #endif
363 main( int argc, char* argv[] )
365 printTypeSign( char, "char" );
366 printTypeSign( short, "short" );
367 printTypeSign( int, "int" );
368 printTypeSign( long, "long" );
370 printTypeSize( char, "char" );
371 printTypeSize( short, "short" );
372 printTypeSize( int, "int" );
373 printTypeSize( long, "long" );
374 printTypeSize( float, "float" );
375 printTypeSize( double, "double" );
376 printTypeSize( void *, "void *" );
378 if ( IsBigEndian() )
379 printf( "BIGENDIAN (Sparc, MC680x0, RS6000)\n" );
380 else
381 printf( "LITTLEENDIAN (Intel, VAX, PowerPC)\n" );
383 if( IsStackGrowingDown() )
384 printf( "Stack waechst nach unten\n" );
385 else
386 printf( "Stack waechst nach oben\n" );
388 printf( "STACKALIGNMENT : %d\n", GetStackAlignment() );
390 PrintArgs( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 );
393 char a[64];
394 int i = 56;
397 printf( "Zugriff long auf %i-Aligned Adresse : ", i / 7 );
398 printf( ( CheckGetAccess( t_long, (long*)&a[i] ) ? "OK\n" : "ERROR\n" ) );
399 i >>= 1;
400 } while( i >= 7 );
404 char a[64];
405 int i = 56;
408 printf( "Zugriff double auf %i-Aligned Adresse : ", i / 7 );
409 printf( ( CheckGetAccess( t_double, (double*)&a[i] ) ? "OK\n" : "ERROR\n" ) );
410 i >>= 1;
411 } while( i >= 7 );
415 char* p = NULL;
416 CheckCharAccess( p );
417 p = (char*)&p;
418 CheckCharAccess( p );
421 if ( argc > 1 )
423 struct Description description;
424 Description_Ctor( &description );
425 Description_Print( &description, argv[1] );
428 exit( 0 );
429 return 0;