Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 5e71b52 | 2011-10-20 13:12:32 -0700 | [diff] [blame] | 17 | #ifndef ART_SRC_HEAP_BITMAP_H_ |
| 18 | #define ART_SRC_HEAP_BITMAP_H_ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 20 | #include "space_bitmap.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 23 | class Heap; |
| 24 | class SpaceBitmap; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 25 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 26 | class HeapBitmap { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 27 | public: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 28 | bool Test(const Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 29 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 30 | SpaceBitmap* bitmap = GetSpaceBitmap(obj); |
| 31 | DCHECK(bitmap != NULL); |
| 32 | return bitmap->Test(obj); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 35 | void Clear(const Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 36 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 37 | 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 Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 43 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 44 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | void Set(const Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 46 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 47 | 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 Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 57 | for (Bitmaps::iterator it = bitmaps_.begin(); it != bitmaps_.end(); ++it) { |
| 58 | if ((*it)->HasAddress(obj)) { |
| 59 | return *it; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | void Walk(SpaceBitmap::Callback* callback, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 66 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 67 | // TODO: C++0x auto |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 68 | 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 Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 75 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 76 | // 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 84 | // Find and replace a bitmap pointer, this is used by for the bitmap swapping in the GC. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | void ReplaceBitmap(SpaceBitmap* old_bitmap, SpaceBitmap* new_bitmap) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 87 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 88 | HeapBitmap(Heap* heap) : heap_(heap) { |
| 89 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 92 | private: |
| 93 | |
| 94 | const Heap* const heap_; |
| 95 | |
| 96 | void AddSpaceBitmap(SpaceBitmap* bitmap); |
| 97 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 98 | typedef std::vector<SpaceBitmap*> Bitmaps; |
| 99 | Bitmaps bitmaps_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 100 | |
| 101 | friend class Heap; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 102 | }; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 103 | } // namespace art |
| 104 | |
Elliott Hughes | 5e71b52 | 2011-10-20 13:12:32 -0700 | [diff] [blame] | 105 | #endif // ART_SRC_HEAP_BITMAP_H_ |