7c7705e958b991b0b6e4914531aa09e443f7a9d6
[mono.git] / support / mph.h
1 /*
2  * Common/shared macros and routines.
3  *
4  * This file contains macros of the form
5  *
6  *   mph_return_if_TYPE_overflow(val);
7  *
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).
11  *
12  * Assumptions:
13  *
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  * :-(
17  *
18  * See the typedefs for type size assumptions.  These typedefs *must* be kept
19  * in sync with the types used in Mono.Posix.dll.
20  *
21  * See also:
22  *   http://developer.apple.com/documentation/Darwin/Reference/ManPages/
23  */
24
25 #ifndef INC_mph_H
26 #define INC_mph_H
27
28 #include <config.h>
29
30 #include <limits.h>             /* LONG_MAX, ULONG_MAX */
31 #include <errno.h>              /* for ERANGE */
32 #include <glib.h>               /* for g* types, etc. */
33
34 #ifdef HAVE_STDINT_H
35 #include <stdint.h>             /* for SIZE_MAX */
36 #endif
37
38 #if __APPLE__ || __BSD__ || __FreeBSD__
39 #define MPH_ON_BSD
40 #endif
41
42 #ifdef HAVE_VISIBILITY_HIDDEN
43 #define MPH_INTERNAL __attribute__((visibility("hidden")))
44 #else
45 #define MPH_INTERNAL
46 #endif
47
48 #if defined (PLATFORM_WIN32) && !defined (EOVERFLOW)
49 #define EOVERFLOW 75
50 #endif /* def PLATFORM_WIN32 && ndef EOVERFLOW */
51
52 #if !defined (PLATFORM_WIN32)
53
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
58  */
59
60 #ifndef L_SET
61 #define L_SET SEEK_SET
62 #endif /* ndef L_SET */
63
64 #ifndef L_INCR
65 #define L_INCR SEEK_CUR
66 #endif /* ndef L_INCR */
67
68 #ifndef L_XTND
69 #define L_XTND SEEK_END
70 #endif /* ndef L_XTND */
71
72 #endif /* ndef PLATFORM_WIN32 */
73
74 typedef    gint64 mph_blkcnt_t;
75 typedef    gint64 mph_blksize_t;
76 typedef   guint64 mph_dev_t;
77 typedef   guint64 mph_ino_t;
78 typedef   guint64 mph_nlink_t;
79 typedef    gint64 mph_off_t;
80 typedef   guint64 mph_size_t;
81 typedef    gint64 mph_ssize_t;
82 typedef    gint32 mph_pid_t;
83 typedef   guint32 mph_gid_t;
84 typedef   guint32 mph_uid_t;
85 typedef    gint64 mph_time_t;
86 typedef    gint64 mph_clock_t;
87 typedef   guint64 mph_fsblkcnt_t;
88 typedef   guint64 mph_fsfilcnt_t;
89
90 /* Some versions of OS X don't define these typedefs, needed by map.c */
91 #ifndef HAVE_BLKCNT_T
92 typedef mph_blkcnt_t blkcnt_t;
93 #endif
94
95 #ifndef HAVE_BLKSIZE_T
96 typedef mph_blksize_t blksize_t;
97 #endif
98
99 #ifndef HAVE_SUSECONDS_T
100 typedef gint64 suseconds_t;
101 #endif
102
103 #ifdef HAVE_LARGE_FILE_SUPPORT
104 #define MPH_OFF_T_MAX G_MAXINT64
105 #define MPH_OFF_T_MIN G_MININT64
106 #else
107 #define MPH_OFF_T_MAX G_MAXINT32
108 #define MPH_OFF_T_MIN G_MININT32
109 #endif
110
111 #ifdef SIZE_MAX
112 #define MPH_SIZE_T_MAX SIZE_MAX
113 #elif SIZEOF_SIZE_T == 8
114 #define MPH_SIZE_T_MAX  G_MAXUINT64
115 #elif SIZEOF_SIZE_T == 4
116 #define MPH_SIZE_T_MAX  G_MAXUINT32
117 #else
118 #error "sizeof(size_t) is unknown!"
119 #endif
120
121 #define _mph_return_val_if_cb_(val, ret, cb) G_STMT_START{ \
122         if (cb (val)) { \
123                 errno = EOVERFLOW; \
124                 return ret; \
125         }}G_STMT_END
126
127 #define mph_have_long_overflow(var) ((var) > LONG_MAX || (var) < LONG_MIN)
128
129 #define mph_return_val_if_long_overflow(var, ret) \
130         _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
131
132 #define mph_return_if_long_overflow(var) mph_return_val_if_long_overflow(var, -1)
133
134 #define mph_have_ulong_overflow(var) ((var) > ULONG_MAX)
135
136 #define mph_return_val_if_ulong_overflow(var, ret) \
137         _mph_return_val_if_cb_(var, ret, mph_have_ulong_overflow)
138
139 #define mph_return_if_ulong_overflow(var) mph_return_val_if_ulong_overflow(var, -1)
140
141 #define mph_have_size_t_overflow(var) ((var) > MPH_SIZE_T_MAX)
142
143 #define mph_return_val_if_size_t_overflow(var, ret) \
144         _mph_return_val_if_cb_(var, ret, mph_have_size_t_overflow)
145
146 #define mph_return_val_if_ssize_t_overflow(var, ret) \
147         _mph_return_val_if_cb_(var, ret, mph_have_long_overflow)
148
149 #define mph_return_if_size_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
150
151 #define mph_return_if_ssize_t_overflow(var) mph_return_val_if_ssize_t_overflow(var, -1)
152
153 #define mph_have_off_t_overflow(var) \
154         (((var) < MPH_OFF_T_MIN) || ((var) > MPH_OFF_T_MAX))
155
156 #define mph_return_val_if_off_t_overflow(var, ret) \
157         _mph_return_val_if_cb_(var, ret, mph_have_off_t_overflow)
158
159 #define mph_return_if_off_t_overflow(var) mph_return_val_if_size_t_overflow(var, -1)
160
161 #define mph_return_if_time_t_overflow(var) mph_return_if_long_overflow(var)
162
163 /*
164  * Helper function for functions which use ERANGE (such as getpwnam_r and
165  * getgrnam_r).  These functions accept buffers which are dynamically
166  * allocated so that they're only as large as necessary.  However, Linux and
167  * Mac OS X differ on how to signal an error value.
168  *
169  * Linux returns the error value directly, while Mac OS X is more traditional,
170  * returning -1 and setting errno accordingly.
171  *
172  * Unify the checking in one place.
173  */
174 static inline int
175 recheck_range (int ret)
176 {
177         if (ret == ERANGE)
178                 return 1;
179         if (ret == -1)
180                 return errno == ERANGE;
181         return 0;
182 }
183
184 MPH_INTERNAL char* 
185 _mph_copy_structure_strings (
186         void *to,         const size_t *to_offsets, 
187         const void *from, const size_t *from_offsets, 
188         size_t num_strings);
189
190 #endif /* ndef INC_mph_H */
191
192 /*
193  * vim: noexpandtab
194  */