Added wine_dbg_sprintf function that allocates a temporary buffer in
[wine] / 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 <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_DIRECT_H
33 # include <direct.h>
34 #endif
35 #ifdef HAVE_IO_H
36 # include <io.h>
37 #endif
38 #include <string.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42
43
44 /****************************************************************
45  * Type definitions
46  */
47
48 #ifndef HAVE_MODE_T
49 typedef int mode_t;
50 #endif
51 #ifndef HAVE_OFF_T
52 typedef long off_t;
53 #endif
54 #ifndef HAVE_PID_T
55 typedef int pid_t;
56 #endif
57 #ifndef HAVE_SIZE_T
58 typedef unsigned int size_t;
59 #endif
60 #ifndef HAVE_SSIZE_T
61 typedef int ssize_t;
62 #endif
63
64 #ifndef HAVE_STATFS
65 # ifdef __BEOS__
66 #  define STATFS_HAS_BFREE
67 struct statfs {
68   long   f_bsize;  /* block_size */
69   long   f_blocks; /* total_blocks */
70   long   f_bfree;  /* free_blocks */
71 };
72 # else /* defined(__BEOS__) */
73 struct statfs;
74 # endif /* defined(__BEOS__) */
75 #endif /* !defined(HAVE_STATFS) */
76
77
78 /****************************************************************
79  * Macro definitions
80  */
81
82 #ifdef HAVE_DLFCN_H
83 #include <dlfcn.h>
84 #else
85 #define RTLD_LAZY    0x001
86 #define RTLD_NOW     0x002
87 #define RTLD_GLOBAL  0x100
88 #endif
89
90 #if !defined(HAVE_FTRUNCATE) && defined(HAVE_CHSIZE)
91 #define ftruncate chsize
92 #endif
93
94 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
95 #define popen _popen
96 #endif
97
98 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
99 #define pclose _pclose
100 #endif
101
102 #if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
103 #define snprintf _snprintf
104 #endif
105
106 #ifndef S_ISLNK
107 # define S_ISLNK(mod) (0)
108 #endif /* S_ISLNK */
109
110 /* So we open files in 64 bit access mode on Linux */
111 #ifndef O_LARGEFILE
112 # define O_LARGEFILE 0
113 #endif
114
115 /* Macros to define assembler functions somewhat portably */
116
117 #ifdef __GNUC__
118 # define __ASM_GLOBAL_FUNC(name,code) \
119       __asm__( ".align 4\n\t" \
120                ".globl " __ASM_NAME(#name) "\n\t" \
121                __ASM_FUNC(#name) "\n" \
122                __ASM_NAME(#name) ":\n\t" \
123                code );
124 #else  /* __GNUC__ */
125 # define __ASM_GLOBAL_FUNC(name,code) \
126       void __asm_dummy_##name(void) { \
127           asm( ".align 4\n\t" \
128                ".globl " __ASM_NAME(#name) "\n\t" \
129                __ASM_FUNC(#name) "\n" \
130                __ASM_NAME(#name) ":\n\t" \
131                code ); \
132       }
133 #endif  /* __GNUC__ */
134
135
136 /* Constructor functions */
137
138 #ifdef __GNUC__
139 # define DECL_GLOBAL_CONSTRUCTOR(func) \
140     static void func(void) __attribute__((constructor)); \
141     static void func(void)
142 #elif defined(__i386__)
143 # define DECL_GLOBAL_CONSTRUCTOR(func) \
144     static void __dummy_init_##func(void) { \
145         asm(".section .init,\"ax\"\n\t" \
146             "call " #func "\n\t" \
147             ".previous"); } \
148     static void func(void)
149 #elif defined(__sparc__)
150 # define DECL_GLOBAL_CONSTRUCTOR(func) \
151     static void __dummy_init_##func(void) { \
152         asm("\t.section \".init\",#alloc,#execinstr\n" \
153             "\tcall " #func "\n" \
154             "\tnop\n" \
155             "\t.section \".text\",#alloc,#execinstr\n" ); } \
156     static void func(void)
157 #else
158 # error You must define the DECL_GLOBAL_CONSTRUCTOR macro for your platform
159 #endif
160
161
162 /****************************************************************
163  * Function definitions (only when using libwine)
164  */
165
166 #ifndef NO_LIBWINE
167
168 #if !defined(HAVE_CLONE) && defined(linux)
169 int clone(int (*fn)(void *arg), void *stack, int flags, void *arg);
170 #endif /* !defined(HAVE_CLONE) && defined(linux) */
171
172 #ifndef HAVE_GETPAGESIZE
173 size_t getpagesize(void);
174 #endif  /* HAVE_GETPAGESIZE */
175
176 #ifndef HAVE_GETSOCKOPT
177 int getsockopt(int socket, int level, int option_name, void *option_value, size_t *option_len);
178 #endif /* !defined(HAVE_GETSOCKOPT) */
179
180 #ifndef HAVE_INET_NETWORK
181 unsigned long inet_network(const char *cp);
182 #endif /* !defined(HAVE_INET_NETWORK) */
183
184 #ifndef HAVE_LSTAT
185 int lstat(const char *file_name, struct stat *buf);
186 #endif /* HAVE_LSTAT */
187
188 #ifndef HAVE_MKSTEMP
189 int mkstemp(char *tmpfn);
190 #endif /* HAVE_MKSTEMP */
191
192 #ifndef HAVE_MEMMOVE
193 void *memmove(void *dest, const void *src, size_t len);
194 #endif /* !defined(HAVE_MEMMOVE) */
195
196 #ifndef HAVE_PREAD
197 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
198 #endif /* HAVE_PREAD */
199
200 #ifndef HAVE_PWRITE
201 ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset );
202 #endif /* HAVE_PWRITE */
203
204 #ifndef HAVE_STATFS
205 int statfs(const char *name, struct statfs *info);
206 #endif /* !defined(HAVE_STATFS) */
207
208 #ifndef HAVE_STRNCASECMP
209 # ifndef HAVE__STRNICMP
210 int strncasecmp(const char *str1, const char *str2, size_t n);
211 # else
212 # define strncasecmp _strnicmp
213 # endif
214 #endif /* !defined(HAVE_STRNCASECMP) */
215
216 #ifndef HAVE_STRERROR
217 const char *strerror(int err);
218 #endif /* !defined(HAVE_STRERROR) */
219
220 #ifndef HAVE_STRCASECMP
221 # ifndef HAVE__STRICMP
222 int strcasecmp(const char *str1, const char *str2);
223 # else
224 # define strcasecmp _stricmp
225 # endif
226 #endif /* !defined(HAVE_STRCASECMP) */
227
228 #ifndef HAVE_USLEEP
229 int usleep (unsigned int useconds);
230 #endif /* !defined(HAVE_USLEEP) */
231
232 #ifdef __i386__
233 #define wine_memcpy_unaligned memcpy
234 #else
235 extern void *wine_memcpy_unaligned( void *dst, const void *src, size_t size );
236 #endif /* __i386__ */
237
238 /* Interlocked functions */
239
240 #if defined(__i386__) && defined(__GNUC__)
241
242 inline static long interlocked_cmpxchg( long *dest, long xchg, long compare )
243 {
244     long ret;
245     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
246                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
247     return ret;
248 }
249
250 inline static void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
251 {
252     void *ret;
253     __asm__ __volatile__( "lock; cmpxchgl %2,(%1)"
254                           : "=a" (ret) : "r" (dest), "r" (xchg), "0" (compare) : "memory" );
255     return ret;
256 }
257
258 inline static long interlocked_xchg( long *dest, long val )
259 {
260     long ret;
261     __asm__ __volatile__( "lock; xchgl %0,(%1)"
262                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
263     return ret;
264 }
265
266 inline static void *interlocked_xchg_ptr( void **dest, void *val )
267 {
268     void *ret;
269     __asm__ __volatile__( "lock; xchgl %0,(%1)"
270                           : "=r" (ret) : "r" (dest), "0" (val) : "memory" );
271     return ret;
272 }
273
274 inline static long interlocked_xchg_add( long *dest, long incr )
275 {
276     long ret;
277     __asm__ __volatile__( "lock; xaddl %0,(%1)"
278                           : "=r" (ret) : "r" (dest), "0" (incr) : "memory" );
279     return ret;
280 }
281
282 #else  /* __i386___ && __GNUC__ */
283
284 extern long interlocked_cmpxchg( long *dest, long xchg, long compare );
285 extern void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare );
286 extern long interlocked_xchg( long *dest, long val );
287 extern void *interlocked_xchg_ptr( void **dest, void *val );
288 extern long interlocked_xchg_add( long *dest, long incr );
289
290 #endif  /* __i386___ && __GNUC__ */
291
292 #else /* NO_LIBWINE */
293
294 #define __WINE_NOT_PORTABLE(func) func##_is_not_portable func##_is_not_portable
295
296 #define clone             __WINE_NOT_PORTABLE(clone)
297 #define getpagesize       __WINE_NOT_PORTABLE(getpagesize)
298 #define getsockopt        __WINE_NOT_PORTABLE(getsockopt)
299 #define inet_network      __WINE_NOT_PORTABLE(inet_network)
300 #define lstat             __WINE_NOT_PORTABLE(lstat)
301 #define memmove           __WINE_NOT_PORTABLE(memmove)
302 #define pread             __WINE_NOT_PORTABLE(pread)
303 #define pwrite            __WINE_NOT_PORTABLE(pwrite)
304 #define statfs            __WINE_NOT_PORTABLE(statfs)
305 #define strcasecmp        __WINE_NOT_PORTABLE(strcasecmp)
306 #define strerror          __WINE_NOT_PORTABLE(strerror)
307 #define strncasecmp       __WINE_NOT_PORTABLE(strncasecmp)
308 #define usleep            __WINE_NOT_PORTABLE(usleep)
309
310 #endif /* NO_LIBWINE */
311
312 #endif /* !defined(__WINE_WINE_PORT_H) */