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

Side by Side Diff: ppapi/shared_impl/private/net_address_private_impl.cc

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: Done 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "ppapi/shared_impl/private/net_address_private_impl.h" 5 #include "ppapi/shared_impl/private/net_address_private_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/containers/stack_container.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "ppapi/c/pp_var.h" 16 #include "ppapi/c/pp_var.h"
16 #include "ppapi/c/private/ppb_net_address_private.h" 17 #include "ppapi/c/private/ppb_net_address_private.h"
17 #include "ppapi/shared_impl/proxy_lock.h" 18 #include "ppapi/shared_impl/proxy_lock.h"
18 #include "ppapi/shared_impl/var.h" 19 #include "ppapi/shared_impl/var.h"
19 #include "ppapi/thunk/thunk.h" 20 #include "ppapi/thunk/thunk.h"
20 21
21 #if defined(OS_WIN) 22 #if defined(OS_WIN)
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 break; 445 break;
445 } 446 }
446 default: 447 default:
447 // InitNetAddress sets net_addr->is_valid to false. 448 // InitNetAddress sets net_addr->is_valid to false.
448 return false; 449 return false;
449 } 450 }
450 return true;} 451 return true;}
451 452
452 // static 453 // static
453 bool NetAddressPrivateImpl::IPEndPointToNetAddress( 454 bool NetAddressPrivateImpl::IPEndPointToNetAddress(
454 const std::vector<uint8_t>& address, 455 const net::IPAddress::IPAddressBytes& address,
455 uint16_t port, 456 uint16_t port,
456 PP_NetAddress_Private* addr) { 457 PP_NetAddress_Private* addr) {
457 if (!addr) 458 if (!addr)
458 return false; 459 return false;
459 460
460 NetAddress* net_addr = InitNetAddress(addr); 461 NetAddress* net_addr = InitNetAddress(addr);
461 switch (address.size()) { 462 switch (address.size()) {
462 case kIPv4AddressSize: { 463 case kIPv4AddressSize: {
463 net_addr->is_valid = true; 464 net_addr->is_valid = true;
464 net_addr->is_ipv6 = false; 465 net_addr->is_ipv6 = false;
(...skipping 12 matching lines...) Expand all
477 // InitNetAddress sets net_addr->is_valid to false. 478 // InitNetAddress sets net_addr->is_valid to false.
478 return false; 479 return false;
479 } 480 }
480 481
481 return true; 482 return true;
482 } 483 }
483 484
484 // static 485 // static
485 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( 486 bool NetAddressPrivateImpl::NetAddressToIPEndPoint(
486 const PP_NetAddress_Private& addr, 487 const PP_NetAddress_Private& addr,
487 std::vector<uint8_t>* address, 488 base::StackVector<uint8_t, 16>* address,
488 uint16_t* port) { 489 uint16_t* port) {
489 if (!address || !port) 490 if (!address || !port)
490 return false; 491 return false;
491 492
492 const NetAddress* net_addr = ToNetAddress(&addr); 493 const NetAddress* net_addr = ToNetAddress(&addr);
493 if (!IsValid(net_addr)) 494 if (!IsValid(net_addr))
494 return false; 495 return false;
495 496
496 *port = net_addr->port; 497 *port = net_addr->port;
497 size_t address_size = GetAddressSize(net_addr); 498 size_t address_size = GetAddressSize(net_addr);
498 address->assign(&net_addr->address[0], &net_addr->address[address_size]); 499 address->container().assign(&net_addr->address[0],
500 &net_addr->address[address_size]);
eroman 2017/05/19 22:00:07 Technically this existing code was relying on unde
Ryan Hamilton 2017/05/20 03:21:44 As per your previous comment, I switched this to t
eroman 2017/05/22 18:06:44 This translation presumes that |address| is the em
Ryan Hamilton 2017/05/22 22:12:32 Done.
499 501
500 return true; 502 return true;
501 } 503 }
502 #endif // !defined(OS_NACL) 504 #endif // !defined(OS_NACL)
503 505
504 // static 506 // static
505 std::string NetAddressPrivateImpl::DescribeNetAddress( 507 std::string NetAddressPrivateImpl::DescribeNetAddress(
506 const PP_NetAddress_Private& addr, 508 const PP_NetAddress_Private& addr,
507 bool include_port) { 509 bool include_port) {
508 const NetAddress* net_addr = ToNetAddress(&addr); 510 const NetAddress* net_addr = ToNetAddress(&addr);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 ipv6_addr->port = ConvertToNetEndian16(net_addr->port); 586 ipv6_addr->port = ConvertToNetEndian16(net_addr->port);
585 587
586 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize, 588 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize,
587 "mismatched IPv6 address size"); 589 "mismatched IPv6 address size");
588 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize); 590 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize);
589 591
590 return true; 592 return true;
591 } 593 }
592 594
593 } // namespace ppapi 595 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698