runtime.h remove extern "C" and distribute normally instead of extra. (#15558)
[mono-project.git] / support / mph.h
blob2fbae6a27cddd04bcbc592ac70a1a501256ce27e
1 /*
2 * Common/shared macros and routines.
4 * This file contains macros of the form
6 * mph_return_if_TYPE_overflow(val);
8 * Which tests `val' for a TYPE underflow/overflow (that is, is `val' within
9 * the range for TYPE?). If `val' can't fit in TYPE, errno is set to
10 * EOVERFLOW, and `return -1' is executed (which is why it's a macro).
12 * Assumptions:
14 * I'm working from GLibc, so that's the basis for my assumptions. They may
15 * not be completely portable, in which case I'll need to fix my assumptions.
16 * :-(
18 * See the typedefs for type size assumptions. These typedefs *must* be kept
19 * in sync with the types used in Mono.Posix.dll.
21 * See also:
22 * http://developer.apple.com/documentation/Darwin/Reference/ManPages/
25 #ifndef INC_mph_H
26 #define INC_mph_H
28 #include <config.h>
30 #include <stddef.h> /* offsetof */
31 #include <limits.h> /* LONG_MAX, ULONG_MAX */
32 #include <errno.h> /* for ERANGE */
33 #include <glib.h> /* for g* types, etc. */
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
39 #ifdef HAVE_STDINT_H
40 #include <stdint.h> /* for SIZE_MAX */
41 #endif
43 #ifdef ANDROID_UNIFIED_HEADERS
44 #ifdef HAVE_STDIO_H
45 #include <stdio.h>
46 #endif
48 #undef st_atime_nsec
49 #undef st_mtime_nsec
50 #undef st_ctime_nsec
52 #ifndef L_cuserid
53 #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
54 #endif
56 /* NDK unified headers will define fpos_t to be 64-bit if large files support is
57 * enabled (which is the case with Mono) so we need to make sure the offsets here
58 * are actually 32-bit for Android APIs before API24 which did NOT have the 64-bit
59 * versions.
61 #if !defined(fgetpos) && __ANDROID_API__ < 24
62 int fgetpos(FILE*, fpos_t*);
63 #endif
65 #if !defined(fsetpos) && __ANDROID_API__ < 24
66 int fsetpos(FILE*, const fpos_t*);
67 #endif
69 #ifdef HAVE_PWD_H
70 #include <pwd.h>
71 #endif
72 /* Unified headers define 'pw_gecos' to be an alias for 'pw_passwd` on 32-bit Android which
73 * results in two fields named 'pw_passwd' in map.h's 'struct passwd'
75 #if !defined(__LP64__) && defined(pw_gecos)
76 #undef pw_gecos
77 #undef HAVE_STRUCT_PASSWD_PW_GECOS
78 #endif
80 #endif
82 #if __APPLE__ || __BSD__ || __FreeBSD__ || __OpenBSD__
83 #define MPH_ON_BSD
84 #endif
86 #ifdef HAVE_VISIBILITY_HIDDEN
87 #define MPH_INTERNAL __attribute__((visibility("hidden")))
88 #else
89 #define MPH_INTERNAL
90 #endif
92 #if !defined(EOVERFLOW)
93 # if defined(HOST_WIN32)
94 # define EOVERFLOW 75
95 # elif defined(__OpenBSD__)
96 # define EOVERFLOW 87
97 # endif
98 #endif /* !defined(EOVERFLOW) */
101 * Solaris/Windows don't define these BSD values, and if they're not present
102 * then map.c:Mono_Posix_FromSeekFlags() breaks badly; see:
103 * http://bugzilla.gnome.org/show_bug.cgi?id=370081
106 #ifndef L_SET
107 #define L_SET SEEK_SET
108 #endif /* ndef L_SET */
110 #ifndef L_INCR
111 #define L_INCR SEEK_CUR
112 #endif /* ndef L_INCR */
114 #ifndef L_XTND
115 #define L_XTND SEEK_END
116 #endif /* ndef L_XTND */
118 #if !defined (HOST_WIN32)
121 * OS X doesn't define MAP_ANONYMOUS, but it does define MAP_ANON.
122 * Alias them to fix: https://bugzilla.xamarin.com/show_bug.cgi?id=3419
124 #ifdef HOST_DARWIN
125 #ifndef MAP_ANONYMOUS
126 #define MAP_ANONYMOUS MAP_ANON
127 #endif /* ndef MAP_ANONYMOUS */
128 #endif /* ndef HOST_DARWIN */
131 * XATTR_AUTO is a synonym for 0 within XattrFlags, but most systems don't
132 * define it. map.c doesn't know that, though, so we ensure that it's defined
133 * so that the value 0 round-trips through MonoPosixHelper.
136 #ifndef XATTR_AUTO
137 #define XATTR_AUTO 0
138 #endif /* ndef XATTR_AUTO */
140 #endif /* ndef HOST_WIN32 */
142 typedef gint64 mph_blkcnt_t;
143 typedef gint64 mph_blksize_t;
144 typedef guint64 mph_dev_t;
145 typedef guint64 mph_ino_t;
146 typedef guint64 mph_nlink_t;
147 typedef gint64 mph_off_t;
148 typedef guint64 mph_size_t;
149 typedef gint64 mph_ssize_t;
150 typedef gint32 mph_pid_t;
151 typedef guint32 mph_gid_t;
152 typedef guint32 mph_uid_t;
153 typedef gint64 mph_time_t;
154 typedef gint64 mph_clock_t;
155 typedef guint64 mph_fsblkcnt_t;
156 typedef guint64 mph_fsfilcnt_t;
158 /* Some versions of OS X don't define these typedefs, needed by map.c */
159 #ifndef HAVE_BLKCNT_T
160 typedef mph_blkcnt_t blkcnt_t;
161 #endif
163 #ifndef HAVE_BLKSIZE_T
164 typedef mph_blksize_t blksize_t;
165 #endif
167 #ifndef HAVE_SUSECONDS_T
168 typedef gint64 suseconds_t;
169 #endif
171 #ifdef HAVE_LARGE_FILE_SUPPORT
172 #define MPH_OFF_T_MAX G_MAXINT64
173 #define MPH_OFF_T_MIN G_MININT64
174 #else
175 #define MPH_OFF_T_MAX G_MAXINT32
176 #define MPH_OFF_T_MIN G_MININT32
177 #endif
179 #ifdef SIZE_MAX
180 #define MPH_SIZE_T_MAX SIZE_MAX
181 #elif SIZEOF_SIZE_T == 8
182 #define MPH_SIZE_T_MAX G_MAXUINT64
183 #elif SIZEOF_SIZE_T == 4
184 #define MPH_SIZE_T_MAX G_MAXUINT32
185 #else
186 #error "sizeof(size_t) is unknown!"
187 #endif
189 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
190 if (cb (val)) { \
191 errno = EOVERFLOW; \
192 return ret; \
193 }}G_STMT_END
195 #define mph_have_uint_overflow(var) ((var) < 0 || (var) > UINT_MAX)
197 #define mph_return_val_if_uint_overflow(var, ret) \
198 _mph_return_val_if_cb_(var, ret, mph_have_uint_overflow)
200 #define mph_return_if_uint_overflow(var) mph_return_val_if_uint_overflow(var, -1)
202 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
204 #define mph_return_val_if_long_overflow(var, ret) \
205 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
207 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
209 #define mph_have_ulong_overflow(var) (var) < 0 || ((var) > ULONG_MAX)
211 #define mph_return_val_if_ulong_overflow(var, ret) \
212 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
214 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
216 #define mph_have_size_t_overflow(var) ((var) < 0 || (var) > MPH_SIZE_T_MAX)
218 #define mph_return_val_if_size_t_overflow(var, ret) \
219 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
221 #define mph_return_val_if_ssize_t_overflow(var, ret) \
222 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
224 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
226 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
228 #define mph_have_off_t_overflow(var) \
229 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
231 #define mph_return_val_if_off_t_overflow(var, ret) \
232 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
234 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
236 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
238 #define mph_return_if_socklen_t_overflow(var) mph_return_if_uint_overflow(var)
240 #define mph_return_if_val_in_list5(var,a,b,c,d,e) \
241 do { \
242 int v = (var); \
243 if (v == a || v == b || v == c || v == d || v == e) \
244 return -1; \
245 } while (0)
248 * Helper function for functions which use ERANGE (such as getpwnam_r and
249 * getgrnam_r). These functions accept buffers which are dynamically
250 * allocated so that they're only as large as necessary. However, Linux and
251 * Mac OS X differ on how to signal an error value.
253 * Linux returns the error value directly, while Mac OS X is more traditional,
254 * returning -1 and setting errno accordingly.
256 * Unify the checking in one place.
258 static inline int
259 recheck_range (int ret)
261 if (ret == ERANGE)
262 return 1;
263 if (ret == -1)
264 return errno == ERANGE;
265 return 0;
268 typedef unsigned int mph_string_offset_t;
270 enum {
271 MPH_STRING_OFFSET_PTR = 0x0,
272 MPH_STRING_OFFSET_ARRAY = 0x1,
273 MPH_STRING_OFFSET_MASK = 0x1
276 #define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind)
278 MPH_INTERNAL char*
279 _mph_copy_structure_strings (
280 void *to, const mph_string_offset_t *to_offsets,
281 const void *from, const mph_string_offset_t *from_offsets,
282 size_t num_strings);
284 #endif /* ndef INC_mph_H */
287 * vim: noexpandtab