The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / platform-posix.cc @ 40c0f755

History | View | Annotate | Download (8.07 KB)

1
// Copyright 2009 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27

    
28
// Platform specific code for POSIX goes here. This is not a platform on its
29
// own but contains the parts which are the same across POSIX platforms Linux,
30
// Mac OS and FreeBSD.
31

    
32
#include <unistd.h>
33
#include <errno.h>
34
#include <time.h>
35

    
36
#include <sys/socket.h>
37
#include <sys/resource.h>
38
#include <sys/time.h>
39
#include <sys/types.h>
40

    
41
#include <arpa/inet.h>
42
#include <netinet/in.h>
43
#include <netdb.h>
44

    
45
#include "v8.h"
46

    
47
#include "platform.h"
48

    
49

    
50
namespace v8 { namespace internal {
51

    
52

    
53
// ----------------------------------------------------------------------------
54
// POSIX date/time support.
55
//
56

    
57
int OS::GetUserTime(uint32_t* secs,  uint32_t* usecs) {
58
  struct rusage usage;
59

    
60
  if (getrusage(RUSAGE_SELF, &usage) < 0) return -1;
61
  *secs = usage.ru_utime.tv_sec;
62
  *usecs = usage.ru_utime.tv_usec;
63
  return 0;
64
}
65

    
66

    
67
double OS::TimeCurrentMillis() {
68
  struct timeval tv;
69
  if (gettimeofday(&tv, NULL) < 0) return 0.0;
70
  return (static_cast<double>(tv.tv_sec) * 1000) +
71
         (static_cast<double>(tv.tv_usec) / 1000);
72
}
73

    
74

    
75
int64_t OS::Ticks() {
76
  // gettimeofday has microsecond resolution.
77
  struct timeval tv;
78
  if (gettimeofday(&tv, NULL) < 0)
79
    return 0;
80
  return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
81
}
82

    
83

    
84
char* OS::LocalTimezone(double time) {
85
  time_t tv = static_cast<time_t>(floor(time/msPerSecond));
86
  struct tm* t = localtime(&tv);
87
  return const_cast<char*>(t->tm_zone);
88
}
89

    
90

    
91
double OS::DaylightSavingsOffset(double time) {
92
  time_t tv = static_cast<time_t>(floor(time/msPerSecond));
93
  struct tm* t = localtime(&tv);
94
  return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
95
}
96

    
97

    
98
double OS::LocalTimeOffset() {
99
  time_t tv = time(NULL);
100
  struct tm* t = localtime(&tv);
101
  // tm_gmtoff includes any daylight savings offset, so subtract it.
102
  return static_cast<double>(t->tm_gmtoff * msPerSecond -
103
                             (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
104
}
105

    
106

    
107
// ----------------------------------------------------------------------------
108
// POSIX stdio support.
109
//
110

    
111
FILE* OS::FOpen(const char* path, const char* mode) {
112
  return fopen(path, mode);
113
}
114

    
115

    
116
const char* OS::LogFileOpenMode = "w";
117

    
118

    
119
void OS::Print(const char* format, ...) {
120
  va_list args;
121
  va_start(args, format);
122
  VPrint(format, args);
123
  va_end(args);
124
}
125

    
126

    
127
void OS::VPrint(const char* format, va_list args) {
128
  vprintf(format, args);
129
}
130

    
131

    
132
void OS::PrintError(const char* format, ...) {
133
  va_list args;
134
  va_start(args, format);
135
  VPrintError(format, args);
136
  va_end(args);
137
}
138

    
139

    
140
void OS::VPrintError(const char* format, va_list args) {
141
  vfprintf(stderr, format, args);
142
}
143

    
144

    
145
int OS::SNPrintF(Vector<char> str, const char* format, ...) {
146
  va_list args;
147
  va_start(args, format);
148
  int result = VSNPrintF(str, format, args);
149
  va_end(args);
150
  return result;
151
}
152

    
153

    
154
int OS::VSNPrintF(Vector<char> str,
155
                  const char* format,
156
                  va_list args) {
157
  int n = vsnprintf(str.start(), str.length(), format, args);
158
  if (n < 0 || n >= str.length()) {
159
    str[str.length() - 1] = '\0';
160
    return -1;
161
  } else {
162
    return n;
163
  }
164
}
165

    
166

    
167
// ----------------------------------------------------------------------------
168
// POSIX string support.
169
//
170

    
171
char* OS::StrChr(char* str, int c) {
172
  return strchr(str, c);
173
}
174

    
175

    
176
void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) {
177
  strncpy(dest.start(), src, n);
178
}
179

    
180

    
181
// ----------------------------------------------------------------------------
182
// POSIX socket support.
183
//
184

    
185
class POSIXSocket : public Socket {
186
 public:
187
  explicit POSIXSocket() {
188
    // Create the socket.
189
    socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
190
  }
191
  explicit POSIXSocket(int socket): socket_(socket) { }
192
  virtual ~POSIXSocket() { Shutdown(); }
193

    
194
  // Server initialization.
195
  bool Bind(const int port);
196
  bool Listen(int backlog) const;
197
  Socket* Accept() const;
198

    
199
  // Client initialization.
200
  bool Connect(const char* host, const char* port);
201

    
202
  // Shutdown socket for both read and write.
203
  bool Shutdown();
204

    
205
  // Data Transimission
206
  int Send(const char* data, int len) const;
207
  int Receive(char* data, int len) const;
208

    
209
  bool SetReuseAddress(bool reuse_address);
210

    
211
  bool IsValid() const { return socket_ != -1; }
212

    
213
 private:
214
  int socket_;
215
};
216

    
217

    
218
bool POSIXSocket::Bind(const int port) {
219
  if (!IsValid())  {
220
    return false;
221
  }
222

    
223
  sockaddr_in addr;
224
  memset(&addr, 0, sizeof(addr));
225
  addr.sin_family = AF_INET;
226
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
227
  addr.sin_port = htons(port);
228
  int status = bind(socket_,
229
                    reinterpret_cast<struct sockaddr *>(&addr),
230
                    sizeof(addr));
231
  return status == 0;
232
}
233

    
234

    
235
bool POSIXSocket::Listen(int backlog) const {
236
  if (!IsValid()) {
237
    return false;
238
  }
239

    
240
  int status = listen(socket_, backlog);
241
  return status == 0;
242
}
243

    
244

    
245
Socket* POSIXSocket::Accept() const {
246
  if (!IsValid()) {
247
    return NULL;
248
  }
249

    
250
  int socket = accept(socket_, NULL, NULL);
251
  if (socket == -1) {
252
    return NULL;
253
  } else {
254
    return new POSIXSocket(socket);
255
  }
256
}
257

    
258

    
259
bool POSIXSocket::Connect(const char* host, const char* port) {
260
  if (!IsValid()) {
261
    return false;
262
  }
263

    
264
  // Lookup host and port.
265
  struct addrinfo *result = NULL;
266
  struct addrinfo hints;
267
  memset(&hints, 0, sizeof(addrinfo));
268
  hints.ai_family = AF_INET;
269
  hints.ai_socktype = SOCK_STREAM;
270
  hints.ai_protocol = IPPROTO_TCP;
271
  int status = getaddrinfo(host, port, &hints, &result);
272
  if (status != 0) {
273
    return false;
274
  }
275

    
276
  // Connect.
277
  status = connect(socket_, result->ai_addr, result->ai_addrlen);
278
  freeaddrinfo(result);
279
  return status == 0;
280
}
281

    
282

    
283
bool POSIXSocket::Shutdown() {
284
  if (IsValid()) {
285
    // Shutdown socket for both read and write.
286
    int status = shutdown(socket_, SHUT_RDWR);
287
    close(socket_);
288
    socket_ = -1;
289
    return status == 0;
290
  }
291
  return true;
292
}
293

    
294

    
295
int POSIXSocket::Send(const char* data, int len) const {
296
  int status = send(socket_, data, len, 0);
297
  return status;
298
}
299

    
300

    
301
int POSIXSocket::Receive(char* data, int len) const {
302
  int status = recv(socket_, data, len, 0);
303
  return status;
304
}
305

    
306

    
307
bool POSIXSocket::SetReuseAddress(bool reuse_address) {
308
  int on = reuse_address ? 1 : 0;
309
  int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
310
  return status == 0;
311
}
312

    
313

    
314
bool Socket::Setup() {
315
  // Nothing to do on POSIX.
316
  return true;
317
}
318

    
319

    
320
int Socket::LastError() {
321
  return errno;
322
}
323

    
324

    
325
uint16_t Socket::HToN(uint16_t value) {
326
  return htons(value);
327
}
328

    
329

    
330
uint16_t Socket::NToH(uint16_t value) {
331
  return ntohs(value);
332
}
333

    
334

    
335
uint32_t Socket::HToN(uint32_t value) {
336
  return htonl(value);
337
}
338

    
339

    
340
uint32_t Socket::NToH(uint32_t value) {
341
  return ntohl(value);
342
}
343

    
344

    
345
Socket* OS::CreateSocket() {
346
  return new POSIXSocket();
347
}
348

    
349

    
350
} }  // namespace v8::internal