| 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 24 matching lines...) Expand all Loading... |
| 35 #include <mach-o/getsect.h> | 35 #include <mach-o/getsect.h> |
| 36 | 36 |
| 37 #include <AvailabilityMacros.h> | 37 #include <AvailabilityMacros.h> |
| 38 | 38 |
| 39 #include <pthread.h> | 39 #include <pthread.h> |
| 40 #include <semaphore.h> | 40 #include <semaphore.h> |
| 41 #include <signal.h> | 41 #include <signal.h> |
| 42 #include <mach/mach.h> | 42 #include <mach/mach.h> |
| 43 #include <mach/semaphore.h> | 43 #include <mach/semaphore.h> |
| 44 #include <mach/task.h> | 44 #include <mach/task.h> |
| 45 #include <mach/vm_statistics.h> |
| 45 #include <sys/time.h> | 46 #include <sys/time.h> |
| 46 #include <sys/resource.h> | 47 #include <sys/resource.h> |
| 47 #include <sys/types.h> | 48 #include <sys/types.h> |
| 48 #include <stdarg.h> | 49 #include <stdarg.h> |
| 49 #include <stdlib.h> | 50 #include <stdlib.h> |
| 50 | 51 |
| 51 #include <errno.h> | 52 #include <errno.h> |
| 52 | 53 |
| 53 #undef MAP_TYPE | 54 #undef MAP_TYPE |
| 54 | 55 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 size_t OS::AllocateAlignment() { | 122 size_t OS::AllocateAlignment() { |
| 122 return getpagesize(); | 123 return getpagesize(); |
| 123 } | 124 } |
| 124 | 125 |
| 125 | 126 |
| 126 void* OS::Allocate(const size_t requested, | 127 void* OS::Allocate(const size_t requested, |
| 127 size_t* allocated, | 128 size_t* allocated, |
| 128 bool is_executable) { | 129 bool is_executable) { |
| 129 const size_t msize = RoundUp(requested, getpagesize()); | 130 const size_t msize = RoundUp(requested, getpagesize()); |
| 130 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 131 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 131 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); | 132 // Use the fd parameter to pass vm_alloc flags to tag the region with the |
| 133 // user-defined tag 255. This helps identify V8-allocated regions in memory |
| 134 // analysis tools like vmmap(1). |
| 135 void* mbase = mmap(NULL, msize, prot, |
| 136 MAP_PRIVATE | MAP_ANON, |
| 137 VM_MAKE_TAG(255), 0); |
| 132 if (mbase == MAP_FAILED) { | 138 if (mbase == MAP_FAILED) { |
| 133 LOG(StringEvent("OS::Allocate", "mmap failed")); | 139 LOG(StringEvent("OS::Allocate", "mmap failed")); |
| 134 return NULL; | 140 return NULL; |
| 135 } | 141 } |
| 136 *allocated = msize; | 142 *allocated = msize; |
| 137 UpdateAllocatedSpaceLimits(mbase, msize); | 143 UpdateAllocatedSpaceLimits(mbase, msize); |
| 138 return mbase; | 144 return mbase; |
| 139 } | 145 } |
| 140 | 146 |
| 141 | 147 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 void* memory_; | 194 void* memory_; |
| 189 int size_; | 195 int size_; |
| 190 }; | 196 }; |
| 191 | 197 |
| 192 | 198 |
| 193 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, | 199 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
| 194 void* initial) { | 200 void* initial) { |
| 195 FILE* file = fopen(name, "w+"); | 201 FILE* file = fopen(name, "w+"); |
| 196 if (file == NULL) return NULL; | 202 if (file == NULL) return NULL; |
| 197 fwrite(initial, size, 1, file); | 203 fwrite(initial, size, 1, file); |
| 198 void* memory = | 204 void* memory = mmap(0, size, PROT_READ | PROT_WRITE, |
| 199 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); | 205 MAP_SHARED, |
| 206 fileno(file), 0); |
| 200 return new PosixMemoryMappedFile(file, memory, size); | 207 return new PosixMemoryMappedFile(file, memory, size); |
| 201 } | 208 } |
| 202 | 209 |
| 203 | 210 |
| 204 PosixMemoryMappedFile::~PosixMemoryMappedFile() { | 211 PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
| 205 if (memory_) munmap(memory_, size_); | 212 if (memory_) munmap(memory_, size_); |
| 206 fclose(file_); | 213 fclose(file_); |
| 207 } | 214 } |
| 208 | 215 |
| 209 | 216 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; | 280 frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
| 274 } | 281 } |
| 275 | 282 |
| 276 DeleteArray(addresses); | 283 DeleteArray(addresses); |
| 277 free(symbols); | 284 free(symbols); |
| 278 | 285 |
| 279 return frames_count; | 286 return frames_count; |
| 280 } | 287 } |
| 281 | 288 |
| 282 | 289 |
| 283 // Constants used for mmap. | 290 // Constants used for mmap calls by the VirtualMemory class. |
| 284 static const int kMmapFd = -1; | 291 // kMmapFd is used to pass vm_alloc flags to tag the region with the user |
| 292 // defined tag 254. This helps identify V8-allocated regions in memory analysis |
| 293 // tools like vmmap(1). |
| 294 static const int kMmapFlags = MAP_PRIVATE | MAP_ANON; |
| 295 static const int kMmapFd = VM_MAKE_TAG(254); |
| 285 static const int kMmapFdOffset = 0; | 296 static const int kMmapFdOffset = 0; |
| 286 | 297 |
| 287 | 298 |
| 288 VirtualMemory::VirtualMemory(size_t size) { | 299 VirtualMemory::VirtualMemory(size_t size) { |
| 289 address_ = mmap(NULL, size, PROT_NONE, | 300 address_ = mmap(NULL, size, PROT_NONE, |
| 290 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | 301 kMmapFlags | MAP_NORESERVE, |
| 291 kMmapFd, kMmapFdOffset); | 302 kMmapFd, kMmapFdOffset); |
| 292 size_ = size; | 303 size_ = size; |
| 293 } | 304 } |
| 294 | 305 |
| 295 | 306 |
| 296 VirtualMemory::~VirtualMemory() { | 307 VirtualMemory::~VirtualMemory() { |
| 297 if (IsReserved()) { | 308 if (IsReserved()) { |
| 298 if (0 == munmap(address(), size())) address_ = MAP_FAILED; | 309 if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
| 299 } | 310 } |
| 300 } | 311 } |
| 301 | 312 |
| 302 | 313 |
| 303 bool VirtualMemory::IsReserved() { | 314 bool VirtualMemory::IsReserved() { |
| 304 return address_ != MAP_FAILED; | 315 return address_ != MAP_FAILED; |
| 305 } | 316 } |
| 306 | 317 |
| 307 | 318 |
| 308 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | 319 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
| 309 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | 320 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
| 310 if (MAP_FAILED == mmap(address, size, prot, | 321 if (MAP_FAILED == mmap(address, size, prot, |
| 311 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | 322 kMmapFlags | MAP_FIXED, |
| 312 kMmapFd, kMmapFdOffset)) { | 323 kMmapFd, kMmapFdOffset)) { |
| 313 return false; | 324 return false; |
| 314 } | 325 } |
| 315 | 326 |
| 316 UpdateAllocatedSpaceLimits(address, size); | 327 UpdateAllocatedSpaceLimits(address, size); |
| 317 return true; | 328 return true; |
| 318 } | 329 } |
| 319 | 330 |
| 320 | 331 |
| 321 bool VirtualMemory::Uncommit(void* address, size_t size) { | 332 bool VirtualMemory::Uncommit(void* address, size_t size) { |
| 322 return mmap(address, size, PROT_NONE, | 333 return mmap(address, size, PROT_NONE, |
| 323 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, | 334 kMmapFlags | MAP_NORESERVE | MAP_FIXED, |
| 324 kMmapFd, kMmapFdOffset) != MAP_FAILED; | 335 kMmapFd, kMmapFdOffset) != MAP_FAILED; |
| 325 } | 336 } |
| 326 | 337 |
| 327 | 338 |
| 328 class ThreadHandle::PlatformData : public Malloced { | 339 class ThreadHandle::PlatformData : public Malloced { |
| 329 public: | 340 public: |
| 330 explicit PlatformData(ThreadHandle::Kind kind) { | 341 explicit PlatformData(ThreadHandle::Kind kind) { |
| 331 Initialize(kind); | 342 Initialize(kind); |
| 332 } | 343 } |
| 333 | 344 |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 | 634 |
| 624 // Deallocate Mach port for thread. | 635 // Deallocate Mach port for thread. |
| 625 if (IsProfiling()) { | 636 if (IsProfiling()) { |
| 626 mach_port_deallocate(data_->task_self_, data_->profiled_thread_); | 637 mach_port_deallocate(data_->task_self_, data_->profiled_thread_); |
| 627 } | 638 } |
| 628 } | 639 } |
| 629 | 640 |
| 630 #endif // ENABLE_LOGGING_AND_PROFILING | 641 #endif // ENABLE_LOGGING_AND_PROFILING |
| 631 | 642 |
| 632 } } // namespace v8::internal | 643 } } // namespace v8::internal |
| OLD | NEW |