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