blob: b23b12e40f458e7fd4ae03ff3ef4348c5db45b24 [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"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080022#include "object_callbacks.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "space_bitmap.h"
24
25namespace art {
26namespace gc {
27
28class Heap;
29
30namespace accounting {
31
32class HeapBitmap {
33 public:
34 bool Test(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
35 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070036 if (LIKELY(bitmap != nullptr)) {
Ian Rogers1d54e732013-05-02 21:10:01 -070037 return bitmap->Test(obj);
38 } else {
39 return GetDiscontinuousSpaceObjectSet(obj) != NULL;
40 }
41 }
42
43 void Clear(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
44 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
45 if (LIKELY(bitmap != NULL)) {
46 bitmap->Clear(obj);
47 } else {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080048 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070049 DCHECK(set != NULL);
50 set->Clear(obj);
51 }
52 }
53
54 void Set(const mirror::Object* obj) EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
55 SpaceBitmap* bitmap = GetContinuousSpaceBitmap(obj);
56 if (LIKELY(bitmap != NULL)) {
57 bitmap->Set(obj);
58 } else {
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080059 ObjectSet* set = GetDiscontinuousSpaceObjectSet(obj);
Ian Rogers1d54e732013-05-02 21:10:01 -070060 DCHECK(set != NULL);
61 set->Set(obj);
62 }
63 }
64
65 SpaceBitmap* GetContinuousSpaceBitmap(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070066 for (const auto& bitmap : continuous_space_bitmaps_) {
Ian Rogers1d54e732013-05-02 21:10:01 -070067 if (bitmap->HasAddress(obj)) {
68 return bitmap;
69 }
70 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070071 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -070072 }
73
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080074 ObjectSet* GetDiscontinuousSpaceObjectSet(const mirror::Object* obj) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070075 for (const auto& space_set : discontinuous_space_sets_) {
76 if (space_set->Test(obj)) {
77 return space_set;
Ian Rogers1d54e732013-05-02 21:10:01 -070078 }
79 }
Mathieu Chartier0e54cd02014-03-20 12:41:23 -070080 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -070081 }
82
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080083 void Walk(ObjectCallback* callback, void* arg)
Ian Rogers1d54e732013-05-02 21:10:01 -070084 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
85
86 template <typename Visitor>
87 void Visit(const Visitor& visitor)
88 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
89 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
91 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
92 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
93 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
94
95 // 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 -080096 void ReplaceObjectSet(ObjectSet* old_set, ObjectSet* new_set)
Ian Rogers1d54e732013-05-02 21:10:01 -070097 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
98
Brian Carlstrom93ba8932013-07-17 21:31:49 -070099 explicit HeapBitmap(Heap* heap) : heap_(heap) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700100
101 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700102 const Heap* const heap_;
103
104 void AddContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800105 void RemoveContinuousSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800106 void AddDiscontinuousObjectSet(ObjectSet* set);
107 void RemoveDiscontinuousObjectSet(ObjectSet* set);
Ian Rogers1d54e732013-05-02 21:10:01 -0700108
109 // Bitmaps covering continuous spaces.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700110 std::vector<SpaceBitmap*, GcAllocator<SpaceBitmap*>> continuous_space_bitmaps_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700111
112 // Sets covering discontinuous spaces.
Mathieu Chartier0e54cd02014-03-20 12:41:23 -0700113 std::vector<ObjectSet*, GcAllocator<ObjectSet*>> discontinuous_space_sets_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700114
115 friend class art::gc::Heap;
116};
117
118} // namespace accounting
119} // namespace gc
120} // namespace art
121
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700122#endif // ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_