| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 } | 64 } |
| 65 | 65 |
| 66 | 66 |
| 67 int fopen_s(FILE** pFile, const char* filename, const char* mode) { | 67 int fopen_s(FILE** pFile, const char* filename, const char* mode) { |
| 68 *pFile = fopen(filename, mode); | 68 *pFile = fopen(filename, mode); |
| 69 return *pFile != NULL ? 0 : 1; | 69 return *pFile != NULL ? 0 : 1; |
| 70 } | 70 } |
| 71 | 71 |
| 72 int _vsnprintf_s(char* buffer, size_t sizeOfBuffer, size_t count, | 72 int _vsnprintf_s(char* buffer, size_t sizeOfBuffer, size_t count, |
| 73 const char* format, va_list argptr) { | 73 const char* format, va_list argptr) { |
| 74 ASSERT(count == _TRUNCATE); | 74 DCHECK(count == _TRUNCATE); |
| 75 return _vsnprintf(buffer, sizeOfBuffer, format, argptr); | 75 return _vsnprintf(buffer, sizeOfBuffer, format, argptr); |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) { | 79 int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) { |
| 80 CHECK(source != NULL); | 80 CHECK(source != NULL); |
| 81 CHECK(dest != NULL); | 81 CHECK(dest != NULL); |
| 82 CHECK_GT(dest_size, 0); | 82 CHECK_GT(dest_size, 0); |
| 83 | 83 |
| 84 if (count == _TRUNCATE) { | 84 if (count == _TRUNCATE) { |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 } | 682 } |
| 683 | 683 |
| 684 | 684 |
| 685 void OS::StrNCpy(char* dest, int length, const char* src, size_t n) { | 685 void OS::StrNCpy(char* dest, int length, const char* src, size_t n) { |
| 686 // Use _TRUNCATE or strncpy_s crashes (by design) if buffer is too small. | 686 // Use _TRUNCATE or strncpy_s crashes (by design) if buffer is too small. |
| 687 size_t buffer_size = static_cast<size_t>(length); | 687 size_t buffer_size = static_cast<size_t>(length); |
| 688 if (n + 1 > buffer_size) // count for trailing '\0' | 688 if (n + 1 > buffer_size) // count for trailing '\0' |
| 689 n = _TRUNCATE; | 689 n = _TRUNCATE; |
| 690 int result = strncpy_s(dest, length, src, n); | 690 int result = strncpy_s(dest, length, src, n); |
| 691 USE(result); | 691 USE(result); |
| 692 ASSERT(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); | 692 DCHECK(result == 0 || (n == _TRUNCATE && result == STRUNCATE)); |
| 693 } | 693 } |
| 694 | 694 |
| 695 | 695 |
| 696 #undef _TRUNCATE | 696 #undef _TRUNCATE |
| 697 #undef STRUNCATE | 697 #undef STRUNCATE |
| 698 | 698 |
| 699 | 699 |
| 700 // Get the system's page size used by VirtualAlloc() or the next power | 700 // Get the system's page size used by VirtualAlloc() or the next power |
| 701 // of two. The reason for always returning a power of two is that the | 701 // of two. The reason for always returning a power of two is that the |
| 702 // rounding up in OS::Allocate expects that. | 702 // rounding up in OS::Allocate expects that. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 | 783 |
| 784 // Windows XP SP2 allows Data Excution Prevention (DEP). | 784 // Windows XP SP2 allows Data Excution Prevention (DEP). |
| 785 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; | 785 int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; |
| 786 | 786 |
| 787 LPVOID mbase = RandomizedVirtualAlloc(msize, | 787 LPVOID mbase = RandomizedVirtualAlloc(msize, |
| 788 MEM_COMMIT | MEM_RESERVE, | 788 MEM_COMMIT | MEM_RESERVE, |
| 789 prot); | 789 prot); |
| 790 | 790 |
| 791 if (mbase == NULL) return NULL; | 791 if (mbase == NULL) return NULL; |
| 792 | 792 |
| 793 ASSERT(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment())); | 793 DCHECK(IsAligned(reinterpret_cast<size_t>(mbase), OS::AllocateAlignment())); |
| 794 | 794 |
| 795 *allocated = msize; | 795 *allocated = msize; |
| 796 return mbase; | 796 return mbase; |
| 797 } | 797 } |
| 798 | 798 |
| 799 | 799 |
| 800 void OS::Free(void* address, const size_t size) { | 800 void OS::Free(void* address, const size_t size) { |
| 801 // TODO(1240712): VirtualFree has a return value which is ignored here. | 801 // TODO(1240712): VirtualFree has a return value which is ignored here. |
| 802 VirtualFree(address, 0, MEM_RELEASE); | 802 VirtualFree(address, 0, MEM_RELEASE); |
| 803 USE(size); | 803 USE(size); |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1221 | 1221 |
| 1222 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } | 1222 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
| 1223 | 1223 |
| 1224 | 1224 |
| 1225 VirtualMemory::VirtualMemory(size_t size) | 1225 VirtualMemory::VirtualMemory(size_t size) |
| 1226 : address_(ReserveRegion(size)), size_(size) { } | 1226 : address_(ReserveRegion(size)), size_(size) { } |
| 1227 | 1227 |
| 1228 | 1228 |
| 1229 VirtualMemory::VirtualMemory(size_t size, size_t alignment) | 1229 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 1230 : address_(NULL), size_(0) { | 1230 : address_(NULL), size_(0) { |
| 1231 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); | 1231 DCHECK(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 1232 size_t request_size = RoundUp(size + alignment, | 1232 size_t request_size = RoundUp(size + alignment, |
| 1233 static_cast<intptr_t>(OS::AllocateAlignment())); | 1233 static_cast<intptr_t>(OS::AllocateAlignment())); |
| 1234 void* address = ReserveRegion(request_size); | 1234 void* address = ReserveRegion(request_size); |
| 1235 if (address == NULL) return; | 1235 if (address == NULL) return; |
| 1236 uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment); | 1236 uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment); |
| 1237 // Try reducing the size by freeing and then reallocating a specific area. | 1237 // Try reducing the size by freeing and then reallocating a specific area. |
| 1238 bool result = ReleaseRegion(address, request_size); | 1238 bool result = ReleaseRegion(address, request_size); |
| 1239 USE(result); | 1239 USE(result); |
| 1240 ASSERT(result); | 1240 DCHECK(result); |
| 1241 address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS); | 1241 address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS); |
| 1242 if (address != NULL) { | 1242 if (address != NULL) { |
| 1243 request_size = size; | 1243 request_size = size; |
| 1244 ASSERT(base == static_cast<uint8_t*>(address)); | 1244 DCHECK(base == static_cast<uint8_t*>(address)); |
| 1245 } else { | 1245 } else { |
| 1246 // Resizing failed, just go with a bigger area. | 1246 // Resizing failed, just go with a bigger area. |
| 1247 address = ReserveRegion(request_size); | 1247 address = ReserveRegion(request_size); |
| 1248 if (address == NULL) return; | 1248 if (address == NULL) return; |
| 1249 } | 1249 } |
| 1250 address_ = address; | 1250 address_ = address; |
| 1251 size_ = request_size; | 1251 size_ = request_size; |
| 1252 } | 1252 } |
| 1253 | 1253 |
| 1254 | 1254 |
| 1255 VirtualMemory::~VirtualMemory() { | 1255 VirtualMemory::~VirtualMemory() { |
| 1256 if (IsReserved()) { | 1256 if (IsReserved()) { |
| 1257 bool result = ReleaseRegion(address(), size()); | 1257 bool result = ReleaseRegion(address(), size()); |
| 1258 ASSERT(result); | 1258 DCHECK(result); |
| 1259 USE(result); | 1259 USE(result); |
| 1260 } | 1260 } |
| 1261 } | 1261 } |
| 1262 | 1262 |
| 1263 | 1263 |
| 1264 bool VirtualMemory::IsReserved() { | 1264 bool VirtualMemory::IsReserved() { |
| 1265 return address_ != NULL; | 1265 return address_ != NULL; |
| 1266 } | 1266 } |
| 1267 | 1267 |
| 1268 | 1268 |
| 1269 void VirtualMemory::Reset() { | 1269 void VirtualMemory::Reset() { |
| 1270 address_ = NULL; | 1270 address_ = NULL; |
| 1271 size_ = 0; | 1271 size_ = 0; |
| 1272 } | 1272 } |
| 1273 | 1273 |
| 1274 | 1274 |
| 1275 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | 1275 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 1276 return CommitRegion(address, size, is_executable); | 1276 return CommitRegion(address, size, is_executable); |
| 1277 } | 1277 } |
| 1278 | 1278 |
| 1279 | 1279 |
| 1280 bool VirtualMemory::Uncommit(void* address, size_t size) { | 1280 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 1281 ASSERT(IsReserved()); | 1281 DCHECK(IsReserved()); |
| 1282 return UncommitRegion(address, size); | 1282 return UncommitRegion(address, size); |
| 1283 } | 1283 } |
| 1284 | 1284 |
| 1285 | 1285 |
| 1286 bool VirtualMemory::Guard(void* address) { | 1286 bool VirtualMemory::Guard(void* address) { |
| 1287 if (NULL == VirtualAlloc(address, | 1287 if (NULL == VirtualAlloc(address, |
| 1288 OS::CommitPageSize(), | 1288 OS::CommitPageSize(), |
| 1289 MEM_COMMIT, | 1289 MEM_COMMIT, |
| 1290 PAGE_NOACCESS)) { | 1290 PAGE_NOACCESS)) { |
| 1291 return false; | 1291 return false; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1390 // Wait for thread to terminate. | 1390 // Wait for thread to terminate. |
| 1391 void Thread::Join() { | 1391 void Thread::Join() { |
| 1392 if (data_->thread_id_ != GetCurrentThreadId()) { | 1392 if (data_->thread_id_ != GetCurrentThreadId()) { |
| 1393 WaitForSingleObject(data_->thread_, INFINITE); | 1393 WaitForSingleObject(data_->thread_, INFINITE); |
| 1394 } | 1394 } |
| 1395 } | 1395 } |
| 1396 | 1396 |
| 1397 | 1397 |
| 1398 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { | 1398 Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
| 1399 DWORD result = TlsAlloc(); | 1399 DWORD result = TlsAlloc(); |
| 1400 ASSERT(result != TLS_OUT_OF_INDEXES); | 1400 DCHECK(result != TLS_OUT_OF_INDEXES); |
| 1401 return static_cast<LocalStorageKey>(result); | 1401 return static_cast<LocalStorageKey>(result); |
| 1402 } | 1402 } |
| 1403 | 1403 |
| 1404 | 1404 |
| 1405 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { | 1405 void Thread::DeleteThreadLocalKey(LocalStorageKey key) { |
| 1406 BOOL result = TlsFree(static_cast<DWORD>(key)); | 1406 BOOL result = TlsFree(static_cast<DWORD>(key)); |
| 1407 USE(result); | 1407 USE(result); |
| 1408 ASSERT(result); | 1408 DCHECK(result); |
| 1409 } | 1409 } |
| 1410 | 1410 |
| 1411 | 1411 |
| 1412 void* Thread::GetThreadLocal(LocalStorageKey key) { | 1412 void* Thread::GetThreadLocal(LocalStorageKey key) { |
| 1413 return TlsGetValue(static_cast<DWORD>(key)); | 1413 return TlsGetValue(static_cast<DWORD>(key)); |
| 1414 } | 1414 } |
| 1415 | 1415 |
| 1416 | 1416 |
| 1417 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { | 1417 void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
| 1418 BOOL result = TlsSetValue(static_cast<DWORD>(key), value); | 1418 BOOL result = TlsSetValue(static_cast<DWORD>(key), value); |
| 1419 USE(result); | 1419 USE(result); |
| 1420 ASSERT(result); | 1420 DCHECK(result); |
| 1421 } | 1421 } |
| 1422 | 1422 |
| 1423 | 1423 |
| 1424 | 1424 |
| 1425 void Thread::YieldCPU() { | 1425 void Thread::YieldCPU() { |
| 1426 Sleep(0); | 1426 Sleep(0); |
| 1427 } | 1427 } |
| 1428 | 1428 |
| 1429 } } // namespace v8::base | 1429 } } // namespace v8::base |
| OLD | NEW |