eliminate GCC warning about initialization order
[reactos.git] / reactos / include / wine / port.h
1 /*
2 * Wine porting definitions
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef __WINE_WINE_PORT_H
22 #define __WINE_WINE_PORT_H
23
24 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
26 #endif
27
28 #define _GNU_SOURCE /* for pread/pwrite */
29 #include <fcntl.h>
30 #include <math.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #ifdef HAVE_DIRECT_H
34 # include <direct.h>
35 #endif
36 #ifdef HAVE_IO_H
37 # include <io.h>
38 #endif
39 #ifdef HAVE_PROCESS_H
40 # include <process.h>
41 #endif
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47
48 /****************************************************************
49 * Type definitions
50 */
51
52 #ifndef HAVE_MODE_T
53 typedef int mode_t;
54 #endif
55 #ifndef HAVE_OFF_T
56 typedef long off_t;
57 #endif
58 #ifndef HAVE_PID_T
59 typedef int pid_t;
60 #endif
61 #ifndef HAVE_SIZE_T
62 typedef unsigned int size_t;
63 #endif
64 #ifndef HAVE_SSIZE_T
65 typedef int ssize_t;
66 #endif
67 //#ifndef HAVE_SOCKLEN_T
68 //typedef unsigned int socklen_t;
69 //#endif
70
71 #ifndef HAVE_STATFS
72 # ifdef __BEOS__
73 # define HAVE_STRUCT_STATFS_F_BFREE
74 struct statfs {
75 long f_bsize; /* block_size */
76 long f_blocks; /* total_blocks */
77 long f_bfree; /* free_blocks */
78 };
79 # else /* defined(__BEOS__) */
80 struct statfs;
81 # endif /* defined(__BEOS__) */
82 #endif /* !defined(HAVE_STATFS) */
83
84
85 /****************************************************************
86 * Macro definitions
87 */
88
89 #ifdef HAVE_DLFCN_H
90 #include <dlfcn.h>
91 #else
92 #define RTLD_LAZY 0x001
93 #define RTLD_NOW 0x002
94 #define RTLD_GLOBAL 0x100
95 #endif
96
97 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
98 #define ftruncate chsize
99 #endif
100
101 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
102 #define popen _popen
103 #endif
104
105 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
106 #define pclose _pclose
107 #endif
108
109 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
110 #define snprintf _snprintf
111 #endif
112
113 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
114 #define vsnprintf _vsnprintf
115 #endif
116
117 #ifndef S_ISLNK
118 # define S_ISLNK(mod) (0)
119 #endif /* S_ISLNK */
120
121 /* So we open files in 64 bit access mode on Linux */
122 #ifndef O_LARGEFILE
123 # define O_LARGEFILE 0
124 #endif
125
126
127 /****************************************************************
128 * Constants
129 */
130
131 #ifndef M_PI
132 #define M_PI 3.14159265358979323846
133 #endif
134
135 #ifndef M_PI_2
136 #define M_PI_2 1.570796326794896619
137 #endif
138
139
140 /* Macros to define assembler functions somewhat portably */
141
142 #ifdef __GNUC__
143 # define __ASM_GLOBAL_FUNC(name,code) \
144 __asm__( ".align 4\n\t" \
145 ".globl " __ASM_NAME(#name) "\n\t" \
146 __ASM_FUNC(#name) "\n" \
147 __ASM_NAME(#name) ":\n\t" \
148 code );
149 #else /* __GNUC__ */
150 # define __ASM_GLOBAL_FUNC(name,code) \
151 void __asm_dummy_##name(void) { \
152 asm( ".align 4\n\t" \
153 ".globl " __ASM_NAME(#name) "\n\t" \
154 __ASM_FUNC(#name) "\n" \
155 __ASM_NAME(#name) ":\n\t" \
156 code ); \
157 }
158 #endif /* __GNUC__ */
159
160
161 /* Constructor functions */
162
163 #ifdef __GNUC__
164 # define DECL_GLOBAL_CONSTRUCTOR(func) \
165 static void func(void) __attribute__((constructor)); \
166 static void func(void)
167 #elif defined(__i386__)
168 # define DECL_GLOBAL_CONSTRUCTOR(func) \
169 static void __dummy_init_##func(void) { \
170 asm(".section .init,\"ax\"\n\t" \
171 "call " #func "\n\t" \
172 ".previous"); } \
173 static void func(void)
174 #elif defined(__sparc__)
175 # define DECL_GLOBAL_CONSTRUCTOR(func) \
176 static void __dummy_init_##func(void) { \
177 asm("\t.section \".init\",#alloc,#execinstr\n" \
178 "\tcall " #func "\n" \
179 "\tnop\n" \
180 "\t.section \".text\",#alloc,#execinstr\n" ); } \
181 static void func(void)
182 #else
183 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
184 #endif
185
186
187 /* Register functions */
188
189 #ifdef __i386__
190 #define DEFINE_REGS_ENTRYPOINT( name, fn, args, pop_args ) \
191 __ASM_GLOBAL_FUNC( name, \
192 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t" \
193 ".long " __ASM_NAME(#fn) "\n\t" \
194 ".byte " #args "," #pop_args )
195 /* FIXME: add support for other CPUs */
196 #endif /* __i386__ */
197
198
199 /****************************************************************
200 * Function definitions (only when using libwine_port)
201 */
202
203 #ifndef NO_LIBWINE_PORT
204
205 #ifndef HAVE_GETOPT_LONG
206 extern char *optarg;
207 extern int optind;
208 extern int opterr;
209 extern int optopt;
210 struct option;
211
212 #ifndef HAVE_STRUCT_OPTION_NAME
213 struct option
214 {
215 const char *name;
216 int has_arg;
217 int *flag;
218 int val;
219 };
220 #endif
221
222 extern int getopt_long (int ___argc, char *const *___argv,
223 const char *__shortopts,
224 const struct option *__longopts, int *__longind);
225 extern int getopt_long_only (int ___argc, char *const *___argv,
226 const char *__shortopts,
227 const struct option *__longopts, int *__longind);
228 #endif /* HAVE_GETOPT_LONG */
229
230 #ifndef HAVE_GETPAGESIZE
231 size_t getpagesize(void);
232 #endif /* HAVE_GETPAGESIZE */
233
234 #ifndef HAVE_GETTID
235 pid_t gettid(void);
236 #endif /* HAVE_GETTID */
237
238 #ifndef HAVE_LSTAT
239 int lstat(const char *file_name, struct stat *buf);
240 #endif /* HAVE_LSTAT */
241
242 #ifndef HAVE_MEMMOVE
243 void *memmove(void *dest, const void *src, size_t len);
244 #endif /* !defined(HAVE_MEMMOVE) */
245
246 #ifndef __REACTOS__
247 #ifndef HAVE_PREAD
248 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
249 #endif /* HAVE_PREAD */
250
251 #ifndef HAVE_PWRITE
252 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
253 #endif /* HAVE_PWRITE */
254 #endif /* __REACTOS__ */
255
256 #ifdef WIN32
257 #ifndef HAVE_SIGSETJMP
258 # include <setjmp.h>
259 typedef jmp_buf sigjmp_buf;
260 int sigsetjmp( sigjmp_buf buf, int savesigs );
261 void siglongjmp( sigjmp_buf buf, int val );
262 #endif /* HAVE_SIGSETJMP */
263 #endif
264
265 #ifndef HAVE_STATFS
266 int statfs(const char *name, struct statfs *info);
267 #endif /* !defined(HAVE_STATFS) */
268
269 #ifndef HAVE_STRNCASECMP
270 # ifndef HAVE__STRNICMP
271 int strncasecmp(const char *str1, const char *str2, size_t n);
272 # else
273 # define strncasecmp _strnicmp
274 # endif
275 #endif /* !defined(HAVE_STRNCASECMP) */
276
277 #ifndef HAVE_STRERROR
278 const char *strerror(int err);
279 #endif /* !defined(HAVE_STRERROR) */
280
281 #ifndef HAVE_STRCASECMP
282 # ifndef HAVE__STRICMP
283 int strcasecmp(const char *str1, const char *str2);
284 # else
285 # define strcasecmp _stricmp
286 # endif
287 #endif /* !defined(HAVE_STRCASECMP) */
288
289 #ifndef HAVE_USLEEP
290 int usleep (unsigned int useconds);
291 #endif /* !defined(HAVE_USLEEP) */
292
293 #ifdef __i386__
294 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
295 {
296 return memcpy( dst, src, size );
297 }
298 #else
299 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
300 #endif /* __i386__ */
301
302 extern int mkstemps(char *template, int suffix_len);
303
304 /* Process creation flags */
305 #ifndef _P_WAIT
306 # define _P_WAIT 0
307 # define _P_NOWAIT 1
308 # define _P_OVERLAY 2
309 # define _P_NOWAITO 3
310 # define _P_DETACH 4
311 #endif
312 #ifndef HAVE_SPAWNVP
313 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
314 #endif
315
316 /* Interlocked functions */
317
318 #if defined(__i386__) && defined(__GNUC__)
319
320 extern inline long interlocked_cmpxchg( long *dest, long xchg, long compare )
321 {
322 long ret;
323 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
324 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
325 return ret;
326 }
327
328 extern inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
329 {
330 void *ret;
331 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
332 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
333 return ret;
334 }
335
336 extern inline long interlocked_xchg( long *dest, long val )
337 {
338 long ret;
339 __asm__ __volatile__( "lock; xchgl %0,(%1)"
340 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
341 return ret;
342 }
343
344 extern inline void *interlocked_xchg_ptr( void **dest, void *val )
345 {
346 void *ret;
347 __asm__ __volatile__( "lock; xchgl %0,(%1)"
348 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
349 return ret;
350 }
351
352 extern inline long interlocked_xchg_add( long *dest, long incr )
353 {
354 long ret;
355 __asm__ __volatile__( "lock; xaddl %0,(%1)"
356 : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
357 return ret;
358 }
359
360 #else /* __i386___ && __GNUC__ */
361
362 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
363 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
364 extern long interlocked_xchg( long *dest, long val );
365 extern void *interlocked_xchg_ptr( void **dest, void *val );
366 extern long interlocked_xchg_add( long *dest, long incr );
367
368 #endif /* __i386___ && __GNUC__ */
369
370 #else /* NO_LIBWINE_PORT */
371
372 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
373
374 #define getopt_long __WINE_NOT_PORTABLE(getopt_long)
375 #define getopt_long_only __WINE_NOT_PORTABLE(getopt_long_only)
376 #define getpagesize __WINE_NOT_PORTABLE(getpagesize)
377 #define interlocked_cmpxchg __WINE_NOT_PORTABLE(interlocked_cmpxchg)
378 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
379 #define interlocked_xchg __WINE_NOT_PORTABLE(interlocked_xchg)
380 #define interlocked_xchg_ptr __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
381 #define interlocked_xchg_add __WINE_NOT_PORTABLE(interlocked_xchg_add)
382 #define lstat __WINE_NOT_PORTABLE(lstat)
383 #define memcpy_unaligned __WINE_NOT_PORTABLE(memcpy_unaligned)
384 #define memmove __WINE_NOT_PORTABLE(memmove)
385 #define pread __WINE_NOT_PORTABLE(pread)
386 #define pwrite __WINE_NOT_PORTABLE(pwrite)
387 #define spawnvp __WINE_NOT_PORTABLE(spawnvp)
388 #define statfs __WINE_NOT_PORTABLE(statfs)
389 #define strcasecmp __WINE_NOT_PORTABLE(strcasecmp)
390 #define strerror __WINE_NOT_PORTABLE(strerror)
391 #define strncasecmp __WINE_NOT_PORTABLE(strncasecmp)
392 #define usleep __WINE_NOT_PORTABLE(usleep)
393
394 #endif /* NO_LIBWINE_PORT */
395
396 #endif /* !defined(__WINE_WINE_PORT_H) */