blob: 0871ed92b5f0595c1c953e199dde527ae390f277 [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"
Elliott Hughesf0605a02012-01-13 20:12:02 -080024#include "utils.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070025
26namespace art {
27/*
28 * Maintain a card table from the write barrier. All writes of
29 * non-NULL values to heap addresses should go through an entry in
30 * WriteBarrier, and from there to here.
31 *
32 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
33 * determined by GC_CARD_SHIFT. The card table contains one byte of
34 * data per card, to be used by the GC. The value of the byte will be
35 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
36 *
37 * After any store of a non-NULL object pointer into a heap object,
38 * code is obliged to mark the card dirty. The setters in
39 * object.h [such as SetFieldObject] do this for you. The
40 * compiler also contains code to mark cards as dirty.
41 *
42 * The card table's base [the "biased card table"] gets set to a
43 * rather strange value. In order to keep the JIT from having to
44 * fabricate or load GC_DIRTY_CARD to store into the card table,
45 * biased base is within the mmap allocation at a point where its low
46 * byte is equal to GC_DIRTY_CARD. See CardTable::Init for details.
47 */
48
jeffhao39da0352011-11-04 14:58:55 -070049CardTable* CardTable::Create(const byte* heap_base, size_t heap_max_size, size_t growth_size) {
Elliott Hughesf0605a02012-01-13 20:12:02 -080050 CardTable* bitmap = new CardTable;
51 bitmap->Init(heap_base, heap_max_size, growth_size);
52 return bitmap;
Ian Rogers5d76c432011-10-31 21:42:49 -070053}
54
55/*
56 * Initializes the card table; must be called before any other
57 * CardTable functions.
58 */
Elliott Hughesf0605a02012-01-13 20:12:02 -080059void CardTable::Init(const byte* heap_base, size_t heap_max_size, size_t growth_size) {
Ian Rogers5d76c432011-10-31 21:42:49 -070060 /* Set up the card table */
61 size_t length = heap_max_size / GC_CARD_SIZE;
62 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
Elliott Hughesf0605a02012-01-13 20:12:02 -080063 mem_map_.reset(MemMap::MapAnonymous("dalvik-card-table", NULL, length + 256, PROT_READ | PROT_WRITE));
64 if (mem_map_.get() == NULL) {
65 std::string maps;
66 ReadFileToString("/proc/self/maps", &maps);
67 LOG(FATAL) << "couldn't allocate card table\n" << maps;
Ian Rogers5d76c432011-10-31 21:42:49 -070068 }
Elliott Hughesf0605a02012-01-13 20:12:02 -080069 byte* alloc_base = mem_map_->GetAddress();
70 CHECK(alloc_base != NULL);
Ian Rogers5d76c432011-10-31 21:42:49 -070071 base_ = alloc_base;
jeffhao39da0352011-11-04 14:58:55 -070072 length_ = growth_size / GC_CARD_SIZE;
73 max_length_ = length;
Ian Rogers5d76c432011-10-31 21:42:49 -070074 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();
Ian Rogers5d76c432011-10-31 21:42:49 -070085}
86
87void CardTable::ClearCardTable() {
88 CHECK(mem_map_->GetAddress() != NULL);
89 memset(mem_map_->GetAddress(), GC_CARD_CLEAN, length_);
90}
91
92/*
93 * Returns the first address in the heap which maps to this card.
94 */
95void* CardTable::AddrFromCard(const byte *cardAddr) const {
96 CHECK(IsValidCard(cardAddr));
97 uintptr_t offset = cardAddr - biased_base_;
98 return (void *)(offset << GC_CARD_SHIFT);
99}
100
101void CardTable::Scan(byte* base, byte* limit, Callback* visitor, void* arg) const {
102 byte* cur = CardFromAddr(base);
103 byte* end = CardFromAddr(limit);
104 while (cur < end) {
105 while (cur < end && *cur == GC_CARD_CLEAN) {
106 cur++;
107 }
108 byte* run_start = cur;
109 size_t run = 0;
110 while (cur < end && *cur == GC_CARD_DIRTY) {
111 run++;
112 cur++;
113 }
114 if (run > 0) {
115 byte* run_end = &cur[run];
116 Heap::GetLiveBits()->VisitRange(reinterpret_cast<uintptr_t>(AddrFromCard(run_start)),
117 reinterpret_cast<uintptr_t>(AddrFromCard(run_end)),
118 visitor, arg);
119 }
120 }
121}
122
123/*
124 * Verifies that gray objects are on a dirty card.
125 */
126void CardTable::VerifyCardTable() {
127 UNIMPLEMENTED(WARNING) << "Card table verification";
128}
129
130} // namespace art