In mono/metadata:
[mono.git] / support / mph.h
blobdb0324cc65139ef7a7e59b3b45843a72624c8176
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__
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 (HOST_WIN32) && !defined (EOVERFLOW)
50 #define EOVERFLOW 75
51 #endif /* def HOST_WIN32 && ndef EOVERFLOW */
53 #if !defined (HOST_WIN32)
55 /*
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
61 #ifndef L_SET
62 #define L_SET SEEK_SET
63 #endif /* ndef L_SET */
65 #ifndef L_INCR
66 #define L_INCR SEEK_CUR
67 #endif /* ndef L_INCR */
69 #ifndef L_XTND
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.
79 #ifndef XATTR_AUTO
80 #define XATTR_AUTO 0
81 #endif /* ndef XATTR_AUTO */
83 #endif /* ndef HOST_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;
104 #endif
106 #ifndef HAVE_BLKSIZE_T
107 typedef mph_blksize_t blksize_t;
108 #endif
110 #ifndef HAVE_SUSECONDS_T
111 typedef gint64 suseconds_t;
112 #endif
114 #ifdef HAVE_LARGE_FILE_SUPPORT
115 #define MPH_OFF_T_MAX G_MAXINT64
116 #define MPH_OFF_T_MIN G_MININT64
117 #else
118 #define MPH_OFF_T_MAX G_MAXINT32
119 #define MPH_OFF_T_MIN G_MININT32
120 #endif
122 #ifdef SIZE_MAX
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
128 #else
129 #error "sizeof(size_t) is unknown!"
130 #endif
132 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
133 if (cb (val)) { \
134 errno = EOVERFLOW; \
135 return ret; \
136 }}G_STMT_END
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) \
175 do { \
176 int v = (var); \
177 if (v == a || v == b || v == c || v == d || v == e) \
178 return -1; \
179 } while (0)
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.
192 static inline int
193 recheck_range (int ret)
195 if (ret == ERANGE)
196 return 1;
197 if (ret == -1)
198 return errno == ERANGE;
199 return 0;
202 typedef unsigned int mph_string_offset_t;
204 enum {
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)
212 MPH_INTERNAL char*
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,
216 size_t num_strings);
218 #endif /* ndef INC_mph_H */
221 * vim: noexpandtab