ddraw/tests: Sync test_clear() with d3d8/9.
[wine.git] / include / wine / port.h
blobfb7251edd6c5e2224bece50a43216c2eb3b7fb6d
1 /*
2 * Wine porting definitions
4 * Copyright 1996 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_PORT_H
22 #define __WINE_WINE_PORT_H
24 #ifndef __WINE_CONFIG_H
25 # error You must include config.h to use this header
26 #endif
28 #ifdef __WINE_BASETSD_H
29 # error You must include port.h before all other headers
30 #endif
32 #ifndef _GNU_SOURCE
33 # define _GNU_SOURCE /* for pread/pwrite, isfinite */
34 #endif
35 #include <fcntl.h>
36 #include <math.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_DIRECT_H
40 # include <direct.h>
41 #endif
42 #ifdef HAVE_IO_H
43 # include <io.h>
44 #endif
45 #ifdef HAVE_PROCESS_H
46 # include <process.h>
47 #endif
48 #include <string.h>
49 #include <stdlib.h>
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
55 /****************************************************************
56 * Type definitions
59 #if !defined(_MSC_VER) && !defined(__int64)
60 # if defined(__x86_64__) || defined(__aarch64__) || defined(_WIN64)
61 # define __int64 long
62 # else
63 # define __int64 long long
64 # endif
65 #endif
67 #ifndef HAVE_MODE_T
68 typedef int mode_t;
69 #endif
70 #ifndef HAVE_OFF_T
71 typedef long off_t;
72 #endif
73 #ifndef HAVE_PID_T
74 typedef int pid_t;
75 #endif
76 #ifndef HAVE_SIZE_T
77 typedef unsigned int size_t;
78 #endif
79 #ifndef HAVE_SSIZE_T
80 typedef int ssize_t;
81 #endif
82 #ifndef HAVE_FSBLKCNT_T
83 typedef unsigned long fsblkcnt_t;
84 #endif
85 #ifndef HAVE_FSFILCNT_T
86 typedef unsigned long fsfilcnt_t;
87 #endif
89 #ifndef HAVE_STRUCT_STATVFS_F_BLOCKS
90 struct statvfs
92 unsigned long f_bsize;
93 unsigned long f_frsize;
94 fsblkcnt_t f_blocks;
95 fsblkcnt_t f_bfree;
96 fsblkcnt_t f_bavail;
97 fsfilcnt_t f_files;
98 fsfilcnt_t f_ffree;
99 fsfilcnt_t f_favail;
100 unsigned long f_fsid;
101 unsigned long f_flag;
102 unsigned long f_namemax;
104 #endif /* HAVE_STRUCT_STATVFS_F_BLOCKS */
107 /****************************************************************
108 * Macro definitions
111 #ifdef HAVE_DLFCN_H
112 #include <dlfcn.h>
113 #else
114 #define RTLD_LAZY 0x001
115 #define RTLD_NOW 0x002
116 #define RTLD_GLOBAL 0x100
117 #endif
119 #ifdef HAVE_ONE_ARG_MKDIR
120 #define mkdir(path,mode) mkdir(path)
121 #endif
123 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
124 #define ftruncate chsize
125 #endif
127 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
128 #define popen _popen
129 #endif
131 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
132 #define pclose _pclose
133 #endif
135 #if !defined(HAVE_STRDUP) && defined(HAVE__STRDUP)
136 #define strdup _strdup
137 #endif
139 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
140 #define snprintf _snprintf
141 #endif
143 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
144 #define vsnprintf _vsnprintf
145 #endif
147 #if !defined(HAVE_STRTOLL) && defined(HAVE__STRTOI64)
148 #define strtoll _strtoi64
149 #endif
151 #if !defined(HAVE_STRTOULL) && defined(HAVE__STRTOUI64)
152 #define strtoull _strtoui64
153 #endif
155 #ifndef S_ISLNK
156 # define S_ISLNK(mod) (0)
157 #endif
159 #ifndef S_ISSOCK
160 # define S_ISSOCK(mod) (0)
161 #endif
163 #ifndef S_ISDIR
164 # define S_ISDIR(mod) (((mod) & _S_IFMT) == _S_IFDIR)
165 #endif
167 #ifndef S_ISCHR
168 # define S_ISCHR(mod) (((mod) & _S_IFMT) == _S_IFCHR)
169 #endif
171 #ifndef S_ISFIFO
172 # define S_ISFIFO(mod) (((mod) & _S_IFMT) == _S_IFIFO)
173 #endif
175 #ifndef S_ISREG
176 # define S_ISREG(mod) (((mod) & _S_IFMT) == _S_IFREG)
177 #endif
179 /* So we open files in 64 bit access mode on Linux */
180 #ifndef O_LARGEFILE
181 # define O_LARGEFILE 0
182 #endif
184 #ifndef O_NONBLOCK
185 # define O_NONBLOCK 0
186 #endif
188 #ifndef O_BINARY
189 # define O_BINARY 0
190 #endif
193 /****************************************************************
194 * Constants
197 #ifndef M_PI
198 #define M_PI 3.14159265358979323846
199 #endif
201 #ifndef M_PI_2
202 #define M_PI_2 1.570796326794896619
203 #endif
205 #ifndef INFINITY
206 static inline float __port_infinity(void)
208 static const unsigned __inf_bytes = 0x7f800000;
209 return *(const float *)&__inf_bytes;
211 #define INFINITY __port_infinity()
212 #endif
214 #ifndef NAN
215 static inline float __port_nan(void)
217 static const unsigned __nan_bytes = 0x7fc00000;
218 return *(const float *)&__nan_bytes;
220 #define NAN __port_nan()
221 #endif
224 /****************************************************************
225 * Function definitions (only when using libwine_port)
228 #ifndef NO_LIBWINE_PORT
230 #ifndef HAVE_FSTATVFS
231 int fstatvfs( int fd, struct statvfs *buf );
232 #endif
234 #ifndef HAVE_GETOPT_LONG_ONLY
235 extern char *optarg;
236 extern int optind;
237 extern int opterr;
238 extern int optopt;
239 struct option;
241 #ifndef HAVE_STRUCT_OPTION_NAME
242 struct option
244 const char *name;
245 int has_arg;
246 int *flag;
247 int val;
249 #endif
251 extern int getopt_long (int ___argc, char *const *___argv,
252 const char *__shortopts,
253 const struct option *__longopts, int *__longind);
254 extern int getopt_long_only (int ___argc, char *const *___argv,
255 const char *__shortopts,
256 const struct option *__longopts, int *__longind);
257 #endif /* HAVE_GETOPT_LONG_ONLY */
259 #ifndef HAVE_FFS
260 int ffs( int x );
261 #endif
263 #ifndef HAVE_ISFINITE
264 int isfinite(double x);
265 #endif
267 #ifndef HAVE_ISINF
268 int isinf(double x);
269 #endif
271 #ifndef HAVE_ISNAN
272 int isnan(double x);
273 #endif
275 #ifndef HAVE_LLRINT
276 __int64 llrint(double x);
277 #endif
279 #ifndef HAVE_LLRINTF
280 __int64 llrintf(float x);
281 #endif
283 #ifndef HAVE_LRINT
284 long lrint(double x);
285 #endif
287 #ifndef HAVE_LRINTF
288 long lrintf(float x);
289 #endif
291 #ifndef HAVE_LSTAT
292 int lstat(const char *file_name, struct stat *buf);
293 #endif /* HAVE_LSTAT */
295 #ifndef HAVE_MEMMOVE
296 void *memmove(void *dest, const void *src, size_t len);
297 #endif /* !defined(HAVE_MEMMOVE) */
299 #ifndef HAVE_POLL
300 struct pollfd
302 int fd;
303 short events;
304 short revents;
306 #define POLLIN 0x01
307 #define POLLPRI 0x02
308 #define POLLOUT 0x04
309 #define POLLERR 0x08
310 #define POLLHUP 0x10
311 #define POLLNVAL 0x20
312 int poll( struct pollfd *fds, unsigned int count, int timeout );
313 #endif /* HAVE_POLL */
315 #ifndef HAVE_PREAD
316 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
317 #endif /* HAVE_PREAD */
319 #ifndef HAVE_PWRITE
320 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
321 #endif /* HAVE_PWRITE */
323 #ifndef HAVE_READLINK
324 int readlink( const char *path, char *buf, size_t size );
325 #endif /* HAVE_READLINK */
327 #ifndef HAVE_RINT
328 double rint(double x);
329 #endif
331 #ifndef HAVE_RINTF
332 float rintf(float x);
333 #endif
335 #ifndef HAVE_STATVFS
336 int statvfs( const char *path, struct statvfs *buf );
337 #endif
339 #ifndef HAVE_STRNCASECMP
340 # ifndef HAVE__STRNICMP
341 int strncasecmp(const char *str1, const char *str2, size_t n);
342 # else
343 # define strncasecmp _strnicmp
344 # endif
345 #endif /* !defined(HAVE_STRNCASECMP) */
347 #ifndef HAVE_STRNLEN
348 size_t strnlen( const char *str, size_t maxlen );
349 #endif /* !defined(HAVE_STRNLEN) */
351 #ifndef HAVE_STRERROR
352 const char *strerror(int err);
353 #endif /* !defined(HAVE_STRERROR) */
355 #ifndef HAVE_STRCASECMP
356 # ifndef HAVE__STRICMP
357 int strcasecmp(const char *str1, const char *str2);
358 # else
359 # define strcasecmp _stricmp
360 # endif
361 #endif /* !defined(HAVE_STRCASECMP) */
363 #ifndef HAVE_SYMLINK
364 int symlink(const char *from, const char *to);
365 #endif
367 #ifndef HAVE_USLEEP
368 int usleep (unsigned int useconds);
369 #endif /* !defined(HAVE_USLEEP) */
371 #ifdef __i386__
372 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
374 return memcpy( dst, src, size );
376 #else
377 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
378 #endif /* __i386__ */
380 extern int mkstemps(char *template, int suffix_len);
382 /* Process creation flags */
383 #ifndef _P_WAIT
384 # define _P_WAIT 0
385 # define _P_NOWAIT 1
386 # define _P_OVERLAY 2
387 # define _P_NOWAITO 3
388 # define _P_DETACH 4
389 #endif
390 #ifndef HAVE__SPAWNVP
391 extern int _spawnvp(int mode, const char *cmdname, const char * const argv[]);
392 #endif
394 /* Interlocked functions */
396 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
398 static inline int interlocked_cmpxchg( int *dest, int xchg, int compare )
400 int ret;
401 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
402 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
403 return ret;
406 static inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
408 void *ret;
409 #ifdef __x86_64__
410 __asm__ __volatile__( "lock; cmpxchgq %2,(%1)"
411 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
412 #else
413 __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
414 : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
415 #endif
416 return ret;
419 static inline int interlocked_xchg( int *dest, int val )
421 int ret;
422 __asm__ __volatile__( "lock; xchgl %0,(%1)"
423 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
424 return ret;
427 static inline void *interlocked_xchg_ptr( void **dest, void *val )
429 void *ret;
430 #ifdef __x86_64__
431 __asm__ __volatile__( "lock; xchgq %0,(%1)"
432 : "=r" (ret) :"r" (dest), "0" (val) : "memory" );
433 #else
434 __asm__ __volatile__( "lock; xchgl %0,(%1)"
435 : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
436 #endif
437 return ret;
440 static inline int interlocked_xchg_add( int *dest, int incr )
442 int ret;
443 __asm__ __volatile__( "lock; xaddl %0,(%1)"
444 : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
445 return ret;
448 #ifdef __x86_64__
449 static inline unsigned char interlocked_cmpxchg128( __int64 *dest, __int64 xchg_high,
450 __int64 xchg_low, __int64 *compare )
452 unsigned char ret;
453 __asm__ __volatile__( "lock cmpxchg16b %0; setz %b2"
454 : "=m" (dest[0]), "=m" (dest[1]), "=r" (ret),
455 "=a" (compare[0]), "=d" (compare[1])
456 : "m" (dest[0]), "m" (dest[1]), "3" (compare[0]), "4" (compare[1]),
457 "c" (xchg_high), "b" (xchg_low) );
458 return ret;
460 #endif
462 #else /* __GNUC__ */
464 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
465 static inline int interlocked_cmpxchg( int *dest, int xchg, int compare )
467 return __sync_val_compare_and_swap( dest, compare, xchg );
470 static inline int interlocked_xchg_add( int *dest, int incr )
472 return __sync_fetch_and_add( dest, incr );
475 static inline int interlocked_xchg( int *dest, int val )
477 int ret;
478 do ret = *dest; while (!__sync_bool_compare_and_swap( dest, ret, val ));
479 return ret;
481 #else
482 extern int interlocked_cmpxchg( int *dest, int xchg, int compare );
483 extern int interlocked_xchg_add( int *dest, int incr );
484 extern int interlocked_xchg( int *dest, int val );
485 #endif
487 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __SIZEOF_POINTER__ == 4) \
488 || (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __SIZEOF_POINTER__ == 8)
489 static inline void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
491 return __sync_val_compare_and_swap( dest, compare, xchg );
494 static inline void *interlocked_xchg_ptr( void **dest, void *val )
496 void *ret;
497 do ret = *dest; while (!__sync_bool_compare_and_swap( dest, ret, val ));
498 return ret;
500 #else
501 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
502 extern void *interlocked_xchg_ptr( void **dest, void *val );
503 #endif
505 #if defined(__x86_64__) || defined(__aarch64__) || defined(_WIN64)
506 extern unsigned char interlocked_cmpxchg128( __int64 *dest, __int64 xchg_high,
507 __int64 xchg_low, __int64 *compare );
508 #endif
510 #endif /* __GNUC__ */
512 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
513 static inline __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare )
515 return __sync_val_compare_and_swap( dest, compare, xchg );
517 #else
518 extern __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare );
519 #endif
521 #else /* NO_LIBWINE_PORT */
523 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
525 #define ffs __WINE_NOT_PORTABLE(ffs)
526 #define fstatvfs __WINE_NOT_PORTABLE(fstatvfs)
527 #define getopt_long __WINE_NOT_PORTABLE(getopt_long)
528 #define getopt_long_only __WINE_NOT_PORTABLE(getopt_long_only)
529 #define interlocked_cmpxchg __WINE_NOT_PORTABLE(interlocked_cmpxchg)
530 #define interlocked_cmpxchg_ptr __WINE_NOT_PORTABLE(interlocked_cmpxchg_ptr)
531 #define interlocked_xchg __WINE_NOT_PORTABLE(interlocked_xchg)
532 #define interlocked_xchg_ptr __WINE_NOT_PORTABLE(interlocked_xchg_ptr)
533 #define interlocked_xchg_add __WINE_NOT_PORTABLE(interlocked_xchg_add)
534 #define lstat __WINE_NOT_PORTABLE(lstat)
535 #define memcpy_unaligned __WINE_NOT_PORTABLE(memcpy_unaligned)
536 #undef memmove
537 #define memmove __WINE_NOT_PORTABLE(memmove)
538 #define pread __WINE_NOT_PORTABLE(pread)
539 #define pwrite __WINE_NOT_PORTABLE(pwrite)
540 #define spawnvp __WINE_NOT_PORTABLE(spawnvp)
541 #define statvfs __WINE_NOT_PORTABLE(statvfs)
542 #define strcasecmp __WINE_NOT_PORTABLE(strcasecmp)
543 #define strerror __WINE_NOT_PORTABLE(strerror)
544 #define strncasecmp __WINE_NOT_PORTABLE(strncasecmp)
545 #define strnlen __WINE_NOT_PORTABLE(strnlen)
546 #define usleep __WINE_NOT_PORTABLE(usleep)
548 #endif /* NO_LIBWINE_PORT */
550 #endif /* !defined(__WINE_WINE_PORT_H) */