Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | /* |
| 4 | * Maintain a card table from the the write barrier. All writes of |
| 5 | * non-NULL values to heap addresses should go through an entry in |
| 6 | * WriteBarrier, and from there to here. |
| 7 | */ |
| 8 | |
| 9 | #ifndef DALVIK_ALLOC_CARDTABLE_H_ |
| 10 | #define DALVIK_ALLOC_CARDTABLE_H_ |
| 11 | |
| 12 | #include "globals.h" |
| 13 | #include "logging.h" |
| 14 | #include "mem_map.h" |
| 15 | #include "UniquePtr.h" |
| 16 | |
| 17 | namespace art { |
| 18 | |
| 19 | class Object; |
| 20 | |
| 21 | #define GC_CARD_SHIFT 7 |
| 22 | #define GC_CARD_SIZE (1 << GC_CARD_SHIFT) |
| 23 | #define GC_CARD_CLEAN 0 |
| 24 | #define GC_CARD_DIRTY 0x70 |
| 25 | |
| 26 | class CardTable { |
| 27 | public: |
| 28 | typedef void Callback(Object* obj, void* arg); |
| 29 | |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame^] | 30 | static CardTable* Create(const byte* heap_base, size_t heap_max_size, size_t growth_size); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Set the card associated with the given address to GC_CARD_DIRTY. |
| 34 | */ |
| 35 | void MarkCard(const void *addr) { |
| 36 | byte* cardAddr = CardFromAddr(addr); |
| 37 | *cardAddr = GC_CARD_DIRTY; |
| 38 | } |
| 39 | |
| 40 | byte* GetBiasedBase() { |
| 41 | return biased_base_; |
| 42 | } |
| 43 | |
| 44 | void Scan(byte* base, byte* limit, Callback* visitor, void* arg) const; |
| 45 | |
| 46 | bool IsDirty(const Object* obj) const { |
| 47 | return *CardFromAddr(obj) == GC_CARD_DIRTY; |
| 48 | } |
| 49 | |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame^] | 50 | void ClearGrowthLimit() { |
| 51 | CHECK_GE(max_length_, length_); |
| 52 | length_ = max_length_; |
| 53 | } |
| 54 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 55 | private: |
| 56 | |
| 57 | CardTable() {} |
| 58 | |
| 59 | /* |
| 60 | * Initializes the card table; must be called before any other |
| 61 | * CardTable functions. |
| 62 | */ |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame^] | 63 | bool Init(const byte* heap_base, size_t heap_max_size, size_t growth_size); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Resets all of the bytes in the card table to clean. |
| 67 | */ |
| 68 | void ClearCardTable(); |
| 69 | |
| 70 | /* |
| 71 | * Returns the address of the relevant byte in the card table, given |
| 72 | * an address on the heap. |
| 73 | */ |
| 74 | byte* CardFromAddr(const void *addr) const { |
| 75 | byte *cardAddr = biased_base_ + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 76 | CHECK(IsValidCard(cardAddr)); |
| 77 | return cardAddr; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Returns the first address in the heap which maps to this card. |
| 82 | */ |
| 83 | void* AddrFromCard(const byte *card) const; |
| 84 | |
| 85 | /* |
| 86 | * Returns true iff the address is within the bounds of the card table. |
| 87 | */ |
| 88 | bool IsValidCard(const byte* cardAddr) const { |
| 89 | byte* begin = mem_map_->GetAddress() + offset_; |
| 90 | byte* end = &begin[length_]; |
| 91 | return cardAddr >= begin && cardAddr < end; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Verifies that all gray objects are on a dirty card. |
| 96 | */ |
| 97 | void VerifyCardTable(); |
| 98 | |
| 99 | |
| 100 | UniquePtr<MemMap> mem_map_; |
| 101 | byte* base_; |
| 102 | byte* biased_base_; |
| 103 | size_t length_; |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame^] | 104 | size_t max_length_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 105 | size_t offset_; |
| 106 | }; |
| 107 | |
| 108 | } // namespace art |
| 109 | #endif // DALVIK_ALLOC_CARDTABLE_H_ |