Skip to content

Commit a74b848

Browse files
authored
Adding constexpr/const/noexcept where relevant (#8519)
* Add const/constexpr and noexcept * Change macOs detection
1 parent 3e6be86 commit a74b848

File tree

2 files changed

+80
-92
lines changed

2 files changed

+80
-92
lines changed

src/remote/inet.cpp

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797

9898
#endif // !WIN_NT
9999

100-
const int INET_RETRY_CALL = 5;
100+
constexpr int INET_RETRY_CALL = 5;
101101

102102
#include "../remote/remote.h"
103103
#include "../remote/SockAddr.h"
@@ -136,7 +136,7 @@ using namespace Firebird;
136136
#define INET_RETRY_ERRNO WSAEINPROGRESS
137137
#define INET_ADDR_IN_USE WSAEADDRINUSE
138138
#define sleep(seconds) Sleep ((seconds) * 1000)
139-
const int NOTASOCKET = WSAENOTSOCK;
139+
constexpr int NOTASOCKET = WSAENOTSOCK;
140140

141141
#else // WIN_NT
142142

@@ -184,9 +184,9 @@ static void SOCLOSE(SOCKET& socket)
184184
#ifdef HAVE_SYS_TIMEB_H
185185
# include <sys/timeb.h>
186186
#endif
187-
const int TRACE_packets = 1 << 0; // bit 0
188-
const int TRACE_operations = 1 << 1; // bit 1
189-
const int TRACE_summary = 1 << 2; // bit 2
187+
constexpr int TRACE_packets = 1 << 0; // bit 0
188+
constexpr int TRACE_operations = 1 << 1; // bit 1
189+
constexpr int TRACE_summary = 1 << 2; // bit 2
190190

191191
static int INET_trace = TRACE_summary | TRACE_packets | TRACE_operations;
192192
static time_t INET_start_time = 0;
@@ -224,20 +224,20 @@ static ULONG inet_debug_timer()
224224
}
225225
#endif // DEBUG
226226

227-
const ULONG MAX_DATA_LW = 1448; // Low Water mark
228-
const ULONG MAX_DATA_HW = 32768; // High Water mark
229-
const ULONG DEF_MAX_DATA = 8192;
227+
constexpr ULONG MAX_DATA_LW = 1448; // Low Water mark
228+
constexpr ULONG MAX_DATA_HW = 32768; // High Water mark
229+
constexpr ULONG DEF_MAX_DATA = 8192;
230230

231-
//const int MAXHOSTLEN = 64;
231+
//constexpr int MAXHOSTLEN = 64;
232232

233-
const int SELECT_TIMEOUT = 60; // Dispatch thread select timeout (sec)
233+
constexpr int SELECT_TIMEOUT = 60; // Dispatch thread select timeout (sec)
234234

235235
class Select
236236
{
237237
#ifdef HAVE_POLL
238238
private:
239-
static const int SEL_INIT_EVENTS = POLLIN;
240-
static const int SEL_CHECK_MASK = POLLIN;
239+
static constexpr int SEL_INIT_EVENTS = POLLIN;
240+
static constexpr int SEL_CHECK_MASK = POLLIN;
241241

242242
pollfd* getPollFd(int n)
243243
{
@@ -471,7 +471,7 @@ class Select
471471
#endif // HAVE_POLL
472472
}
473473

474-
int getCount()
474+
int getCount() noexcept
475475
{
476476
return slct_count;
477477
}
@@ -696,7 +696,7 @@ rem_port* INET_analyze(ClntAuthBlock* cBlock,
696696

697697
// Should compression be tried?
698698

699-
bool compression = config && (*config)->getWireCompression();
699+
const bool compression = config && (*config)->getWireCompression();
700700

701701
// Establish connection to server
702702
// If we want user verification, we can't speak anything less than version 7
@@ -706,7 +706,7 @@ rem_port* INET_analyze(ClntAuthBlock* cBlock,
706706
cnct->p_cnct_user_id.cstr_length = (ULONG) user_id.getBufferLength();
707707
cnct->p_cnct_user_id.cstr_address = user_id.getBuffer();
708708

709-
static const p_cnct::p_cnct_repeat protocols_to_try[] =
709+
static constexpr p_cnct::p_cnct_repeat protocols_to_try[] =
710710
{
711711
REMOTE_PROTOCOL(PROTOCOL_VERSION10, ptype_lazy_send, 1),
712712
REMOTE_PROTOCOL(PROTOCOL_VERSION11, ptype_lazy_send, 2),
@@ -775,7 +775,7 @@ rem_port* INET_analyze(ClntAuthBlock* cBlock,
775775
cc->p_cc_reply = 1;
776776
}
777777
UCHAR* reply = buf.getBuffer(cc->p_cc_reply);
778-
unsigned l = cryptCb->callback(cc->p_cc_data.cstr_length,
778+
const unsigned l = cryptCb->callback(cc->p_cc_data.cstr_length,
779779
cc->p_cc_data.cstr_address, cc->p_cc_reply, reply);
780780

781781
REMOTE_free_packet(port, packet, true);
@@ -815,7 +815,7 @@ rem_port* INET_analyze(ClntAuthBlock* cBlock,
815815
throw;
816816
}
817817
// fall through - response is not a required accept
818-
818+
[[fallthrough]];
819819
default:
820820
disconnect(port);
821821
delete rdb;
@@ -841,7 +841,7 @@ rem_port* INET_analyze(ClntAuthBlock* cBlock,
841841
port->port_flags |= PORT_symmetric;
842842
}
843843

844-
bool compress = accept->p_acpt_type & pflag_compress;
844+
const bool compress = accept->p_acpt_type & pflag_compress;
845845
accept->p_acpt_type &= ptype_MASK;
846846

847847
if (accept->p_acpt_type != ptype_out_of_band) {
@@ -955,8 +955,7 @@ rem_port* INET_connect(const TEXT* name,
955955
// Prepare hints
956956
const bool ipv6 = os_utils::isIPv6supported();
957957

958-
struct addrinfo gai_hints;
959-
memset(&gai_hints, 0, sizeof(gai_hints));
958+
addrinfo gai_hints {};
960959
if (packet)
961960
gai_hints.ai_family = af;
962961
else
@@ -1082,7 +1081,7 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
10821081
*
10831082
**************************************/
10841083

1085-
int ipv6_v6only = port->getPortConfig()->getIPv6V6Only() ? 1 : 0;
1084+
const int ipv6_v6only = port->getPortConfig()->getIPv6V6Only() ? 1 : 0;
10861085

10871086
int n = setsockopt(port->port_handle, IPPROTO_IPV6, IPV6_V6ONLY,
10881087
(SCHAR*) &ipv6_v6only, sizeof(ipv6_v6only));
@@ -1097,7 +1096,7 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
10971096
// e.g. while it's listening. This is surely not what we want.
10981097
// We set this options for any kind of listener, including standalone Classic.
10991098

1100-
int optval = TRUE;
1099+
constexpr int optval = TRUE;
11011100
n = setsockopt(port->port_handle, SOL_SOCKET, SO_REUSEADDR,
11021101
(SCHAR*) &optval, sizeof(optval));
11031102
if (n == -1)
@@ -1108,9 +1107,7 @@ static rem_port* listener_socket(rem_port* port, USHORT flag, const addrinfo* pa
11081107

11091108
if (flag & SRVR_multi_client)
11101109
{
1111-
struct linger lingerInfo;
1112-
lingerInfo.l_onoff = 0;
1113-
lingerInfo.l_linger = 0;
1110+
const linger lingerInfo {};
11141111

11151112
// Get any values for SO_LINGER so that they can be reset during
11161113
// disconnect. SO_LINGER should be set by default on the socket
@@ -1393,7 +1390,7 @@ static rem_port* alloc_port(rem_port* const parent, const USHORT flags)
13931390
{
13941391
#ifdef WIN_NT
13951392
static WSADATA wsadata;
1396-
const WORD version = MAKEWORD(2, 0);
1393+
constexpr WORD version = MAKEWORD(2, 0);
13971394
const int wsaError = WSAStartup(version, &wsadata);
13981395
if (wsaError)
13991396
{
@@ -1493,9 +1490,8 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
14931490

14941491
if (port->port_server_flags)
14951492
{
1496-
struct timeval timeout;
1493+
timeval timeout {};
14971494
timeout.tv_sec = port->port_connect_timeout;
1498-
timeout.tv_usec = 0;
14991495

15001496
Select slct;
15011497
slct.set(port->port_channel);
@@ -1515,7 +1511,7 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
15151511

15161512
const ISC_STATUS error_code =
15171513
(count == 0) ? isc_net_event_connect_timeout : isc_net_event_connect_err;
1518-
int savedError = inetErrNo;
1514+
const int savedError = inetErrNo;
15191515
SOCLOSE(port->port_channel);
15201516
inet_error(false, port, "select", error_code, savedError);
15211517
}
@@ -1529,7 +1525,7 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
15291525

15301526
if (n == INVALID_SOCKET)
15311527
{
1532-
int savedError = inetErrNo;
1528+
const int savedError = inetErrNo;
15331529
SOCLOSE(port->port_channel);
15341530
inet_error(false, port, "accept", isc_net_event_connect_err, savedError);
15351531
}
@@ -1563,7 +1559,7 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
15631559
int status = address.getpeername(port->port_handle);
15641560
if (status != 0)
15651561
{
1566-
int savedError = INET_ERRNO;
1562+
const int savedError = INET_ERRNO;
15671563
port->auxAcceptError(packet);
15681564
inet_error(false, port, "socket", isc_net_event_connect_err, savedError);
15691565
}
@@ -1576,7 +1572,7 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
15761572
SOCKET n = os_utils::socket(address.family(), SOCK_STREAM, 0);
15771573
if (n == INVALID_SOCKET)
15781574
{
1579-
int savedError = INET_ERRNO;
1575+
const int savedError = INET_ERRNO;
15801576
port->auxAcceptError(packet);
15811577
inet_error(false, port, "socket", isc_net_event_connect_err, savedError);
15821578
}
@@ -1586,7 +1582,7 @@ static rem_port* aux_connect(rem_port* port, PACKET* packet)
15861582
status = address.connect(n);
15871583
if (status < 0)
15881584
{
1589-
int savedError = INET_ERRNO;
1585+
const int savedError = INET_ERRNO;
15901586
SOCLOSE(n);
15911587
port->auxAcceptError(packet);
15921588
inet_error(false, port, "connect", isc_net_event_connect_err, savedError);
@@ -1621,10 +1617,10 @@ static rem_port* aux_request( rem_port* port, PACKET* packet)
16211617
gds__log("INET/aux_request: failed to get local address of the original socket");
16221618
inet_error(false, port, "getsockname", isc_net_event_listen_err, INET_ERRNO);
16231619
}
1624-
unsigned short aux_port = port->getPortConfig()->getRemoteAuxPort();
1620+
const unsigned short aux_port = port->getPortConfig()->getRemoteAuxPort();
16251621
our_address.setPort(aux_port); // may be 0
16261622

1627-
SOCKET n = os_utils::socket(our_address.family(), SOCK_STREAM, 0);
1623+
const SOCKET n = os_utils::socket(our_address.family(), SOCK_STREAM, 0);
16281624
if (n == INVALID_SOCKET)
16291625
{
16301626
inet_error(false, port, "socket", isc_net_event_listen_err, INET_ERRNO);
@@ -1686,19 +1682,20 @@ static rem_port* aux_request( rem_port* port, PACKET* packet)
16861682
// Here we try to make this case work. However it's not bullet-proof for others platforms and architectures.
16871683
// A proper solution would be to just send the port number in a protocol friendly way.
16881684

1689-
bool macOsClient =
1685+
const bool macOsClient =
16901686
port->port_client_arch == arch_darwin_ppc ||
16911687
port->port_client_arch == arch_darwin_x64 ||
16921688
port->port_client_arch == arch_darwin_ppc64;
16931689

1694-
bool macOsServer =
1690+
if constexpr (
16951691
ARCHITECTURE == arch_darwin_ppc ||
16961692
ARCHITECTURE == arch_darwin_x64 ||
1697-
ARCHITECTURE == arch_darwin_ppc64;
1698-
1699-
if (macOsServer && !macOsClient)
1700-
port_address.convertFromMacOsToPosixWindows();
1701-
else if (!macOsServer && macOsClient)
1693+
ARCHITECTURE == arch_darwin_ppc64)
1694+
{
1695+
if (!macOsClient)
1696+
port_address.convertFromMacOsToPosixWindows();
1697+
}
1698+
else if (macOsClient)
17021699
port_address.convertFromPosixWindowsToMacOs();
17031700

17041701
response->p_resp_data.cstr_length = (ULONG) port_address.length();
@@ -2211,7 +2208,7 @@ static void select_port(rem_port* main_port, Select* selct, RemPortPtr& port)
22112208
MutexLockGuard guard(port_mutex, FB_FUNCTION);
22122209
while (true)
22132210
{
2214-
Select::HandleState result = selct->checkNext(port);
2211+
const Select::HandleState result = selct->checkNext(port);
22152212
if (!port)
22162213
return;
22172214

@@ -2254,7 +2251,6 @@ static bool select_wait( rem_port* main_port, Select* selct)
22542251
* to read from them.
22552252
*
22562253
**************************************/
2257-
struct timeval timeout;
22582254
bool checkPorts = false;
22592255

22602256
for (;;)
@@ -2305,13 +2301,12 @@ static bool select_wait( rem_port* main_port, Select* selct)
23052301
// in current fdset. Search and return it to caller to close
23062302
// broken connection correctly
23072303

2308-
struct linger lngr;
2304+
linger lngr {};
23092305
socklen_t optlen = sizeof(lngr);
2310-
const bool badSocket =
23112306
#ifdef WIN_NT
2312-
false;
2307+
constexpr bool badSocket = false;
23132308
#else
2314-
(port->port_handle < 0 || port->port_handle >= FD_SETSIZE);
2309+
const bool badSocket = (port->port_handle < 0 || port->port_handle >= FD_SETSIZE);
23152310
#endif
23162311

23172312
if (badSocket || getsockopt(port->port_handle,
@@ -2365,9 +2360,8 @@ static bool select_wait( rem_port* main_port, Select* selct)
23652360

23662361
// Some platforms change the timeout in the select call.
23672362
// Reset timeout for each iteration to avoid problems.
2363+
timeval timeout {};
23682364
timeout.tv_sec = SELECT_TIMEOUT;
2369-
timeout.tv_usec = 0;
2370-
23712365
selct->select(&timeout);
23722366
const int inetErrNo = INET_ERRNO;
23732367

@@ -2529,7 +2523,7 @@ void get_peer_info(rem_port* port)
25292523
address.unmapV4(); // convert mapped IPv4 to regular IPv4
25302524
char host[64]; // 32 digits, 7 colons, 1 trailing null byte
25312525
char serv[16];
2532-
int nameinfo = getnameinfo(address.ptr(), address.length(), host, sizeof(host),
2526+
const int nameinfo = getnameinfo(address.ptr(), address.length(), host, sizeof(host),
25332527
serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
25342528

25352529
if (!nameinfo)
@@ -2969,8 +2963,7 @@ static bool packet_receive(rem_port* port, UCHAR* buffer, SSHORT buffer_length,
29692963
return false;
29702964
}
29712965

2972-
timeval timeout;
2973-
timeout.tv_usec = 0;
2966+
timeval timeout{};
29742967
timeval* time_ptr = NULL;
29752968

29762969
if (port->port_protocol == 0)
@@ -3330,8 +3323,8 @@ static bool setNoNagleOption(rem_port* port)
33303323
**************************************/
33313324
if (port->getPortConfig()->getTcpNoNagle())
33323325
{
3333-
int optval = TRUE;
3334-
int n = setsockopt(port->port_handle, IPPROTO_TCP, TCP_NODELAY,
3326+
constexpr int optval = TRUE;
3327+
const int n = setsockopt(port->port_handle, IPPROTO_TCP, TCP_NODELAY,
33353328
(SCHAR*) &optval, sizeof(optval));
33363329

33373330
if (n == -1)
@@ -3355,8 +3348,8 @@ static bool setKeepAlive(SOCKET s)
33553348
* in case of unexpected error
33563349
*
33573350
**************************************/
3358-
int optval = 1;
3359-
int n = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
3351+
constexpr int optval = 1;
3352+
const int n = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
33603353
(SCHAR*) &optval, sizeof(optval));
33613354
return n != -1;
33623355
}

0 commit comments

Comments
 (0)