1 /*
2  * Copyright (C) 2012 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_ACCOUNTING_HEAP_BITMAP_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
19 
20 #include "base/allocator.h"
21 #include "base/logging.h"
22 #include "object_callbacks.h"
23 #include "space_bitmap.h"
24 
25 namespace art {
26 namespace gc {
27 
28 class Heap;
29 
30 namespace collector {
31   class ConcurrentCopying;
32 }  // namespace collector
33 
34 namespace accounting {
35 
36 class HeapBitmap {
37  public:
38   bool Test(const mirror::Object* obj) SHARED_REQUIRES(Locks::heap_bitmap_lock_);
39   void Clear(const mirror::Object* obj) REQUIRES(Locks::heap_bitmap_lock_);
40   template<typename LargeObjectSetVisitor>
41   bool Set(const mirror::Object* obj, const LargeObjectSetVisitor& visitor)
42       SHARED_REQUIRES(Locks::mutator_lock_)
43       REQUIRES(Locks::heap_bitmap_lock_) ALWAYS_INLINE;
44   template<typename LargeObjectSetVisitor>
45   bool AtomicTestAndSet(const mirror::Object* obj, const LargeObjectSetVisitor& visitor)
46       SHARED_REQUIRES(Locks::mutator_lock_)
47       REQUIRES(Locks::heap_bitmap_lock_) ALWAYS_INLINE;
48   ContinuousSpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) const;
49   LargeObjectBitmap* GetLargeObjectBitmap(const mirror::Object* obj) const;
50 
51   void Walk(ObjectCallback* callback, void* arg)
52       SHARED_REQUIRES(Locks::heap_bitmap_lock_);
53 
54   template <typename Visitor>
55   void Visit(const Visitor& visitor)
56       REQUIRES(Locks::heap_bitmap_lock_)
57       SHARED_REQUIRES(Locks::mutator_lock_);
58 
59   // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
60   void ReplaceBitmap(ContinuousSpaceBitmap* old_bitmap, ContinuousSpaceBitmap* new_bitmap)
61       REQUIRES(Locks::heap_bitmap_lock_);
62 
63   // Find and replace a object set pointer, this is used by for the bitmap swapping in the GC.
64   void ReplaceLargeObjectBitmap(LargeObjectBitmap* old_bitmap, LargeObjectBitmap* new_bitmap)
65       REQUIRES(Locks::heap_bitmap_lock_);
66 
HeapBitmap(Heap * heap)67   explicit HeapBitmap(Heap* heap) : heap_(heap) {}
68 
69  private:
70   const Heap* const heap_;
71 
72   void AddContinuousSpaceBitmap(ContinuousSpaceBitmap* bitmap);
73   void RemoveContinuousSpaceBitmap(ContinuousSpaceBitmap* bitmap);
74   void AddLargeObjectBitmap(LargeObjectBitmap* bitmap);
75   void RemoveLargeObjectBitmap(LargeObjectBitmap* bitmap);
76 
77   // Bitmaps covering continuous spaces.
78   std::vector<ContinuousSpaceBitmap*,
79               TrackingAllocator<ContinuousSpaceBitmap*, kAllocatorTagHeapBitmap>>
80       continuous_space_bitmaps_;
81 
82   // Sets covering discontinuous spaces.
83   std::vector<LargeObjectBitmap*,
84               TrackingAllocator<LargeObjectBitmap*, kAllocatorTagHeapBitmapLOS>>
85       large_object_bitmaps_;
86 
87   friend class art::gc::Heap;
88   friend class art::gc::collector::ConcurrentCopying;
89 };
90 
91 }  // namespace accounting
92 }  // namespace gc
93 }  // namespace art
94 
95 #endif  // ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
96