diff --git a/include/asterisk/select.h b/include/asterisk/select.h index a8f7c3c60..d6d34fbbd 100644 --- a/include/asterisk/select.h +++ b/include/asterisk/select.h @@ -43,12 +43,14 @@ typedef struct { TYPEOF_FD_SET_FDS_BITS fds_bits[ast_FDMAX / 8 / SIZEOF_FD_SET_FDS_BITS]; /* 32768 bits */ } ast_fdset; +#define _bitsize(a) (sizeof(a) * 8) + #undef FD_ZERO #define FD_ZERO(a) \ do { \ TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) a; \ int i; \ - for (i = 0; i < sizeof(*(a)) / SIZEOF_FD_SET_FDS_BITS; i++) { \ + for (i = 0; i < ast_FDMAX / _bitsize(TYPEOF_FD_SET_FDS_BITS); i++) { \ bytes[i] = 0; \ } \ } while (0) @@ -56,8 +58,10 @@ typedef struct { #define FD_SET(fd, fds) \ do { \ TYPEOF_FD_SET_FDS_BITS *bytes = (TYPEOF_FD_SET_FDS_BITS *) fds; \ - if (fd / sizeof(*bytes) + ((fd + 1) % sizeof(*bytes) ? 1 : 0) < sizeof(*(fds))) { \ - bytes[fd / (sizeof(*bytes) * 8)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % (sizeof(*bytes) * 8)); \ + /* 32bit: FD / 32 + ((FD + 1) % 32 ? 1 : 0) < 1024 */ \ + /* 64bit: FD / 64 + ((FD + 1) % 64 ? 1 : 0) < 512 */ \ + if (fd / _bitsize(*bytes) + ((fd + 1) % _bitsize(*bytes) ? 1 : 0) < sizeof(*(fds)) / SIZEOF_FD_SET_FDS_BITS) { \ + bytes[fd / _bitsize(*bytes)] |= ((TYPEOF_FD_SET_FDS_BITS) 1) << (fd % _bitsize(*bytes)); \ } else { \ fprintf(stderr, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \ } \