[runtime] Cleanup the horrible ifdef hell that is the stack bounds discovery code.
[mono-project.git] / support / mph.h
blobaedff01b7f32d6f8ce7f2a570c0472afa6e48c6e
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_STDINT_H
36 #include <stdint.h> /* for SIZE_MAX */
37 #endif
39 #if __APPLE__ || __BSD__ || __FreeBSD__ || __OpenBSD__
40 #define MPH_ON_BSD
41 #endif
43 #ifdef HAVE_VISIBILITY_HIDDEN
44 #define MPH_INTERNAL __attribute__((visibility("hidden")))
45 #else
46 #define MPH_INTERNAL
47 #endif
49 #if !defined(EOVERFLOW)
50 # if defined(HOST_WIN32)
51 # define EOVERFLOW 75
52 # elif defined(__OpenBSD__)
53 # define EOVERFLOW 87
54 # endif
55 #endif /* !defined(EOVERFLOW) */
57 #if !defined (HOST_WIN32)
59 /*
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
65 #ifndef L_SET
66 #define L_SET SEEK_SET
67 #endif /* ndef L_SET */
69 #ifndef L_INCR
70 #define L_INCR SEEK_CUR
71 #endif /* ndef L_INCR */
73 #ifndef L_XTND
74 #define L_XTND SEEK_END
75 #endif /* ndef L_XTND */
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
82 #ifndef MAP_ANONYMOUS
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.
93 #ifndef XATTR_AUTO
94 #define XATTR_AUTO 0
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;
118 #endif
120 #ifndef HAVE_BLKSIZE_T
121 typedef mph_blksize_t blksize_t;
122 #endif
124 #ifndef HAVE_SUSECONDS_T
125 typedef gint64 suseconds_t;
126 #endif
128 #ifdef HAVE_LARGE_FILE_SUPPORT
129 #define MPH_OFF_T_MAX G_MAXINT64
130 #define MPH_OFF_T_MIN G_MININT64
131 #else
132 #define MPH_OFF_T_MAX G_MAXINT32
133 #define MPH_OFF_T_MIN G_MININT32
134 #endif
136 #ifdef SIZE_MAX
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
142 #else
143 #error "sizeof(size_t) is unknown!"
144 #endif
146 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
147 if (cb (val)) { \
148 errno = EOVERFLOW; \
149 return ret; \
150 }}G_STMT_END
152 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
154 #define mph_return_val_if_long_overflow(var, ret) \
155 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
157 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
159 #define mph_have_ulong_overflow(var) ((var) > ULONG_MAX)
161 #define mph_return_val_if_ulong_overflow(var, ret) \
162 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
164 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
166 #define mph_have_size_t_overflow(var) ((var) > MPH_SIZE_T_MAX)
168 #define mph_return_val_if_size_t_overflow(var, ret) \
169 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
171 #define mph_return_val_if_ssize_t_overflow(var, ret) \
172 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
174 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
176 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
178 #define mph_have_off_t_overflow(var) \
179 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
181 #define mph_return_val_if_off_t_overflow(var, ret) \
182 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
184 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
186 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
188 #define mph_return_if_val_in_list5(var,a,b,c,d,e) \
189 do { \
190 int v = (var); \
191 if (v == a || v == b || v == c || v == d || v == e) \
192 return -1; \
193 } while (0)
196 * Helper function for functions which use ERANGE (such as getpwnam_r and
197 * getgrnam_r). These functions accept buffers which are dynamically
198 * allocated so that they're only as large as necessary. However, Linux and
199 * Mac OS X differ on how to signal an error value.
201 * Linux returns the error value directly, while Mac OS X is more traditional,
202 * returning -1 and setting errno accordingly.
204 * Unify the checking in one place.
206 static inline int
207 recheck_range (int ret)
209 if (ret == ERANGE)
210 return 1;
211 if (ret == -1)
212 return errno == ERANGE;
213 return 0;
216 typedef unsigned int mph_string_offset_t;
218 enum {
219 MPH_STRING_OFFSET_PTR = 0x0,
220 MPH_STRING_OFFSET_ARRAY = 0x1,
221 MPH_STRING_OFFSET_MASK = 0x1
224 #define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind)
226 MPH_INTERNAL char*
227 _mph_copy_structure_strings (
228 void *to, const mph_string_offset_t *to_offsets,
229 const void *from, const mph_string_offset_t *from_offsets,
230 size_t num_strings);
232 #endif /* ndef INC_mph_H */
235 * vim: noexpandtab