blob: 23dcd47d53f048032f89e4b0407710630678c4ad [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 Rogers00f7d0e2012-07-19 15:28:27 -070028 bool Test(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070029 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070030 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070031 if (LIKELY(bitmap != NULL)) {
32 return bitmap->Test(obj);
33 } else {
34 return large_objects_->Test(obj);
35 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070036 }
37
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 void Clear(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070039 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070040 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070041 if (LIKELY(bitmap != NULL)) {
42 return bitmap->Clear(obj);
43 } else {
44 return large_objects_->Clear(obj);
45 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070046 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 void Set(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070050 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070051 if (LIKELY(bitmap != NULL)) {
52 bitmap->Set(obj);
53 } else {
54 large_objects_->Set(obj);
55 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070056 }
57
58 SpaceBitmap* GetSpaceBitmap(const Object* obj) {
59 // TODO: C++0x auto
Mathieu Chartierfd678be2012-08-30 14:50:54 -070060 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
61 if ((*it)->HasAddress(obj)) {
62 return *it;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070063 }
64 }
65 return NULL;
66 }
67
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 void Walk(SpaceBitmap::Callback* callback, void* arg)
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070069 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -070070
71 template <typename Visitor>
72 void Visit(const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierfd678be2012-08-30 14:50:54 -070074 // TODO: C++0x auto
75 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
76 SpaceBitmap* bitmap = *it;
77 bitmap->VisitMarkedRange(bitmap->HeapBegin(), bitmap->HeapLimit(), visitor,
78 IdentityFunctor());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070079 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070080 large_objects_->Visit(visitor);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 }
82
Mathieu Chartier654d3a22012-07-11 17:54:18 -070083 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
Ian Rogersb726dcb2012-09-05 08:57:23 -070085 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier654d3a22012-07-11 17:54:18 -070086
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070087 HeapBitmap(Heap* heap);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070088
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070089 inline SpaceSetMap* GetLargeObjects() const {
90 return large_objects_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070091 }
92
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070093 void SetLargeObjects(SpaceSetMap* large_objects);
94
Mathieu Chartiercc236d72012-07-20 10:29:05 -070095 private:
96
97 const Heap* const heap_;
98
99 void AddSpaceBitmap(SpaceBitmap* bitmap);
100
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700101 typedef std::vector<SpaceBitmap*> Bitmaps;
102 Bitmaps bitmaps_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700104 // Large object sets.
105 SpaceSetMap* large_objects_;
106
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 friend class Heap;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700108 };
Carl Shapiro69759ea2011-07-21 18:13:35 -0700109} // namespace art
110
Elliott Hughes5e71b522011-10-20 13:12:32 -0700111#endif // ART_SRC_HEAP_BITMAP_H_