1 /*
2  * Copyright (C) 2015 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  */
16 
17 #ifndef ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
18 #define ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
19 
20 #include "base/locks.h"
21 #include "base/macros.h"
22 #include "gc/space/space.h"
23 #include "immune_region.h"
24 
25 #include <set>
26 
27 namespace art {
28 namespace gc {
29 namespace space {
30 class ContinuousSpace;
31 }  // namespace space
32 
33 namespace collector {
34 
35 // ImmuneSpaces is a set of spaces which are not going to have any objects become marked during the
36 // GC.
37 class ImmuneSpaces {
38   class CompareByBegin {
39    public:
40     bool operator()(space::ContinuousSpace* a, space::ContinuousSpace* b) const;
41   };
42 
43  public:
ImmuneSpaces()44   ImmuneSpaces() {}
45   void Reset();
46 
47   // Add a continuous space to the immune spaces set.
48   void AddSpace(space::ContinuousSpace* space) REQUIRES(Locks::heap_bitmap_lock_);
49 
50   // Returns true if an object is inside of the immune region (assumed to be marked). Only returns
51   // true for the largest immune region. The object can still be inside of an immune space.
IsInImmuneRegion(const mirror::Object * obj)52   ALWAYS_INLINE bool IsInImmuneRegion(const mirror::Object* obj) const {
53     return largest_immune_region_.ContainsObject(obj);
54   }
55 
56   // Return true if the spaces is contained.
57   bool ContainsSpace(space::ContinuousSpace* space) const;
58 
59   // Return the set of spaces in the immune region.
GetSpaces()60   const std::set<space::ContinuousSpace*, CompareByBegin>& GetSpaces() {
61     return spaces_;
62   }
63 
64   // Return the associated largest immune region.
GetLargestImmuneRegion()65   const ImmuneRegion& GetLargestImmuneRegion() const {
66     return largest_immune_region_;
67   }
68 
69   // Return true if the object is contained by any of the immune space.s
ContainsObject(const mirror::Object * obj)70   ALWAYS_INLINE bool ContainsObject(const mirror::Object* obj) const {
71     if (largest_immune_region_.ContainsObject(obj)) {
72       return true;
73     }
74     for (space::ContinuousSpace* space : spaces_) {
75       if (space->HasAddress(obj)) {
76         return true;
77       }
78     }
79     return false;
80   }
81 
82  private:
83   // Setup the immune region to the largest continuous set of immune spaces. The immune region is
84   // just the for the fast path lookup.
85   void CreateLargestImmuneRegion();
86 
87   std::set<space::ContinuousSpace*, CompareByBegin> spaces_;
88   ImmuneRegion largest_immune_region_;
89 };
90 
91 }  // namespace collector
92 }  // namespace gc
93 }  // namespace art
94 
95 #endif  // ART_RUNTIME_GC_COLLECTOR_IMMUNE_SPACES_H_
96