blob: 5ff40c64263029ccc6be2ca0266175a72b7f1a9b [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
17#ifndef ART_SRC_GC_ACCOUNTING_HEAP_BITMAP_H_
18#define ART_SRC_GC_ACCOUNTING_HEAP_BITMAP_H_
19
20#include "base/logging.h"
21#include "locks.h"
22#include "space_bitmap.h"
23
24namespace art {
25namespace gc {
26
27class Heap;
28
29namespace accounting {
30
31class HeapBitmap {
32 public:
33 bool Test(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
34 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
35 if (LIKELY(bitmap != NULL)) {
36 return bitmap->Test(obj);
37 } else {
38 return GetDiscontinuousSpaceObjectSet(obj) != NULL;
39 }
40 }
41
42 void Clear(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
43 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
44 if (LIKELY(bitmap != NULL)) {
45 bitmap->Clear(obj);
46 } else {
47 SpaceSetMap* set = GetDiscontinuousSpaceObjectSet(obj);
48 DCHECK(set != NULL);
49 set->Clear(obj);
50 }
51 }
52
53 void Set(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
54 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
55 if (LIKELY(bitmap != NULL)) {
56 bitmap->Set(obj);
57 } else {
58 SpaceSetMap* set = GetDiscontinuousSpaceObjectSet(obj);
59 DCHECK(set != NULL);
60 set->Set(obj);
61 }
62 }
63
64 SpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) {
65 // TODO: C++0x auto
66 typedef std::vector<SpaceBitmap*>::iterator It;
67 for (It it = continuous_space_bitmaps_.begin(), end = continuous_space_bitmaps_.end();
68 it != end; ++it) {
69 SpaceBitmap* bitmap = *it;
70 if (bitmap->HasAddress(obj)) {
71 return bitmap;
72 }
73 }
74 return NULL;
75 }
76
77 SpaceSetMap* GetDiscontinuousSpaceObjectSet(const mirror::Object* obj) {
78 // TODO: C++0x auto
79 typedef std::vector<SpaceSetMap*>::iterator It;
80 for (It it = discontinuous_space_sets_.begin(), end = discontinuous_space_sets_.end();
81 it != end; ++it) {
82 SpaceSetMap* set = *it;
83 if (set->Test(obj)) {
84 return set;
85 }
86 }
87 return NULL;
88 }
89
90 void Walk(SpaceBitmap::Callback* callback, void* arg)
91 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
92
93 template <typename Visitor>
94 void Visit(const Visitor& visitor)
95 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97
98 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
99 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
100 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
101
102 // Find and replace a object set pointer, this is used by for the bitmap swapping in the GC.
103 void ReplaceObjectSet(SpaceSetMap* old_set, SpaceSetMap* new_set)
104 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
105
106 HeapBitmap(Heap* heap) : heap_(heap) {
107 }
108
109 private:
110
111 const Heap* const heap_;
112
113 void AddContinuousSpaceBitmap(SpaceBitmap* bitmap);
114 void AddDiscontinuousObjectSet(SpaceSetMap* set);
115
116 // Bitmaps covering continuous spaces.
117 std::vector<SpaceBitmap*> continuous_space_bitmaps_;
118
119 // Sets covering discontinuous spaces.
120 std::vector<SpaceSetMap*> discontinuous_space_sets_;
121
122 friend class art::gc::Heap;
123};
124
125} // namespace accounting
126} // namespace gc
127} // namespace art
128
129#endif // ART_SRC_GC_ACCOUNTING_HEAP_BITMAP_H_