1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Don't include heavy STL headers like <algorithm> 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
This commit is contained in:
Matias Fontanini
2017-04-30 09:28:00 -07:00
parent 22c72955f5
commit 3e7d30e01c
14 changed files with 31 additions and 37 deletions

View File

@@ -31,7 +31,6 @@
#define TINS_BOOTP_H
#include <stdint.h>
#include <algorithm>
#include <vector>
#include "pdu.h"
#include "macros.h"
@@ -268,15 +267,14 @@ public:
*/
template<size_t n>
void chaddr(const HWAddress<n>& 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;
}
}
}

View File

@@ -33,8 +33,6 @@
#if TINS_IS_CXX11
#include <type_traits>
#endif
#include <sstream>
#include <string>
#include <stdint.h>
#include "constants.h"

View File

@@ -31,7 +31,7 @@
#define TINS_IPADDRESS_H
#include <string>
#include <iostream>
#include <iosfwd>
#include <stdint.h>
#include "cxxstd.h"
#include "macros.h"

View File

@@ -32,6 +32,7 @@
#include <string>
#include <stdexcept>
#include <iosfwd>
#include <stdint.h>
#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

View File

@@ -32,7 +32,6 @@
#include <stdint.h>
#include <cstring>
#include <algorithm>
#include <vector>
#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);
}

View File

@@ -30,7 +30,7 @@
#ifndef TINS_PDU_CACHER_H
#define TINS_PDU_CACHER_H
#include <algorithm>
#include <cstring>
#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_;

View File

@@ -33,9 +33,7 @@
#include <vector>
#include <iterator>
#include <cstring>
#include <algorithm>
#include <string>
#include <limits>
#include <stdint.h>
#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<typename ForwardIterator>
void set_payload_contents(ForwardIterator start, ForwardIterator end) {
size_t total_size = std::distance(start, end);
if (total_size > std::numeric_limits<uint16_t>::max()) {
if (total_size > 65535) {
throw option_payload_too_large();
}
real_size_ = static_cast<uint16_t>(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_];

View File

@@ -48,7 +48,7 @@ class NetworkInterface;
class PacketSender;
class PDU;
class IPv6Address;
template <size_t n, typename Storage>
template <size_t n>
class HWAddress;
/**