MySQL 8.4.0
Source Code Documentation
x_platform.h
Go to the documentation of this file.
1/* Copyright (c) 2010, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef X_PLATFORM_H
25#define X_PLATFORM_H
26
27/*
28 * Abstraction layer for lower level OS stuff needed
29 */
30
31#ifdef XCOM_STANDALONE
32/*
33 Disable MY_ATTRIBUTE for Sun Studio and Visual Studio.
34 Note that Sun Studio supports some __attribute__ variants,
35 but not unused which we use quite a lot.
36*/
37#ifndef MY_ATTRIBUTE
38#if defined(__GNUC__)
39#define MY_ATTRIBUTE(A) __attribute__(A)
40#else
41#define MY_ATTRIBUTE(A)
42#endif
43#endif
44#endif
45
46/*
47 * Platform independent functions
48 */
49#if defined(_WIN32)
50
51#include <io.h> /* open() */
52#include <process.h>
53#include <rpc/types.h>
54#include <string.h>
55#include <sys/locking.h>
56
57#define NO_SIGPIPE
58#define strdup _strdup
59
60#define bzero(p, n) memset(p, 0, n)
61
62#ifndef __cplusplus
63#ifndef inline
64#define inline _inline
65#endif
66#endif
67#define xcom_close(fd) _close(fd)
68#define xcom_fclose(f) fclose(f)
69#define xcom_chdir(s) _chdir(s)
70#define xcom_dup(s) _dup(s)
71#define xcom_dup2(s, t) _dup2(s, t)
72#define xcom_lseek(fd, of, h) _lseek(fd, of, h)
73#define xcom_open(n, f, m) _open(n, f, m)
74#define xcom_fopen(n, m) fopen(n, m)
75#define xcom_fstat(f, b) _fstat(f, b)
76
77#define XCOM_F_TLOCK _LK_NBLCK /* Lock without retry in _locking() */
78#define xcom_lockf(fd, f, sz) _locking(fd, f, sz)
79
80#define xcom_lrand48() rand()
81#define xcom_srand48(x) srand(x)
82#define xcom_drand48() ((double)rand() / RAND_MAX)
83
84#define xcom_write(fd, buf, l) _write(fd, buf, (unsigned int)l)
85#define xcom_read(fd, buf, c) _read(fd, buf, (unsigned int)c)
86
87#define xcom_mktemp(s) _mktemp(s)
88
89/* WARNING: snprintf() != _snprintf() */
90/* #define xcom_snprintf(argv) _snprintf(argv) */
91#define xcom_strdup(x) _strdup(x)
92
93#define xcom_strtok(b, d, c) strtok_s(b, d, c)
94
95#define xcom_strcasecmp(a, b) _stricmp(a, b)
96
97#define xcom_execv _execv
98
99/** Posix states that the optval argument should be (const) void*, but
100 on Windows it is (const) char* (See
101 http://msdn.microsoft.com/en-us/library/windows/desktop/ms740476(v=vs.85).aspx
102 and since we normally pass in int* a cast is required on Windows.
103
104 With this typedef we can call xsockopt functions uniformly across
105 platorms. */
106typedef char *xcom_sockoptptr_t;
107
108typedef int mode_t;
109typedef SSIZE_T ssize_t;
110
111#ifndef HAVE_STDINT_H
112#ifndef UINT64_MAX
113#define UINT64_MAX _UI64_MAX
114#endif
115#ifndef INT64_MAX
116#define INT64_MAX _I64_MAX
117#endif
118#ifndef INT64_MIN
119#define INT64_MIN _I64_MIN
120#endif
121#ifndef UINT32_MAX
122#define UINT32_MAX _UI32_MAX
123#endif
124#ifndef INT32_MAX
125#define INT32_MAX _I32_MAX
126#endif
127#ifndef INT32_MIN
128#define INT32_MIN _I32_MIN
129#endif
130#ifndef UINT16_MAX
131#define UINT16_MAX _UI16_MAX
132#endif
133#ifndef INT16_MAX
134#define INT16_MAX _I16_MAX
135#endif
136#ifndef INT16_MIN
137#define INT16_MIN _I16_MIN
138#endif
139#endif
140
141#ifndef PRIu64
142#ifdef _LP64
143#define PRIu64 "lu"
144#else /* _ILP32 */
145#define PRIu64 "llu"
146#endif
147#endif
148
149#ifndef PRIu32
150#define PRIu32 "u"
151#endif
152
153#define XCOM_O_CREAT _O_CREAT
154#define XCOM_O_WRONLY _O_WRONLY
155#define XCOM_O_APPEND _O_APPEND
156#define XCOM_O_LARGEFILE 0
157#define XCOM_O_EXCL _O_EXCL
158#define XCOM_O_RDWR _O_RDWR
159#define XCOM_O_RDONLY _O_RDONLY
160#define XCOM_O_RSYNC 0
161#define XCOM_O_BINARY _O_BINARY
162
163/* Only global flags can be set on Windows */
164#define XCOM_S_IRUSR _S_IREAD
165#define XCOM_S_IWUSR _S_IWRITE
166#define XCOM_S_IXUSR 0
167#define XCOM_S_IWRXU XCOM_S_IRUSR | XCOM_S_IWUSR | XCOM_S_IXUSR
168#define XCOM_S_IRGRP 0
169#define XCOM_S_IWGRP 0
170#define XCOM_S_IROTH 0
171#define XCOM_S_IWOTH 0
172
173#define XCOM_FILE_WRITE_MODE "w"
174
175/**
176 * Process exitcode macros.
177 *
178 * EXCEPTION_ or STATUS_ exits on windows start with 0xCxxxxxxx. The below
179 * attempts to use 2nd highest bit to determine if normal or error exit.
180 *
181 * These macros are normally found on *ix systems, and is used to mimic *ix.
182 */
183#define WEXITSTATUS(s) (s)
184#define WIFEXITED(s) ((s) == -1 ? 1 : ((s)&0x40000000) == 0)
185#define WIFSIGNALED(s) ((s) == -1 ? 0 : ((s)&0x40000000) != 0)
186#define WTERMSIG(s) ((s) == -1 ? 0 : ((s)&0x3FFFFFFF))
187#define WCOREDUMP(s) (((s) == CORE_EXIT_VALUE_WIN32) ? TRUE : FALSE)
188
189typedef int mode_t;
190typedef SSIZE_T ssize_t;
191
192#define XCOM_CLRSYSERR (errno = 0, WSASetLastError(0))
193#define XCOM_ISSYSERR (errno != 0 || WSAGetLastError() != 0)
194#define XCOM_SYSERRNUM (WSAGetLastError() ? WSAGetLastError() : (errno))
195#define XCOM_SYSERRSTR \
196 (WSAGetLastError() ? g_strerror(WSAGetLastError()) : g_strerror(errno))
197#define xcom_gmtime_r(time, res) gmtime_s(res, time)
198#define xcom_localtime_r(time, res) localtime_s(res, time)
199
200static inline void thread_yield() { SwitchToThread(); }
201#else /* defined (_WIN32)*/
202
203#include <fcntl.h>
204#include <sched.h>
205#include <string.h>
206#include <strings.h>
207#include <unistd.h>
208
209#ifndef O_LARGEFILE
210#define O_LARGEFILE 0
211#endif
212
213/* TODO: Decide in one prefix for system-independent names (xcom or
214 xcom). Or perhaps really something generic that doesn't depend on
215 the current product name */
216#define xcom_close(fd) close(fd)
217#define xcom_fclose(f) fclose(f)
218#define xcom_chdir(s) chdir(s)
219#define xcom_dup(s) dup(s)
220#define xcom_dup2(s, t) dup2(s, t)
221#define xcom_lseek(fd, of, h) lseek(fd, of, h)
222#define xcom_open(n, f, m) open(n, f, m)
223#define xcom_fopen(n, m) fopen(n, m)
224#define xcom_fstat(f, b) fstat(f, b)
225
226#define XCOM_F_TLOCK F_TLOCK
227#define xcom_lockf(fd, f, sz) lockf(fd, f, sz)
228
229#define xcom_lrand48() lrand48()
230#define xcom_srand48(x) srand48(x)
231#define xcom_drand48() drand48()
232
233#define xcom_write(fd, buf, l) write(fd, buf, l)
234#define xcom_read(fd, buf, c) read(fd, buf, c)
235#define xcom_execv execv
236
237#define xcom_mktemp(s) mktemp(s)
238
239/* WARNING: snprintf() != _snprintf() */
240/* #define xcom_snprintf(argv) snprintf(argv) */
241#define xcom_strdup(x) strdup(x)
242#if defined(_WIN32)
243#define xcom_strtok(b, d, c) strtok_s(b, d, c)
244#else
245#define xcom_strtok(b, d, c) strtok_r(b, d, c)
246#endif
247
248#define xcom_strcasecmp(a, b) strcasecmp(a, b)
249
250/** Posix states that the optval argument should be (const) void*, but
251 on Windows it is (const) char* and since we normally pass in int*
252 a cast is required on Windows.
253
254 With this typedef we can call xsockopt functions uniformly across
255 platorms, allbeit with an unnecessary cast to void* on *nix. */
256typedef void *xcom_sockoptptr_t;
257
258#define XCOM_O_CREAT O_CREAT
259#define XCOM_O_WRONLY O_WRONLY
260#define XCOM_O_APPEND O_APPEND
261#define XCOM_O_LARGEFILE O_LARGEFILE
262#define XCOM_O_EXCL O_EXCL
263#define XCOM_O_RDWR O_RDWR
264#define XCOM_O_RDONLY O_RDONLY
265#ifdef __APPLE__
266/* OSX does not define O_RSYNC, use O_DSYNC instead */
267#define XCOM_O_RSYNC O_DSYNC
268#else
269#define XCOM_O_RSYNC O_RSYNC
270#endif
271#define XCOM_O_BINARY 0 /** Empty define - applicable WIN32 only */
272
273#define XCOM_S_IRUSR S_IRUSR
274#define XCOM_S_IWUSR S_IWUSR
275#define XCOM_S_IXUSR S_IXUSR
276#define XCOM_S_IWRXU XCOM_S_IRUSR | XCOM_S_IWUSR | XCOM_S_IXUSR
277#define XCOM_S_IRGRP S_IRGRP
278#define XCOM_S_IWGRP S_IWGRP
279#define XCOM_S_IROTH S_IROTH
280#define XCOM_S_IWOTH S_IWOTH
281
282#define XCOM_FILE_WRITE_MODE "wF"
283
284#define SOCKET_ERROR -1
285
286#define XCOM_CLRSYSERR errno = 0
287#define XCOM_ISSYSERR (errno != 0)
288#define XCOM_SYSERRNUM ((errno) + 0)
289#define XCOM_SYSERRSTR g_strerror(errno)
290
291#define xcom_gmtime_r(time, res) gmtime_r(time, res)
292#define xcom_localtime_r(time, res) localtime_r(time, res)
293
294#define thread_yield sched_yield
295
296#endif /* defined (_WIN32) */
297
298#ifdef _WIN32
299#define NEWLINE "\r\n"
300#else
301#define NEWLINE "\n"
302#endif
303
304#ifdef _WIN32
305#define xcom_buf char
306#else
307#define xcom_buf void
308#endif
309
310#endif
void * xcom_sockoptptr_t
Posix states that the optval argument should be (const) void*, but on Windows it is (const) char* and...
Definition: x_platform.h:256
#define thread_yield
Definition: x_platform.h:294