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

Side by Side Diff: net/base/ip_address.h

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: Resize 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_IP_ADDRESS_H_ 5 #ifndef NET_BASE_IP_ADDRESS_H_
6 #define NET_BASE_IP_ADDRESS_H_ 6 #define NET_BASE_IP_ADDRESS_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <array>
11 #include <string> 12 #include <string>
12 #include <vector> 13 #include <vector>
13 14
14 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
15 #include "base/strings/string_piece.h" 16 #include "base/strings/string_piece.h"
16 #include "net/base/net_export.h" 17 #include "net/base/net_export.h"
17 18
18 namespace net { 19 namespace net {
19 20
20 class NET_EXPORT IPAddress { 21 class NET_EXPORT IPAddress {
21 public: 22 public:
22 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 }; 23 enum : size_t { kIPv4AddressSize = 4, kIPv6AddressSize = 16 };
23 24
25 // Helper class to represent the sequence of bytes in an IP address.
26 // A vector<uint8_t> would be simpler but incurs heap allocation, so
27 // IPAddressBytes uses a fixed size array
eroman 2017/05/18 18:43:17 nits: end with period.
Ryan Hamilton 2017/05/19 03:38:50 Done.
28 class IPAddressBytes {
29 public:
30 IPAddressBytes();
31 IPAddressBytes(const uint8_t* data, size_t data_len);
32 IPAddressBytes(const IPAddressBytes& other);
33 ~IPAddressBytes();
34
35 // Returns the number of elements in the underlying array.
36 size_t size() const { return size_; }
37
38 // Sets the size to be |size|. Does not actually change the size
39 // of the underlying array.
eroman 2017/05/18 18:43:17 or zero-initialize the bytes.
Ryan Hamilton 2017/05/19 03:38:50 Done.
40 void Resize(size_t size) {
41 DCHECK_LE(size, 16u);
42 size_ = static_cast<uint8_t>(size);
43 }
44
45 // Returns true if the underlying array is empty.
46 bool empty() const { return size_ == 0; }
47
48 // Returns a pointer to the underlying array of bytes.
49 const uint8_t* data() const { return bytes_.data(); }
50 uint8_t* data() { return bytes_.data(); }
51
52 // Returns a pointer to the first element.
53 const uint8_t* begin() const { return data(); }
54
55 // Returns a pointer past the last element.
56 const uint8_t* end() const { return data() + size_; }
57
58 // Returns a reference to the last element.
59 uint8_t& back() {
60 DCHECK(!empty());
61 return bytes_[size_ - 1];
62 }
63 const uint8_t& back() const {
64 DCHECK(!empty());
65 return bytes_[size_ - 1];
66 }
67
68 // Appends |val| to the end and increments the size.
69 void push_back(uint8_t val) {
70 DCHECK_GT(16, size_);
71 bytes_[size_++] = val;
72 }
73
74 // Returns a reference to the byte at index |pos|.
75 uint8_t& operator[](size_t pos) {
76 DCHECK_LT(pos, size_);
77 return bytes_[pos];
78 }
79 const uint8_t& operator[](size_t pos) const {
80 DCHECK_LT(pos, size_);
81 return bytes_[pos];
82 }
83
84 private:
85 friend bool operator>(const IPAddressBytes& lhs, const IPAddressBytes& rhs);
eroman 2017/05/18 18:43:17 optional: Instead of friending, what about definin
Ryan Hamilton 2017/05/19 03:38:50 Why did I not do that at first?! Done.
86 friend bool operator<(const IPAddressBytes& lhs, const IPAddressBytes& rhs);
87 friend bool operator!=(const IPAddressBytes& lhs,
88 const IPAddressBytes& rhs);
89 friend bool operator==(const IPAddressBytes& lhs,
90 const IPAddressBytes& rhs);
91
92 // Underlying sequence of bytes
93 std::array<uint8_t, kIPv6AddressSize> bytes_;
94
95 // Number of elements in |bytes_|. Should be either kIPv4AddressSize
96 // or kIPv6AddressSize or 0.
97 uint8_t size_;
98 };
99
24 // Creates a zero-sized, invalid address. 100 // Creates a zero-sized, invalid address.
25 IPAddress(); 101 IPAddress();
26 102
27 // Copies the input address to |ip_address_|. The input is expected to be in 103 // Copies the input address to |ip_address_|. The input is expected to be in
28 // network byte order. 104 // network byte order.
29 explicit IPAddress(const std::vector<uint8_t>& address); 105 explicit IPAddress(const std::vector<uint8_t>& address);
30 106
31 IPAddress(const IPAddress& other); 107 IPAddress(const IPAddress& other);
32 108
33 // Copies the input address to |ip_address_|. The input is expected to be in 109 // Copies the input address to |ip_address_|. The input is expected to be in
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 // Returns the canonical string representation of an IP address. 171 // Returns the canonical string representation of an IP address.
96 // For example: "192.168.0.1" or "::1". Returns the empty string when 172 // For example: "192.168.0.1" or "::1". Returns the empty string when
97 // |ip_address_| is invalid. 173 // |ip_address_| is invalid.
98 std::string ToString() const; 174 std::string ToString() const;
99 175
100 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. 176 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
101 // Returns true on success and fills |ip_address_| with the numeric value. 177 // Returns true on success and fills |ip_address_| with the numeric value.
102 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) 178 bool AssignFromIPLiteral(const base::StringPiece& ip_literal)
103 WARN_UNUSED_RESULT; 179 WARN_UNUSED_RESULT;
104 180
105 // Returns the underlying byte vector. 181 // Returns the underlying bytes.
106 const std::vector<uint8_t>& bytes() const { return ip_address_; }; 182 const IPAddressBytes& bytes() const { return ip_address_; };
183
184 // Returns the underlying bytes as a vector.
eroman 2017/05/18 18:43:17 optional: Add a comment discouraging use of this f
Ryan Hamilton 2017/05/19 03:38:50 Done.
185 std::vector<uint8_t> BytesAsVector() const;
107 186
108 // Returns an IPAddress instance representing the 127.0.0.1 address. 187 // Returns an IPAddress instance representing the 127.0.0.1 address.
109 static IPAddress IPv4Localhost(); 188 static IPAddress IPv4Localhost();
110 189
111 // Returns an IPAddress instance representing the ::1 address. 190 // Returns an IPAddress instance representing the ::1 address.
112 static IPAddress IPv6Localhost(); 191 static IPAddress IPv6Localhost();
113 192
114 // Returns an IPAddress made up of |num_zero_bytes| zeros. 193 // Returns an IPAddress made up of |num_zero_bytes| zeros.
115 static IPAddress AllZeros(size_t num_zero_bytes); 194 static IPAddress AllZeros(size_t num_zero_bytes);
116 195
117 // Returns an IPAddress instance representing the 0.0.0.0 address. 196 // Returns an IPAddress instance representing the 0.0.0.0 address.
118 static IPAddress IPv4AllZeros(); 197 static IPAddress IPv4AllZeros();
119 198
120 // Returns an IPAddress instance representing the :: address. 199 // Returns an IPAddress instance representing the :: address.
121 static IPAddress IPv6AllZeros(); 200 static IPAddress IPv6AllZeros();
122 201
123 bool operator==(const IPAddress& that) const; 202 bool operator==(const IPAddress& that) const;
124 bool operator!=(const IPAddress& that) const; 203 bool operator!=(const IPAddress& that) const;
125 bool operator<(const IPAddress& that) const; 204 bool operator<(const IPAddress& that) const;
126 205
127 private: 206 private:
128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address 207 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address
eroman 2017/05/18 18:43:17 optional: I think this comment can be removed, sin
Ryan Hamilton 2017/05/19 03:38:50 Good point. Done.
129 // will have length kIPv6AddressSize. 208 // will have length kIPv6AddressSize.
130 std::vector<uint8_t> ip_address_; 209 IPAddressBytes ip_address_;
131 210
132 // This class is copyable and assignable. 211 // This class is copyable and assignable.
133 }; 212 };
134 213
214 bool operator>(const IPAddress::IPAddressBytes& lhs,
215 const IPAddress::IPAddressBytes& rhs);
216 bool operator<(const IPAddress::IPAddressBytes& lhs,
217 const IPAddress::IPAddressBytes& rhs);
218 bool operator!=(const IPAddress::IPAddressBytes& lhs,
219 const IPAddress::IPAddressBytes& rhs);
220 bool operator==(const IPAddress::IPAddressBytes& lhs,
221 const IPAddress::IPAddressBytes& rhs);
222
135 using IPAddressList = std::vector<IPAddress>; 223 using IPAddressList = std::vector<IPAddress>;
136 224
137 // Returns the canonical string representation of an IP address along with its 225 // Returns the canonical string representation of an IP address along with its
138 // port. For example: "192.168.0.1:99" or "[::1]:80". 226 // port. For example: "192.168.0.1:99" or "[::1]:80".
139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, 227 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address,
140 uint16_t port); 228 uint16_t port);
141 229
142 // Returns the address as a sequence of bytes in network-byte-order. 230 // Returns the address as a sequence of bytes in network-byte-order.
143 NET_EXPORT std::string IPAddressToPackedString(const IPAddress& address); 231 NET_EXPORT std::string IPAddressToPackedString(const IPAddress& address);
144 232
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 template <size_t N> 287 template <size_t N>
200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { 288 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) {
201 if (address.size() < N) 289 if (address.size() < N)
202 return false; 290 return false;
203 return std::equal(prefix, prefix + N, address.bytes().begin()); 291 return std::equal(prefix, prefix + N, address.bytes().begin());
204 } 292 }
205 293
206 } // namespace net 294 } // namespace net
207 295
208 #endif // NET_BASE_IP_ADDRESS_H_ 296 #endif // NET_BASE_IP_ADDRESS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698