1 /*
2  * Copyright (C) 2014 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_REGION_H_
18 #define ART_RUNTIME_GC_COLLECTOR_IMMUNE_REGION_H_
19 
20 #include "base/macros.h"
21 
22 namespace art {
23 namespace mirror {
24 class Object;
25 }  // namespace mirror
26 namespace gc {
27 namespace space {
28 class ContinuousSpace;
29 }  // namespace space
30 
31 namespace collector {
32 
33 // An immune region is a continuous region of memory for which all objects contained are assumed to
34 // be marked. This is used as an optimization in the GC to avoid needing to test the mark bitmap of
35 // the zygote, image spaces, and sometimes non moving spaces. Doing the ContainsObject check is
36 // faster than doing a bitmap read. There is no support for discontinuous spaces and you need to be
37 // careful that your immune region doesn't contain any large objects.
38 class ImmuneRegion {
39  public:
40   ImmuneRegion();
41 
42   void Reset();
43 
44   // Returns true if an object is inside of the immune region (assumed to be marked).
ContainsObject(const mirror::Object * obj)45   ALWAYS_INLINE bool ContainsObject(const mirror::Object* obj) const {
46     // Note: Relies on integer underflow behavior.
47     return reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(begin_) < size_;
48   }
49 
SetBegin(mirror::Object * begin)50   void SetBegin(mirror::Object* begin) {
51     begin_ = begin;
52     UpdateSize();
53   }
54 
SetEnd(mirror::Object * end)55   void SetEnd(mirror::Object* end) {
56     end_ = end;
57     UpdateSize();
58   }
59 
Begin()60   mirror::Object* Begin() const {
61     return begin_;
62   }
63 
End()64   mirror::Object* End() const {
65     return end_;
66   }
67 
Size()68   size_t Size() const {
69     return size_;
70   }
71 
72  private:
UpdateSize()73   void UpdateSize() {
74     size_ = reinterpret_cast<uintptr_t>(end_) - reinterpret_cast<uintptr_t>(begin_);
75   }
76 
77   mirror::Object* begin_;
78   mirror::Object* end_;
79   uintptr_t size_;
80 };
81 
82 }  // namespace collector
83 }  // namespace gc
84 }  // namespace art
85 
86 #endif  // ART_RUNTIME_GC_COLLECTOR_IMMUNE_REGION_H_
87