From: Amos Jeffries Date: Sat, 4 Feb 2017 11:16:36 +0000 (+1300) Subject: Cleanup: update class fde and _fde_disk to C++11 X-Git-Tag: M-staged-PR71~286 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7d40d52ae86020fec3e5bbecad448b9ade51f65;p=thirdparty%2Fsquid.git Cleanup: update class fde and _fde_disk to C++11 This update moves the initialization of most class members into default initizliation values. So clear() can be optimized to a fast emplace initialization by the compiler and removing the need for memset(). NP: implicit initialization of function pointers and char* arrays is not supported by C++11. So use of default constructor is still required for now. Also, - replace internal uses of fd_table macro with fde::Table. - replace use of NULL with nullptr - update some method declaratiosn to current coding style - improve inline ability for noteUse() method - const correctness for several inline methods - documentation updates --- diff --git a/src/fde.cc b/src/fde.cc index 8c3df5a3d8..b48bb63869 100644 --- a/src/fde.cc +++ b/src/fde.cc @@ -16,25 +16,24 @@ #include "SquidTime.h" #include "Store.h" -fde *fde::Table = NULL; +fde *fde::Table = nullptr; bool -fde::readPending(int fdNumber) +fde::readPending(int fdNumber) const { if (type == FD_SOCKET) return Comm::MonitorsRead(fdNumber); - return read_handler ? true : false ; + return read_handler != nullptr; } void -fde::dumpStats (StoreEntry &dumpEntry, int fdNumber) +fde::dumpStats(StoreEntry &dumpEntry, int fdNumber) const { if (!flags.open) return; #if _SQUID_WINDOWS_ - storeAppendPrintf(&dumpEntry, "%4d 0x%-8lX %-6.6s %4d %7" PRId64 "%c %7" PRId64 "%c %-21s %s\n", fdNumber, win32.handle, @@ -43,7 +42,7 @@ fde::dumpStats (StoreEntry &dumpEntry, int fdNumber) fdNumber, #endif fdTypeStr[type], - timeoutHandler != NULL ? (int) (timeout - squid_curtime) : 0, + timeoutHandler ? (int) (timeout - squid_curtime) : 0, bytes_read, readPending(fdNumber) ? '*' : ' ', bytes_written, @@ -53,12 +52,10 @@ fde::dumpStats (StoreEntry &dumpEntry, int fdNumber) } void -fde::DumpStats (StoreEntry *dumpEntry) +fde::DumpStats(StoreEntry *dumpEntry) { - int i; storeAppendPrintf(dumpEntry, "Active file descriptors:\n"); #if _SQUID_WINDOWS_ - storeAppendPrintf(dumpEntry, "%-4s %-10s %-6s %-4s %-7s* %-7s* %-21s %s\n", "File", "Handle", @@ -78,30 +75,24 @@ fde::DumpStats (StoreEntry *dumpEntry) storeAppendPrintf(dumpEntry, "---- ------ ---- -------- -------- --------------------- ------------------------------\n"); #endif - for (i = 0; i < Squid_MaxFD; ++i) { - fd_table[i].dumpStats(*dumpEntry, i); + for (int i = 0; i < Squid_MaxFD; ++i) { + fde::Table[i].dumpStats(*dumpEntry, i); } } char const * fde::remoteAddr() const { - LOCAL_ARRAY(char, buf, MAX_IPSTRLEN ); - - if (type != FD_SOCKET) - return null_string; - - if ( *ipaddr ) - snprintf( buf, MAX_IPSTRLEN, "%s:%d", ipaddr, (int)remote_port); - else - local_addr.toUrl(buf,MAX_IPSTRLEN); // toHostStr does not include port. + static char buf[MAX_IPSTRLEN]; + *buf = 0; + + if (type == FD_SOCKET) { + if (*ipaddr) + snprintf(buf, MAX_IPSTRLEN, "%s:%u", ipaddr, remote_port); + else + local_addr.toUrl(buf,MAX_IPSTRLEN); // toHostStr does not include port. + } return buf; } -void -fde::noteUse() -{ - ++ pconn.uses; -} - diff --git a/src/fde.h b/src/fde.h index 9983961b50..89e978f362 100644 --- a/src/fde.h +++ b/src/fde.h @@ -19,6 +19,7 @@ #if USE_DELAY_POOLS class ClientInfo; #endif +class dwrite_q; /** * READ_HANDLER functions return < 0 if, and only if, they fail with an error. @@ -32,89 +33,102 @@ typedef int READ_HANDLER(int, char *, int); */ typedef int WRITE_HANDLER(int, const char *, int); -class dwrite_q; class _fde_disk { public: + _fde_disk() { wrt_handle = nullptr; } + DWCB *wrt_handle; - void *wrt_handle_data; - dwrite_q *write_q; - dwrite_q *write_q_tail; - off_t offset; - _fde_disk() { memset(this, 0, sizeof(_fde_disk)); } + void *wrt_handle_data = nullptr; + dwrite_q *write_q = nullptr; + dwrite_q *write_q_tail = nullptr; + off_t offset = 0; }; class fde { public: - fde() { clear(); }; + fde() { + *ipaddr = 0; + *desc = 0; + read_handler = nullptr; + write_handler = nullptr; + read_method = nullptr; + write_method = nullptr; + } + + /// Clear the fde class back to NULL equivalent. + void clear() { *this = fde(); } /// True if comm_close for this fd has been called - bool closing() { return flags.close_request; } + bool closing() const { return flags.close_request; } /* NOTE: memset is used on fdes today. 20030715 RBC */ - static void DumpStats (StoreEntry *); + static void DumpStats(StoreEntry *); char const *remoteAddr() const; - void dumpStats (StoreEntry &, int); - bool readPending(int); - void noteUse(); + void dumpStats(StoreEntry &, int) const; + bool readPending(int) const; + + /// record a transaction on this FD + void noteUse() { ++pconn.uses; } public: /// global table of FD and their state. static fde* Table; - unsigned int type; - unsigned short remote_port; + unsigned int type = 0; + unsigned short remote_port = 0; Ip::Address local_addr; - tos_t tosToServer; /**< The TOS value for packets going towards the server. + tos_t tosToServer = '\0'; /**< The TOS value for packets going towards the server. See also tosFromServer. */ - nfmark_t nfmarkToServer; /**< The netfilter mark for packets going towards the server. + nfmark_t nfmarkToServer = 0; /**< The netfilter mark for packets going towards the server. See also nfmarkFromServer. */ - int sock_family; + int sock_family = 0; char ipaddr[MAX_IPSTRLEN]; /* dotted decimal address of peer */ char desc[FD_DESC_SZ]; struct _fde_flags { - bool open; - bool close_request; ///< true if file_ or comm_close has been called - bool write_daemon; - bool socket_eof; - bool nolinger; - bool nonblocking; - bool ipc; - bool called_connect; - bool nodelay; - bool close_on_exec; - bool read_pending; + bool open = false; + bool close_request = false; ///< true if file_ or comm_close has been called + bool write_daemon = false; + bool socket_eof = false; + bool nolinger = false; + bool nonblocking = false; + bool ipc = false; + bool called_connect = false; + bool nodelay = false; + bool close_on_exec = false; + bool read_pending = false; //bool write_pending; //XXX seems not to be used - bool transparent; + bool transparent = false; } flags; - int64_t bytes_read; - int64_t bytes_written; + int64_t bytes_read = 0; + int64_t bytes_written = 0; struct { - int uses; /* ie # req's over persistent conn */ + int uses = 0; /* ie # req's over persistent conn */ } pconn; #if USE_DELAY_POOLS - ClientInfo * clientInfo;/* pointer to client info used in client write limiter or NULL if not present */ + /// pointer to client info used in client write limiter or nullptr if not present + ClientInfo * clientInfo = nullptr; #endif - unsigned epoll_state; + unsigned epoll_state = 0; _fde_disk disk; PF *read_handler; - void *read_data; + void *read_data = nullptr; PF *write_handler; - void *write_data; + void *write_data = nullptr; AsyncCall::Pointer timeoutHandler; - time_t timeout; - time_t writeStart; - void *lifetime_data; + time_t timeout = 0; + time_t writeStart = 0; + void *lifetime_data = nullptr; AsyncCall::Pointer closeHandler; AsyncCall::Pointer halfClosedReader; /// read handler for half-closed fds READ_HANDLER *read_method; @@ -123,66 +137,27 @@ public: Security::ContextPointer dynamicTlsContext; ///< cached and then freed when fd is closed #if _SQUID_WINDOWS_ struct { - long handle; + long handle = (long)nullptr; } win32; #endif - tos_t tosFromServer; /**< Stores the TOS flags of the packets from the remote server. + tos_t tosFromServer = '\0'; /**< Stores the TOS flags of the packets from the remote server. See FwdState::dispatch(). Note that this differs to tosToServer in that this is the value we *receive* from the, connection, whereas tosToServer is the value to set on packets *leaving* Squid. */ - unsigned int nfmarkFromServer; /**< Stores the Netfilter mark value of the connection from the remote + unsigned int nfmarkFromServer = 0; /**< Stores the Netfilter mark value of the connection from the remote server. See FwdState::dispatch(). Note that this differs to nfmarkToServer in that this is the value we *receive* from the, connection, whereas nfmarkToServer is the value to set on packets *leaving* Squid. */ - - /** Clear the fde class back to NULL equivalent. */ - inline void clear() { - type = 0; - remote_port = 0; - local_addr.setEmpty(); - tosToServer = '\0'; - nfmarkToServer = 0; - sock_family = 0; - memset(ipaddr, '\0', MAX_IPSTRLEN); - memset(desc,'\0',FD_DESC_SZ); - memset(&flags,0,sizeof(_fde_flags)); - bytes_read = 0; - bytes_written = 0; - pconn.uses = 0; -#if USE_DELAY_POOLS - clientInfo = NULL; -#endif - epoll_state = 0; - read_handler = NULL; - read_data = NULL; - write_handler = NULL; - write_data = NULL; - timeoutHandler = NULL; - timeout = 0; - writeStart = 0; - lifetime_data = NULL; - closeHandler = NULL; - halfClosedReader = NULL; - read_method = NULL; - write_method = NULL; - ssl.reset(); - dynamicTlsContext.reset(); -#if _SQUID_WINDOWS_ - win32.handle = (long)NULL; -#endif - tosFromServer = '\0'; - nfmarkFromServer = 0; - } }; #define fd_table fde::Table int fdNFree(void); -#define FD_READ_METHOD(fd, buf, len) (*fd_table[fd].read_method)(fd, buf, len) -#define FD_WRITE_METHOD(fd, buf, len) (*fd_table[fd].write_method)(fd, buf, len) +#define FD_READ_METHOD(fd, buf, len) (*fde::Table[fd].read_method)(fd, buf, len) +#define FD_WRITE_METHOD(fd, buf, len) (*fde::Table[fd].write_method)(fd, buf, len) #endif /* SQUID_FDE_H */