Patches to improve Hurd parts: include/sys/socket.h
Thomas Schwinge
tschwinge@gnu.org
Wed Dec 17 17:14:00 GMT 2008
We need two helper functions and should do some compile-time consistency
checks.
2008-12-17 Thomas Schwinge <tschwinge@gnu.org>
* include/sys/socket.h: Include <fcntl.h>.
(COMPILE_TIME_ASSERT): New definition. Use it for doing some
consistency checks.
(sock_to_o_flags, o_to_sock_flags): New always-inline functions.
diff --git a/include/sys/socket.h b/include/sys/socket.h
index df89278..7d783ca 100644
--- a/include/sys/socket.h
+++ b/include/sys/socket.h
@@ -160,5 +160,49 @@ extern int __have_sock_cloexec;
unless it is really necessary. */
# define __have_paccept __have_sock_cloexec
#endif
+
+#include <fcntl.h>
+
+/* Unless the condition is true, a compile-time error will be emitted. */
+#define COMPILE_TIME_ASSERT(condition) \
+ extern int compile_time_assert[!!(condition) - 1]
+
+/* Do some compile-time checks for the SOCK_* constants, which we rely on. */
+COMPILE_TIME_ASSERT (SOCK_CLOEXEC == O_CLOEXEC);
+#if defined (SOCK_MAX) && defined (SOCK_TYPE_MASK)
+COMPILE_TIME_ASSERT ((SOCK_MAX | SOCK_TYPE_MASK) == SOCK_TYPE_MASK);
+COMPILE_TIME_ASSERT ((SOCK_CLOEXEC & SOCK_TYPE_MASK) == 0);
+COMPILE_TIME_ASSERT ((SOCK_NONBLOCK & SOCK_TYPE_MASK) == 0);
+#endif
+
+#undef COMPILE_TIME_ASSERT
+
+__extern_always_inline
+int
+sock_to_o_flags (int in)
+{
+ int out = 0;
+
+ if (in & SOCK_NONBLOCK)
+ out |= O_NONBLOCK;
+ /* Others are passed through unfiltered. */
+ out |= in & ~(SOCK_NONBLOCK);
+
+ return out;
+}
+
+__extern_always_inline
+int
+o_to_sock_flags (int in)
+{
+ int out = 0;
+
+ if (in & O_NONBLOCK)
+ out |= SOCK_NONBLOCK;
+ /* Others are passed through unfiltered. */
+ out |= in & ~(O_NONBLOCK);
+
+ return out;
+}
#endif
Some questions remain. Note that all this is not Hurd-specific.
Are we allowed to include <fcntl.h> from include/sys/socket.h?
Where to put the COMPILE_TIME_ASSERT definition? For sure into some
other file, but into which one?
Where to put these COMPILE_TIME_ASSERTs? And where to put
sock_to_o_flags and o_to_sock_flags?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 191 bytes
Desc: Digital signature
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20081217/ef39a111/attachment.sig>
More information about the Libc-alpha
mailing list