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

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

Issue 2881673002: Avoid heap allocations in IPAddress (Closed)
Patch Set: Rename and Assign 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>
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 break; 444 break;
445 } 445 }
446 default: 446 default:
447 // InitNetAddress sets net_addr->is_valid to false. 447 // InitNetAddress sets net_addr->is_valid to false.
448 return false; 448 return false;
449 } 449 }
450 return true;} 450 return true;}
451 451
452 // static 452 // static
453 bool NetAddressPrivateImpl::IPEndPointToNetAddress( 453 bool NetAddressPrivateImpl::IPEndPointToNetAddress(
454 const std::vector<uint8_t>& address, 454 const net::IPAddressBytes& address,
455 uint16_t port, 455 uint16_t port,
456 PP_NetAddress_Private* addr) { 456 PP_NetAddress_Private* addr) {
457 if (!addr) 457 if (!addr)
458 return false; 458 return false;
459 459
460 NetAddress* net_addr = InitNetAddress(addr); 460 NetAddress* net_addr = InitNetAddress(addr);
461 switch (address.size()) { 461 switch (address.size()) {
462 case kIPv4AddressSize: { 462 case kIPv4AddressSize: {
463 net_addr->is_valid = true; 463 net_addr->is_valid = true;
464 net_addr->is_ipv6 = false; 464 net_addr->is_ipv6 = false;
(...skipping 12 matching lines...) Expand all
477 // InitNetAddress sets net_addr->is_valid to false. 477 // InitNetAddress sets net_addr->is_valid to false.
478 return false; 478 return false;
479 } 479 }
480 480
481 return true; 481 return true;
482 } 482 }
483 483
484 // static 484 // static
485 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( 485 bool NetAddressPrivateImpl::NetAddressToIPEndPoint(
486 const PP_NetAddress_Private& addr, 486 const PP_NetAddress_Private& addr,
487 std::vector<uint8_t>* address, 487 net::IPAddressBytes* address,
488 uint16_t* port) { 488 uint16_t* port) {
489 if (!address || !port) 489 if (!address || !port)
490 return false; 490 return false;
491 491
492 const NetAddress* net_addr = ToNetAddress(&addr); 492 const NetAddress* net_addr = ToNetAddress(&addr);
493 if (!IsValid(net_addr)) 493 if (!IsValid(net_addr))
494 return false; 494 return false;
495 495
496 *port = net_addr->port; 496 *port = net_addr->port;
497 size_t address_size = GetAddressSize(net_addr); 497 size_t address_size = GetAddressSize(net_addr);
498 address->assign(&net_addr->address[0], &net_addr->address[address_size]); 498 address->Assign(net_addr->address, address_size);
499
500 return true; 499 return true;
501 } 500 }
502 #endif // !defined(OS_NACL) 501 #endif // !defined(OS_NACL)
503 502
504 // static 503 // static
505 std::string NetAddressPrivateImpl::DescribeNetAddress( 504 std::string NetAddressPrivateImpl::DescribeNetAddress(
506 const PP_NetAddress_Private& addr, 505 const PP_NetAddress_Private& addr,
507 bool include_port) { 506 bool include_port) {
508 const NetAddress* net_addr = ToNetAddress(&addr); 507 const NetAddress* net_addr = ToNetAddress(&addr);
509 if (!IsValid(net_addr)) 508 if (!IsValid(net_addr))
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 ipv6_addr->port = ConvertToNetEndian16(net_addr->port); 583 ipv6_addr->port = ConvertToNetEndian16(net_addr->port);
585 584
586 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize, 585 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize,
587 "mismatched IPv6 address size"); 586 "mismatched IPv6 address size");
588 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize); 587 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize);
589 588
590 return true; 589 return true;
591 } 590 }
592 591
593 } // namespace ppapi 592 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698