xref: /aosp_15_r20/art/runtime/gc/collector/immune_region.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_COLLECTOR_IMMUNE_REGION_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_COLLECTOR_IMMUNE_REGION_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
23*795d594fSAndroid Build Coastguard Worker namespace mirror {
24*795d594fSAndroid Build Coastguard Worker class Object;
25*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
26*795d594fSAndroid Build Coastguard Worker namespace gc {
27*795d594fSAndroid Build Coastguard Worker namespace space {
28*795d594fSAndroid Build Coastguard Worker class ContinuousSpace;
29*795d594fSAndroid Build Coastguard Worker }  // namespace space
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace collector {
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker // An immune region is a continuous region of memory for which all objects contained are assumed to
34*795d594fSAndroid Build Coastguard Worker // be marked. This is used as an optimization in the GC to avoid needing to test the mark bitmap of
35*795d594fSAndroid Build Coastguard Worker // the zygote, image spaces, and sometimes non moving spaces. Doing the ContainsObject check is
36*795d594fSAndroid Build Coastguard Worker // faster than doing a bitmap read. There is no support for discontinuous spaces and you need to be
37*795d594fSAndroid Build Coastguard Worker // careful that your immune region doesn't contain any large objects.
38*795d594fSAndroid Build Coastguard Worker class ImmuneRegion {
39*795d594fSAndroid Build Coastguard Worker  public:
40*795d594fSAndroid Build Coastguard Worker   ImmuneRegion();
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker   void Reset();
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker   // Returns true if an object is inside of the immune region (assumed to be marked).
ContainsObject(const mirror::Object * obj)45*795d594fSAndroid Build Coastguard Worker   ALWAYS_INLINE bool ContainsObject(const mirror::Object* obj) const {
46*795d594fSAndroid Build Coastguard Worker     // Note: Relies on integer underflow behavior.
47*795d594fSAndroid Build Coastguard Worker     return reinterpret_cast<uintptr_t>(obj) - reinterpret_cast<uintptr_t>(begin_) < size_;
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
SetBegin(mirror::Object * begin)50*795d594fSAndroid Build Coastguard Worker   void SetBegin(mirror::Object* begin) {
51*795d594fSAndroid Build Coastguard Worker     begin_ = begin;
52*795d594fSAndroid Build Coastguard Worker     UpdateSize();
53*795d594fSAndroid Build Coastguard Worker   }
54*795d594fSAndroid Build Coastguard Worker 
SetEnd(mirror::Object * end)55*795d594fSAndroid Build Coastguard Worker   void SetEnd(mirror::Object* end) {
56*795d594fSAndroid Build Coastguard Worker     end_ = end;
57*795d594fSAndroid Build Coastguard Worker     UpdateSize();
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker 
Begin()60*795d594fSAndroid Build Coastguard Worker   mirror::Object* Begin() const {
61*795d594fSAndroid Build Coastguard Worker     return begin_;
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
End()64*795d594fSAndroid Build Coastguard Worker   mirror::Object* End() const {
65*795d594fSAndroid Build Coastguard Worker     return end_;
66*795d594fSAndroid Build Coastguard Worker   }
67*795d594fSAndroid Build Coastguard Worker 
Size()68*795d594fSAndroid Build Coastguard Worker   size_t Size() const {
69*795d594fSAndroid Build Coastguard Worker     return size_;
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker  private:
UpdateSize()73*795d594fSAndroid Build Coastguard Worker   void UpdateSize() {
74*795d594fSAndroid Build Coastguard Worker     size_ = reinterpret_cast<uintptr_t>(end_) - reinterpret_cast<uintptr_t>(begin_);
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   mirror::Object* begin_;
78*795d594fSAndroid Build Coastguard Worker   mirror::Object* end_;
79*795d594fSAndroid Build Coastguard Worker   uintptr_t size_;
80*795d594fSAndroid Build Coastguard Worker };
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker }  // namespace collector
83*795d594fSAndroid Build Coastguard Worker }  // namespace gc
84*795d594fSAndroid Build Coastguard Worker }  // namespace art
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_COLLECTOR_IMMUNE_REGION_H_
87