From f88cf9b025fdfc2c3c9160ef16213fa2eb1fe3ad Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 30 Apr 2017 09:28:00 -0700 Subject: [PATCH] Don't include heavy STL headers like in header files This provides a considerable compilation time reduction and most of these were just using std::copy/fill which can be replaced by memcpy/memset, as all of their uses were applied to POD types --- examples/traceroute.cpp | 1 + include/tins/bootp.h | 18 ++++++++---------- include/tins/internals.h | 2 -- include/tins/ip_address.h | 2 +- include/tins/ipv6_address.h | 6 ++---- include/tins/memory_helpers.h | 5 ++--- include/tins/pdu_cacher.h | 4 ++-- include/tins/pdu_option.h | 16 +++------------- include/tins/utils.h | 2 +- src/dhcp.cpp | 1 + src/handshake_capturer.cpp | 1 + src/icmpv6.cpp | 1 + src/ip_address.cpp | 1 + src/ipv6_address.cpp | 8 +++++++- 14 files changed, 31 insertions(+), 37 deletions(-) diff --git a/examples/traceroute.cpp b/examples/traceroute.cpp index 09a7991..9a4da46 100644 --- a/examples/traceroute.cpp +++ b/examples/traceroute.cpp @@ -35,6 +35,7 @@ #define _GLIBCXX_USE_NANOSLEEP #include +#include #include #include #include diff --git a/include/tins/bootp.h b/include/tins/bootp.h index 7d88936..eaf3f1c 100644 --- a/include/tins/bootp.h +++ b/include/tins/bootp.h @@ -31,7 +31,6 @@ #define TINS_BOOTP_H #include -#include #include #include "pdu.h" #include "macros.h" @@ -268,15 +267,14 @@ public: */ template void chaddr(const HWAddress& new_chaddr) { - // Copy the new addr - uint8_t* end = std::copy( - new_chaddr.begin(), - new_chaddr.begin() + std::min(n, sizeof(bootp_.chaddr)), - bootp_.chaddr - ); - // Fill what's left with zeros - if (end < bootp_.chaddr + chaddr_type::address_size) { - std::fill(end, bootp_.chaddr + chaddr_type::address_size, 0); + size_t copy_threshold = std::min(n, sizeof(bootp_.chaddr)); + for (size_t i = 0; i < copy_threshold; ++i) { + if (i < copy_threshold) { + bootp_.chaddr[i] = new_chaddr[i]; + } + else { + bootp_.chaddr[i] = 0; + } } } diff --git a/include/tins/internals.h b/include/tins/internals.h index 3e78030..17f4cc4 100644 --- a/include/tins/internals.h +++ b/include/tins/internals.h @@ -33,8 +33,6 @@ #if TINS_IS_CXX11 #include #endif - -#include #include #include #include "constants.h" diff --git a/include/tins/ip_address.h b/include/tins/ip_address.h index 01055f4..bfbcb6c 100644 --- a/include/tins/ip_address.h +++ b/include/tins/ip_address.h @@ -31,7 +31,7 @@ #define TINS_IPADDRESS_H #include -#include +#include #include #include "cxxstd.h" #include "macros.h" diff --git a/include/tins/ipv6_address.h b/include/tins/ipv6_address.h index e04e240..87ba039 100644 --- a/include/tins/ipv6_address.h +++ b/include/tins/ipv6_address.h @@ -32,6 +32,7 @@ #include #include +#include #include #include "cxxstd.h" #include "macros.h" @@ -203,10 +204,7 @@ public: * \param addr The parameter to be written. * \return std::ostream& pointing to the os parameter. */ - friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr) { - return os << addr.to_string(); - } - + friend std::ostream& operator<<(std::ostream& os, const IPv6Address& addr); /** * Applies a mask to an address diff --git a/include/tins/memory_helpers.h b/include/tins/memory_helpers.h index 0e7f0e8..5828365 100644 --- a/include/tins/memory_helpers.h +++ b/include/tins/memory_helpers.h @@ -32,7 +32,6 @@ #include #include -#include #include #include "exceptions.h" #include "ip_address.h" @@ -213,7 +212,7 @@ public: if (TINS_UNLIKELY(size_ < length)) { throw serialization_error(); } - std::copy(start, end, buffer_); + std::memcpy(buffer_, &*start, length); skip(length); } @@ -238,7 +237,7 @@ public: if (TINS_UNLIKELY(size_ < size)) { throw serialization_error(); } - std::fill(buffer_, buffer_ + size, value); + std::memset(buffer_, value, size); skip(size); } diff --git a/include/tins/pdu_cacher.h b/include/tins/pdu_cacher.h index 0e8ccdc..ab37d45 100644 --- a/include/tins/pdu_cacher.h +++ b/include/tins/pdu_cacher.h @@ -30,7 +30,7 @@ #ifndef TINS_PDU_CACHER_H #define TINS_PDU_CACHER_H -#include +#include #include "pdu.h" #include "macros.h" @@ -148,7 +148,7 @@ private: if (cached_serialization_.size() != total_sz) { cached_serialization_ = cached_.serialize(); } - std::copy(cached_serialization_.begin(), cached_serialization_.end(), buffer); + std::memcpy(buffer, &*cached_serialization_.begin(), cached_serialization_.size()); } cached_type cached_; diff --git a/include/tins/pdu_option.h b/include/tins/pdu_option.h index 97d704f..dc1ecdd 100644 --- a/include/tins/pdu_option.h +++ b/include/tins/pdu_option.h @@ -33,9 +33,7 @@ #include #include #include -#include #include -#include #include #include "exceptions.h" #include "endianness.h" @@ -375,11 +373,7 @@ public: rhs.real_size_ = 0; } else { - std::copy( - rhs.data_ptr(), - rhs.data_ptr() + rhs.data_size(), - payload_.small_buffer - ); + std::memcpy(payload_.small_buffer, rhs.data_ptr(), rhs.data_size()); } return* this; } @@ -516,16 +510,12 @@ private: template void set_payload_contents(ForwardIterator start, ForwardIterator end) { size_t total_size = std::distance(start, end); - if (total_size > std::numeric_limits::max()) { + if (total_size > 65535) { throw option_payload_too_large(); } real_size_ = static_cast(total_size); if (real_size_ <= small_buffer_size) { - std::copy( - start, - end, - payload_.small_buffer - ); + std::memcpy(payload_.small_buffer, &*start, total_size); } else { payload_.big_buffer_ptr = new data_type[real_size_]; diff --git a/include/tins/utils.h b/include/tins/utils.h index 7beff5d..1ac877d 100644 --- a/include/tins/utils.h +++ b/include/tins/utils.h @@ -48,7 +48,7 @@ class NetworkInterface; class PacketSender; class PDU; class IPv6Address; -template +template class HWAddress; /** diff --git a/src/dhcp.cpp b/src/dhcp.cpp index fcd4eec..46dd78f 100644 --- a/src/dhcp.cpp +++ b/src/dhcp.cpp @@ -29,6 +29,7 @@ #include #include +#include #include "endianness.h" #include "dhcp.h" #include "ethernetII.h" diff --git a/src/handshake_capturer.cpp b/src/handshake_capturer.cpp index 80b0029..86aa726 100644 --- a/src/handshake_capturer.cpp +++ b/src/handshake_capturer.cpp @@ -31,6 +31,7 @@ #ifdef TINS_HAVE_DOT11 +#include #include "dot11/dot11_data.h" using std::max_element; diff --git a/src/icmpv6.cpp b/src/icmpv6.cpp index 78cb7f5..52475eb 100644 --- a/src/icmpv6.cpp +++ b/src/icmpv6.cpp @@ -28,6 +28,7 @@ */ #include +#include #include "icmpv6.h" #include "ipv6.h" #include "rawpdu.h" diff --git a/src/ip_address.cpp b/src/ip_address.cpp index f1a7d44..659f736 100644 --- a/src/ip_address.cpp +++ b/src/ip_address.cpp @@ -35,6 +35,7 @@ #endif // _WIN32 #include #include +#include #include "ip_address.h" #include "endianness.h" #include "address_range.h" diff --git a/src/ipv6_address.cpp b/src/ipv6_address.cpp index 3833df3..3d32128 100644 --- a/src/ipv6_address.cpp +++ b/src/ipv6_address.cpp @@ -27,7 +27,6 @@ * */ -#include #include "macros.h" #ifndef _WIN32 #include @@ -38,14 +37,17 @@ #include #include #endif +#include #include #include +#include #include "ipv6_address.h" #include "address_range.h" #include "exceptions.h" using std::fill; using std::string; +using std::ostream; namespace Tins { @@ -132,6 +134,10 @@ bool IPv6Address::is_multicast() const { return multicast_range.contains(*this); } +ostream& operator<<(ostream& os, const IPv6Address& addr) { + return os << addr.to_string(); +} + IPv6Address operator&(const IPv6Address& lhs, const IPv6Address& rhs) { IPv6Address output = lhs; IPv6Address::iterator addr_iter = output.begin();