blob: bcf36a2f0b6318c257faa8faadd48930f369fb26 [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:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070034 typedef std::vector<SpaceBitmap*, GcAllocator<SpaceBitmap*> > SpaceBitmapVector;
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080035 typedef std::vector<ObjectSet*, GcAllocator<ObjectSet*> > ObjectSetVector;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070036
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 {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080051 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070052 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 {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080062 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070063 DCHECK(set != NULL);
64 set->Set(obj);
65 }
66 }
67
68 SpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070069 for (const auto& bitmap : continuous_space_bitmaps_) {
Ian Rogers1d54e732013-05-02 21:10:01 -070070 if (bitmap->HasAddress(obj)) {
71 return bitmap;
72 }
73 }
74 return NULL;
75 }
76
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080077 ObjectSet* GetDiscontinuousSpaceObjectSet(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070078 for (const auto& space_set : discontinuous_space_sets_) {
79 if (space_set->Test(obj)) {
80 return space_set;
Ian Rogers1d54e732013-05-02 21:10:01 -070081 }
82 }
83 return NULL;
84 }
85
86 void Walk(SpaceBitmap::Callback* callback, void* arg)
87 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
88
89 template <typename Visitor>
90 void Visit(const Visitor& visitor)
91 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
94 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
95 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
96 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
97
98 // 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 -080099 void ReplaceObjectSet(ObjectSet* old_set, ObjectSet* new_set)
Ian Rogers1d54e732013-05-02 21:10:01 -0700100 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
101
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700102 explicit HeapBitmap(Heap* heap) : heap_(heap) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700103
104 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700105 const Heap* const heap_;
106
107 void AddContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800108 void RemoveContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800109 void AddDiscontinuousObjectSet(ObjectSet* set);
110 void RemoveDiscontinuousObjectSet(ObjectSet* set);
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
112 // Bitmaps covering continuous spaces.
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700113 SpaceBitmapVector continuous_space_bitmaps_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700114
115 // Sets covering discontinuous spaces.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800116 ObjectSetVector discontinuous_space_sets_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700117
118 friend class art::gc::Heap;
119};
120
121} // namespace accounting
122} // namespace gc
123} // namespace art
124
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700125#endif // ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_