OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 double OS::LocalTimeOffset() { | 73 double OS::LocalTimeOffset() { |
74 time_t tv = time(NULL); | 74 time_t tv = time(NULL); |
75 struct tm* t = localtime(&tv); | 75 struct tm* t = localtime(&tv); |
76 // tm_gmtoff includes any daylight savings offset, so subtract it. | 76 // tm_gmtoff includes any daylight savings offset, so subtract it. |
77 return static_cast<double>(t->tm_gmtoff * msPerSecond - | 77 return static_cast<double>(t->tm_gmtoff * msPerSecond - |
78 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); | 78 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); |
79 } | 79 } |
80 | 80 |
81 | 81 |
82 void* OS::Allocate(const size_t requested, | |
83 size_t* allocated, | |
84 bool is_executable) { | |
85 const size_t msize = RoundUp(requested, AllocateAlignment()); | |
86 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | |
87 void* addr = OS::GetRandomMmapAddr(); | |
88 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); | |
89 if (mbase == MAP_FAILED) { | |
90 LOG(i::Isolate::Current(), | |
91 StringEvent("OS::Allocate", "mmap failed")); | |
92 return NULL; | |
93 } | |
94 *allocated = msize; | |
95 return mbase; | |
96 } | |
97 | |
98 | |
99 void OS::DumpBacktrace() { | 82 void OS::DumpBacktrace() { |
100 // Currently unsupported. | 83 // Currently unsupported. |
101 } | 84 } |
102 | 85 |
103 | 86 |
104 class PosixMemoryMappedFile : public OS::MemoryMappedFile { | 87 class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
105 public: | 88 public: |
106 PosixMemoryMappedFile(FILE* file, void* memory, int size) | 89 PosixMemoryMappedFile(FILE* file, void* memory, int size) |
107 : file_(file), memory_(memory), size_(size) { } | 90 : file_(file), memory_(memory), size_(size) { } |
108 virtual ~PosixMemoryMappedFile(); | 91 virtual ~PosixMemoryMappedFile(); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 symbols[i]); | 236 symbols[i]); |
254 // Make sure line termination is in place. | 237 // Make sure line termination is in place. |
255 frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; | 238 frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
256 } | 239 } |
257 | 240 |
258 free(symbols); | 241 free(symbols); |
259 | 242 |
260 return frames_count; | 243 return frames_count; |
261 } | 244 } |
262 | 245 |
263 | |
264 // Constants used for mmap. | |
265 static const int kMmapFd = -1; | |
266 static const int kMmapFdOffset = 0; | |
267 | |
268 | |
269 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { } | |
270 | |
271 | |
272 VirtualMemory::VirtualMemory(size_t size) | |
273 : address_(ReserveRegion(size)), size_(size) { } | |
274 | |
275 | |
276 VirtualMemory::VirtualMemory(size_t size, size_t alignment) | |
277 : address_(NULL), size_(0) { | |
278 ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); | |
279 size_t request_size = RoundUp(size + alignment, | |
280 static_cast<intptr_t>(OS::AllocateAlignment())); | |
281 void* reservation = mmap(OS::GetRandomMmapAddr(), | |
282 request_size, | |
283 PROT_NONE, | |
284 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
285 kMmapFd, | |
286 kMmapFdOffset); | |
287 if (reservation == MAP_FAILED) return; | |
288 | |
289 Address base = static_cast<Address>(reservation); | |
290 Address aligned_base = RoundUp(base, alignment); | |
291 ASSERT_LE(base, aligned_base); | |
292 | |
293 // Unmap extra memory reserved before and after the desired block. | |
294 if (aligned_base != base) { | |
295 size_t prefix_size = static_cast<size_t>(aligned_base - base); | |
296 OS::Free(base, prefix_size); | |
297 request_size -= prefix_size; | |
298 } | |
299 | |
300 size_t aligned_size = RoundUp(size, OS::AllocateAlignment()); | |
301 ASSERT_LE(aligned_size, request_size); | |
302 | |
303 if (aligned_size != request_size) { | |
304 size_t suffix_size = request_size - aligned_size; | |
305 OS::Free(aligned_base + aligned_size, suffix_size); | |
306 request_size -= suffix_size; | |
307 } | |
308 | |
309 ASSERT(aligned_size == request_size); | |
310 | |
311 address_ = static_cast<void*>(aligned_base); | |
312 size_ = aligned_size; | |
313 } | |
314 | |
315 | |
316 VirtualMemory::~VirtualMemory() { | |
317 if (IsReserved()) { | |
318 bool result = ReleaseRegion(address(), size()); | |
319 ASSERT(result); | |
320 USE(result); | |
321 } | |
322 } | |
323 | |
324 | |
325 bool VirtualMemory::IsReserved() { | |
326 return address_ != NULL; | |
327 } | |
328 | |
329 | |
330 void VirtualMemory::Reset() { | |
331 address_ = NULL; | |
332 size_ = 0; | |
333 } | |
334 | |
335 | |
336 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { | |
337 return CommitRegion(address, size, is_executable); | |
338 } | |
339 | |
340 | |
341 bool VirtualMemory::Uncommit(void* address, size_t size) { | |
342 return UncommitRegion(address, size); | |
343 } | |
344 | |
345 | |
346 bool VirtualMemory::Guard(void* address) { | |
347 OS::Guard(address, OS::CommitPageSize()); | |
348 return true; | |
349 } | |
350 | |
351 | |
352 void* VirtualMemory::ReserveRegion(size_t size) { | |
353 void* result = mmap(OS::GetRandomMmapAddr(), | |
354 size, | |
355 PROT_NONE, | |
356 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, | |
357 kMmapFd, | |
358 kMmapFdOffset); | |
359 | |
360 if (result == MAP_FAILED) return NULL; | |
361 | |
362 return result; | |
363 } | |
364 | |
365 | |
366 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { | |
367 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); | |
368 if (MAP_FAILED == mmap(base, | |
369 size, | |
370 prot, | |
371 MAP_PRIVATE | MAP_ANON | MAP_FIXED, | |
372 kMmapFd, | |
373 kMmapFdOffset)) { | |
374 return false; | |
375 } | |
376 return true; | |
377 } | |
378 | |
379 | |
380 bool VirtualMemory::UncommitRegion(void* base, size_t size) { | |
381 return mmap(base, | |
382 size, | |
383 PROT_NONE, | |
384 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED, | |
385 kMmapFd, | |
386 kMmapFdOffset) != MAP_FAILED; | |
387 } | |
388 | |
389 | |
390 bool VirtualMemory::ReleaseRegion(void* base, size_t size) { | |
391 return munmap(base, size) == 0; | |
392 } | |
393 | |
394 | |
395 bool VirtualMemory::HasLazyCommits() { | |
396 // TODO(alph): implement for the platform. | |
397 return false; | |
398 } | |
399 | |
400 } } // namespace v8::internal | 246 } } // namespace v8::internal |
OLD | NEW |