Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "mark_stack.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 19 | #include "globals.h" |
| 20 | #include "logging.h" |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 21 | #include "UniquePtr.h" |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 22 | #include "utils.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 26 | MarkStack* MarkStack::Create(const std::string& name, size_t length) { |
| 27 | UniquePtr<MarkStack> mark_stack(new MarkStack(name)); |
| 28 | mark_stack->Init(length); |
Jesse Wilson | 078f9b0 | 2011-11-18 17:51:47 -0500 | [diff] [blame] | 29 | return mark_stack.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 32 | void MarkStack::Init(size_t length) { |
| 33 | mem_map_.reset(MemMap::MapAnonymous(name_.c_str(), NULL, length, PROT_READ | PROT_WRITE)); |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 34 | if (mem_map_.get() == NULL) { |
| 35 | std::string maps; |
| 36 | ReadFileToString("/proc/self/maps", &maps); |
| 37 | LOG(FATAL) << "couldn't allocate mark stack\n" << maps; |
| 38 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 39 | byte* addr = mem_map_->Begin(); |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 40 | CHECK(addr != NULL); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 41 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 42 | begin_ = reinterpret_cast<const Object**>(addr); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 43 | limit_ = reinterpret_cast<const Object**>(addr + length); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 44 | |
| 45 | Reset(); |
| 46 | } |
| 47 | |
| 48 | void MarkStack::Reset() { |
| 49 | DCHECK(mem_map_.get() != NULL); |
| 50 | DCHECK(begin_ != NULL); |
| 51 | DCHECK(limit_ != NULL); |
| 52 | byte* addr = const_cast<byte*>(reinterpret_cast<const byte*>(begin_)); |
| 53 | const size_t length = limit_ - begin_; |
| 54 | ptr_ = reinterpret_cast<const Object**>(addr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | int result = madvise(addr, length, MADV_DONTNEED); |
| 56 | if (result == -1) { |
| 57 | PLOG(WARNING) << "madvise failed"; |
| 58 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Elliott Hughes | 28a452a | 2012-01-17 20:39:44 -0800 | [diff] [blame] | 61 | MarkStack::~MarkStack() { |
| 62 | CHECK(IsEmpty()); |
| 63 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 64 | |
| 65 | } // namespace art |