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 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 free(symbols); | 326 free(symbols); |
327 | 327 |
328 return frames_count; | 328 return frames_count; |
329 } | 329 } |
330 | 330 |
331 | 331 |
332 // Constants used for mmap. | 332 // Constants used for mmap. |
333 static const int kMmapFd = -1; | 333 static const int kMmapFd = -1; |
334 static const int kMmapFdOffset = 0; | 334 static const int kMmapFdOffset = 0; |
335 | 335 |
| 336 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } |
336 | 337 |
337 VirtualMemory::VirtualMemory(size_t size) { | 338 VirtualMemory::VirtualMemory(size_t size) { |
338 address_ = mmap(NULL, size, PROT_NONE, | 339 address_ = ReserveRegion(size); |
339 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
340 kMmapFd, kMmapFdOffset); | |
341 size_ = size; | 340 size_ = size; |
342 } | 341 } |
343 | 342 |
344 | 343 |
| 344 VirtualMemory::VirtualMemory(size_t size, size_t alignment) |
| 345 : address_(NULL), size_(0) { |
| 346 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
| 347 size_t request_size = RoundUp(size + alignment, |
| 348 static_cast<intptr_t>(OS::AllocateAlignment())); |
| 349 void* reservation = mmap(OS::GetRandomMmapAddr(), |
| 350 request_size, |
| 351 PROT_NONE, |
| 352 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 353 kMmapFd, |
| 354 kMmapFdOffset); |
| 355 if (reservation == MAP_FAILED) return; |
| 356 |
| 357 Address base = static_cast<Address>(reservation); |
| 358 Address aligned_base = RoundUp(base, alignment); |
| 359 ASSERT_LE(base, aligned_base); |
| 360 |
| 361 // Unmap extra memory reserved before and after the desired block. |
| 362 if (aligned_base != base) { |
| 363 size_t prefix_size = static_cast<size_t>(aligned_base - base); |
| 364 OS::Free(base, prefix_size); |
| 365 request_size -= prefix_size; |
| 366 } |
| 367 |
| 368 size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); |
| 369 ASSERT_LE(aligned_size, request_size); |
| 370 |
| 371 if (aligned_size != request_size) { |
| 372 size_t suffix_size = request_size - aligned_size; |
| 373 OS::Free(aligned_base + aligned_size, suffix_size); |
| 374 request_size -= suffix_size; |
| 375 } |
| 376 |
| 377 ASSERT(aligned_size == request_size); |
| 378 |
| 379 address_ = static_cast<void*>(aligned_base); |
| 380 size_ = aligned_size; |
| 381 } |
| 382 |
| 383 |
345 VirtualMemory::~VirtualMemory() { | 384 VirtualMemory::~VirtualMemory() { |
346 if (IsReserved()) { | 385 if (IsReserved()) { |
347 if (0 == munmap(address(), size())) address_ = MAP_FAILED; | 386 bool result = ReleaseRegion(address(), size()); |
| 387 ASSERT(result); |
| 388 USE(result); |
348 } | 389 } |
349 } | 390 } |
350 | 391 |
351 | 392 |
352 bool VirtualMemory::IsReserved() { | 393 bool VirtualMemory::IsReserved() { |
353 return address_ != MAP_FAILED; | 394 return address_ != NULL; |
354 } | 395 } |
355 | 396 |
356 | 397 |
357 bool VirtualMemory::Commit(void* address, size_t size, bool executable) { | 398 void VirtualMemory::Reset() { |
358 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 399 address_ = NULL; |
359 if (MAP_FAILED == mmap(address, size, prot, | 400 size_ = 0; |
| 401 } |
| 402 |
| 403 |
| 404 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 405 return CommitRegion(address, size, is_executable); |
| 406 } |
| 407 |
| 408 |
| 409 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 410 return UncommitRegion(address, size); |
| 411 } |
| 412 |
| 413 |
| 414 void* VirtualMemory::ReserveRegion(size_t size) { |
| 415 void* result = mmap(OS::GetRandomMmapAddr(), |
| 416 size, |
| 417 PROT_NONE, |
| 418 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 419 kMmapFd, |
| 420 kMmapFdOffset); |
| 421 |
| 422 if (result == MAP_FAILED) return NULL; |
| 423 |
| 424 return result; |
| 425 } |
| 426 |
| 427 |
| 428 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
| 429 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 430 if (MAP_FAILED == mmap(base, |
| 431 size, |
| 432 prot, |
360 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | 433 MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
361 kMmapFd, kMmapFdOffset)) { | 434 kMmapFd, |
| 435 kMmapFdOffset)) { |
362 return false; | 436 return false; |
363 } | 437 } |
364 | 438 |
365 UpdateAllocatedSpaceLimits(address, size); | 439 UpdateAllocatedSpaceLimits(base, size); |
366 return true; | 440 return true; |
367 } | 441 } |
368 | 442 |
369 | 443 |
370 bool VirtualMemory::Uncommit(void* address, size_t size) { | 444 bool VirtualMemory::UncommitRegion(void* base, size_t size) { |
371 return mmap(address, size, PROT_NONE, | 445 return mmap(base, |
| 446 size, |
| 447 PROT_NONE, |
372 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, | 448 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, |
373 kMmapFd, kMmapFdOffset) != MAP_FAILED; | 449 kMmapFd, |
| 450 kMmapFdOffset) != MAP_FAILED; |
374 } | 451 } |
375 | 452 |
376 | 453 |
| 454 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { |
| 455 return munmap(base, size) == 0; |
| 456 } |
| 457 |
| 458 |
377 class Thread::PlatformData : public Malloced { | 459 class Thread::PlatformData : public Malloced { |
378 public: | 460 public: |
379 pthread_t thread_; // Thread handle for pthread. | 461 pthread_t thread_; // Thread handle for pthread. |
380 }; | 462 }; |
381 | 463 |
382 | 464 |
383 Thread::Thread(const Options& options) | 465 Thread::Thread(const Options& options) |
384 : data_(new PlatformData), | 466 : data_(new PlatformData), |
385 stack_size_(options.stack_size) { | 467 stack_size_(options.stack_size) { |
386 set_name(options.name); | 468 set_name(options.name); |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 873 |
792 | 874 |
793 void Sampler::Stop() { | 875 void Sampler::Stop() { |
794 ASSERT(IsActive()); | 876 ASSERT(IsActive()); |
795 SignalSender::RemoveActiveSampler(this); | 877 SignalSender::RemoveActiveSampler(this); |
796 SetActive(false); | 878 SetActive(false); |
797 } | 879 } |
798 | 880 |
799 | 881 |
800 } } // namespace v8::internal | 882 } } // namespace v8::internal |
OLD | NEW |