blob: 19629c3a19f9fa79b0b4c59b8798566f328d8368 [file] [log] [blame]
Ian Rogers5d76c432011-10-31 21:42:49 -07001// 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
17namespace art {
18
19class 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
26class CardTable {
27 public:
28 typedef void Callback(Object* obj, void* arg);
29
30 static CardTable* Create(const byte* heap_base, size_t heap_max_size);
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
50 private:
51
52 CardTable() {}
53
54 /*
55 * Initializes the card table; must be called before any other
56 * CardTable functions.
57 */
58 bool Init(const byte* heap_base, size_t heap_max_size);
59
60 /*
61 * Resets all of the bytes in the card table to clean.
62 */
63 void ClearCardTable();
64
65 /*
66 * Returns the address of the relevant byte in the card table, given
67 * an address on the heap.
68 */
69 byte* CardFromAddr(const void *addr) const {
70 byte *cardAddr = biased_base_ + ((uintptr_t)addr >> GC_CARD_SHIFT);
71 CHECK(IsValidCard(cardAddr));
72 return cardAddr;
73 }
74
75 /*
76 * Returns the first address in the heap which maps to this card.
77 */
78 void* AddrFromCard(const byte *card) const;
79
80 /*
81 * Returns true iff the address is within the bounds of the card table.
82 */
83 bool IsValidCard(const byte* cardAddr) const {
84 byte* begin = mem_map_->GetAddress() + offset_;
85 byte* end = &begin[length_];
86 return cardAddr >= begin && cardAddr < end;
87 }
88
89 /*
90 * Verifies that all gray objects are on a dirty card.
91 */
92 void VerifyCardTable();
93
94
95 UniquePtr<MemMap> mem_map_;
96 byte* base_;
97 byte* biased_base_;
98 size_t length_;
99 size_t offset_;
100};
101
102} // namespace art
103#endif // DALVIK_ALLOC_CARDTABLE_H_