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).
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.
18 * See the typedefs for type size assumptions. These typedefs *must* be kept
19 * in sync with the types used in Mono.Posix.dll.
22 * http://developer.apple.com/documentation/Darwin/Reference/ManPages/
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
40 #include <stdint.h> /* for SIZE_MAX */
43 #ifdef ANDROID_UNIFIED_HEADERS
53 #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */
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
61 #if !defined(fgetpos) && __ANDROID_API__ < 24
62 int fgetpos(FILE*, fpos_t*);
65 #if !defined(fsetpos) && __ANDROID_API__ < 24
66 int fsetpos(FILE*, const fpos_t*);
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)
77 #undef HAVE_STRUCT_PASSWD_PW_GECOS
82 #if __APPLE__ || __BSD__ || __FreeBSD__ || __OpenBSD__
86 #ifdef HAVE_VISIBILITY_HIDDEN
87 #define MPH_INTERNAL __attribute__((visibility("hidden")))
92 #if !defined(EOVERFLOW)
93 # if defined(HOST_WIN32)
95 # elif defined(__OpenBSD__)
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
107 #define L_SET SEEK_SET
108 #endif /* ndef L_SET */
111 #define L_INCR SEEK_CUR
112 #endif /* ndef L_INCR */
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
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.
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
;
163 #ifndef HAVE_BLKSIZE_T
164 typedef mph_blksize_t blksize_t
;
167 #ifndef HAVE_SUSECONDS_T
168 typedef gint64 suseconds_t
;
171 #ifdef HAVE_LARGE_FILE_SUPPORT
172 #define MPH_OFF_T_MAX G_MAXINT64
173 #define MPH_OFF_T_MIN G_MININT64
175 #define MPH_OFF_T_MAX G_MAXINT32
176 #define MPH_OFF_T_MIN G_MININT32
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
186 #error "sizeof(size_t) is unknown!"
189 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
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) \
243 if (v == a || v == b || v == c || v == d || v == e) \
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.
259 recheck_range (int ret
)
264 return errno
== ERANGE
;
268 typedef unsigned int mph_string_offset_t
;
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)
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
,
284 #endif /* ndef INC_mph_H */