Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
| 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. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef DALVIK_ALLOC_CARDTABLE_H_ |
| 18 | #define DALVIK_ALLOC_CARDTABLE_H_ |
| 19 | |
| 20 | #include "globals.h" |
| 21 | #include "logging.h" |
| 22 | #include "mem_map.h" |
| 23 | #include "UniquePtr.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 27 | class Heap; |
| 28 | class HeapBitmap; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 29 | class Object; |
| 30 | |
| 31 | #define GC_CARD_SHIFT 7 |
| 32 | #define GC_CARD_SIZE (1 << GC_CARD_SHIFT) |
| 33 | #define GC_CARD_CLEAN 0 |
| 34 | #define GC_CARD_DIRTY 0x70 |
| 35 | |
Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 36 | // Maintain a card table from the the write barrier. All writes of |
| 37 | // non-NULL values to heap addresses should go through an entry in |
| 38 | // WriteBarrier, and from there to here. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 39 | class CardTable { |
| 40 | public: |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 41 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 42 | static CardTable* Create(const byte* heap_begin, size_t heap_capacity); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 43 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 44 | // Set the card associated with the given address to GC_CARD_DIRTY. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 45 | void MarkCard(const void *addr) { |
| 46 | byte* cardAddr = CardFromAddr(addr); |
| 47 | *cardAddr = GC_CARD_DIRTY; |
| 48 | } |
| 49 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 50 | // Is the object on a dirty card? |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 51 | bool IsDirty(const Object* obj) const { |
| 52 | return *CardFromAddr(obj) == GC_CARD_DIRTY; |
| 53 | } |
| 54 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 55 | // Returns a value that when added to a heap address >> GC_CARD_SHIFT will address the appropriate |
| 56 | // card table byte. For convenience this value is cached in every Thread |
| 57 | byte* GetBiasedBegin() const { |
| 58 | return biased_begin_; |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 61 | // For every dirty card between begin and end invoke the visitor with the specified argument |
| 62 | typedef void Callback(Object* obj, void* arg); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 63 | void Scan(HeapBitmap* bitmap, byte* begin, byte* end, Callback* visitor, void* arg) const; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 64 | |
| 65 | |
| 66 | // Assertion used to check the given address is covered by the card table |
| 67 | void CheckAddrIsInCardTable(const byte* addr) const; |
| 68 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 69 | // Resets all of the bytes in the card table to clean. |
| 70 | void ClearCardTable(); |
| 71 | |
| 72 | // Resets all of the bytes in the card table to clean. |
| 73 | void ClearNonImageSpaceCards(Heap* heap); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 75 | private: |
| 76 | CardTable(MemMap* begin, byte* biased_begin, size_t offset); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 77 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 78 | // Returns the address of the relevant byte in the card table, given an address on the heap. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 79 | byte* CardFromAddr(const void *addr) const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 80 | byte *cardAddr = biased_begin_ + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 81 | // Sanity check the caller was asking for address covered by the card table |
| 82 | DCHECK(IsValidCard(cardAddr)) << "addr: " << addr |
| 83 | << " cardAddr: " << reinterpret_cast<void*>(cardAddr); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 84 | return cardAddr; |
| 85 | } |
| 86 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 87 | // Returns the first address in the heap which maps to this card. |
| 88 | void* AddrFromCard(const byte *cardAddr) const { |
| 89 | DCHECK(IsValidCard(cardAddr)); |
| 90 | uintptr_t offset = cardAddr - biased_begin_; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 91 | return reinterpret_cast<void*>(offset << GC_CARD_SHIFT); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 92 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 93 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 94 | // Returns true iff the card table address is within the bounds of the card table. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 95 | bool IsValidCard(const byte* cardAddr) const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 96 | byte* begin = mem_map_->Begin() + offset_; |
| 97 | byte* end = mem_map_->End(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 98 | return cardAddr >= begin && cardAddr < end; |
| 99 | } |
| 100 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 101 | // Verifies that all gray objects are on a dirty card. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 102 | void VerifyCardTable(); |
| 103 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 104 | // Mmapped pages for the card table |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 105 | UniquePtr<MemMap> mem_map_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 106 | // Value used to compute card table addresses from object addresses, see GetBiasedBegin |
| 107 | byte* const biased_begin_; |
| 108 | // Card table doesn't begin at the beginning of the mem_map_, instead it is displaced by offset |
| 109 | // to allow the byte value of biased_begin_ to equal GC_CARD_DIRTY |
| 110 | const size_t offset_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace art |
| 114 | #endif // DALVIK_ALLOC_CARDTABLE_H_ |