blob: e8eec38f3d71e7253a4ef4e74224987997e39467 [file] [log] [blame]
Ian Rogers5d76c432011-10-31 21:42:49 -07001/*
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
25namespace 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
48CardTable* CardTable::Create(const byte* heap_base, size_t heap_max_size) {
49 UniquePtr<CardTable> bitmap(new CardTable);
50 if (!bitmap->Init(heap_base, heap_max_size)) {
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 */
61bool CardTable::Init(const byte* heap_base, size_t heap_max_size) {
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 */
65 mem_map_.reset(MemMap::Map(length + 256, PROT_READ | PROT_WRITE));
66 byte* alloc_base = mem_map_->GetAddress();
67 if (alloc_base == NULL) {
68 return false;
69 }
70 base_ = alloc_base;
71 length_ = length;
72 offset_ = 0;
73 /* All zeros is the correct initial value; all clean. */
74 CHECK_EQ(GC_CARD_CLEAN, 0);
75 biased_base_ = (byte *)((uintptr_t)alloc_base -((uintptr_t)heap_base >> GC_CARD_SHIFT));
76 if (((uintptr_t)biased_base_ & 0xff) != GC_CARD_DIRTY) {
77 int offset = GC_CARD_DIRTY - (reinterpret_cast<int>(biased_base_) & 0xff);
78 offset_ = offset + (offset < 0 ? 0x100 : 0);
79 biased_base_ += offset_;
80 }
81 CHECK_EQ(reinterpret_cast<int>(biased_base_) & 0xff, GC_CARD_DIRTY);
82 ClearCardTable();
83 return true;
84}
85
86void CardTable::ClearCardTable() {
87 CHECK(mem_map_->GetAddress() != NULL);
88 memset(mem_map_->GetAddress(), GC_CARD_CLEAN, length_);
89}
90
91/*
92 * Returns the first address in the heap which maps to this card.
93 */
94void* CardTable::AddrFromCard(const byte *cardAddr) const {
95 CHECK(IsValidCard(cardAddr));
96 uintptr_t offset = cardAddr - biased_base_;
97 return (void *)(offset << GC_CARD_SHIFT);
98}
99
100void CardTable::Scan(byte* base, byte* limit, Callback* visitor, void* arg) const {
101 byte* cur = CardFromAddr(base);
102 byte* end = CardFromAddr(limit);
103 while (cur < end) {
104 while (cur < end && *cur == GC_CARD_CLEAN) {
105 cur++;
106 }
107 byte* run_start = cur;
108 size_t run = 0;
109 while (cur < end && *cur == GC_CARD_DIRTY) {
110 run++;
111 cur++;
112 }
113 if (run > 0) {
114 byte* run_end = &cur[run];
115 Heap::GetLiveBits()->VisitRange(reinterpret_cast<uintptr_t>(AddrFromCard(run_start)),
116 reinterpret_cast<uintptr_t>(AddrFromCard(run_end)),
117 visitor, arg);
118 }
119 }
120}
121
122/*
123 * Verifies that gray objects are on a dirty card.
124 */
125void CardTable::VerifyCardTable() {
126 UNIMPLEMENTED(WARNING) << "Card table verification";
127}
128
129} // namespace art