1763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier/*
2763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * Copyright (C) 2015 The Android Open Source Project
3763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier *
4763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * you may not use this file except in compliance with the License.
6763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * You may obtain a copy of the License at
7763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier *
8763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier *
10763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * See the License for the specific language governing permissions and
14763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier * limitations under the License.
15763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier */
16763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
17763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#ifndef ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
18763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#define ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
19763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
20763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#include "base/macros.h"
21763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#include "base/mutex.h"
22763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#include "gc/space/space.h"
23763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#include "immune_region.h"
24763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
25763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#include <set>
26763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
27763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartiernamespace art {
28763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartiernamespace gc {
29763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartiernamespace space {
30763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartierclass ContinuousSpace;
31763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier}  // namespace space
32763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
33763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartiernamespace collector {
34763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
35763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier// ImmuneSpaces is a set of spaces which are not going to have any objects become marked during the
36763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier// GC.
37763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartierclass ImmuneSpaces {
38763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  class CompareByBegin {
39763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier   public:
40763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    bool operator()(space::ContinuousSpace* a, space::ContinuousSpace* b) const;
41763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  };
42763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
43763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier public:
44763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  ImmuneSpaces() {}
45763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  void Reset();
46763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
47763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Add a continuous space to the immune spaces set.
48763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  void AddSpace(space::ContinuousSpace* space) REQUIRES(Locks::heap_bitmap_lock_);
49763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
50763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Returns true if an object is inside of the immune region (assumed to be marked). Only returns
51763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // true for the largest immune region. The object can still be inside of an immune space.
52763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  ALWAYS_INLINE bool IsInImmuneRegion(const mirror::Object* obj) const {
53763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    return largest_immune_region_.ContainsObject(obj);
54763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  }
55763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
56763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Return true if the spaces is contained.
57763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  bool ContainsSpace(space::ContinuousSpace* space) const;
58763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
59763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Return the set of spaces in the immune region.
60763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  const std::set<space::ContinuousSpace*, CompareByBegin>& GetSpaces() {
61763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    return spaces_;
62763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  }
63763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
64763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Return the associated largest immune region.
65763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  const ImmuneRegion& GetLargestImmuneRegion() const {
66763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    return largest_immune_region_;
67763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  }
68763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
69763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Return true if the object is contained by any of the immune space.s
70763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  ALWAYS_INLINE bool ContainsObject(const mirror::Object* obj) const {
71763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    if (largest_immune_region_.ContainsObject(obj)) {
72763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier      return true;
73763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    }
74763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    for (space::ContinuousSpace* space : spaces_) {
75763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier      if (space->HasAddress(obj)) {
76763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier        return true;
77763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier      }
78763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    }
79763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier    return false;
80763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  }
81763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
82763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier private:
83763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // Setup the immune region to the largest continuous set of immune spaces. The immune region is
84763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  // just the for the fast path lookup.
85763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  void CreateLargestImmuneRegion();
86763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
87763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  std::set<space::ContinuousSpace*, CompareByBegin> spaces_;
88763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier  ImmuneRegion largest_immune_region_;
89763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier};
90763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
91763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier}  // namespace collector
92763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier}  // namespace gc
93763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier}  // namespace art
94763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier
95763a31ed7a2bfad22a9cb07f5301a71c0f97ca49Mathieu Chartier#endif  // ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
96