Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "mark_stack.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 4 | |
| 5 | #include <sys/mman.h> |
| 6 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | #include "globals.h" |
| 8 | #include "logging.h" |
| 9 | #include "scoped_ptr.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | MarkStack* MarkStack::Create(size_t maximum_size) { |
| 14 | scoped_ptr<MarkStack> mark_stack(new MarkStack()); |
| 15 | bool success = mark_stack->Init(maximum_size); |
| 16 | if (!success) { |
| 17 | return NULL; |
| 18 | } else { |
| 19 | return mark_stack.release(); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | bool MarkStack::Init(size_t maximum_size) { |
| 24 | size_t length = 64 * MB; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 25 | void* addr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 26 | if (addr == MAP_FAILED) { |
| 27 | PLOG(ERROR) << "mmap failed"; |
| 28 | return false; |
| 29 | } |
| 30 | base_ = reinterpret_cast<const Object**>(addr); |
| 31 | limit_ = reinterpret_cast<const Object**>((byte*)addr + length); |
| 32 | ptr_ = reinterpret_cast<Object const**>(addr); |
| 33 | int result = madvise(addr, length, MADV_DONTNEED); |
| 34 | if (result == -1) { |
| 35 | PLOG(WARNING) << "madvise failed"; |
| 36 | } |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | MarkStack::~MarkStack() { |
| 41 | int result = munmap((void*)base_, limit_ - base_); |
| 42 | if (result == -1) { |
| 43 | PLOG(WARNING) << "munmap failed"; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | } // namespace art |