| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 intptr_t OS::MaxVirtualMemory() { | 137 intptr_t OS::MaxVirtualMemory() { |
| 138 return 0; | 138 return 0; |
| 139 } | 139 } |
| 140 | 140 |
| 141 | 141 |
| 142 double ceiling(double x) { | 142 double ceiling(double x) { |
| 143 return ceil(x); | 143 return ceil(x); |
| 144 } | 144 } |
| 145 | 145 |
| 146 | 146 |
| 147 static Mutex* limit_mutex = NULL; | |
| 148 | |
| 149 #if V8_TARGET_ARCH_IA32 | 147 #if V8_TARGET_ARCH_IA32 |
| 150 static void MemMoveWrapper(void* dest, const void* src, size_t size) { | 148 static void MemMoveWrapper(void* dest, const void* src, size_t size) { |
| 151 memmove(dest, src, size); | 149 memmove(dest, src, size); |
| 152 } | 150 } |
| 153 | 151 |
| 154 | 152 |
| 155 // Initialize to library version so we can call this at any time during startup. | 153 // Initialize to library version so we can call this at any time during startup. |
| 156 static OS::MemMoveFunction memmove_function = &MemMoveWrapper; | 154 static OS::MemMoveFunction memmove_function = &MemMoveWrapper; |
| 157 | 155 |
| 158 // Defined in codegen-ia32.cc. | 156 // Defined in codegen-ia32.cc. |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 n = _TRUNCATE; | 741 n = _TRUNCATE; |
| 744 int result = strncpy_s(dest.start(), dest.length(), src, n); | 742 int result = strncpy_s(dest.start(), dest.length(), src, n); |
| 745 USE(result); | 743 USE(result); |
| 746 ASSERT(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); | 744 ASSERT(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); |
| 747 } | 745 } |
| 748 | 746 |
| 749 | 747 |
| 750 #undef _TRUNCATE | 748 #undef _TRUNCATE |
| 751 #undef STRUNCATE | 749 #undef STRUNCATE |
| 752 | 750 |
| 753 // We keep the lowest and highest addresses mapped as a quick way of | |
| 754 // determining that pointers are outside the heap (used mostly in assertions | |
| 755 // and verification). The estimate is conservative, i.e., not all addresses in | |
| 756 // 'allocated' space are actually allocated to our heap. The range is | |
| 757 // [lowest, highest), inclusive on the low and and exclusive on the high end. | |
| 758 static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); | |
| 759 static void* highest_ever_allocated = reinterpret_cast<void*>(0); | |
| 760 | |
| 761 | |
| 762 static void UpdateAllocatedSpaceLimits(void* address, int size) { | |
| 763 ASSERT(limit_mutex != NULL); | |
| 764 LockGuard<Mutex> lock_guard(limit_mutex); | |
| 765 | |
| 766 lowest_ever_allocated = Min(lowest_ever_allocated, address); | |
| 767 highest_ever_allocated = | |
| 768 Max(highest_ever_allocated, | |
| 769 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); | |
| 770 } | |
| 771 | |
| 772 | |
| 773 bool OS::IsOutsideAllocatedSpace(void* pointer) { | |
| 774 if (pointer < lowest_ever_allocated || pointer >= highest_ever_allocated) | |
| 775 return true; | |
| 776 // Ask the Windows API | |
| 777 if (IsBadWritePtr(pointer, 1)) | |
| 778 return true; | |
| 779 return false; | |
| 780 } | |
| 781 | |
| 782 | 751 |
| 783 // Get the system's page size used by VirtualAlloc() or the next power | 752 // Get the system's page size used by VirtualAlloc() or the next power |
| 784 // of two. The reason for always returning a power of two is that the | 753 // of two. The reason for always returning a power of two is that the |
| 785 // rounding up in OS::Allocate expects that. | 754 // rounding up in OS::Allocate expects that. |
| 786 static size_t GetPageSize() { | 755 static size_t GetPageSize() { |
| 787 static size_t page_size = 0; | 756 static size_t page_size = 0; |
| 788 if (page_size == 0) { | 757 if (page_size == 0) { |
| 789 SYSTEM_INFO info; | 758 SYSTEM_INFO info; |
| 790 GetSystemInfo(&info); | 759 GetSystemInfo(&info); |
| 791 page_size = RoundUpToPowerOf2(info.dwPageSize); | 760 page_size = RoundUpToPowerOf2(info.dwPageSize); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 prot); | 834 prot); |
| 866 | 835 |
| 867 if (mbase == NULL) { | 836 if (mbase == NULL) { |
| 868 LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed")); | 837 LOG(ISOLATE, StringEvent("OS::Allocate", "VirtualAlloc failed")); |
| 869 return NULL; | 838 return NULL; |
| 870 } | 839 } |
| 871 | 840 |
| 872 ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment())); | 841 ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment())); |
| 873 | 842 |
| 874 *allocated = msize; | 843 *allocated = msize; |
| 875 UpdateAllocatedSpaceLimits(mbase, static_cast<int>(msize)); | |
| 876 return mbase; | 844 return mbase; |
| 877 } | 845 } |
| 878 | 846 |
| 879 | 847 |
| 880 void OS::Free(void* address, const size_t size) { | 848 void OS::Free(void* address, const size_t size) { |
| 881 // TODO(1240712): VirtualFree has a return value which is ignored here. | 849 // TODO(1240712): VirtualFree has a return value which is ignored here. |
| 882 VirtualFree(address, 0, MEM_RELEASE); | 850 VirtualFree(address, 0, MEM_RELEASE); |
| 883 USE(size); | 851 USE(size); |
| 884 } | 852 } |
| 885 | 853 |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1483 void* VirtualMemory::ReserveRegion(size_t size) { | 1451 void* VirtualMemory::ReserveRegion(size_t size) { |
| 1484 return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS); | 1452 return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS); |
| 1485 } | 1453 } |
| 1486 | 1454 |
| 1487 | 1455 |
| 1488 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { | 1456 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 1489 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; | 1457 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
| 1490 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) { | 1458 if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) { |
| 1491 return false; | 1459 return false; |
| 1492 } | 1460 } |
| 1493 | |
| 1494 UpdateAllocatedSpaceLimits(base, static_cast<int>(size)); | |
| 1495 return true; | 1461 return true; |
| 1496 } | 1462 } |
| 1497 | 1463 |
| 1498 | 1464 |
| 1499 bool VirtualMemory::UncommitRegion(void* base, size_t size) { | 1465 bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
| 1500 return VirtualFree(base, size, MEM_DECOMMIT) != 0; | 1466 return VirtualFree(base, size, MEM_DECOMMIT) != 0; |
| 1501 } | 1467 } |
| 1502 | 1468 |
| 1503 | 1469 |
| 1504 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { | 1470 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1608 ASSERT(result); | 1574 ASSERT(result); |
| 1609 } | 1575 } |
| 1610 | 1576 |
| 1611 | 1577 |
| 1612 | 1578 |
| 1613 void Thread::YieldCPU() { | 1579 void Thread::YieldCPU() { |
| 1614 Sleep(0); | 1580 Sleep(0); |
| 1615 } | 1581 } |
| 1616 | 1582 |
| 1617 | 1583 |
| 1618 // ---------------------------------------------------------------------------- | |
| 1619 // Win32 socket support. | |
| 1620 // | |
| 1621 | |
| 1622 class Win32Socket : public Socket { | |
| 1623 public: | |
| 1624 explicit Win32Socket() { | |
| 1625 // Create the socket. | |
| 1626 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
| 1627 } | |
| 1628 explicit Win32Socket(SOCKET socket): socket_(socket) { } | |
| 1629 virtual ~Win32Socket() { Shutdown(); } | |
| 1630 | |
| 1631 // Server initialization. | |
| 1632 bool Bind(const int port); | |
| 1633 bool Listen(int backlog) const; | |
| 1634 Socket* Accept() const; | |
| 1635 | |
| 1636 // Client initialization. | |
| 1637 bool Connect(const char* host, const char* port); | |
| 1638 | |
| 1639 // Shutdown socket for both read and write. | |
| 1640 bool Shutdown(); | |
| 1641 | |
| 1642 // Data Transimission | |
| 1643 int Send(const char* data, int len) const; | |
| 1644 int Receive(char* data, int len) const; | |
| 1645 | |
| 1646 bool SetReuseAddress(bool reuse_address); | |
| 1647 | |
| 1648 bool IsValid() const { return socket_ != INVALID_SOCKET; } | |
| 1649 | |
| 1650 private: | |
| 1651 SOCKET socket_; | |
| 1652 }; | |
| 1653 | |
| 1654 | |
| 1655 bool Win32Socket::Bind(const int port) { | |
| 1656 if (!IsValid()) { | |
| 1657 return false; | |
| 1658 } | |
| 1659 | |
| 1660 sockaddr_in addr; | |
| 1661 memset(&addr, 0, sizeof(addr)); | |
| 1662 addr.sin_family = AF_INET; | |
| 1663 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
| 1664 addr.sin_port = htons(port); | |
| 1665 int status = bind(socket_, | |
| 1666 reinterpret_cast<struct sockaddr *>(&addr), | |
| 1667 sizeof(addr)); | |
| 1668 return status == 0; | |
| 1669 } | |
| 1670 | |
| 1671 | |
| 1672 bool Win32Socket::Listen(int backlog) const { | |
| 1673 if (!IsValid()) { | |
| 1674 return false; | |
| 1675 } | |
| 1676 | |
| 1677 int status = listen(socket_, backlog); | |
| 1678 return status == 0; | |
| 1679 } | |
| 1680 | |
| 1681 | |
| 1682 Socket* Win32Socket::Accept() const { | |
| 1683 if (!IsValid()) { | |
| 1684 return NULL; | |
| 1685 } | |
| 1686 | |
| 1687 SOCKET socket = accept(socket_, NULL, NULL); | |
| 1688 if (socket == INVALID_SOCKET) { | |
| 1689 return NULL; | |
| 1690 } else { | |
| 1691 return new Win32Socket(socket); | |
| 1692 } | |
| 1693 } | |
| 1694 | |
| 1695 | |
| 1696 bool Win32Socket::Connect(const char* host, const char* port) { | |
| 1697 if (!IsValid()) { | |
| 1698 return false; | |
| 1699 } | |
| 1700 | |
| 1701 // Lookup host and port. | |
| 1702 struct addrinfo *result = NULL; | |
| 1703 struct addrinfo hints; | |
| 1704 memset(&hints, 0, sizeof(addrinfo)); | |
| 1705 hints.ai_family = AF_INET; | |
| 1706 hints.ai_socktype = SOCK_STREAM; | |
| 1707 hints.ai_protocol = IPPROTO_TCP; | |
| 1708 int status = getaddrinfo(host, port, &hints, &result); | |
| 1709 if (status != 0) { | |
| 1710 return false; | |
| 1711 } | |
| 1712 | |
| 1713 // Connect. | |
| 1714 status = connect(socket_, | |
| 1715 result->ai_addr, | |
| 1716 static_cast<int>(result->ai_addrlen)); | |
| 1717 freeaddrinfo(result); | |
| 1718 return status == 0; | |
| 1719 } | |
| 1720 | |
| 1721 | |
| 1722 bool Win32Socket::Shutdown() { | |
| 1723 if (IsValid()) { | |
| 1724 // Shutdown socket for both read and write. | |
| 1725 int status = shutdown(socket_, SD_BOTH); | |
| 1726 closesocket(socket_); | |
| 1727 socket_ = INVALID_SOCKET; | |
| 1728 return status == SOCKET_ERROR; | |
| 1729 } | |
| 1730 return true; | |
| 1731 } | |
| 1732 | |
| 1733 | |
| 1734 int Win32Socket::Send(const char* data, int len) const { | |
| 1735 if (len <= 0) return 0; | |
| 1736 int written = 0; | |
| 1737 while (written < len) { | |
| 1738 int status = send(socket_, data + written, len - written, 0); | |
| 1739 if (status == 0) { | |
| 1740 break; | |
| 1741 } else if (status > 0) { | |
| 1742 written += status; | |
| 1743 } else { | |
| 1744 return 0; | |
| 1745 } | |
| 1746 } | |
| 1747 return written; | |
| 1748 } | |
| 1749 | |
| 1750 | |
| 1751 int Win32Socket::Receive(char* data, int len) const { | |
| 1752 if (len <= 0) return 0; | |
| 1753 int status = recv(socket_, data, len, 0); | |
| 1754 return (status == SOCKET_ERROR) ? 0 : status; | |
| 1755 } | |
| 1756 | |
| 1757 | |
| 1758 bool Win32Socket::SetReuseAddress(bool reuse_address) { | |
| 1759 BOOL on = reuse_address ? true : false; | |
| 1760 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, | |
| 1761 reinterpret_cast<char*>(&on), sizeof(on)); | |
| 1762 return status == SOCKET_ERROR; | |
| 1763 } | |
| 1764 | |
| 1765 | |
| 1766 bool Socket::SetUp() { | |
| 1767 // Initialize Winsock32 | |
| 1768 int err; | |
| 1769 WSADATA winsock_data; | |
| 1770 WORD version_requested = MAKEWORD(1, 0); | |
| 1771 err = WSAStartup(version_requested, &winsock_data); | |
| 1772 if (err != 0) { | |
| 1773 PrintF("Unable to initialize Winsock, err = %d\n", Socket::LastError()); | |
| 1774 } | |
| 1775 | |
| 1776 return err == 0; | |
| 1777 } | |
| 1778 | |
| 1779 | |
| 1780 int Socket::LastError() { | |
| 1781 return WSAGetLastError(); | |
| 1782 } | |
| 1783 | |
| 1784 | |
| 1785 uint16_t Socket::HToN(uint16_t value) { | |
| 1786 return htons(value); | |
| 1787 } | |
| 1788 | |
| 1789 | |
| 1790 uint16_t Socket::NToH(uint16_t value) { | |
| 1791 return ntohs(value); | |
| 1792 } | |
| 1793 | |
| 1794 | |
| 1795 uint32_t Socket::HToN(uint32_t value) { | |
| 1796 return htonl(value); | |
| 1797 } | |
| 1798 | |
| 1799 | |
| 1800 uint32_t Socket::NToH(uint32_t value) { | |
| 1801 return ntohl(value); | |
| 1802 } | |
| 1803 | |
| 1804 | |
| 1805 Socket* OS::CreateSocket() { | |
| 1806 return new Win32Socket(); | |
| 1807 } | |
| 1808 | |
| 1809 | |
| 1810 void OS::SetUp() { | 1584 void OS::SetUp() { |
| 1811 // Seed the random number generator. | 1585 // Seed the random number generator. |
| 1812 // Convert the current time to a 64-bit integer first, before converting it | 1586 // Convert the current time to a 64-bit integer first, before converting it |
| 1813 // to an unsigned. Going directly can cause an overflow and the seed to be | 1587 // to an unsigned. Going directly can cause an overflow and the seed to be |
| 1814 // set to all ones. The seed will be identical for different instances that | 1588 // set to all ones. The seed will be identical for different instances that |
| 1815 // call this setup code within the same millisecond. | 1589 // call this setup code within the same millisecond. |
| 1816 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); | 1590 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
| 1817 srand(static_cast<unsigned int>(seed)); | 1591 srand(static_cast<unsigned int>(seed)); |
| 1818 limit_mutex = new Mutex(); | |
| 1819 } | 1592 } |
| 1820 | 1593 |
| 1821 | |
| 1822 void OS::TearDown() { | |
| 1823 delete limit_mutex; | |
| 1824 } | |
| 1825 | |
| 1826 | |
| 1827 } } // namespace v8::internal | 1594 } } // namespace v8::internal |
| OLD | NEW |