blob: a2616be7dd0889bc9d8afeed3dbb2bc55a3fba32 [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
jeffhao39da0352011-11-04 14:58:55 -070030 static CardTable* Create(const byte* heap_base, size_t heap_max_size, size_t growth_size);
Ian Rogers5d76c432011-10-31 21:42:49 -070031
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
jeffhao39da0352011-11-04 14:58:55 -070050 void ClearGrowthLimit() {
51 CHECK_GE(max_length_, length_);
52 length_ = max_length_;
53 }
54
Ian Rogers5d76c432011-10-31 21:42:49 -070055 private:
56
57 CardTable() {}
58
59 /*
60 * Initializes the card table; must be called before any other
61 * CardTable functions.
62 */
jeffhao39da0352011-11-04 14:58:55 -070063 bool Init(const byte* heap_base, size_t heap_max_size, size_t growth_size);
Ian Rogers5d76c432011-10-31 21:42:49 -070064
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_;
jeffhao39da0352011-11-04 14:58:55 -0700104 size_t max_length_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700105 size_t offset_;
106};
107
108} // namespace art
109#endif // DALVIK_ALLOC_CARDTABLE_H_