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

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

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: Address comments 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
« no previous file with comments | « content/public/common/common_param_traits.cc ('k') | net/base/ip_address.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
eroman 2017/05/22 18:06:44 optional: I would suggest renaming this net::IPAdd
Ryan Hamilton 2017/05/22 22:12:32 Done.
26 // A vector<uint8_t> would be simpler but incurs heap allocation, so
27 // IPAddressBytes uses a fixed size array.
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 or zero-initialize the bytes.
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 uint8_t* begin() { return data(); }
55
56 // Returns a pointer past the last element.
57 const uint8_t* end() const { return data() + size_; }
58 uint8_t* end() { return data() + size_; }
59
60 // Returns a reference to the last element.
61 uint8_t& back() {
62 DCHECK(!empty());
63 return bytes_[size_ - 1];
64 }
65 const uint8_t& back() const {
66 DCHECK(!empty());
67 return bytes_[size_ - 1];
68 }
69
70 // Appends |val| to the end and increments the size.
71 void push_back(uint8_t val) {
72 DCHECK_GT(16, size_);
73 bytes_[size_++] = val;
74 }
75
76 // Returns a reference to the byte at index |pos|.
77 uint8_t& operator[](size_t pos) {
78 DCHECK_LT(pos, size_);
79 return bytes_[pos];
80 }
81 const uint8_t& operator[](size_t pos) const {
82 DCHECK_LT(pos, size_);
83 return bytes_[pos];
84 }
85
86 bool operator<(const IPAddressBytes& other) const;
87 bool operator!=(const IPAddressBytes& other) const;
88 bool operator==(const IPAddressBytes& other) const;
89
90 private:
91 // Underlying sequence of bytes
92 std::array<uint8_t, kIPv6AddressSize> bytes_;
93
94 // Number of elements in |bytes_|. Should be either kIPv4AddressSize
95 // or kIPv6AddressSize or 0.
96 uint8_t size_;
97 };
98
24 // Creates a zero-sized, invalid address. 99 // Creates a zero-sized, invalid address.
25 IPAddress(); 100 IPAddress();
26 101
27 // Copies the input address to |ip_address_|. The input is expected to be in
28 // network byte order.
29 explicit IPAddress(const std::vector<uint8_t>& address);
30
31 IPAddress(const IPAddress& other); 102 IPAddress(const IPAddress& other);
32 103
33 // Copies the input address to |ip_address_|. The input is expected to be in 104 // Copies the input address to |ip_address_|. The input is expected to be in
34 // network byte order. 105 // network byte order.
35 template <size_t N> 106 template <size_t N>
36 IPAddress(const uint8_t(&address)[N]) 107 IPAddress(const uint8_t(&address)[N])
37 : IPAddress(address, N) {} 108 : IPAddress(address, N) {}
38 109
39 // Copies the input address to |ip_address_| taking an additional length 110 // Copies the input address to |ip_address_| taking an additional length
40 // parameter. The input is expected to be in network byte order. 111 // parameter. The input is expected to be in network byte order.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 // Returns the canonical string representation of an IP address. 166 // Returns the canonical string representation of an IP address.
96 // For example: "192.168.0.1" or "::1". Returns the empty string when 167 // For example: "192.168.0.1" or "::1". Returns the empty string when
97 // |ip_address_| is invalid. 168 // |ip_address_| is invalid.
98 std::string ToString() const; 169 std::string ToString() const;
99 170
100 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. 171 // 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. 172 // Returns true on success and fills |ip_address_| with the numeric value.
102 bool AssignFromIPLiteral(const base::StringPiece& ip_literal) 173 bool AssignFromIPLiteral(const base::StringPiece& ip_literal)
103 WARN_UNUSED_RESULT; 174 WARN_UNUSED_RESULT;
104 175
105 // Returns the underlying byte vector. 176 // Returns the underlying bytes.
106 const std::vector<uint8_t>& bytes() const { return ip_address_; }; 177 const IPAddressBytes& bytes() const { return ip_address_; };
178
179 // Copies the bytes to a new vector. Generally callers should be using
180 // |bytes()| and the IPAddressBytes abstraction. This method is provided as a
181 // convenience for call sites that existed prior to the introduction of
182 // IPAddressBytes.
183 std::vector<uint8_t> CopyBytesToVector() const;
107 184
108 // Returns an IPAddress instance representing the 127.0.0.1 address. 185 // Returns an IPAddress instance representing the 127.0.0.1 address.
109 static IPAddress IPv4Localhost(); 186 static IPAddress IPv4Localhost();
110 187
111 // Returns an IPAddress instance representing the ::1 address. 188 // Returns an IPAddress instance representing the ::1 address.
112 static IPAddress IPv6Localhost(); 189 static IPAddress IPv6Localhost();
113 190
114 // Returns an IPAddress made up of |num_zero_bytes| zeros. 191 // Returns an IPAddress made up of |num_zero_bytes| zeros.
115 static IPAddress AllZeros(size_t num_zero_bytes); 192 static IPAddress AllZeros(size_t num_zero_bytes);
116 193
117 // Returns an IPAddress instance representing the 0.0.0.0 address. 194 // Returns an IPAddress instance representing the 0.0.0.0 address.
118 static IPAddress IPv4AllZeros(); 195 static IPAddress IPv4AllZeros();
119 196
120 // Returns an IPAddress instance representing the :: address. 197 // Returns an IPAddress instance representing the :: address.
121 static IPAddress IPv6AllZeros(); 198 static IPAddress IPv6AllZeros();
122 199
123 bool operator==(const IPAddress& that) const; 200 bool operator==(const IPAddress& that) const;
124 bool operator!=(const IPAddress& that) const; 201 bool operator!=(const IPAddress& that) const;
125 bool operator<(const IPAddress& that) const; 202 bool operator<(const IPAddress& that) const;
126 203
127 private: 204 private:
128 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address 205 IPAddressBytes ip_address_;
129 // will have length kIPv6AddressSize.
130 std::vector<uint8_t> ip_address_;
131 206
132 // This class is copyable and assignable. 207 // This class is copyable and assignable.
133 }; 208 };
134 209
135 using IPAddressList = std::vector<IPAddress>; 210 using IPAddressList = std::vector<IPAddress>;
136 211
137 // Returns the canonical string representation of an IP address along with its 212 // Returns the canonical string representation of an IP address along with its
138 // port. For example: "192.168.0.1:99" or "[::1]:80". 213 // port. For example: "192.168.0.1:99" or "[::1]:80".
139 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address, 214 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddress& address,
140 uint16_t port); 215 uint16_t port);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 template <size_t N> 274 template <size_t N>
200 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) { 275 bool IPAddressStartsWith(const IPAddress& address, const uint8_t (&prefix)[N]) {
201 if (address.size() < N) 276 if (address.size() < N)
202 return false; 277 return false;
203 return std::equal(prefix, prefix + N, address.bytes().begin()); 278 return std::equal(prefix, prefix + N, address.bytes().begin());
204 } 279 }
205 280
206 } // namespace net 281 } // namespace net
207 282
208 #endif // NET_BASE_IP_ADDRESS_H_ 283 #endif // NET_BASE_IP_ADDRESS_H_
OLDNEW
« 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