2007-03-09 Chris Toshok <toshok@ximian.com>
[mono-project/dkf.git] / support / mph.h
blob56f83e52b16e6c832bab0b8ec843fd640c2b3abb
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 <limits.h> /* LONG_MAX, ULONG_MAX */
31 #include <errno.h> /* for ERANGE */
32 #include <glib.h> /* for g* types, etc. */
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h> /* for SIZE_MAX */
36 #endif
38 #if __APPLE__ || __BSD__ || __FreeBSD__
39 #define MPH_ON_BSD
40 #endif
42 #ifdef HAVE_VISIBILITY_HIDDEN
43 #define MPH_INTERNAL __attribute__((visibility("hidden")))
44 #else
45 #define MPH_INTERNAL
46 #endif
48 #if defined (PLATFORM_WIN32) && !defined (EOVERFLOW)
49 #define EOVERFLOW 75
50 #endif /* def PLATFORM_WIN32 && ndef EOVERFLOW */
52 #if !defined (PLATFORM_WIN32)
54 /*
55 * Solaris doesn't define these BSD values, and if they're not present then
56 * map.c:Mono_Posix_FromSeekFlags() breaks badly; see:
57 * http://bugzilla.gnome.org/show_bug.cgi?id=370081
60 #ifndef L_SET
61 #define L_SET SEEK_SET
62 #endif /* ndef L_SET */
64 #ifndef L_INCR
65 #define L_INCR SEEK_CUR
66 #endif /* ndef L_INCR */
68 #ifndef L_XTND
69 #define L_XTND SEEK_END
70 #endif /* ndef L_XTND */
73 * XATTR_AUTO is a synonym for 0 within XattrFlags, but most systems don't
74 * define it. map.c doesn't know that, though, so we ensure that it's defined
75 * so that the value 0 round-trips through MonoPosixHelper.
78 #ifndef XATTR_AUTO
79 #define XATTR_AUTO 0
80 #endif /* ndef XATTR_AUTO */
82 #endif /* ndef PLATFORM_WIN32 */
84 typedef gint64 mph_blkcnt_t;
85 typedef gint64 mph_blksize_t;
86 typedef guint64 mph_dev_t;
87 typedef guint64 mph_ino_t;
88 typedef guint64 mph_nlink_t;
89 typedef gint64 mph_off_t;
90 typedef guint64 mph_size_t;
91 typedef gint64 mph_ssize_t;
92 typedef gint32 mph_pid_t;
93 typedef guint32 mph_gid_t;
94 typedef guint32 mph_uid_t;
95 typedef gint64 mph_time_t;
96 typedef gint64 mph_clock_t;
97 typedef guint64 mph_fsblkcnt_t;
98 typedef guint64 mph_fsfilcnt_t;
100 /* Some versions of OS X don't define these typedefs, needed by map.c */
101 #ifndef HAVE_BLKCNT_T
102 typedef mph_blkcnt_t blkcnt_t;
103 #endif
105 #ifndef HAVE_BLKSIZE_T
106 typedef mph_blksize_t blksize_t;
107 #endif
109 #ifndef HAVE_SUSECONDS_T
110 typedef gint64 suseconds_t;
111 #endif
113 #ifdef HAVE_LARGE_FILE_SUPPORT
114 #define MPH_OFF_T_MAX G_MAXINT64
115 #define MPH_OFF_T_MIN G_MININT64
116 #else
117 #define MPH_OFF_T_MAX G_MAXINT32
118 #define MPH_OFF_T_MIN G_MININT32
119 #endif
121 #ifdef SIZE_MAX
122 #define MPH_SIZE_T_MAX SIZE_MAX
123 #elif SIZEOF_SIZE_T == 8
124 #define MPH_SIZE_T_MAX G_MAXUINT64
125 #elif SIZEOF_SIZE_T == 4
126 #define MPH_SIZE_T_MAX G_MAXUINT32
127 #else
128 #error "sizeof(size_t) is unknown!"
129 #endif
131 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
132 if (cb (val)) { \
133 errno = EOVERFLOW; \
134 return ret; \
135 }}G_STMT_END
137 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
139 #define mph_return_val_if_long_overflow(var, ret) \
140 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
142 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
144 #define mph_have_ulong_overflow(var) ((var) > ULONG_MAX)
146 #define mph_return_val_if_ulong_overflow(var, ret) \
147 _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
149 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
151 #define mph_have_size_t_overflow(var) ((var) > MPH_SIZE_T_MAX)
153 #define mph_return_val_if_size_t_overflow(var, ret) \
154 _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
156 #define mph_return_val_if_ssize_t_overflow(var, ret) \
157 _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
159 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
161 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
163 #define mph_have_off_t_overflow(var) \
164 (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
166 #define mph_return_val_if_off_t_overflow(var, ret) \
167 _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
169 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
171 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
174 * Helper function for functions which use ERANGE (such as getpwnam_r and
175 * getgrnam_r). These functions accept buffers which are dynamically
176 * allocated so that they're only as large as necessary. However, Linux and
177 * Mac OS X differ on how to signal an error value.
179 * Linux returns the error value directly, while Mac OS X is more traditional,
180 * returning -1 and setting errno accordingly.
182 * Unify the checking in one place.
184 static inline int
185 recheck_range (int ret)
187 if (ret == ERANGE)
188 return 1;
189 if (ret == -1)
190 return errno == ERANGE;
191 return 0;
194 MPH_INTERNAL char*
195 _mph_copy_structure_strings (
196 void *to, const size_t *to_offsets,
197 const void *from, const size_t *from_offsets,
198 size_t num_strings);
200 #endif /* ndef INC_mph_H */
203 * vim: noexpandtab