From 1297a513dad3ce3bc6f8472ed592a865aea18fac Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 6 Nov 2013 11:34:02 +0100 Subject: [PATCH] Speed up and explain fd_isset() Signed-off-by: Denys Vlasenko --- desc.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/desc.c b/desc.c index e18735ca..39572269 100644 --- a/desc.c +++ b/desc.c @@ -477,12 +477,20 @@ sys_getdtablesize(struct tcb *tcp) } #endif -static int -fd_isset(int d, fd_set *fds) +/* FD_ISSET from libc would abort for large fd if built with + * debug flags/library hacks which enforce array bound checks + * (fd_set contains a fixed-size array of longs). + * We need to use a homegrown replacement. + */ +static inline int +fd_isset(unsigned fd, fd_set *fds) { - const int bpl = 8 * sizeof(long); - long *s = (long *) fds; - return !!(s[d / bpl] & (1L << (d % bpl))); + /* Using unsigned types to avoid signed divisions and shifts, + * which are slow(er) on many CPUs. + */ + const unsigned bpl = 8 * sizeof(long); + unsigned long *s = (unsigned long *) fds; + return s[fd / bpl] & (1UL << (fd % bpl)); } static int -- 2.40.0