Sync with trunk rev.61910 to get latest improvements and bugfixes.
[reactos.git] / include / reactos / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #ifndef _GNU_SOURCE
29 # define _GNU_SOURCE /* for pread/pwrite, isfinite */
30 #endif
31 #include <fcntl.h>
32 #include <math.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #ifdef HAVE_DIRECT_H
36 # include <direct.h>
37 #endif
38 #ifdef HAVE_IO_H
39 # include <io.h>
40 #endif
41 #ifdef HAVE_PROCESS_H
42 # include <process.h>
43 #endif
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49
50 /****************************************************************
51 * Type definitions
52 */
53
54 #if !defined(_MSC_VER) && !defined(__int64)
55 # if defined(__x86_64__) || defined(_WIN64)
56 # define __int64 long
57 # else
58 # define __int64 long long
59 # endif
60 #endif
61
62 #if !defined(HAVE_MODE_T) && !defined(_MODE_T)
63 typedef int mode_t;
64 #endif
65 #if !defined(HAVE_OFF_T) && !defined(_OFF_T)
66 typedef long off_t;
67 #endif
68 #if !defined(HAVE_PID_T) && !defined(_PID_T)
69 typedef int pid_t;
70 #endif
71 #if !defined(HAVE_SIZE_T) && !defined(_SIZE_T)
72 typedef unsigned int size_t;
73 #endif
74 #if !defined(HAVE_SSIZE_T) && !defined(_SSIZE_T)
75 typedef int ssize_t;
76 #endif
77 //#ifndef HAVE_SOCKLEN_T
78 //typedef unsigned int socklen_t;
79 //#endif
80
81 #ifndef HAVE_STATFS
82 # ifdef __BEOS__
83 # define HAVE_STRUCT_STATFS_F_BFREE
84 struct statfs {
85 long f_bsize; /* block_size */
86 long f_blocks; /* total_blocks */
87 long f_bfree; /* free_blocks */
88 };
89 # else /* defined(__BEOS__) */
90 struct statfs;
91 # endif /* defined(__BEOS__) */
92 #endif /* !defined(HAVE_STATFS) */
93
94
95 /****************************************************************
96 * Macro definitions
97 */
98
99 #ifdef HAVE_DLFCN_H
100 #include <dlfcn.h>
101 #else
102 #define RTLD_LAZY 0x001
103 #define RTLD_NOW 0x002
104 #define RTLD_GLOBAL 0x100
105 #endif
106
107 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
108 #define ftruncate chsize
109 #endif
110
111 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
112 #define popen _popen
113 #endif
114
115 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
116 #define pclose _pclose
117 #endif
118
119 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
120 #define snprintf _snprintf
121 #endif
122
123 #if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF)
124 #define vsnprintf _vsnprintf
125 #endif
126
127 #ifndef S_ISLNK
128 # define S_ISLNK(mod) (0)
129 #endif /* S_ISLNK */
130
131 /* So we open files in 64 bit access mode on Linux */
132 #ifndef O_LARGEFILE
133 # define O_LARGEFILE 0
134 #endif
135
136 #ifndef O_BINARY
137 # define O_BINARY 0
138 #endif
139
140 /****************************************************************
141 * Constants
142 */
143
144 #ifndef M_PI
145 #define M_PI 3.14159265358979323846
146 #endif
147
148 #ifndef M_PI_2
149 #define M_PI_2 1.570796326794896619
150 #endif
151
152 #ifndef M_PI_4
153 #define M_PI_4 0.785398163397448309616
154 #endif
155
156 #ifndef INFINITY
157 static inline float __port_infinity(void)
158 {
159 static const unsigned __inf_bytes = 0x7f800000;
160 return *(const float *)&__inf_bytes;
161 }
162 #define INFINITY __port_infinity()
163 #endif
164
165 #ifndef NAN
166 static inline float __port_nan(void)
167 {
168 static const unsigned __nan_bytes = 0x7fc00000;
169 return *(const float *)&__nan_bytes;
170 }
171 #define NAN __port_nan()
172 #endif
173
174 /* Constructor functions */
175
176 #ifdef _MSC_VER
177 # define DECL_GLOBAL_CONSTRUCTOR(func) /* nothing */
178 #elif defined(__GNUC__)
179 # define DECL_GLOBAL_CONSTRUCTOR(func) \
180 static void func(void) __attribute__((constructor)); \
181 static void func(void)
182 #elif defined(__i386__)
183 # define DECL_GLOBAL_CONSTRUCTOR(func) \
184 static void __dummy_init_##func(void) { \
185 asm(".section .init,\"ax\"\n\t" \
186 "call " #func "\n\t" \
187 ".previous"); } \
188 static void func(void)
189 #elif defined(__sparc__)
190 # define DECL_GLOBAL_CONSTRUCTOR(func) \
191 static void __dummy_init_##func(void) { \
192 asm("\t.section \".init\",#alloc,#execinstr\n" \
193 "\tcall " #func "\n" \
194 "\tnop\n" \
195 "\t.section \".text\",#alloc,#execinstr\n" ); } \
196 static void func(void)
197 #elif defined(_M_AMD64)
198 #pragma message("You must define the DECL_GLOBAL_CONSTRUCTOR macro for amd64")
199 #else
200 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
201 #endif
202
203
204 /* Register functions */
205
206 #ifdef __i386__
207 #define DEFINE_REGS_ENTRYPOINT( name, fn, args, pop_args ) \
208 __ASM_GLOBAL_FUNC( name, \
209 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t" \
210 ".long " __ASM_NAME(#fn) "\n\t" \
211 ".byte " #args "," #pop_args )
212 /* FIXME: add support for other CPUs */
213 #endif /* __i386__ */
214
215
216 /****************************************************************
217 * Function definitions (only when using libwine_port)
218 */
219
220 #ifndef NO_LIBWINE_PORT
221
222 #ifndef HAVE_GETOPT_LONG
223 extern char *optarg;
224 extern int optind;
225 extern int opterr;
226 extern int optopt;
227 struct option;
228
229 #ifndef HAVE_STRUCT_OPTION_NAME
230 struct option
231 {
232 const char *name;
233 int has_arg;
234 int *flag;
235 int val;
236 };
237 #endif
238
239 extern int getopt_long (int ___argc, char *const *___argv,
240 const char *__shortopts,
241 const struct option *__longopts, int *__longind);
242 extern int getopt_long_only (int ___argc, char *const *___argv,
243 const char *__shortopts,
244 const struct option *__longopts, int *__longind);
245 #endif /* HAVE_GETOPT_LONG */
246
247 #ifndef HAVE_GETPAGESIZE
248 size_t getpagesize(void);
249 #endif /* HAVE_GETPAGESIZE */
250
251 #if !defined(HAVE_ISFINITE) && !defined(isfinite)
252 int isfinite(double x);
253 #endif
254
255 #if !defined(HAVE_ISINF) && !defined(isinf)
256 int isinf(double x);
257 #endif
258
259 #if !defined(HAVE_ISNAN) && !defined(isnan)
260 int isnan(double x);
261 #endif
262
263 #ifndef HAVE_LSTAT
264 int lstat(const char *file_name, struct stat *buf);
265 #endif /* HAVE_LSTAT */
266
267 #ifndef HAVE_MEMMOVE
268 void *memmove(void *dest, const void *src, size_t len);
269 #endif /* !defined(HAVE_MEMMOVE) */
270
271 #ifndef __REACTOS__
272 #ifndef HAVE_PREAD
273 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
274 #endif /* HAVE_PREAD */
275
276 #ifndef HAVE_PWRITE
277 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
278 #endif /* HAVE_PWRITE */
279 #endif /* __REACTOS__ */
280
281 #ifdef WIN32
282 #ifndef HAVE_SIGSETJMP
283 # include <setjmp.h>
284 typedef jmp_buf sigjmp_buf;
285 int sigsetjmp( sigjmp_buf buf, int savesigs );
286 void siglongjmp( sigjmp_buf buf, int val );
287 #endif /* HAVE_SIGSETJMP */
288 #endif
289
290 #ifndef HAVE_STATFS
291 int statfs(const char *name, struct statfs *info);
292 #endif /* !defined(HAVE_STATFS) */
293
294 #ifndef HAVE_STRNCASECMP
295 # ifndef HAVE__STRNICMP
296 int strncasecmp(const char *str1, const char *str2, size_t n);
297 # else
298 # define strncasecmp _strnicmp
299 # endif
300 #endif /* !defined(HAVE_STRNCASECMP) */
301
302 #ifndef HAVE_STRERROR
303 const char *strerror(int err);
304 #endif /* !defined(HAVE_STRERROR) */
305
306 #ifndef HAVE_STRCASECMP
307 # ifndef HAVE__STRICMP
308 int strcasecmp(const char *str1, const char *str2);
309 # else
310 # define strcasecmp _stricmp
311 # endif
312 #endif /* !defined(HAVE_STRCASECMP) */
313
314 #if !defined(HAVE_USLEEP) && !defined(__CYGWIN__)
315 int usleep (unsigned int useconds);
316 #endif /* !defined(HAVE_USLEEP) */
317
318 #ifdef __i386__
319 static inline void *memcpy_unaligned( void *dst, const void *src, size_t size )
320 {
321 return memcpy( dst, src, size );
322 }
323 #else
324 extern void *memcpy_unaligned( void *dst, const void *src, size_t size );
325 #endif /* __i386__ */
326
327 extern int mkstemps(char *template, int suffix_len);
328
329 /* Process creation flags */
330 #ifndef _P_WAIT
331 # define _P_WAIT 0
332 # define _P_NOWAIT 1
333 # define _P_OVERLAY 2
334 # define _P_NOWAITO 3
335 # define _P_DETACH 4
336 #endif
337 #ifndef HAVE_SPAWNVP
338 extern int spawnvp(int mode, const char *cmdname, const char * const argv[]);
339 #endif
340
341 /* Interlocked functions */
342
343 #if defined(_MSC_VER) || (defined(__i386__) && defined(__GNUC__) && !defined(WINE_PORT_NO_INTERLOCKED))
344
345 #define interlocked_cmpxchg InterlockedCompareExchange
346 #define interlocked_cmpxchg_ptr InterlockedCompareExchangePtr
347 #define interlocked_xchg InterlockedExchange
348 #define interlocked_xchg_ptr InterlockedExchangePtr
349 #define interlocked_xchg_add InterlockedExchangeAdd
350
351
352 #endif /* __i386___ && __GNUC__ */
353
354 #if defined(_MSC_VER)
355 __forceinline
356 int
357 ffs(int mask)
358 {
359 long index;
360 if (_BitScanForward(&index, mask) == 0) return 0;
361 return index;
362 }
363 #else
364 #define ffs __builtin_ffs
365 #endif
366
367 #else /* NO_LIBWINE_PORT */
368
369 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
370
371 #define getopt_long __WINE_NOT_PORTABLE(getopt_long)
372 #define getopt_long_only __WINE_NOT_PORTABLE(getopt_long_only)
373 #define getpagesize __WINE_NOT_PORTABLE(getpagesize)
374 #define lstat __WINE_NOT_PORTABLE(lstat)
375 #define memcpy_unaligned __WINE_NOT_PORTABLE(memcpy_unaligned)
376 #define memmove __WINE_NOT_PORTABLE(memmove)
377 #define pread __WINE_NOT_PORTABLE(pread)
378 #define pwrite __WINE_NOT_PORTABLE(pwrite)
379 #define spawnvp __WINE_NOT_PORTABLE(spawnvp)
380 #define statfs __WINE_NOT_PORTABLE(statfs)
381 #define strcasecmp __WINE_NOT_PORTABLE(strcasecmp)
382 #define strerror __WINE_NOT_PORTABLE(strerror)
383 #define strncasecmp __WINE_NOT_PORTABLE(strncasecmp)
384 #define usleep __WINE_NOT_PORTABLE(usleep)
385
386 #endif /* NO_LIBWINE_PORT */
387
388 #endif /* !defined(__WINE_WINE_PORT_H) */