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. */
36 #include <stdint.h> /* for SIZE_MAX */
39 #if __APPLE__ || __BSD__ || __FreeBSD__ || __OpenBSD__
43 #ifdef HAVE_VISIBILITY_HIDDEN
44 #define MPH_INTERNAL __attribute__((visibility("hidden")))
49 #if !defined(EOVERFLOW)
50 # if defined(HOST_WIN32)
52 # elif defined(__OpenBSD__)
55 #endif /* !defined(EOVERFLOW) */
58 * Solaris/Windows don't define these BSD values, and if they're not present
59 * then map.c:Mono_Posix_FromSeekFlags() breaks badly; see:
60 * http://bugzilla.gnome.org/show_bug.cgi?id=370081
64 #define L_SET SEEK_SET
65 #endif /* ndef L_SET */
68 #define L_INCR SEEK_CUR
69 #endif /* ndef L_INCR */
72 #define L_XTND SEEK_END
73 #endif /* ndef L_XTND */
75 #if !defined (HOST_WIN32)
78 * OS X doesn't define MAP_ANONYMOUS, but it does define MAP_ANON.
79 * Alias them to fix: https://bugzilla.xamarin.com/show_bug.cgi?id=3419
81 #ifdef PLATFORM_MACOSX
83 #define MAP_ANONYMOUS MAP_ANON
84 #endif /* ndef MAP_ANONYMOUS */
85 #endif /* ndef PLATFORM_MACOSX */
88 * XATTR_AUTO is a synonym for 0 within XattrFlags, but most systems don't
89 * define it. map.c doesn't know that, though, so we ensure that it's defined
90 * so that the value 0 round-trips through MonoPosixHelper.
95 #endif /* ndef XATTR_AUTO */
97 #endif /* ndef HOST_WIN32 */
99 typedef gint64 mph_blkcnt_t
;
100 typedef gint64 mph_blksize_t
;
101 typedef guint64 mph_dev_t
;
102 typedef guint64 mph_ino_t
;
103 typedef guint64 mph_nlink_t
;
104 typedef gint64 mph_off_t
;
105 typedef guint64 mph_size_t
;
106 typedef gint64 mph_ssize_t
;
107 typedef gint32 mph_pid_t
;
108 typedef guint32 mph_gid_t
;
109 typedef guint32 mph_uid_t
;
110 typedef gint64 mph_time_t
;
111 typedef gint64 mph_clock_t
;
112 typedef guint64 mph_fsblkcnt_t
;
113 typedef guint64 mph_fsfilcnt_t
;
115 /* Some versions of OS X don't define these typedefs, needed by map.c */
116 #ifndef HAVE_BLKCNT_T
117 typedef mph_blkcnt_t blkcnt_t
;
120 #ifndef HAVE_BLKSIZE_T
121 typedef mph_blksize_t blksize_t
;
124 #ifndef HAVE_SUSECONDS_T
125 typedef gint64 suseconds_t
;
128 #ifdef HAVE_LARGE_FILE_SUPPORT
129 #define MPH_OFF_T_MAX G_MAXINT64
130 #define MPH_OFF_T_MIN G_MININT64
132 #define MPH_OFF_T_MAX G_MAXINT32
133 #define MPH_OFF_T_MIN G_MININT32
137 #define MPH_SIZE_T_MAX SIZE_MAX
138 #elif SIZEOF_SIZE_T == 8
139 #define MPH_SIZE_T_MAX G_MAXUINT64
140 #elif SIZEOF_SIZE_T == 4
141 #define MPH_SIZE_T_MAX G_MAXUINT32
143 #error "sizeof(size_t) is unknown!"
146 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
152 #define mph_have_uint_overflow(var) ((var) < 0 || (var) > UINT_MAX)
154 #define mph_return_val_if_uint_overflow(var, ret) \
155 _mph_return_val_if_cb_(var, ret, mph_have_uint_overflow)
157 #define mph_return_if_uint_overflow(var) mph_return_val_if_uint_overflow(var, -1)
159 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
161 #define mph_return_val_if_long_overflow(var, ret) \
162 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
164 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
166 #define mph_have_ulong_overflow(var) (var) < 0 || ((var) > ULONG_MAX)
168 #define mph_return_val_if_ulong_overflow(var, ret) \
169 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
171 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
173 #define mph_have_size_t_overflow(var) ((var) < 0 || (var) > MPH_SIZE_T_MAX)
175 #define mph_return_val_if_size_t_overflow(var, ret) \
176 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
178 #define mph_return_val_if_ssize_t_overflow(var, ret) \
179 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
181 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
183 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
185 #define mph_have_off_t_overflow(var) \
186 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
188 #define mph_return_val_if_off_t_overflow(var, ret) \
189 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
191 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
193 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
195 #define mph_return_if_socklen_t_overflow(var) mph_return_if_uint_overflow(var)
197 #define mph_return_if_val_in_list5(var,a,b,c,d,e) \
200 if (v == a || v == b || v == c || v == d || v == e) \
205 * Helper function for functions which use ERANGE (such as getpwnam_r and
206 * getgrnam_r). These functions accept buffers which are dynamically
207 * allocated so that they're only as large as necessary. However, Linux and
208 * Mac OS X differ on how to signal an error value.
210 * Linux returns the error value directly, while Mac OS X is more traditional,
211 * returning -1 and setting errno accordingly.
213 * Unify the checking in one place.
216 recheck_range (int ret
)
221 return errno
== ERANGE
;
225 typedef unsigned int mph_string_offset_t
;
228 MPH_STRING_OFFSET_PTR
= 0x0,
229 MPH_STRING_OFFSET_ARRAY
= 0x1,
230 MPH_STRING_OFFSET_MASK
= 0x1
233 #define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind)
236 _mph_copy_structure_strings (
237 void *to
, const mph_string_offset_t
*to_offsets
,
238 const void *from
, const mph_string_offset_t
*from_offsets
,
241 #endif /* ndef INC_mph_H */