2010-04-08 Rodrigo Kumpera <rkumpera@novell.com>
[mono-project.git] / support / mph.h
blob8a61999942641e4e81bb5c953e5c04f494510c4f
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 * 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.
83 #ifndef XATTR_AUTO
84 #define XATTR_AUTO 0
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;
108 #endif
110 #ifndef HAVE_BLKSIZE_T
111 typedef mph_blksize_t blksize_t;
112 #endif
114 #ifndef HAVE_SUSECONDS_T
115 typedef gint64 suseconds_t;
116 #endif
118 #ifdef HAVE_LARGE_FILE_SUPPORT
119 #define MPH_OFF_T_MAX G_MAXINT64
120 #define MPH_OFF_T_MIN G_MININT64
121 #else
122 #define MPH_OFF_T_MAX G_MAXINT32
123 #define MPH_OFF_T_MIN G_MININT32
124 #endif
126 #ifdef SIZE_MAX
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
132 #else
133 #error "sizeof(size_t) is unknown!"
134 #endif
136 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
137 if (cb (val)) { \
138 errno = EOVERFLOW; \
139 return ret; \
140 }}G_STMT_END
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) \
179 do { \
180 int v = (var); \
181 if (v == a || v == b || v == c || v == d || v == e) \
182 return -1; \
183 } while (0)
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.
196 static inline int
197 recheck_range (int ret)
199 if (ret == ERANGE)
200 return 1;
201 if (ret == -1)
202 return errno == ERANGE;
203 return 0;
206 typedef unsigned int mph_string_offset_t;
208 enum {
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)
216 MPH_INTERNAL char*
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,
220 size_t num_strings);
222 #endif /* ndef INC_mph_H */
225 * vim: noexpandtab