mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Performance improvements
* Remove option size memers on IP/TCP and instead calculate these when they're needed * Reorder members on IP/TCP so as to remove/decrease padding * Move most memory helpers impl into header file so they can be inlined * Make PDUOption's move ctor/assignment operator noexcept
This commit is contained in:
@@ -523,7 +523,6 @@ public:
|
||||
* \param opt The option to be added.
|
||||
*/
|
||||
void add_option(option &&opt) {
|
||||
internal_add_option(opt);
|
||||
options_.push_back(std::move(opt));
|
||||
}
|
||||
|
||||
@@ -538,7 +537,6 @@ public:
|
||||
template<typename... Args>
|
||||
void add_option(Args&&... args) {
|
||||
options_.emplace_back(std::forward<Args>(args)...);
|
||||
internal_add_option(options_.back());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -756,7 +754,8 @@ private:
|
||||
void tot_len(uint16_t new_tot_len);
|
||||
|
||||
void prepare_for_serialize();
|
||||
void internal_add_option(const option& option);
|
||||
uint32_t calculate_options_size() const;
|
||||
uint32_t pad_options_size(uint32_t size) const;
|
||||
void init_ip_fields();
|
||||
void write_serialization(uint8_t* buffer, uint32_t total_sz);
|
||||
void write_option(const option& opt, Memory::OutputMemoryStream& stream);
|
||||
@@ -765,11 +764,9 @@ private:
|
||||
void checksum(uint16_t new_check);
|
||||
options_type::const_iterator search_option_iterator(option_identifier id) const;
|
||||
options_type::iterator search_option_iterator(option_identifier id);
|
||||
void update_padded_options_size();
|
||||
|
||||
ip_header header_;
|
||||
uint16_t options_size_, padded_options_size_;
|
||||
options_type options_;
|
||||
ip_header header_;
|
||||
};
|
||||
|
||||
} // Tins
|
||||
|
||||
@@ -68,8 +68,12 @@ void write_value(uint8_t* buffer, const T& value) {
|
||||
|
||||
class InputMemoryStream {
|
||||
public:
|
||||
InputMemoryStream(const uint8_t* buffer, size_t total_sz);
|
||||
InputMemoryStream(const std::vector<uint8_t>& data);
|
||||
InputMemoryStream(const uint8_t* buffer, size_t total_sz)
|
||||
: buffer_(buffer), size_(total_sz) {
|
||||
}
|
||||
|
||||
InputMemoryStream(const std::vector<uint8_t>& data) : buffer_(&data[0]), size_(data.size()) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T read() {
|
||||
@@ -97,17 +101,46 @@ public:
|
||||
skip(sizeof(value));
|
||||
}
|
||||
|
||||
void skip(size_t size);
|
||||
bool can_read(size_t byte_count) const;
|
||||
void skip(size_t size) {
|
||||
if (TINS_UNLIKELY(size > size_)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
buffer_ += size;
|
||||
size_ -= size;
|
||||
}
|
||||
|
||||
bool can_read(size_t byte_count) const {
|
||||
return TINS_LIKELY(size_ >= byte_count);
|
||||
}
|
||||
|
||||
void read(void* output_buffer, size_t output_buffer_size) {
|
||||
if (!can_read(output_buffer_size)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
read_data(buffer_, (uint8_t*)output_buffer, output_buffer_size);
|
||||
skip(output_buffer_size);
|
||||
}
|
||||
|
||||
const uint8_t* pointer() const {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
void size(size_t new_size) {
|
||||
size_ = new_size;
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return size_ > 0;
|
||||
}
|
||||
|
||||
void read(std::vector<uint8_t>& value, size_t count);
|
||||
void read(HWAddress<6>& address);
|
||||
void read(IPv4Address& address);
|
||||
void read(IPv6Address& address);
|
||||
void read(void* output_buffer, size_t output_buffer_size);
|
||||
const uint8_t* pointer() const;
|
||||
size_t size() const;
|
||||
void size(size_t new_size);
|
||||
operator bool() const;
|
||||
private:
|
||||
const uint8_t* buffer_;
|
||||
size_t size_;
|
||||
@@ -115,8 +148,13 @@ private:
|
||||
|
||||
class OutputMemoryStream {
|
||||
public:
|
||||
OutputMemoryStream(uint8_t* buffer, size_t total_sz);
|
||||
OutputMemoryStream(std::vector<uint8_t>& buffer);
|
||||
OutputMemoryStream(uint8_t* buffer, size_t total_sz)
|
||||
: buffer_(buffer), size_(total_sz) {
|
||||
}
|
||||
|
||||
OutputMemoryStream(std::vector<uint8_t>& buffer)
|
||||
: buffer_(&buffer[0]), size_(buffer.size()) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void write(const T& value) {
|
||||
@@ -151,14 +189,37 @@ public:
|
||||
skip(length);
|
||||
}
|
||||
|
||||
void skip(size_t size);
|
||||
void write(const uint8_t* ptr, size_t length);
|
||||
void skip(size_t size) {
|
||||
if (TINS_UNLIKELY(size > size_)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
buffer_ += size;
|
||||
size_ -= size;
|
||||
}
|
||||
|
||||
void write(const uint8_t* ptr, size_t length) {
|
||||
write(ptr, ptr + length);
|
||||
}
|
||||
|
||||
void fill(size_t size, uint8_t value) {
|
||||
if (TINS_UNLIKELY(size_ < size)) {
|
||||
throw serialization_error();
|
||||
}
|
||||
std::memset(buffer_, value, size);
|
||||
skip(size);
|
||||
}
|
||||
|
||||
uint8_t* pointer() {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
void write(const HWAddress<6>& address);
|
||||
void write(const IPv4Address& address);
|
||||
void write(const IPv6Address& address);
|
||||
void fill(size_t size, uint8_t value);
|
||||
uint8_t* pointer();
|
||||
size_t size() const;
|
||||
private:
|
||||
uint8_t* buffer_;
|
||||
size_t size_;
|
||||
|
||||
@@ -226,7 +226,7 @@ public:
|
||||
* \brief Move constructor.
|
||||
* \param rhs The PDUOption to be moved.
|
||||
*/
|
||||
PDUOption(PDUOption&& rhs) {
|
||||
PDUOption(PDUOption&& rhs) TINS_NOEXCEPT {
|
||||
real_size_ = 0;
|
||||
*this = std::move(rhs);
|
||||
}
|
||||
@@ -235,7 +235,7 @@ public:
|
||||
* \brief Move assignment operator.
|
||||
* \param rhs The PDUOption to be moved.
|
||||
*/
|
||||
PDUOption& operator=(PDUOption&& rhs) {
|
||||
PDUOption& operator=(PDUOption&& rhs) TINS_NOEXCEPT {
|
||||
option_ = rhs.option_;
|
||||
size_ = rhs.size_;
|
||||
if (real_size_ > small_buffer_size) {
|
||||
@@ -250,7 +250,7 @@ public:
|
||||
else {
|
||||
std::memcpy(payload_.small_buffer, rhs.data_ptr(), rhs.data_size());
|
||||
}
|
||||
return* this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif // TINS_IS_CXX11
|
||||
|
||||
@@ -475,7 +475,6 @@ public:
|
||||
* \param option The option to be added.
|
||||
*/
|
||||
void add_option(option &&opt) {
|
||||
internal_add_option(opt);
|
||||
options_.push_back(std::move(opt));
|
||||
}
|
||||
|
||||
@@ -490,7 +489,6 @@ public:
|
||||
template <typename... Args>
|
||||
void add_option(Args&&... args) {
|
||||
options_.emplace_back(std::forward<Args>(args)...);
|
||||
internal_add_option(options_.back());
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -606,18 +604,17 @@ private:
|
||||
return opt->to<T>();
|
||||
}
|
||||
|
||||
void internal_add_option(const option& option);
|
||||
void write_serialization(uint8_t* buffer, uint32_t total_sz);
|
||||
void checksum(uint16_t new_check);
|
||||
void update_options_size();
|
||||
uint32_t calculate_options_size() const;
|
||||
uint32_t pad_options_size(uint32_t size) const;
|
||||
options_type::const_iterator search_option_iterator(OptionTypes type) const;
|
||||
options_type::iterator search_option_iterator(OptionTypes type);
|
||||
|
||||
void write_option(const option& opt, Memory::OutputMemoryStream& stream);
|
||||
|
||||
tcp_header header_;
|
||||
uint16_t options_size_, total_options_size_;
|
||||
options_type options_;
|
||||
tcp_header header_;
|
||||
};
|
||||
|
||||
} // Tins
|
||||
|
||||
Reference in New Issue
Block a user