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) */
57 #if !defined (HOST_WIN32)
60 * Solaris doesn't define these BSD values, and if they're not present then
61 * map.c:Mono_Posix_FromSeekFlags() breaks badly; see:
62 * http://bugzilla.gnome.org/show_bug.cgi?id=370081
66 #define L_SET SEEK_SET
67 #endif /* ndef L_SET */
70 #define L_INCR SEEK_CUR
71 #endif /* ndef L_INCR */
74 #define L_XTND SEEK_END
75 #endif /* ndef L_XTND */
78 * XATTR_AUTO is a synonym for 0 within XattrFlags, but most systems don't
79 * define it. map.c doesn't know that, though, so we ensure that it's defined
80 * so that the value 0 round-trips through MonoPosixHelper.
85 #endif /* ndef XATTR_AUTO */
87 #endif /* ndef HOST_WIN32 */
89 typedef gint64 mph_blkcnt_t
;
90 typedef gint64 mph_blksize_t
;
91 typedef guint64 mph_dev_t
;
92 typedef guint64 mph_ino_t
;
93 typedef guint64 mph_nlink_t
;
94 typedef gint64 mph_off_t
;
95 typedef guint64 mph_size_t
;
96 typedef gint64 mph_ssize_t
;
97 typedef gint32 mph_pid_t
;
98 typedef guint32 mph_gid_t
;
99 typedef guint32 mph_uid_t
;
100 typedef gint64 mph_time_t
;
101 typedef gint64 mph_clock_t
;
102 typedef guint64 mph_fsblkcnt_t
;
103 typedef guint64 mph_fsfilcnt_t
;
105 /* Some versions of OS X don't define these typedefs, needed by map.c */
106 #ifndef HAVE_BLKCNT_T
107 typedef mph_blkcnt_t blkcnt_t
;
110 #ifndef HAVE_BLKSIZE_T
111 typedef mph_blksize_t blksize_t
;
114 #ifndef HAVE_SUSECONDS_T
115 typedef gint64 suseconds_t
;
118 #ifdef HAVE_LARGE_FILE_SUPPORT
119 #define MPH_OFF_T_MAX G_MAXINT64
120 #define MPH_OFF_T_MIN G_MININT64
122 #define MPH_OFF_T_MAX G_MAXINT32
123 #define MPH_OFF_T_MIN G_MININT32
127 #define MPH_SIZE_T_MAX SIZE_MAX
128 #elif SIZEOF_SIZE_T == 8
129 #define MPH_SIZE_T_MAX G_MAXUINT64
130 #elif SIZEOF_SIZE_T == 4
131 #define MPH_SIZE_T_MAX G_MAXUINT32
133 #error "sizeof(size_t) is unknown!"
136 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
142 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
144 #define mph_return_val_if_long_overflow(var, ret) \
145 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
147 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
149 #define mph_have_ulong_overflow(var) ((var) > ULONG_MAX)
151 #define mph_return_val_if_ulong_overflow(var, ret) \
152 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
154 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
156 #define mph_have_size_t_overflow(var) ((var) > MPH_SIZE_T_MAX)
158 #define mph_return_val_if_size_t_overflow(var, ret) \
159 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
161 #define mph_return_val_if_ssize_t_overflow(var, ret) \
162 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
164 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
166 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
168 #define mph_have_off_t_overflow(var) \
169 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
171 #define mph_return_val_if_off_t_overflow(var, ret) \
172 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
174 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
176 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
178 #define mph_return_if_val_in_list5(var,a,b,c,d,e) \
181 if (v == a || v == b || v == c || v == d || v == e) \
186 * Helper function for functions which use ERANGE (such as getpwnam_r and
187 * getgrnam_r). These functions accept buffers which are dynamically
188 * allocated so that they're only as large as necessary. However, Linux and
189 * Mac OS X differ on how to signal an error value.
191 * Linux returns the error value directly, while Mac OS X is more traditional,
192 * returning -1 and setting errno accordingly.
194 * Unify the checking in one place.
197 recheck_range (int ret
)
202 return errno
== ERANGE
;
206 typedef unsigned int mph_string_offset_t
;
209 MPH_STRING_OFFSET_PTR
= 0x0,
210 MPH_STRING_OFFSET_ARRAY
= 0x1,
211 MPH_STRING_OFFSET_MASK
= 0x1
214 #define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind)
217 _mph_copy_structure_strings (
218 void *to
, const mph_string_offset_t
*to_offsets
,
219 const void *from
, const mph_string_offset_t
*from_offsets
,
222 #endif /* ndef INC_mph_H */