blob: 1610df8fe254c0140a7d25b7528d4e63f4d44577 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002 * Copyright (C) 2012 The Android Open Source Project
Elliott Hughes2faa5f12012-01-30 14:42:07 -08003 *
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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Elliott Hughes5e71b522011-10-20 13:12:32 -070017#ifndef ART_SRC_HEAP_BITMAP_H_
18#define ART_SRC_HEAP_BITMAP_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070020#include "space_bitmap.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070021
22namespace art {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070023 class Heap;
24 class SpaceBitmap;
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026 class HeapBitmap {
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070027 public:
Ian Rogersf0bbeab2012-10-10 18:26:27 -070028 bool Test(const Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070029 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070030 if (LIKELY(bitmap != NULL)) {
31 return bitmap->Test(obj);
32 } else {
33 return large_objects_->Test(obj);
34 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070035 }
36
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037 void Clear(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070038 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070039 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070040 if (LIKELY(bitmap != NULL)) {
41 return bitmap->Clear(obj);
42 } else {
43 return large_objects_->Clear(obj);
44 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070045 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070046
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 void Set(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070048 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070049 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070050 if (LIKELY(bitmap != NULL)) {
51 bitmap->Set(obj);
52 } else {
53 large_objects_->Set(obj);
54 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070055 }
56
57 SpaceBitmap* GetSpaceBitmap(const Object* obj) {
58 // TODO: C++0x auto
Mathieu Chartierfd678be2012-08-30 14:50:54 -070059 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
60 if ((*it)->HasAddress(obj)) {
61 return *it;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070062 }
63 }
64 return NULL;
65 }
66
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 void Walk(SpaceBitmap::Callback* callback, void* arg)
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070068 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -070069
70 template <typename Visitor>
71 void Visit(const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierfd678be2012-08-30 14:50:54 -070073 // TODO: C++0x auto
74 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
75 SpaceBitmap* bitmap = *it;
76 bitmap->VisitMarkedRange(bitmap->HeapBegin(), bitmap->HeapLimit(), visitor,
77 IdentityFunctor());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070079 large_objects_->Visit(visitor);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080 }
81
Mathieu Chartier654d3a22012-07-11 17:54:18 -070082 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
Ian Rogersb726dcb2012-09-05 08:57:23 -070084 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier654d3a22012-07-11 17:54:18 -070085
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070086 HeapBitmap(Heap* heap);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070087
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070088 inline SpaceSetMap* GetLargeObjects() const {
89 return large_objects_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090 }
91
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070092 void SetLargeObjects(SpaceSetMap* large_objects);
93
Mathieu Chartiercc236d72012-07-20 10:29:05 -070094 private:
95
96 const Heap* const heap_;
97
98 void AddSpaceBitmap(SpaceBitmap* bitmap);
99
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700100 typedef std::vector<SpaceBitmap*> Bitmaps;
101 Bitmaps bitmaps_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700102
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700103 // Large object sets.
104 SpaceSetMap* large_objects_;
105
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700106 friend class Heap;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700107 };
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108} // namespace art
109
Elliott Hughes5e71b522011-10-20 13:12:32 -0700110#endif // ART_SRC_HEAP_BITMAP_H_