blob: dde1425abf49ba20bcb8c4be1ee148d3af9e54e3 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
18#define ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
20#include "base/logging.h"
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070021#include "gc_allocator.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "locks.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080023#include "object_callbacks.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "space_bitmap.h"
25
26namespace art {
27namespace gc {
28
29class Heap;
30
31namespace accounting {
32
33class HeapBitmap {
34 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070035 typedef std::vector<SpaceBitmap*, GcAllocator<SpaceBitmap*> > SpaceBitmapVector;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080036 typedef std::vector<ObjectSet*, GcAllocator<ObjectSet*> > ObjectSetVector;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070037
Ian Rogers1d54e732013-05-02 21:10:01 -070038 bool Test(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
39 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
40 if (LIKELY(bitmap != NULL)) {
41 return bitmap->Test(obj);
42 } else {
43 return GetDiscontinuousSpaceObjectSet(obj) != NULL;
44 }
45 }
46
47 void Clear(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
48 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
49 if (LIKELY(bitmap != NULL)) {
50 bitmap->Clear(obj);
51 } else {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080052 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070053 DCHECK(set != NULL);
54 set->Clear(obj);
55 }
56 }
57
58 void Set(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
59 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
60 if (LIKELY(bitmap != NULL)) {
61 bitmap->Set(obj);
62 } else {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080063 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070064 DCHECK(set != NULL);
65 set->Set(obj);
66 }
67 }
68
69 SpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070070 for (const auto& bitmap : continuous_space_bitmaps_) {
Ian Rogers1d54e732013-05-02 21:10:01 -070071 if (bitmap->HasAddress(obj)) {
72 return bitmap;
73 }
74 }
75 return NULL;
76 }
77
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080078 ObjectSet* GetDiscontinuousSpaceObjectSet(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070079 for (const auto& space_set : discontinuous_space_sets_) {
80 if (space_set->Test(obj)) {
81 return space_set;
Ian Rogers1d54e732013-05-02 21:10:01 -070082 }
83 }
84 return NULL;
85 }
86
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080087 void Walk(ObjectCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -070088 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
89
90 template <typename Visitor>
91 void Visit(const Visitor& visitor)
92 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
95 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
96 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
97 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
98
99 // Find and replace a object set pointer, this is used by for the bitmap swapping in the GC.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800100 void ReplaceObjectSet(ObjectSet* old_set, ObjectSet* new_set)
Ian Rogers1d54e732013-05-02 21:10:01 -0700101 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
102
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700103 explicit HeapBitmap(Heap* heap) : heap_(heap) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700104
105 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 const Heap* const heap_;
107
108 void AddContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800109 void RemoveContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800110 void AddDiscontinuousObjectSet(ObjectSet* set);
111 void RemoveDiscontinuousObjectSet(ObjectSet* set);
Ian Rogers1d54e732013-05-02 21:10:01 -0700112
113 // Bitmaps covering continuous spaces.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700114 SpaceBitmapVector continuous_space_bitmaps_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700115
116 // Sets covering discontinuous spaces.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800117 ObjectSetVector discontinuous_space_sets_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700118
119 friend class art::gc::Heap;
120};
121
122} // namespace accounting
123} // namespace gc
124} // namespace art
125
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700126#endif // ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_