Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Misc. functions for systems that don't have them |
| 3 | * |
| 4 | * Copyright 1996 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
François Gouget | 1425941 | 2001-11-06 20:57:11 +0000 | [diff] [blame] | 21 | #include "config.h" |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 23 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 24 | #ifdef __BEOS__ |
| 25 | #include <be/kernel/fs_info.h> |
| 26 | #include <be/kernel/OS.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <assert.h> |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Marcus Meissner | 3b09284 | 1999-02-20 16:46:39 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Alexandre Julliard | 335b9e3 | 2000-12-19 19:38:48 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
David Luyer | ee517e8 | 1999-02-28 12:27:56 +0000 | [diff] [blame] | 33 | #include <string.h> |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
Michal Pasternak | 6098399 | 2002-03-29 18:04:43 +0000 | [diff] [blame] | 36 | #ifdef HAVE_SYS_INTTYPES_H |
| 37 | # include <sys/inttypes.h> |
| 38 | #endif |
Patrik Stridvall | 136fae5 | 2002-03-11 05:08:38 +0000 | [diff] [blame] | 39 | #ifdef HAVE_SYS_TIME_h |
| 40 | # include <sys/time.h> |
| 41 | #endif |
Alexandre Julliard | 8da12c4 | 1999-01-17 16:55:11 +0000 | [diff] [blame] | 42 | #include <sys/stat.h> |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 43 | #ifdef HAVE_SYS_IOCTL_H |
Alexandre Julliard | 8da12c4 | 1999-01-17 16:55:11 +0000 | [diff] [blame] | 44 | #include <sys/ioctl.h> |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 45 | #endif |
Alexandre Julliard | 8da12c4 | 1999-01-17 16:55:11 +0000 | [diff] [blame] | 46 | #include <errno.h> |
| 47 | #include <fcntl.h> |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 48 | #ifdef HAVE_TERMIOS_H |
Alexandre Julliard | 8da12c4 | 1999-01-17 16:55:11 +0000 | [diff] [blame] | 49 | #include <termios.h> |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 50 | #endif |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 51 | #ifdef HAVE_SYS_MMAN_H |
| 52 | #include <sys/mman.h> |
| 53 | #endif |
Marcus Meissner | 592ba10 | 1999-01-20 14:18:55 +0000 | [diff] [blame] | 54 | #ifdef HAVE_LIBIO_H |
| 55 | # include <libio.h> |
| 56 | #endif |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 57 | #ifdef HAVE_SYSCALL_H |
| 58 | # include <syscall.h> |
| 59 | #endif |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 60 | #ifdef HAVE_STDINT_H |
| 61 | # include <stdint.h> |
| 62 | #endif |
Patrik Stridvall | 9af5e69 | 1999-12-11 23:02:15 +0000 | [diff] [blame] | 63 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 64 | /*********************************************************************** |
| 65 | * usleep |
| 66 | */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 67 | #ifndef HAVE_USLEEP |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 68 | int usleep (unsigned int useconds) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 69 | { |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 70 | #if defined(__EMX__) |
| 71 | DosSleep(useconds); |
| 72 | return 0; |
| 73 | #elif defined(__BEOS__) |
| 74 | return snooze(useconds); |
| 75 | #elif defined(HAVE_SELECT) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 76 | struct timeval delay; |
| 77 | |
Alexandre Julliard | 2fa4f76 | 2000-10-31 00:20:51 +0000 | [diff] [blame] | 78 | delay.tv_sec = useconds / 1000000; |
| 79 | delay.tv_usec = useconds % 1000000; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 80 | |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 81 | select( 0, 0, 0, 0, &delay ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 82 | return 0; |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 83 | #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */ |
| 84 | errno = ENOSYS; |
| 85 | return -1; |
| 86 | #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 87 | } |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 88 | #endif /* HAVE_USLEEP */ |
| 89 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 90 | /*********************************************************************** |
| 91 | * memmove |
| 92 | */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 93 | #ifndef HAVE_MEMMOVE |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 94 | void *memmove( void *dest, const void *src, unsigned int len ) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 95 | { |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 96 | register char *dst = dest; |
| 97 | |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 98 | /* Use memcpy if not overlapping */ |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 99 | if ((dst + len <= (char *)src) || ((char *)src + len <= dst)) |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 100 | { |
| 101 | memcpy( dst, src, len ); |
| 102 | } |
| 103 | /* Otherwise do it the hard way (FIXME: could do better than this) */ |
| 104 | else if (dst < src) |
| 105 | { |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 106 | while (len--) *dst++ = *((char *)src)++; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 107 | } |
| 108 | else |
| 109 | { |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 110 | dst += len - 1; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 111 | src = (char *)src + len - 1; |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 112 | while (len--) *dst-- = *((char *)src)--; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 113 | } |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 114 | return dest; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 115 | } |
| 116 | #endif /* HAVE_MEMMOVE */ |
Alexandre Julliard | 44ed71f | 1997-12-21 19:17:50 +0000 | [diff] [blame] | 117 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 118 | /*********************************************************************** |
| 119 | * strerror |
| 120 | */ |
Alexandre Julliard | 44ed71f | 1997-12-21 19:17:50 +0000 | [diff] [blame] | 121 | #ifndef HAVE_STRERROR |
| 122 | const char *strerror( int err ) |
| 123 | { |
| 124 | /* Let's hope we have sys_errlist then */ |
| 125 | return sys_errlist[err]; |
| 126 | } |
| 127 | #endif /* HAVE_STRERROR */ |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 128 | |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 129 | |
| 130 | /*********************************************************************** |
| 131 | * getpagesize |
| 132 | */ |
| 133 | #ifndef HAVE_GETPAGESIZE |
| 134 | size_t getpagesize(void) |
| 135 | { |
| 136 | # ifdef __svr4__ |
| 137 | return sysconf(_SC_PAGESIZE); |
Alexandre Julliard | 6044307 | 2002-05-17 03:31:08 +0000 | [diff] [blame] | 138 | # elif defined(__i386__) |
| 139 | return 4096; |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 140 | # else |
| 141 | # error Cannot get the page size on this platform |
| 142 | # endif |
| 143 | } |
| 144 | #endif /* HAVE_GETPAGESIZE */ |
| 145 | |
| 146 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 147 | /*********************************************************************** |
| 148 | * clone |
| 149 | */ |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 150 | #if !defined(HAVE_CLONE) && defined(__linux__) |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 151 | int clone( int (*fn)(void *), void *stack, int flags, void *arg ) |
| 152 | { |
| 153 | #ifdef __i386__ |
| 154 | int ret; |
| 155 | void **stack_ptr = (void **)stack; |
| 156 | *--stack_ptr = arg; /* Push argument on stack */ |
| 157 | *--stack_ptr = fn; /* Push function pointer (popped into ebx) */ |
| 158 | __asm__ __volatile__( "pushl %%ebx\n\t" |
| 159 | "movl %2,%%ebx\n\t" |
| 160 | "int $0x80\n\t" |
| 161 | "popl %%ebx\n\t" /* Contains fn in the child */ |
| 162 | "testl %%eax,%%eax\n\t" |
| 163 | "jnz 0f\n\t" |
Alexandre Julliard | 0f586c4 | 2001-02-20 01:59:27 +0000 | [diff] [blame] | 164 | "xorl %ebp,%ebp\n\t" /* Terminate the stack frames */ |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 165 | "call *%%ebx\n\t" /* Should never return */ |
| 166 | "xorl %%eax,%%eax\n\t" /* Just in case it does*/ |
| 167 | "0:" |
| 168 | : "=a" (ret) |
Marcus Meissner | 4ede296 | 1999-02-21 18:18:42 +0000 | [diff] [blame] | 169 | : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) ); |
Alexandre Julliard | 0623a6f | 1998-01-18 18:01:49 +0000 | [diff] [blame] | 170 | assert( ret ); /* If ret is 0, we returned from the child function */ |
| 171 | if (ret > 0) return ret; |
| 172 | errno = -ret; |
| 173 | return -1; |
| 174 | #else |
| 175 | errno = EINVAL; |
| 176 | return -1; |
| 177 | #endif /* __i386__ */ |
| 178 | } |
| 179 | #endif /* !HAVE_CLONE && __linux__ */ |
Alexandre Julliard | 8da12c4 | 1999-01-17 16:55:11 +0000 | [diff] [blame] | 180 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 181 | /*********************************************************************** |
| 182 | * strcasecmp |
| 183 | */ |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 184 | #ifndef HAVE_STRCASECMP |
| 185 | int strcasecmp( const char *str1, const char *str2 ) |
| 186 | { |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 187 | const unsigned char *ustr1 = (const unsigned char *)str1; |
| 188 | const unsigned char *ustr2 = (const unsigned char *)str2; |
| 189 | |
| 190 | while (*ustr1 && toupper(*ustr1) == toupper(*ustr2)) { |
| 191 | ustr1++; |
| 192 | ustr2++; |
| 193 | } |
| 194 | return toupper(*ustr1) - toupper(*ustr2); |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 195 | } |
| 196 | #endif /* HAVE_STRCASECMP */ |
| 197 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 198 | /*********************************************************************** |
| 199 | * strncasecmp |
| 200 | */ |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 201 | #ifndef HAVE_STRNCASECMP |
| 202 | int strncasecmp( const char *str1, const char *str2, size_t n ) |
| 203 | { |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 204 | const unsigned char *ustr1 = (const unsigned char *)str1; |
| 205 | const unsigned char *ustr2 = (const unsigned char *)str2; |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 206 | int res; |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 207 | |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 208 | if (!n) return 0; |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 209 | while ((--n > 0) && *ustr1) { |
| 210 | if ((res = toupper(*ustr1) - toupper(*ustr2))) return res; |
| 211 | ustr1++; |
| 212 | ustr2++; |
| 213 | } |
| 214 | return toupper(*ustr1) - toupper(*ustr2); |
Alexandre Julliard | 3b96efc | 1999-09-04 14:36:02 +0000 | [diff] [blame] | 215 | } |
| 216 | #endif /* HAVE_STRNCASECMP */ |
| 217 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 218 | /*********************************************************************** |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 219 | * getsockopt |
| 220 | */ |
| 221 | #ifndef HAVE_GETSOCKOPT |
| 222 | int getsockopt(int socket, int level, int option_name, |
Alexandre Julliard | 2fa4f76 | 2000-10-31 00:20:51 +0000 | [diff] [blame] | 223 | void *option_value, size_t *option_len) |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 224 | { |
| 225 | errno = ENOSYS; |
| 226 | return -1; |
| 227 | } |
| 228 | #endif /* !defined(HAVE_GETSOCKOPT) */ |
| 229 | |
| 230 | /*********************************************************************** |
| 231 | * inet_network |
| 232 | */ |
| 233 | #ifndef HAVE_INET_NETWORK |
| 234 | unsigned long inet_network(const char *cp) |
| 235 | { |
| 236 | errno = ENOSYS; |
| 237 | return 0; |
| 238 | } |
| 239 | #endif /* defined(HAVE_INET_NETWORK) */ |
| 240 | |
| 241 | /*********************************************************************** |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 242 | * statfs |
| 243 | */ |
| 244 | #ifndef HAVE_STATFS |
| 245 | int statfs(const char *name, struct statfs *info) |
| 246 | { |
| 247 | #ifdef __BEOS__ |
| 248 | dev_t mydev; |
| 249 | fs_info fsinfo; |
Alexandre Julliard | 2fa4f76 | 2000-10-31 00:20:51 +0000 | [diff] [blame] | 250 | |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 251 | if(!info) { |
| 252 | errno = ENOSYS; |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | if ((mydev = dev_for_path(name)) < 0) { |
| 257 | errno = ENOSYS; |
Alexandre Julliard | 2fa4f76 | 2000-10-31 00:20:51 +0000 | [diff] [blame] | 258 | return -1; |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (fs_stat_dev(mydev,&fsinfo) < 0) { |
| 262 | errno = ENOSYS; |
Alexandre Julliard | 2fa4f76 | 2000-10-31 00:20:51 +0000 | [diff] [blame] | 263 | return -1; |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | info->f_bsize = fsinfo.block_size; |
| 267 | info->f_blocks = fsinfo.total_blocks; |
| 268 | info->f_bfree = fsinfo.free_blocks; |
Patrik Stridvall | b901021 | 1999-11-13 22:23:35 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | #else /* defined(__BEOS__) */ |
| 271 | errno = ENOSYS; |
| 272 | return -1; |
| 273 | #endif /* defined(__BEOS__) */ |
| 274 | } |
| 275 | #endif /* !defined(HAVE_STATFS) */ |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 276 | |
| 277 | |
| 278 | /*********************************************************************** |
Alexandre Julliard | 27bb311 | 2000-11-29 17:48:06 +0000 | [diff] [blame] | 279 | * lstat |
| 280 | */ |
| 281 | #ifndef HAVE_LSTAT |
| 282 | int lstat(const char *file_name, struct stat *buf) |
| 283 | { |
| 284 | return stat( file_name, buf ); |
| 285 | } |
| 286 | #endif /* HAVE_LSTAT */ |
| 287 | |
Marcus Meissner | 786d249 | 2002-07-29 23:55:39 +0000 | [diff] [blame] | 288 | /*********************************************************************** |
| 289 | * mkstemp |
| 290 | */ |
| 291 | #ifndef HAVE_MKSTEMP |
| 292 | int mkstemp(char *tmpfn) |
| 293 | { |
| 294 | int tries; |
| 295 | char *xstart; |
| 296 | |
| 297 | xstart = tmpfn+strlen(tmpfn)-1; |
| 298 | while ((xstart > tmpfn) && (*xstart == 'X')) |
| 299 | xstart--; |
| 300 | tries = 10; |
| 301 | while (tries--) { |
| 302 | char *newfn = mktemp(tmpfn); |
| 303 | int fd; |
| 304 | if (!newfn) /* something else broke horribly */ |
| 305 | return -1; |
| 306 | fd = open(newfn,O_CREAT|O_RDWR|O_EXCL,0600); |
| 307 | if (fd!=-1) |
| 308 | return fd; |
| 309 | newfn = xstart; |
| 310 | /* fill up with X and try again ... */ |
| 311 | while (*newfn) *newfn++ = 'X'; |
| 312 | } |
| 313 | return -1; |
| 314 | } |
| 315 | #endif /* HAVE_MKSTEMP */ |
| 316 | |
Alexandre Julliard | f1a0de9 | 2002-01-07 21:00:27 +0000 | [diff] [blame] | 317 | |
| 318 | /*********************************************************************** |
| 319 | * pread |
| 320 | * |
| 321 | * FIXME: this is not thread-safe |
| 322 | */ |
| 323 | #ifndef HAVE_PREAD |
| 324 | ssize_t pread( int fd, void *buf, size_t count, off_t offset ) |
| 325 | { |
| 326 | ssize_t ret; |
| 327 | off_t old_pos; |
| 328 | |
| 329 | if ((old_pos = lseek( fd, 0, SEEK_CUR )) == -1) return -1; |
| 330 | if (lseek( fd, offset, SEEK_SET ) == -1) return -1; |
| 331 | if ((ret = read( fd, buf, count )) == -1) |
| 332 | { |
| 333 | int err = errno; /* save errno */ |
| 334 | lseek( fd, old_pos, SEEK_SET ); |
| 335 | errno = err; |
| 336 | return -1; |
| 337 | } |
| 338 | if (lseek( fd, old_pos, SEEK_SET ) == -1) return -1; |
| 339 | return ret; |
| 340 | } |
| 341 | #endif /* HAVE_PREAD */ |
| 342 | |
| 343 | |
| 344 | /*********************************************************************** |
| 345 | * pwrite |
| 346 | * |
| 347 | * FIXME: this is not thread-safe |
| 348 | */ |
| 349 | #ifndef HAVE_PWRITE |
| 350 | ssize_t pwrite( int fd, const void *buf, size_t count, off_t offset ) |
| 351 | { |
| 352 | ssize_t ret; |
| 353 | off_t old_pos; |
| 354 | |
| 355 | if ((old_pos = lseek( fd, 0, SEEK_CUR )) == -1) return -1; |
| 356 | if (lseek( fd, offset, SEEK_SET ) == -1) return -1; |
| 357 | if ((ret = write( fd, buf, count )) == -1) |
| 358 | { |
| 359 | int err = errno; /* save errno */ |
| 360 | lseek( fd, old_pos, SEEK_SET ); |
| 361 | errno = err; |
| 362 | return -1; |
| 363 | } |
| 364 | if (lseek( fd, old_pos, SEEK_SET ) == -1) return -1; |
| 365 | return ret; |
| 366 | } |
| 367 | #endif /* HAVE_PWRITE */ |
| 368 | |
| 369 | |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 370 | #if defined(__svr4__) || defined(__NetBSD__) |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 371 | /*********************************************************************** |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 372 | * try_mmap_fixed |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 373 | * |
| 374 | * The purpose of this routine is to emulate the behaviour of |
| 375 | * the Linux mmap() routine if a non-NULL address is passed, |
| 376 | * but the MAP_FIXED flag is not set. Linux in this case tries |
| 377 | * to place the mapping at the specified address, *unless* the |
| 378 | * range is already in use. Solaris, however, completely ignores |
| 379 | * the address argument in this case. |
| 380 | * |
| 381 | * As Wine code occasionally relies on the Linux behaviour, e.g. to |
| 382 | * be able to map non-relocateable PE executables to their proper |
| 383 | * start addresses, or to map the DOS memory to 0, this routine |
| 384 | * emulates the Linux behaviour by checking whether the desired |
| 385 | * address range is still available, and placing the mapping there |
| 386 | * using MAP_FIXED if so. |
| 387 | */ |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 388 | static int try_mmap_fixed (void *addr, size_t len, int prot, int flags, |
| 389 | int fildes, off_t off) |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 390 | { |
| 391 | char * volatile result = NULL; |
| 392 | int pagesize = getpagesize(); |
| 393 | pid_t pid; |
| 394 | |
| 395 | /* We only try to map to a fixed address if |
| 396 | addr is non-NULL and properly aligned, |
| 397 | and MAP_FIXED isn't already specified. */ |
| 398 | |
| 399 | if ( !addr ) |
François Gouget | 1526872 | 2001-11-05 23:52:30 +0000 | [diff] [blame] | 400 | return 0; |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 401 | if ( (uintptr_t)addr & (pagesize-1) ) |
François Gouget | 1526872 | 2001-11-05 23:52:30 +0000 | [diff] [blame] | 402 | return 0; |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 403 | if ( flags & MAP_FIXED ) |
François Gouget | 1526872 | 2001-11-05 23:52:30 +0000 | [diff] [blame] | 404 | return 0; |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 405 | |
| 406 | /* We use vfork() to freeze all threads of the |
| 407 | current process. This allows us to check without |
| 408 | race condition whether the desired memory range is |
| 409 | already in use. Note that because vfork() shares |
| 410 | the address spaces between parent and child, we |
| 411 | can actually perform the mapping in the child. */ |
| 412 | |
| 413 | if ( (pid = vfork()) == -1 ) |
| 414 | { |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 415 | perror("try_mmap_fixed: vfork"); |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 416 | exit(1); |
| 417 | } |
| 418 | if ( pid == 0 ) |
| 419 | { |
| 420 | int i; |
| 421 | char vec; |
| 422 | |
| 423 | /* We call mincore() for every page in the desired range. |
| 424 | If any of these calls succeeds, the page is already |
| 425 | mapped and we must fail. */ |
| 426 | for ( i = 0; i < len; i += pagesize ) |
| 427 | if ( mincore( (caddr_t)addr + i, pagesize, &vec ) != -1 ) |
| 428 | _exit(1); |
| 429 | |
| 430 | /* Perform the mapping with MAP_FIXED set. This is safe |
| 431 | now, as none of the pages is currently in use. */ |
| 432 | result = mmap( addr, len, prot, flags | MAP_FIXED, fildes, off ); |
| 433 | if ( result == addr ) |
| 434 | _exit(0); |
| 435 | |
| 436 | if ( result != (void *) -1 ) /* This should never happen ... */ |
| 437 | munmap( result, len ); |
| 438 | |
| 439 | _exit(1); |
| 440 | } |
| 441 | |
| 442 | /* vfork() lets the parent continue only after the child |
| 443 | has exited. Furthermore, Wine sets SIGCHLD to SIG_IGN, |
| 444 | so we don't need to wait for the child. */ |
| 445 | |
| 446 | return result == addr; |
| 447 | } |
| 448 | #endif |
| 449 | |
Dimitrie O. Paun | 1ec253d | 2000-12-01 20:47:11 +0000 | [diff] [blame] | 450 | /*********************************************************************** |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 451 | * wine_anon_mmap |
| 452 | * |
| 453 | * Portable wrapper for anonymous mmaps |
| 454 | */ |
| 455 | void *wine_anon_mmap( void *start, size_t size, int prot, int flags ) |
| 456 | { |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 457 | #ifdef HAVE_MMAP |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 458 | static int fdzero = -1; |
| 459 | |
| 460 | #ifdef MAP_ANON |
| 461 | flags |= MAP_ANON; |
| 462 | #else |
| 463 | if (fdzero == -1) |
| 464 | { |
| 465 | if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1) |
| 466 | { |
| 467 | perror( "/dev/zero: open" ); |
| 468 | exit(1); |
| 469 | } |
| 470 | } |
| 471 | #endif /* MAP_ANON */ |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 472 | |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 473 | #ifdef MAP_SHARED |
| 474 | flags &= ~MAP_SHARED; |
| 475 | #endif |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 476 | |
| 477 | /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */ |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 478 | #ifdef MAP_PRIVATE |
| 479 | flags |= MAP_PRIVATE; |
| 480 | #endif |
Morten Welinder | a2d11df | 2000-12-18 03:50:15 +0000 | [diff] [blame] | 481 | |
Bang Jun-Young | d0b304b | 2001-12-14 22:47:45 +0000 | [diff] [blame] | 482 | #if defined(__svr4__) || defined(__NetBSD__) |
| 483 | if ( try_mmap_fixed( start, size, prot, flags, fdzero, 0 ) ) |
Ulrich Weigand | c52a11e | 2001-09-07 18:46:14 +0000 | [diff] [blame] | 484 | return start; |
| 485 | #endif |
| 486 | |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 487 | return mmap( start, size, prot, flags, fdzero, 0 ); |
Alexandre Julliard | c3e06df | 2002-05-14 23:18:23 +0000 | [diff] [blame] | 488 | #else |
| 489 | return (void *)-1; |
| 490 | #endif |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 491 | } |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 492 | |
| 493 | |
| 494 | /* |
| 495 | * These functions provide wrappers around dlopen() and associated |
| 496 | * functions. They work around a bug in glibc 2.1.x where calling |
| 497 | * a dl*() function after a previous dl*() function has failed |
| 498 | * without a dlerror() call between the two will cause a crash. |
| 499 | * They all take a pointer to a buffer that |
| 500 | * will receive the error description (from dlerror()). This |
| 501 | * parameter may be NULL if the error description is not required. |
| 502 | */ |
| 503 | |
| 504 | /*********************************************************************** |
| 505 | * wine_dlopen |
| 506 | */ |
| 507 | void *wine_dlopen( const char *filename, int flag, char *error, int errorsize ) |
| 508 | { |
Alexandre Julliard | ed2f19a | 2001-06-27 21:42:00 +0000 | [diff] [blame] | 509 | #ifdef HAVE_DLOPEN |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 510 | void *ret; |
Francois Gouget | 0763abf | 2002-04-01 21:08:16 +0000 | [diff] [blame] | 511 | const char *s; |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 512 | dlerror(); dlerror(); |
| 513 | ret = dlopen( filename, flag ); |
| 514 | s = dlerror(); |
| 515 | if (error) |
| 516 | { |
| 517 | strncpy( error, s ? s : "", errorsize ); |
| 518 | error[errorsize - 1] = '\0'; |
| 519 | } |
| 520 | dlerror(); |
| 521 | return ret; |
| 522 | #else |
| 523 | if (error) |
| 524 | { |
| 525 | strncpy( error, "dlopen interface not detected by configure", errorsize ); |
| 526 | error[errorsize - 1] = '\0'; |
| 527 | } |
| 528 | return NULL; |
| 529 | #endif |
| 530 | } |
| 531 | |
| 532 | /*********************************************************************** |
| 533 | * wine_dlsym |
| 534 | */ |
| 535 | void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize ) |
| 536 | { |
Alexandre Julliard | ed2f19a | 2001-06-27 21:42:00 +0000 | [diff] [blame] | 537 | #ifdef HAVE_DLOPEN |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 538 | void *ret; |
Francois Gouget | 0763abf | 2002-04-01 21:08:16 +0000 | [diff] [blame] | 539 | const char *s; |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 540 | dlerror(); dlerror(); |
| 541 | ret = dlsym( handle, symbol ); |
| 542 | s = dlerror(); |
| 543 | if (error) |
| 544 | { |
| 545 | strncpy( error, s ? s : "", errorsize ); |
| 546 | error[errorsize - 1] = '\0'; |
| 547 | } |
| 548 | dlerror(); |
| 549 | return ret; |
| 550 | #else |
| 551 | if (error) |
| 552 | { |
| 553 | strncpy( error, "dlopen interface not detected by configure", errorsize ); |
| 554 | error[errorsize - 1] = '\0'; |
| 555 | } |
| 556 | return NULL; |
| 557 | #endif |
| 558 | } |
| 559 | |
| 560 | /*********************************************************************** |
| 561 | * wine_dlclose |
| 562 | */ |
| 563 | int wine_dlclose( void *handle, char *error, int errorsize ) |
| 564 | { |
Alexandre Julliard | ed2f19a | 2001-06-27 21:42:00 +0000 | [diff] [blame] | 565 | #ifdef HAVE_DLOPEN |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 566 | int ret; |
Francois Gouget | 0763abf | 2002-04-01 21:08:16 +0000 | [diff] [blame] | 567 | const char *s; |
James Abbatiello | e675887 | 2000-12-13 21:32:55 +0000 | [diff] [blame] | 568 | dlerror(); dlerror(); |
| 569 | ret = dlclose( handle ); |
| 570 | s = dlerror(); |
| 571 | if (error) |
| 572 | { |
| 573 | strncpy( error, s ? s : "", errorsize ); |
| 574 | error[errorsize - 1] = '\0'; |
| 575 | } |
| 576 | dlerror(); |
| 577 | return ret; |
| 578 | #else |
| 579 | if (error) |
| 580 | { |
| 581 | strncpy( error, "dlopen interface not detected by configure", errorsize ); |
| 582 | error[errorsize - 1] = '\0'; |
| 583 | } |
| 584 | return 1; |
| 585 | #endif |
| 586 | } |
François Gouget | 5051020 | 2000-12-19 04:50:49 +0000 | [diff] [blame] | 587 | |
| 588 | /*********************************************************************** |
| 589 | * wine_rewrite_s4tos2 |
| 590 | * |
| 591 | * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place. |
| 592 | * This is only practical if literal strings are writable. |
| 593 | */ |
| 594 | unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 ) |
| 595 | { |
| 596 | unsigned short *str2,*s2; |
| 597 | |
| 598 | if (str4==NULL) |
| 599 | return NULL; |
| 600 | |
| 601 | if ((*str4 & 0xffff0000) != 0) { |
| 602 | /* This string has already been converted. Return it as is */ |
| 603 | return (unsigned short*)str4; |
| 604 | } |
| 605 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 606 | /* Note that we can also end up here if the string has a single |
| 607 | * character. In such a case we will convert the string over and |
François Gouget | 5051020 | 2000-12-19 04:50:49 +0000 | [diff] [blame] | 608 | * over again. But this is harmless. |
| 609 | */ |
| 610 | str2=s2=(unsigned short*)str4; |
| 611 | do { |
| 612 | *s2=(unsigned short)*str4; |
| 613 | s2++; |
| 614 | } while (*str4++ != L'\0'); |
| 615 | |
| 616 | return str2; |
| 617 | } |
Andreas Mohr | a7ca2ba | 2001-01-12 23:07:11 +0000 | [diff] [blame] | 618 | |
| 619 | #ifndef HAVE_ECVT |
| 620 | /* |
| 621 | * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though. |
| 622 | * Fix/verify these implementations ! |
| 623 | */ |
| 624 | |
| 625 | /*********************************************************************** |
| 626 | * ecvt |
| 627 | */ |
| 628 | char *ecvt (double number, int ndigits, int *decpt, int *sign) |
| 629 | { |
Gerald Pfeifer | 71406615 | 2001-10-02 17:18:09 +0000 | [diff] [blame] | 630 | static char buf[40]; /* ought to be enough */ |
Andreas Mohr | a7ca2ba | 2001-01-12 23:07:11 +0000 | [diff] [blame] | 631 | char *dec; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 632 | sprintf(buf, "%.*e", ndigits /* FIXME wrong */, number); |
Andreas Mohr | a7ca2ba | 2001-01-12 23:07:11 +0000 | [diff] [blame] | 633 | *sign = (number < 0); |
| 634 | dec = strchr(buf, '.'); |
| 635 | *decpt = (dec) ? (int)dec - (int)buf : -1; |
| 636 | return buf; |
| 637 | } |
| 638 | |
| 639 | /*********************************************************************** |
| 640 | * fcvt |
| 641 | */ |
| 642 | char *fcvt (double number, int ndigits, int *decpt, int *sign) |
| 643 | { |
Gerald Pfeifer | 71406615 | 2001-10-02 17:18:09 +0000 | [diff] [blame] | 644 | static char buf[40]; /* ought to be enough */ |
Andreas Mohr | a7ca2ba | 2001-01-12 23:07:11 +0000 | [diff] [blame] | 645 | char *dec; |
| 646 | sprintf(buf, "%.*e", ndigits, number); |
| 647 | *sign = (number < 0); |
| 648 | dec = strchr(buf, '.'); |
| 649 | *decpt = (dec) ? (int)dec - (int)buf : -1; |
| 650 | return buf; |
| 651 | } |
| 652 | |
| 653 | /*********************************************************************** |
| 654 | * gcvt |
| 655 | * |
| 656 | * FIXME: uses both E and F. |
| 657 | */ |
| 658 | char *gcvt (double number, size_t ndigit, char *buff) |
| 659 | { |
Gerald Pfeifer | 71406615 | 2001-10-02 17:18:09 +0000 | [diff] [blame] | 660 | sprintf(buff, "%.*E", (int)ndigit, number); |
Andreas Mohr | a7ca2ba | 2001-01-12 23:07:11 +0000 | [diff] [blame] | 661 | return buff; |
| 662 | } |
| 663 | #endif /* HAVE_ECVT */ |
Alexandre Julliard | 894b188 | 2002-04-25 21:40:56 +0000 | [diff] [blame] | 664 | |
| 665 | |
| 666 | /*********************************************************************** |
| 667 | * interlocked functions |
| 668 | */ |
| 669 | #ifdef __i386__ |
| 670 | |
| 671 | __ASM_GLOBAL_FUNC(interlocked_cmpxchg, |
| 672 | "movl 12(%esp),%eax\n\t" |
| 673 | "movl 8(%esp),%ecx\n\t" |
| 674 | "movl 4(%esp),%edx\n\t" |
| 675 | "lock; cmpxchgl %ecx,(%edx)\n\t" |
| 676 | "ret"); |
| 677 | __ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr, |
| 678 | "movl 12(%esp),%eax\n\t" |
| 679 | "movl 8(%esp),%ecx\n\t" |
| 680 | "movl 4(%esp),%edx\n\t" |
| 681 | "lock; cmpxchgl %ecx,(%edx)\n\t" |
| 682 | "ret"); |
| 683 | __ASM_GLOBAL_FUNC(interlocked_xchg, |
| 684 | "movl 8(%esp),%eax\n\t" |
| 685 | "movl 4(%esp),%edx\n\t" |
| 686 | "lock; xchgl %eax,(%edx)\n\t" |
| 687 | "ret"); |
| 688 | __ASM_GLOBAL_FUNC(interlocked_xchg_ptr, |
| 689 | "movl 8(%esp),%eax\n\t" |
| 690 | "movl 4(%esp),%edx\n\t" |
| 691 | "lock; xchgl %eax,(%edx)\n\t" |
| 692 | "ret"); |
| 693 | __ASM_GLOBAL_FUNC(interlocked_xchg_add, |
| 694 | "movl 8(%esp),%eax\n\t" |
| 695 | "movl 4(%esp),%edx\n\t" |
| 696 | "lock; xaddl %eax,(%edx)\n\t" |
| 697 | "ret"); |
| 698 | |
Alexandre Julliard | c1dec29 | 2002-08-06 23:51:25 +0000 | [diff] [blame] | 699 | #elif defined(__powerpc__) |
| 700 | void* interlocked_cmpxchg_ptr( void **dest, void* xchg, void* compare) |
| 701 | { |
| 702 | long ret; |
| 703 | long scratch; |
| 704 | __asm__ __volatile__( |
| 705 | "sync; " |
| 706 | "0: lwarx %0,0,%2 ;" |
| 707 | " xor. %1,%4,%0;" |
| 708 | " bne 1f;" |
| 709 | " stwcx. %3,0,%2;" |
| 710 | " bne- 0b;" |
| 711 | "1: " |
| 712 | "sync; " |
| 713 | : "=&r"(ret), "=&r"(scratch) |
| 714 | : "r"(dest), "r"(xchg), "r"(compare) |
| 715 | : "cr0", "memory"); |
| 716 | return (void*)ret; |
| 717 | } |
| 718 | |
| 719 | long interlocked_cmpxchg( long *dest, long xchg, long compare) |
| 720 | { |
| 721 | long ret; |
| 722 | long scratch; |
| 723 | __asm__ __volatile__( |
| 724 | "sync; " |
| 725 | "0: lwarx %0,0,%2 ;" |
| 726 | " xor. %1,%4,%0;" |
| 727 | " bne 1f;" |
| 728 | " stwcx. %3,0,%2;" |
| 729 | " bne- 0b;" |
| 730 | "1: " |
| 731 | "sync; " |
| 732 | : "=&r"(ret), "=&r"(scratch) |
| 733 | : "r"(dest), "r"(xchg), "r"(compare) |
| 734 | : "cr0", "memory"); |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | long interlocked_xchg_add( long *dest, long incr ) |
| 739 | { |
| 740 | void *ret __attribute__ ((aligned (4))) = &ret; |
| 741 | long inc = incr; |
| 742 | long zero = 0; |
| 743 | __asm__ __volatile__( |
| 744 | "sync; " |
| 745 | "0: lwarx %0, %3, %1;" |
| 746 | " add %0, %2, %0;" |
| 747 | " stwcx. %0, %3, %1;" |
| 748 | " bne- 0b;" |
| 749 | "sync; " |
| 750 | : "=&r"(ret) |
| 751 | : "r"(dest), "r"(inc), "r"(zero) |
| 752 | : "cr0", "memory" |
| 753 | ); |
| 754 | return (long)ret; |
| 755 | } |
| 756 | |
| 757 | long interlocked_xchg( long* dest, long val ) |
| 758 | { |
| 759 | void *ret __attribute__ ((aligned (4))) = &ret; |
| 760 | __asm__ __volatile__( |
| 761 | "sync; " |
| 762 | "0: lwarx %0,0,%1 ;" |
| 763 | " stwcx. %2,0,%1;" |
| 764 | " bne- 0b;" |
| 765 | "sync; " |
| 766 | : "=&r"(ret) |
| 767 | : "r"(dest), "r"(val) |
| 768 | : "cr0", "memory"); |
| 769 | return (long)ret; |
| 770 | } |
| 771 | |
| 772 | void* interlocked_xchg_ptr( void** dest, void* val ) |
| 773 | { |
| 774 | void *ret __attribute__ ((aligned (4))) = &ret; |
| 775 | __asm__ __volatile__( |
| 776 | "sync; " |
| 777 | "0: lwarx %0,0,%1 ;" |
| 778 | " stwcx. %2,0,%1;" |
| 779 | " bne- 0b;" |
| 780 | "sync; " |
| 781 | : "=&r"(ret) |
| 782 | : "r"(dest), "r"(val) |
| 783 | : "cr0", "memory"); |
| 784 | return (void*)ret; |
| 785 | } |
| 786 | |
Alexandre Julliard | 894b188 | 2002-04-25 21:40:56 +0000 | [diff] [blame] | 787 | #elif defined(__sparc__) && defined(__sun__) |
| 788 | |
| 789 | /* |
| 790 | * As the earlier Sparc processors lack necessary atomic instructions, |
| 791 | * I'm simply falling back to the library-provided _lwp_mutex routines |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 792 | * to ensure mutual exclusion in a way appropriate for the current |
| 793 | * architecture. |
Alexandre Julliard | 894b188 | 2002-04-25 21:40:56 +0000 | [diff] [blame] | 794 | * |
| 795 | * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above) |
| 796 | * we could use this to speed up the Interlocked operations ... |
| 797 | */ |
| 798 | #include <synch.h> |
| 799 | static lwp_mutex_t interlocked_mutex = DEFAULTMUTEX; |
| 800 | |
| 801 | long interlocked_cmpxchg( long *dest, long xchg, long compare ) |
| 802 | { |
| 803 | _lwp_mutex_lock( &interlocked_mutex ); |
| 804 | if (*dest == compare) *dest = xchg; |
| 805 | else compare = *dest; |
| 806 | _lwp_mutex_unlock( &interlocked_mutex ); |
| 807 | return compare; |
| 808 | } |
| 809 | |
| 810 | void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare ) |
| 811 | { |
| 812 | _lwp_mutex_lock( &interlocked_mutex ); |
| 813 | if (*dest == compare) *dest = xchg; |
| 814 | else compare = *dest; |
| 815 | _lwp_mutex_unlock( &interlocked_mutex ); |
| 816 | return compare; |
| 817 | } |
| 818 | |
| 819 | long interlocked_xchg( long *dest, long val ) |
| 820 | { |
| 821 | long retv; |
| 822 | _lwp_mutex_lock( &interlocked_mutex ); |
| 823 | retv = *dest; |
| 824 | *dest = val; |
| 825 | _lwp_mutex_unlock( &interlocked_mutex ); |
| 826 | return retv; |
| 827 | } |
| 828 | |
| 829 | void *interlocked_xchg_ptr( void **dest, void *val ) |
| 830 | { |
| 831 | long retv; |
| 832 | _lwp_mutex_lock( &interlocked_mutex ); |
| 833 | retv = *dest; |
| 834 | *dest = val; |
| 835 | _lwp_mutex_unlock( &interlocked_mutex ); |
| 836 | return retv; |
| 837 | } |
| 838 | |
| 839 | long interlocked_xchg_add( long *dest, long incr ) |
| 840 | { |
| 841 | long retv; |
| 842 | _lwp_mutex_lock( &interlocked_mutex ); |
| 843 | retv = *dest; |
| 844 | *dest += incr; |
| 845 | _lwp_mutex_unlock( &interlocked_mutex ); |
| 846 | return retv; |
| 847 | } |
Alexandre Julliard | 894b188 | 2002-04-25 21:40:56 +0000 | [diff] [blame] | 848 | #else |
| 849 | # error You must implement the interlocked* functions for your CPU |
| 850 | #endif |