OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project 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 // Platform-specific code for Win32. | 5 // Platform-specific code for Win32. |
6 | 6 |
7 // Secure API functions are not available using MinGW with msvcrt.dll | 7 // Secure API functions are not available using MinGW with msvcrt.dll |
8 // on Windows XP. Make sure MINGW_HAS_SECURE_API is not defined to | 8 // on Windows XP. Make sure MINGW_HAS_SECURE_API is not defined to |
9 // disable definition of secure API functions in standard headers that | 9 // disable definition of secure API functions in standard headers that |
10 // would conflict with our own implementation. | 10 // would conflict with our own implementation. |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 std_tz_name_[kTzNameSize - 1] = '\0'; | 144 std_tz_name_[kTzNameSize - 1] = '\0'; |
145 WideCharToMultiByte(CP_UTF8, 0, tzinfo_.DaylightName, -1, | 145 WideCharToMultiByte(CP_UTF8, 0, tzinfo_.DaylightName, -1, |
146 dst_tz_name_, kTzNameSize, NULL, NULL); | 146 dst_tz_name_, kTzNameSize, NULL, NULL); |
147 dst_tz_name_[kTzNameSize - 1] = '\0'; | 147 dst_tz_name_[kTzNameSize - 1] = '\0'; |
148 | 148 |
149 // If OS returned empty string or resource id (like "@tzres.dll,-211") | 149 // If OS returned empty string or resource id (like "@tzres.dll,-211") |
150 // simply guess the name from the UTC bias of the timezone. | 150 // simply guess the name from the UTC bias of the timezone. |
151 // To properly resolve the resource identifier requires a library load, | 151 // To properly resolve the resource identifier requires a library load, |
152 // which is not possible in a sandbox. | 152 // which is not possible in a sandbox. |
153 if (std_tz_name_[0] == '\0' || std_tz_name_[0] == '@') { | 153 if (std_tz_name_[0] == '\0' || std_tz_name_[0] == '@') { |
154 OS::SNPrintF(Vector<char>(std_tz_name_, kTzNameSize - 1), | 154 OS::SNPrintF(std_tz_name_, kTzNameSize - 1, |
155 "%s Standard Time", | 155 "%s Standard Time", |
156 GuessTimezoneNameFromBias(tzinfo_.Bias)); | 156 GuessTimezoneNameFromBias(tzinfo_.Bias)); |
157 } | 157 } |
158 if (dst_tz_name_[0] == '\0' || dst_tz_name_[0] == '@') { | 158 if (dst_tz_name_[0] == '\0' || dst_tz_name_[0] == '@') { |
159 OS::SNPrintF(Vector<char>(dst_tz_name_, kTzNameSize - 1), | 159 OS::SNPrintF(dst_tz_name_, kTzNameSize - 1, |
160 "%s Daylight Time", | 160 "%s Daylight Time", |
161 GuessTimezoneNameFromBias(tzinfo_.Bias)); | 161 GuessTimezoneNameFromBias(tzinfo_.Bias)); |
162 } | 162 } |
163 // Timezone information initialized. | 163 // Timezone information initialized. |
164 initialized_ = true; | 164 initialized_ = true; |
165 } | 165 } |
166 | 166 |
167 // Guess the name of the timezone from the bias. | 167 // Guess the name of the timezone from the bias. |
168 // The guess is very biased towards the northern hemisphere. | 168 // The guess is very biased towards the northern hemisphere. |
169 const char* GuessTimezoneNameFromBias(int bias) { | 169 const char* GuessTimezoneNameFromBias(int bias) { |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } | 544 } |
545 return output_mode == CONSOLE; | 545 return output_mode == CONSOLE; |
546 } | 546 } |
547 | 547 |
548 | 548 |
549 static void VPrintHelper(FILE* stream, const char* format, va_list args) { | 549 static void VPrintHelper(FILE* stream, const char* format, va_list args) { |
550 if ((stream == stdout || stream == stderr) && !HasConsole()) { | 550 if ((stream == stdout || stream == stderr) && !HasConsole()) { |
551 // It is important to use safe print here in order to avoid | 551 // It is important to use safe print here in order to avoid |
552 // overflowing the buffer. We might truncate the output, but this | 552 // overflowing the buffer. We might truncate the output, but this |
553 // does not crash. | 553 // does not crash. |
554 EmbeddedVector<char, 4096> buffer; | 554 char buffer[4096]; |
555 OS::VSNPrintF(buffer, format, args); | 555 OS::VSNPrintF(buffer, sizeof(buffer), format, args); |
556 OutputDebugStringA(buffer.start()); | 556 OutputDebugStringA(buffer); |
557 } else { | 557 } else { |
558 vfprintf(stream, format, args); | 558 vfprintf(stream, format, args); |
559 } | 559 } |
560 } | 560 } |
561 | 561 |
562 | 562 |
563 FILE* OS::FOpen(const char* path, const char* mode) { | 563 FILE* OS::FOpen(const char* path, const char* mode) { |
564 FILE* result; | 564 FILE* result; |
565 if (fopen_s(&result, path, mode) == 0) { | 565 if (fopen_s(&result, path, mode) == 0) { |
566 return result; | 566 return result; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 VPrintError(format, args); | 631 VPrintError(format, args); |
632 va_end(args); | 632 va_end(args); |
633 } | 633 } |
634 | 634 |
635 | 635 |
636 void OS::VPrintError(const char* format, va_list args) { | 636 void OS::VPrintError(const char* format, va_list args) { |
637 VPrintHelper(stderr, format, args); | 637 VPrintHelper(stderr, format, args); |
638 } | 638 } |
639 | 639 |
640 | 640 |
641 int OS::SNPrintF(Vector<char> str, const char* format, ...) { | 641 int OS::SNPrintF(char* str, int length, const char* format, ...) { |
642 va_list args; | 642 va_list args; |
643 va_start(args, format); | 643 va_start(args, format); |
644 int result = VSNPrintF(str, format, args); | 644 int result = VSNPrintF(str, length, format, args); |
645 va_end(args); | 645 va_end(args); |
646 return result; | 646 return result; |
647 } | 647 } |
648 | 648 |
649 | 649 |
650 int OS::VSNPrintF(Vector<char> str, const char* format, va_list args) { | 650 int OS::VSNPrintF(char* str, int length, const char* format, va_list args) { |
651 int n = _vsnprintf_s(str.start(), str.length(), _TRUNCATE, format, args); | 651 int n = _vsnprintf_s(str, length, _TRUNCATE, format, args); |
652 // Make sure to zero-terminate the string if the output was | 652 // Make sure to zero-terminate the string if the output was |
653 // truncated or if there was an error. | 653 // truncated or if there was an error. |
654 if (n < 0 || n >= str.length()) { | 654 if (n < 0 || n >= length) { |
655 if (str.length() > 0) | 655 if (length > 0) |
656 str[str.length() - 1] = '\0'; | 656 str[length - 1] = '\0'; |
657 return -1; | 657 return -1; |
658 } else { | 658 } else { |
659 return n; | 659 return n; |
660 } | 660 } |
661 } | 661 } |
662 | 662 |
663 | 663 |
664 char* OS::StrChr(char* str, int c) { | 664 char* OS::StrChr(char* str, int c) { |
665 return const_cast<char*>(strchr(str, c)); | 665 return const_cast<char*>(strchr(str, c)); |
666 } | 666 } |
667 | 667 |
668 | 668 |
669 void OS::StrNCpy(Vector<char> dest, const char* src, size_t n) { | 669 void OS::StrNCpy(char* dest, int length, const char* src, size_t n) { |
670 // Use _TRUNCATE or strncpy_s crashes (by design) if buffer is too small. | 670 // Use _TRUNCATE or strncpy_s crashes (by design) if buffer is too small. |
671 size_t buffer_size = static_cast<size_t>(dest.length()); | 671 size_t buffer_size = static_cast<size_t>(length); |
672 if (n + 1 > buffer_size) // count for trailing '\0' | 672 if (n + 1 > buffer_size) // count for trailing '\0' |
673 n = _TRUNCATE; | 673 n = _TRUNCATE; |
674 int result = strncpy_s(dest.start(), dest.length(), src, n); | 674 int result = strncpy_s(dest, length, src, n); |
675 USE(result); | 675 USE(result); |
676 ASSERT(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); | 676 ASSERT(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); |
677 } | 677 } |
678 | 678 |
679 | 679 |
680 #undef _TRUNCATE | 680 #undef _TRUNCATE |
681 #undef STRUNCATE | 681 #undef STRUNCATE |
682 | 682 |
683 | 683 |
684 // Get the system's page size used by VirtualAlloc() or the next power | 684 // Get the system's page size used by VirtualAlloc() or the next power |
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1335 | 1335 |
1336 Thread::Thread(const Options& options) | 1336 Thread::Thread(const Options& options) |
1337 : stack_size_(options.stack_size()), | 1337 : stack_size_(options.stack_size()), |
1338 start_semaphore_(NULL) { | 1338 start_semaphore_(NULL) { |
1339 data_ = new PlatformData(kNoThread); | 1339 data_ = new PlatformData(kNoThread); |
1340 set_name(options.name()); | 1340 set_name(options.name()); |
1341 } | 1341 } |
1342 | 1342 |
1343 | 1343 |
1344 void Thread::set_name(const char* name) { | 1344 void Thread::set_name(const char* name) { |
1345 OS::StrNCpy(Vector<char>(name_, sizeof(name_)), name, strlen(name)); | 1345 OS::StrNCpy(name_, sizeof(name_), name, strlen(name)); |
1346 name_[sizeof(name_) - 1] = '\0'; | 1346 name_[sizeof(name_) - 1] = '\0'; |
1347 } | 1347 } |
1348 | 1348 |
1349 | 1349 |
1350 // Close our own handle for the thread. | 1350 // Close our own handle for the thread. |
1351 Thread::~Thread() { | 1351 Thread::~Thread() { |
1352 if (data_->thread_ != kNoThread) CloseHandle(data_->thread_); | 1352 if (data_->thread_ != kNoThread) CloseHandle(data_->thread_); |
1353 delete data_; | 1353 delete data_; |
1354 } | 1354 } |
1355 | 1355 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 ASSERT(result); | 1401 ASSERT(result); |
1402 } | 1402 } |
1403 | 1403 |
1404 | 1404 |
1405 | 1405 |
1406 void Thread::YieldCPU() { | 1406 void Thread::YieldCPU() { |
1407 Sleep(0); | 1407 Sleep(0); |
1408 } | 1408 } |
1409 | 1409 |
1410 } } // namespace v8::internal | 1410 } } // namespace v8::internal |
OLD | NEW |