blob: 87e08483f534503e42aaed387b5cc3cf7e6bdf85 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#ifndef ART_SRC_GC_HEAP_BITMAP_H_
18#define ART_SRC_GC_HEAP_BITMAP_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "locks.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070021#include "space_bitmap.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
23namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024class Heap;
Carl Shapiro69759ea2011-07-21 18:13:35 -070025
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026class HeapBitmap {
27 public:
28 bool Test(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
29 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
30 if (LIKELY(bitmap != NULL)) {
31 return bitmap->Test(obj);
32 } else {
33 return large_objects_->Test(obj);
34 }
35 }
36
37 void Clear(const mirror::Object* obj)
38 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
39 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
40 if (LIKELY(bitmap != NULL)) {
41 bitmap->Clear(obj);
42 } else {
43 large_objects_->Clear(obj);
44 }
45 }
46
47 void Set(const mirror::Object* obj)
48 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
49 SpaceBitmap* bitmap = GetSpaceBitmap(obj);
50 if (LIKELY(bitmap != NULL)) {
51 bitmap->Set(obj);
52 } else {
53 large_objects_->Set(obj);
54 }
55 }
56
57 SpaceBitmap* GetSpaceBitmap(const mirror::Object* obj) {
58 // TODO: C++0x auto
59 for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) {
60 if ((*it)->HasAddress(obj)) {
61 return *it;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070062 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070063 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 return NULL;
65 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070066
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 void Walk(SpaceBitmap::Callback* callback, void* arg)
68 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070069
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 template <typename Visitor>
71 void Visit(const Visitor& visitor)
72 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070074
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC.
76 void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap)
77 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 HeapBitmap(Heap* heap);
Mathieu Chartierfd678be2012-08-30 14:50:54 -070080
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 inline SpaceSetMap* GetLargeObjects() const {
82 return large_objects_;
83 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 void SetLargeObjects(SpaceSetMap* large_objects);
Mathieu Chartier654d3a22012-07-11 17:54:18 -070086
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 private:
Mathieu Chartiercc236d72012-07-20 10:29:05 -070088
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 const Heap* const heap_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 void AddSpaceBitmap(SpaceBitmap* bitmap);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070092
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 typedef std::vector<SpaceBitmap*> Bitmaps;
94 Bitmaps bitmaps_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 // Large object sets.
97 SpaceSetMap* large_objects_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070098
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 friend class Heap;
100};
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700101
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102} // namespace art
103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104#endif // ART_SRC_GC_HEAP_BITMAP_H_