blob: 2f188984dd5776cc6d4890532814299192875119 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "mark_stack.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "globals.h"
20#include "logging.h"
Elliott Hughesa168c832012-06-12 15:34:20 -070021#include "UniquePtr.h"
Elliott Hughesf0605a02012-01-13 20:12:02 -080022#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070023
24namespace art {
25
Mathieu Chartier357e9be2012-08-01 11:00:14 -070026MarkStack* MarkStack::Create(const std::string& name, size_t length) {
27 UniquePtr<MarkStack> mark_stack(new MarkStack(name));
28 mark_stack->Init(length);
Jesse Wilson078f9b02011-11-18 17:51:47 -050029 return mark_stack.release();
Carl Shapiro69759ea2011-07-21 18:13:35 -070030}
31
Mathieu Chartier357e9be2012-08-01 11:00:14 -070032void MarkStack::Init(size_t length) {
33 mem_map_.reset(MemMap::MapAnonymous(name_.c_str(), NULL, length, PROT_READ | PROT_WRITE));
Elliott Hughesf0605a02012-01-13 20:12:02 -080034 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 Rogers30fab402012-01-23 15:43:46 -080039 byte* addr = mem_map_->Begin();
Elliott Hughesf0605a02012-01-13 20:12:02 -080040 CHECK(addr != NULL);
Mathieu Chartier5301cd22012-05-31 12:11:36 -070041
Ian Rogers30fab402012-01-23 15:43:46 -080042 begin_ = reinterpret_cast<const Object**>(addr);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 limit_ = reinterpret_cast<const Object**>(addr + length);
Mathieu Chartier5301cd22012-05-31 12:11:36 -070044
45 Reset();
46}
47
48void 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 Shapiro69759ea2011-07-21 18:13:35 -070055 int result = madvise(addr, length, MADV_DONTNEED);
56 if (result == -1) {
57 PLOG(WARNING) << "madvise failed";
58 }
Carl Shapiro69759ea2011-07-21 18:13:35 -070059}
60
Elliott Hughes28a452a2012-01-17 20:39:44 -080061MarkStack::~MarkStack() {
62 CHECK(IsEmpty());
63}
Carl Shapiro69759ea2011-07-21 18:13:35 -070064
65} // namespace art