Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Unified Diff: net/base/ip_address.h

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: DCHECK Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/public/common/common_param_traits.cc ('k') | net/base/ip_address.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/ip_address.h
diff --git a/net/base/ip_address.h b/net/base/ip_address.h
index e326e78da6e08a75f97fc4647c2c7252d7bd321a..e2d3d18b789fd866c5fa65844a13e7180bbed4a2 100644
--- a/net/base/ip_address.h
+++ b/net/base/ip_address.h
@@ -8,6 +8,7 @@
#include <stddef.h>
#include <stdint.h>
+#include <array>
#include <string>
#include <vector>
@@ -21,6 +22,83 @@ class NET_EXPORT IPAddress {
public:
enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 };
+ // Helper class to represent the sequence of bytes in an IP address.
+ // A vector<uint8_t> would be simpler but incurs heap allocation, so
+ // IPAddressBytes uses a fixed size array
+ class IPAddressBytes {
+ public:
+ IPAddressBytes();
+ IPAddressBytes(const uint8_t* data, size_t data_len);
+ IPAddressBytes(const IPAddressBytes& other);
+ ~IPAddressBytes();
+
+ // Returns the number of elements in the underlying array.
+ size_t size() const { return size_; }
+
+ // Sets the size to be |size|. Does not actually change the size
+ // of the underlying array. Zeros out all elements in [0, |size|).
eroman 2017/05/17 20:48:16 This is not the same way that std::vector<>::resiz
Ryan Hamilton 2017/05/17 23:32:37 Ah, ok. Renamed to Resize().
+ void resize(size_t size) {
+ DCHECK_LE(size, 16u);
+ if (size != 0)
+ memset(bytes_.data(), 0, size);
+ size_ = static_cast<uint8_t>(size);
+ }
+
+ // Returns true if the underlying array is empty.
+ bool empty() const { return size_ == 0; }
+
+ // Returns a pointer to the underlying array of bytes.
+ const uint8_t* data() const { return bytes_.data(); }
+ uint8_t* data() { return bytes_.data(); }
+
+ // Returns a pointer to the first element.
+ const uint8_t* begin() const { return data(); }
+
+ // Returns a pointer past the last element.
+ const uint8_t* end() const { return data() + size_; }
+
+ // Returns a reference to the last element.
+ uint8_t& back() {
+ DCHECK(!empty());
+ return bytes_[size_ - 1];
+ }
+ const uint8_t& back() const {
+ DCHECK(!empty());
+ return bytes_[size_ - 1];
+ }
+
+ // Appends |val| to the end and increments the size.
+ void push_back(uint8_t val) {
+ DCHECK_GT(16, size_);
+ bytes_[size_++] = val;
+ }
+
+ // Returns a reference to the byte at index |pos|.
+ uint8_t& operator[](size_t pos) {
+ DCHECK_LT(pos, size_);
+ return bytes_[pos];
+ }
+ const uint8_t& operator[](size_t pos) const {
+ DCHECK_LT(pos, size_);
+ return bytes_[pos];
+ }
+
+ private:
+ friend bool operator>(const IPAddressBytes& lhs, const IPAddressBytes& rhs);
+ friend bool operator<(const IPAddressBytes& lhs, const IPAddressBytes& rhs);
+ friend bool operator!=(const IPAddressBytes& lhs,
+ const IPAddressBytes& rhs);
+ friend bool operator==(const IPAddressBytes& lhs,
+ const IPAddressBytes& rhs);
+
+ // Underlying sequence of bytes
+ std::array<uint8_t, kIPv6AddressSize> bytes_;
+
+ // Number of elements in |bytes_|. Should be either kIPv4AddressSize
+ // or kIPv6AddressSize or 0.
+ uint8_t size_;
+ };
+
// Creates a zero-sized, invalid address.
IPAddress();
@@ -102,8 +180,11 @@ class NET_EXPORT IPAddress {
bool AssignFromIPLiteral(const base::StringPiece& ip_literal)
WARN_UNUSED_RESULT;
- // Returns the underlying byte vector.
- const std::vector<uint8_t>& bytes() const { return ip_address_; };
+ // Returns the underlying bytes.
+ const IPAddressBytes& bytes() const { return ip_address_; };
+
+ // Returns the underlying bytes as a vector.
+ std::vector<uint8_t> BytesAsVector() const;
// Returns an IPAddress instance representing the 127.0.0.1 address.
static IPAddress IPv4Localhost();
@@ -127,11 +208,20 @@ class NET_EXPORT IPAddress {
private:
// IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address
// will have length kIPv6AddressSize.
- std::vector<uint8_t> ip_address_;
+ IPAddressBytes ip_address_;
// This class is copyable and assignable.
};
+bool operator>(const IPAddress::IPAddressBytes& lhs,
+ const IPAddress::IPAddressBytes& rhs);
+bool operator<(const IPAddress::IPAddressBytes& lhs,
+ const IPAddress::IPAddressBytes& rhs);
+bool operator!=(const IPAddress::IPAddressBytes& lhs,
+ const IPAddress::IPAddressBytes& rhs);
+bool operator==(const IPAddress::IPAddressBytes& lhs,
+ const IPAddress::IPAddressBytes& rhs);
+
using IPAddressList = std::vector<IPAddress>;
// Returns the canonical string representation of an IP address along with its
« no previous file with comments | « content/public/common/common_param_traits.cc ('k') | net/base/ip_address.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698