blob: 50ecc7a9eeccc1d84b52aaace6e82cba223b4447 [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);
31 DCHECK(bitmap != NULL);
32 return bitmap->Test(obj);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070033 }
34
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035 void Clear(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070036 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070037 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
38 DCHECK(bitmap != NULL)
39 << "tried to clear object "
40 << reinterpret_cast<const void*>(obj)
41 << " which did not belong to any bitmaps";
42 return bitmap->Clear(obj);
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070043 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070044
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 void Set(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
48 DCHECK(bitmap != NULL)
49 << "tried to mark object "
50 << reinterpret_cast<const void*>(obj)
51 << " which did not belong to any bitmaps";
52 bitmap->Set(obj);
53 }
54
55 SpaceBitmap* GetSpaceBitmap(const Object* obj) {
56 // TODO: C++0x auto
Mathieu Chartierfd678be2012-08-30 14:50:54 -070057 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
58 if ((*it)->HasAddress(obj)) {
59 return *it;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070060 }
61 }
62 return NULL;
63 }
64
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 void Walk(SpaceBitmap::Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067 // TODO: C++0x auto
Mathieu Chartierfd678be2012-08-30 14:50:54 -070068 for (Bitmaps::iterator it = bitmaps_.begin(); it!= bitmaps_.end(); ++it) {
69 (*it)->Walk(callback, arg);
70 }
71 }
72
73 template <typename Visitor>
74 void Visit(const Visitor& visitor)
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierfd678be2012-08-30 14:50:54 -070076 // TODO: C++0x auto
77 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
78 SpaceBitmap* bitmap = *it;
79 bitmap->VisitMarkedRange(bitmap->HeapBegin(), bitmap->HeapLimit(), visitor,
80 IdentityFunctor());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 }
82 }
83
Mathieu Chartier654d3a22012-07-11 17:54:18 -070084 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier654d3a22012-07-11 17:54:18 -070087
Mathieu Chartiercc236d72012-07-20 10:29:05 -070088 HeapBitmap(Heap* heap) : heap_(heap) {
89
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090 }
91
Mathieu Chartiercc236d72012-07-20 10:29:05 -070092 private:
93
94 const Heap* const heap_;
95
96 void AddSpaceBitmap(SpaceBitmap* bitmap);
97
Mathieu Chartier654d3a22012-07-11 17:54:18 -070098 typedef std::vector<SpaceBitmap*> Bitmaps;
99 Bitmaps bitmaps_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700100
101 friend class Heap;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700102 };
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103} // namespace art
104
Elliott Hughes5e71b522011-10-20 13:12:32 -0700105#endif // ART_SRC_HEAP_BITMAP_H_