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__
43 #ifdef HAVE_VISIBILITY_HIDDEN
44 #define MPH_INTERNAL __attribute__((visibility("hidden")))
49 #if defined (PLATFORM_WIN32) && !defined (EOVERFLOW)
51 #endif /* def PLATFORM_WIN32 && ndef EOVERFLOW */
53 #if !defined (PLATFORM_WIN32)
56 * Solaris doesn't define these BSD values, and if they're not present then
57 * map.c:Mono_Posix_FromSeekFlags() breaks badly; see:
58 * http://bugzilla.gnome.org/show_bug.cgi?id=370081
62 #define L_SET SEEK_SET
63 #endif /* ndef L_SET */
66 #define L_INCR SEEK_CUR
67 #endif /* ndef L_INCR */
70 #define L_XTND SEEK_END
71 #endif /* ndef L_XTND */
74 * XATTR_AUTO is a synonym for 0 within XattrFlags, but most systems don't
75 * define it. map.c doesn't know that, though, so we ensure that it's defined
76 * so that the value 0 round-trips through MonoPosixHelper.
81 #endif /* ndef XATTR_AUTO */
83 #endif /* ndef PLATFORM_WIN32 */
85 typedef gint64 mph_blkcnt_t
;
86 typedef gint64 mph_blksize_t
;
87 typedef guint64 mph_dev_t
;
88 typedef guint64 mph_ino_t
;
89 typedef guint64 mph_nlink_t
;
90 typedef gint64 mph_off_t
;
91 typedef guint64 mph_size_t
;
92 typedef gint64 mph_ssize_t
;
93 typedef gint32 mph_pid_t
;
94 typedef guint32 mph_gid_t
;
95 typedef guint32 mph_uid_t
;
96 typedef gint64 mph_time_t
;
97 typedef gint64 mph_clock_t
;
98 typedef guint64 mph_fsblkcnt_t
;
99 typedef guint64 mph_fsfilcnt_t
;
101 /* Some versions of OS X don't define these typedefs, needed by map.c */
102 #ifndef HAVE_BLKCNT_T
103 typedef mph_blkcnt_t blkcnt_t
;
106 #ifndef HAVE_BLKSIZE_T
107 typedef mph_blksize_t blksize_t
;
110 #ifndef HAVE_SUSECONDS_T
111 typedef gint64 suseconds_t
;
114 #ifdef HAVE_LARGE_FILE_SUPPORT
115 #define MPH_OFF_T_MAX G_MAXINT64
116 #define MPH_OFF_T_MIN G_MININT64
118 #define MPH_OFF_T_MAX G_MAXINT32
119 #define MPH_OFF_T_MIN G_MININT32
123 #define MPH_SIZE_T_MAX SIZE_MAX
124 #elif SIZEOF_SIZE_T == 8
125 #define MPH_SIZE_T_MAX G_MAXUINT64
126 #elif SIZEOF_SIZE_T == 4
127 #define MPH_SIZE_T_MAX G_MAXUINT32
129 #error "sizeof(size_t) is unknown!"
132 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
138 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
140 #define mph_return_val_if_long_overflow(var, ret) \
141 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
143 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
145 #define mph_have_ulong_overflow(var) ((var) > ULONG_MAX)
147 #define mph_return_val_if_ulong_overflow(var, ret) \
148 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
150 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
152 #define mph_have_size_t_overflow(var) ((var) > MPH_SIZE_T_MAX)
154 #define mph_return_val_if_size_t_overflow(var, ret) \
155 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
157 #define mph_return_val_if_ssize_t_overflow(var, ret) \
158 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
160 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
162 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
164 #define mph_have_off_t_overflow(var) \
165 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
167 #define mph_return_val_if_off_t_overflow(var, ret) \
168 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
170 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
172 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
174 #define mph_return_if_val_in_list5(var,a,b,c,d,e) \
177 if (v == a || v == b || v == c || v == d || v == e) \
182 * Helper function for functions which use ERANGE (such as getpwnam_r and
183 * getgrnam_r). These functions accept buffers which are dynamically
184 * allocated so that they're only as large as necessary. However, Linux and
185 * Mac OS X differ on how to signal an error value.
187 * Linux returns the error value directly, while Mac OS X is more traditional,
188 * returning -1 and setting errno accordingly.
190 * Unify the checking in one place.
193 recheck_range (int ret
)
198 return errno
== ERANGE
;
202 typedef unsigned int mph_string_offset_t
;
205 MPH_STRING_OFFSET_PTR
= 0x0,
206 MPH_STRING_OFFSET_ARRAY
= 0x1,
207 MPH_STRING_OFFSET_MASK
= 0x1
210 #define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind)
213 _mph_copy_structure_strings (
214 void *to
, const mph_string_offset_t
*to_offsets
,
215 const void *from
, const mph_string_offset_t
*from_offsets
,
218 #endif /* ndef INC_mph_H */