This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
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?
Attachment:
signature.asc
Description: Digital signature
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |