OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Platform specific code for FreeBSD goes here | 28 // Platform specific code for FreeBSD goes here. For the POSIX comaptible parts |
| 29 // the implementation is in platform-posix.cc. |
29 | 30 |
30 #include <pthread.h> | 31 #include <pthread.h> |
31 #include <semaphore.h> | 32 #include <semaphore.h> |
32 #include <signal.h> | 33 #include <signal.h> |
33 #include <sys/time.h> | 34 #include <sys/time.h> |
34 #include <sys/resource.h> | 35 #include <sys/resource.h> |
35 #include <sys/socket.h> | |
36 #include <sys/types.h> | 36 #include <sys/types.h> |
37 #include <sys/ucontext.h> | 37 #include <sys/ucontext.h> |
38 #include <stdlib.h> | 38 #include <stdlib.h> |
39 | 39 |
40 #include <sys/types.h> // mmap & munmap | 40 #include <sys/types.h> // mmap & munmap |
41 #include <sys/mman.h> // mmap & munmap | 41 #include <sys/mman.h> // mmap & munmap |
42 #include <sys/stat.h> // open | 42 #include <sys/stat.h> // open |
43 #include <sys/fcntl.h> // open | 43 #include <sys/fcntl.h> // open |
44 #include <unistd.h> // getpagesize | 44 #include <unistd.h> // getpagesize |
45 #include <execinfo.h> // backtrace, backtrace_symbols | 45 #include <execinfo.h> // backtrace, backtrace_symbols |
46 #include <strings.h> // index | 46 #include <strings.h> // index |
47 #include <arpa/inet.h> | |
48 #include <netinet/in.h> | |
49 #include <netdb.h> | |
50 #include <errno.h> | 47 #include <errno.h> |
51 #include <stdarg.h> | 48 #include <stdarg.h> |
52 #include <limits.h> | 49 #include <limits.h> |
53 | 50 |
54 #undef MAP_TYPE | 51 #undef MAP_TYPE |
55 | 52 |
56 #include "v8.h" | 53 #include "v8.h" |
57 | 54 |
58 #include "platform.h" | 55 #include "platform.h" |
59 | 56 |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. | 629 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. |
633 } | 630 } |
634 } | 631 } |
635 | 632 |
636 | 633 |
637 Semaphore* OS::CreateSemaphore(int count) { | 634 Semaphore* OS::CreateSemaphore(int count) { |
638 return new FreeBSDSemaphore(count); | 635 return new FreeBSDSemaphore(count); |
639 } | 636 } |
640 | 637 |
641 | 638 |
642 // ---------------------------------------------------------------------------- | |
643 // FreeBSD socket support. | |
644 // | |
645 | |
646 class FreeBSDSocket : public Socket { | |
647 public: | |
648 explicit FreeBSDSocket() { | |
649 // Create the socket. | |
650 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
651 } | |
652 explicit FreeBSDSocket(int socket): socket_(socket) { } | |
653 virtual ~FreeBSDSocket() { Shutdown(); } | |
654 | |
655 // Server initialization. | |
656 bool Bind(const int port); | |
657 bool Listen(int backlog) const; | |
658 Socket* Accept() const; | |
659 | |
660 // Client initialization. | |
661 bool Connect(const char* host, const char* port); | |
662 | |
663 // Shutdown socket for both read and write. | |
664 bool Shutdown(); | |
665 | |
666 // Data Transimission | |
667 int Send(const char* data, int len) const; | |
668 int Receive(char* data, int len) const; | |
669 | |
670 bool SetReuseAddress(bool reuse_address); | |
671 | |
672 bool IsValid() const { return socket_ != -1; } | |
673 | |
674 private: | |
675 int socket_; | |
676 }; | |
677 | |
678 | |
679 bool FreeBSDSocket::Bind(const int port) { | |
680 if (!IsValid()) { | |
681 return false; | |
682 } | |
683 | |
684 sockaddr_in addr; | |
685 memset(&addr, 0, sizeof(addr)); | |
686 addr.sin_family = AF_INET; | |
687 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
688 addr.sin_port = htons(port); | |
689 int status = bind(socket_, | |
690 reinterpret_cast<struct sockaddr *>(&addr), | |
691 sizeof(addr)); | |
692 return status == 0; | |
693 } | |
694 | |
695 | |
696 bool FreeBSDSocket::Listen(int backlog) const { | |
697 if (!IsValid()) { | |
698 return false; | |
699 } | |
700 | |
701 int status = listen(socket_, backlog); | |
702 return status == 0; | |
703 } | |
704 | |
705 | |
706 Socket* FreeBSDSocket::Accept() const { | |
707 if (!IsValid()) { | |
708 return NULL; | |
709 } | |
710 | |
711 int socket = accept(socket_, NULL, NULL); | |
712 if (socket == -1) { | |
713 return NULL; | |
714 } else { | |
715 return new FreeBSDSocket(socket); | |
716 } | |
717 } | |
718 | |
719 | |
720 bool FreeBSDSocket::Connect(const char* host, const char* port) { | |
721 if (!IsValid()) { | |
722 return false; | |
723 } | |
724 | |
725 // Lookup host and port. | |
726 struct addrinfo *result = NULL; | |
727 struct addrinfo hints; | |
728 memset(&hints, 0, sizeof(addrinfo)); | |
729 hints.ai_family = AF_INET; | |
730 hints.ai_socktype = SOCK_STREAM; | |
731 hints.ai_protocol = IPPROTO_TCP; | |
732 int status = getaddrinfo(host, port, &hints, &result); | |
733 if (status != 0) { | |
734 return false; | |
735 } | |
736 | |
737 // Connect. | |
738 status = connect(socket_, result->ai_addr, result->ai_addrlen); | |
739 freeaddrinfo(result); | |
740 return status == 0; | |
741 } | |
742 | |
743 | |
744 bool FreeBSDSocket::Shutdown() { | |
745 if (IsValid()) { | |
746 // Shutdown socket for both read and write. | |
747 int status = shutdown(socket_, SHUT_RDWR); | |
748 close(socket_); | |
749 socket_ = -1; | |
750 return status == 0; | |
751 } | |
752 return true; | |
753 } | |
754 | |
755 | |
756 int FreeBSDSocket::Send(const char* data, int len) const { | |
757 int status = send(socket_, data, len, 0); | |
758 return status; | |
759 } | |
760 | |
761 | |
762 int FreeBSDSocket::Receive(char* data, int len) const { | |
763 int status = recv(socket_, data, len, 0); | |
764 return status; | |
765 } | |
766 | |
767 | |
768 bool FreeBSDSocket::SetReuseAddress(bool reuse_address) { | |
769 int on = reuse_address ? 1 : 0; | |
770 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
771 return status == 0; | |
772 } | |
773 | |
774 | |
775 bool Socket::Setup() { | |
776 // Nothing to do on FreeBSD. | |
777 return true; | |
778 } | |
779 | |
780 | |
781 int Socket::LastError() { | |
782 return errno; | |
783 } | |
784 | |
785 | |
786 uint16_t Socket::HToN(uint16_t value) { | |
787 return htons(value); | |
788 } | |
789 | |
790 | |
791 uint16_t Socket::NToH(uint16_t value) { | |
792 return ntohs(value); | |
793 } | |
794 | |
795 | |
796 uint32_t Socket::HToN(uint32_t value) { | |
797 return htonl(value); | |
798 } | |
799 | |
800 | |
801 uint32_t Socket::NToH(uint32_t value) { | |
802 return ntohl(value); | |
803 } | |
804 | |
805 | |
806 Socket* OS::CreateSocket() { | |
807 return new FreeBSDSocket(); | |
808 } | |
809 | |
810 | |
811 #ifdef ENABLE_LOGGING_AND_PROFILING | 639 #ifdef ENABLE_LOGGING_AND_PROFILING |
812 | 640 |
813 static Sampler* active_sampler_ = NULL; | 641 static Sampler* active_sampler_ = NULL; |
814 | 642 |
815 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | 643 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
816 USE(info); | 644 USE(info); |
817 if (signal != SIGPROF) return; | 645 if (signal != SIGPROF) return; |
818 if (active_sampler_ == NULL) return; | 646 if (active_sampler_ == NULL) return; |
819 | 647 |
820 TickSample sample; | 648 TickSample sample; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 } | 729 } |
902 | 730 |
903 // This sampler is no longer the active sampler. | 731 // This sampler is no longer the active sampler. |
904 active_sampler_ = NULL; | 732 active_sampler_ = NULL; |
905 active_ = false; | 733 active_ = false; |
906 } | 734 } |
907 | 735 |
908 #endif // ENABLE_LOGGING_AND_PROFILING | 736 #endif // ENABLE_LOGGING_AND_PROFILING |
909 | 737 |
910 } } // namespace v8::internal | 738 } } // namespace v8::internal |
OLD | NEW |