blob: ada976fff6d4ac3a62d6d402098db759646a002b [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"
23#include "space_bitmap.h"
24
25namespace art {
26namespace gc {
27
28class Heap;
29
30namespace accounting {
31
32class HeapBitmap {
33 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070034 typedef std::vector<SpaceBitmap*, GCAllocator<SpaceBitmap*> > SpaceBitmapVector;
35 typedef std::vector<SpaceSetMap*, GCAllocator<SpaceSetMap*> > SpaceSetMapVector;
36
Ian Rogers1d54e732013-05-02 21:10:01 -070037 bool Test(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
38 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
39 if (LIKELY(bitmap != NULL)) {
40 return bitmap->Test(obj);
41 } else {
42 return GetDiscontinuousSpaceObjectSet(obj) != NULL;
43 }
44 }
45
46 void Clear(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
47 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
48 if (LIKELY(bitmap != NULL)) {
49 bitmap->Clear(obj);
50 } else {
51 SpaceSetMap* set = GetDiscontinuousSpaceObjectSet(obj);
52 DCHECK(set != NULL);
53 set->Clear(obj);
54 }
55 }
56
57 void Set(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
58 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
59 if (LIKELY(bitmap != NULL)) {
60 bitmap->Set(obj);
61 } else {
62 SpaceSetMap* set = GetDiscontinuousSpaceObjectSet(obj);
63 DCHECK(set != NULL);
64 set->Set(obj);
65 }
66 }
67
68 SpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) {
69 // TODO: C++0x auto
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070070 typedef SpaceBitmapVector::iterator It;
Ian Rogers1d54e732013-05-02 21:10:01 -070071 for (It it = continuous_space_bitmaps_.begin(), end = continuous_space_bitmaps_.end();
72 it != end; ++it) {
73 SpaceBitmap* bitmap = *it;
74 if (bitmap->HasAddress(obj)) {
75 return bitmap;
76 }
77 }
78 return NULL;
79 }
80
81 SpaceSetMap* GetDiscontinuousSpaceObjectSet(const mirror::Object* obj) {
82 // TODO: C++0x auto
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070083 typedef SpaceSetMapVector::iterator It;
Ian Rogers1d54e732013-05-02 21:10:01 -070084 for (It it = discontinuous_space_sets_.begin(), end = discontinuous_space_sets_.end();
85 it != end; ++it) {
86 SpaceSetMap* set = *it;
87 if (set->Test(obj)) {
88 return set;
89 }
90 }
91 return NULL;
92 }
93
94 void Walk(SpaceBitmap::Callback* callback, void* arg)
95 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
96
97 template <typename Visitor>
98 void Visit(const Visitor& visitor)
99 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
102 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
103 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
104 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
105
106 // Find and replace a object set pointer, this is used by for the bitmap swapping in the GC.
107 void ReplaceObjectSet(SpaceSetMap* old_set, SpaceSetMap* new_set)
108 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
109
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700110 explicit HeapBitmap(Heap* heap) : heap_(heap) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
112 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700113 const Heap* const heap_;
114
115 void AddContinuousSpaceBitmap(SpaceBitmap* bitmap);
116 void AddDiscontinuousObjectSet(SpaceSetMap* set);
117
118 // Bitmaps covering continuous spaces.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700119 SpaceBitmapVector continuous_space_bitmaps_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700120
121 // Sets covering discontinuous spaces.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700122 SpaceSetMapVector discontinuous_space_sets_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700123
124 friend class art::gc::Heap;
125};
126
127} // namespace accounting
128} // namespace gc
129} // namespace art
130
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700131#endif // ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_