forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portable.h
240 lines (216 loc) · 5.78 KB
/
portable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/// @file
#ifndef C3_PORTABLE_H
#define C3_PORTABLE_H
# ifndef __GNUC__
# error "port me"
# endif
# ifndef _GNU_SOURCE
# define _GNU_SOURCE
# endif
/** System include files.
***
*** Do not put include files that are only used in the
*** porting layer here. Include them directly in the
*** C file.
**/
# if defined(U3_OS_linux)
# ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 700
# endif
# include <ctype.h>
# include <inttypes.h>
# include <stdlib.h>
# include <string.h>
# include <stdarg.h>
# include <unistd.h>
# include <stdint.h>
# include <assert.h>
# include <byteswap.h>
# include <setjmp.h>
# include <stdio.h>
# include <signal.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/mman.h>
# include <sys/sendfile.h>
# elif defined(U3_OS_osx)
# include <ctype.h>
# include <inttypes.h>
# include <stdlib.h>
# include <string.h>
# include <stdarg.h>
# include <unistd.h>
# include <stdint.h>
# include <assert.h>
# include <setjmp.h>
# include <signal.h>
# include <machine/endian.h>
# include <machine/byte_order.h>
# include <stdio.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/syslimits.h>
# include <sys/mman.h>
# include <sys/clonefile.h>
# include <copyfile.h>
# elif defined(U3_OS_bsd)
# include <ctype.h>
# include <inttypes.h>
# include <stdlib.h>
# include <string.h>
# include <stdarg.h>
# include <unistd.h>
# include <stdint.h>
# include <assert.h>
# include <machine/endian.h>
# include <setjmp.h>
# include <stdio.h>
# include <signal.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/mman.h>
# else
#error "port: headers"
# endif
# ifndef __has_feature
# define __has_feature(x) 0
# endif
# if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
# define ASAN_ENABLED
# endif
/** Platform string.
**/
# if defined(U3_OS_linux)
# ifdef __LP64__
# ifdef U3_CPU_aarch64
# define U3_OS_ARCH "linux-aarch64"
# else
# define U3_OS_ARCH "linux-x86_64"
# endif
# endif
# elif defined(U3_OS_osx)
# ifdef __LP64__
# ifdef U3_CPU_aarch64
# define U3_OS_ARCH "macos-aarch64"
# else
# define U3_OS_ARCH "macos-x86_64"
# endif
# endif
# endif
# define U3_BIN_ALIAS ".run"
/** Address space layout.
***
*** NB: 2^30 words == 4G
**/
# if defined(U3_OS_linux)
# ifdef __LP64__
# ifdef ASAN_ENABLED
# define U3_OS_LoomBase 0x10007ffff000
# else
# define U3_OS_LoomBase 0x200000000
# endif
# else
# define U3_OS_LoomBase 0x36000000
# endif
# define U3_OS_LoomBits 30
# elif defined(U3_OS_osx)
# ifdef __LP64__
# ifdef ASAN_ENABLED
# define U3_OS_LoomBase 0x728000000000
# else
# define U3_OS_LoomBase 0x28000000000
# endif
# else
# define U3_OS_LoomBase 0x4000000
# endif
# define U3_OS_LoomBits 30
# elif defined(U3_OS_bsd)
# ifdef __LP64__
# define U3_OS_LoomBase 0x200000000
# else
# define U3_OS_LoomBase 0x4000000
# endif
# define U3_OS_LoomBits 30
# else
# error "port: LoomBase"
# endif
/** Private C "extensions."
***
*** Except for these and main(), any function, macro, or structure
*** names must be prefixed either by u3_/U3_ (for public names),
*** or _ (for static and other file-local names).
**/
/* Endianness.
*/
# define c3_endian_little 0
# define c3_endian_big 1
# ifdef U3_OS_ENDIAN_little
# define c3_endian c3_endian_little
# elif defined(U3_OS_ENDIAN_big)
# define c3_endian c3_endian_big
# else
# error "port: U3_OS_ENDIAN"
# endif
/* Byte swapping.
*/
# if defined(U3_OS_linux) || defined(U3_OS_bsd)
# define c3_bswap_16(x) bswap_16(x)
# define c3_bswap_32(x) bswap_32(x)
# define c3_bswap_64(x) bswap_64(x)
# elif defined(U3_OS_osx)
# define c3_bswap_16(x) NXSwapShort(x)
# define c3_bswap_32(x) NXSwapInt(x)
# define c3_bswap_64(x) NXSwapLongLong(x)
# else
# error "port: byte swap"
# endif
/* Sync.
*/
# if defined(U3_OS_linux)
# define c3_sync(fd) (fdatasync(fd))
# elif defined(U3_OS_osx)
# define c3_sync(fd) (fcntl(fd, F_FULLFSYNC, 0))
# elif defined(U3_OS_bsd)
# define c3_sync(fd) (fsync(fd))
# else
# error "port: sync"
# endif
/* Purge.
*/
# if defined(U3_OS_linux)
# include <stdio_ext.h>
# define c3_fpurge __fpurge
# elif defined(U3_OS_bsd) || defined(U3_OS_osx)
# define c3_fpurge fpurge
# else
# error "port: fpurge"
# endif
/* Stat.
*/
# if defined(U3_OS_linux)
# define c3_stat_mtime(dp) (u3_time_t_in_ts((dp)->st_mtime))
# elif defined(U3_OS_osx)
# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtimespec)))
# define lseek64 lseek
# elif defined(U3_OS_bsd)
# define c3_stat_mtime(dp) (u3_time_in_ts(&((dp)->st_mtim)))
# define lseek64 lseek
# else
# error "port: timeconvert"
# endif
/* Null.
*/
# if defined(U3_OS_linux) || defined(U3_OS_bsd) || defined(U3_OS_osx)
# define c3_dev_null "/dev/null"
# else
# error "port: /dev/null"
# endif
/* Static assertion.
TODO: we could just use static_assert (c23)/_Static_assert (c11) in
<assert.h>
*/
# define ASSERT_CONCAT_(a, b) a##b
# define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
# define STATIC_ASSERT(e,m) \
enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) }
#endif /* ifndef C3_PORTABLE_H */