Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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. |
| 15 | */ |
| 16 | |
| 17 | #include "card_table.h" |
| 18 | |
| 19 | #include <sys/mman.h> /* for PROT_* */ |
| 20 | |
| 21 | #include "heap.h" |
| 22 | #include "heap_bitmap.h" |
| 23 | #include "logging.h" |
| 24 | |
| 25 | namespace art { |
| 26 | /* |
| 27 | * Maintain a card table from the write barrier. All writes of |
| 28 | * non-NULL values to heap addresses should go through an entry in |
| 29 | * WriteBarrier, and from there to here. |
| 30 | * |
| 31 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 32 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 33 | * data per card, to be used by the GC. The value of the byte will be |
| 34 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| 35 | * |
| 36 | * After any store of a non-NULL object pointer into a heap object, |
| 37 | * code is obliged to mark the card dirty. The setters in |
| 38 | * object.h [such as SetFieldObject] do this for you. The |
| 39 | * compiler also contains code to mark cards as dirty. |
| 40 | * |
| 41 | * The card table's base [the "biased card table"] gets set to a |
| 42 | * rather strange value. In order to keep the JIT from having to |
| 43 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 44 | * biased base is within the mmap allocation at a point where its low |
| 45 | * byte is equal to GC_DIRTY_CARD. See CardTable::Init for details. |
| 46 | */ |
| 47 | |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 48 | CardTable* 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] | 49 | UniquePtr<CardTable> bitmap(new CardTable); |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 50 | if (!bitmap->Init(heap_base, heap_max_size, growth_size)) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 51 | return NULL; |
| 52 | } else { |
| 53 | return bitmap.release(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Initializes the card table; must be called before any other |
| 59 | * CardTable functions. |
| 60 | */ |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 61 | bool CardTable::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] | 62 | /* Set up the card table */ |
| 63 | size_t length = heap_max_size / GC_CARD_SIZE; |
| 64 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 65 | mem_map_.reset( |
| 66 | MemMap::MapAnonymous("dalvik-card-table", NULL, length + 256, PROT_READ | PROT_WRITE)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 67 | byte* alloc_base = mem_map_->GetAddress(); |
| 68 | if (alloc_base == NULL) { |
| 69 | return false; |
| 70 | } |
| 71 | base_ = alloc_base; |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 72 | length_ = growth_size / GC_CARD_SIZE; |
| 73 | max_length_ = length; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 74 | offset_ = 0; |
| 75 | /* All zeros is the correct initial value; all clean. */ |
| 76 | CHECK_EQ(GC_CARD_CLEAN, 0); |
| 77 | biased_base_ = (byte *)((uintptr_t)alloc_base -((uintptr_t)heap_base >> GC_CARD_SHIFT)); |
| 78 | if (((uintptr_t)biased_base_ & 0xff) != GC_CARD_DIRTY) { |
| 79 | int offset = GC_CARD_DIRTY - (reinterpret_cast<int>(biased_base_) & 0xff); |
| 80 | offset_ = offset + (offset < 0 ? 0x100 : 0); |
| 81 | biased_base_ += offset_; |
| 82 | } |
| 83 | CHECK_EQ(reinterpret_cast<int>(biased_base_) & 0xff, GC_CARD_DIRTY); |
| 84 | ClearCardTable(); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void CardTable::ClearCardTable() { |
| 89 | CHECK(mem_map_->GetAddress() != NULL); |
| 90 | memset(mem_map_->GetAddress(), GC_CARD_CLEAN, length_); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Returns the first address in the heap which maps to this card. |
| 95 | */ |
| 96 | void* CardTable::AddrFromCard(const byte *cardAddr) const { |
| 97 | CHECK(IsValidCard(cardAddr)); |
| 98 | uintptr_t offset = cardAddr - biased_base_; |
| 99 | return (void *)(offset << GC_CARD_SHIFT); |
| 100 | } |
| 101 | |
| 102 | void CardTable::Scan(byte* base, byte* limit, Callback* visitor, void* arg) const { |
| 103 | byte* cur = CardFromAddr(base); |
| 104 | byte* end = CardFromAddr(limit); |
| 105 | while (cur < end) { |
| 106 | while (cur < end && *cur == GC_CARD_CLEAN) { |
| 107 | cur++; |
| 108 | } |
| 109 | byte* run_start = cur; |
| 110 | size_t run = 0; |
| 111 | while (cur < end && *cur == GC_CARD_DIRTY) { |
| 112 | run++; |
| 113 | cur++; |
| 114 | } |
| 115 | if (run > 0) { |
| 116 | byte* run_end = &cur[run]; |
| 117 | Heap::GetLiveBits()->VisitRange(reinterpret_cast<uintptr_t>(AddrFromCard(run_start)), |
| 118 | reinterpret_cast<uintptr_t>(AddrFromCard(run_end)), |
| 119 | visitor, arg); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Verifies that gray objects are on a dirty card. |
| 126 | */ |
| 127 | void CardTable::VerifyCardTable() { |
| 128 | UNIMPLEMENTED(WARNING) << "Card table verification"; |
| 129 | } |
| 130 | |
| 131 | } // namespace art |