blob: 8fde9fe32a58e905a9a3cb6dbff273eaf5173a62 [file] [log] [blame]
Ian Rogers5d76c432011-10-31 21:42:49 -07001/*
Elliott Hughes2faa5f12012-01-30 14:42:07 -08002 * Copyright (C) 2011 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.
Ian Rogers5d76c432011-10-31 21:42:49 -070015 */
16
17#ifndef DALVIK_ALLOC_CARDTABLE_H_
18#define DALVIK_ALLOC_CARDTABLE_H_
19
20#include "globals.h"
21#include "logging.h"
22#include "mem_map.h"
23#include "UniquePtr.h"
24
25namespace art {
26
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070027class Heap;
28class HeapBitmap;
Ian Rogers5d76c432011-10-31 21:42:49 -070029class Object;
30
31#define GC_CARD_SHIFT 7
32#define GC_CARD_SIZE (1 << GC_CARD_SHIFT)
33#define GC_CARD_CLEAN 0
34#define GC_CARD_DIRTY 0x70
35
Elliott Hughes2faa5f12012-01-30 14:42:07 -080036// Maintain a card table from the the write barrier. All writes of
37// non-NULL values to heap addresses should go through an entry in
38// WriteBarrier, and from there to here.
Ian Rogers5d76c432011-10-31 21:42:49 -070039class CardTable {
40 public:
Ian Rogers30fab402012-01-23 15:43:46 -080041 static CardTable* Create(const byte* heap_begin, size_t heap_capacity);
Ian Rogers5d76c432011-10-31 21:42:49 -070042
Ian Rogers30fab402012-01-23 15:43:46 -080043 // Set the card associated with the given address to GC_CARD_DIRTY.
Ian Rogers5d76c432011-10-31 21:42:49 -070044 void MarkCard(const void *addr) {
Elliott Hughes24edeb52012-06-18 15:29:46 -070045 byte* card_addr = CardFromAddr(addr);
46 *card_addr = GC_CARD_DIRTY;
Ian Rogers5d76c432011-10-31 21:42:49 -070047 }
48
Ian Rogers30fab402012-01-23 15:43:46 -080049 // Is the object on a dirty card?
Ian Rogers5d76c432011-10-31 21:42:49 -070050 bool IsDirty(const Object* obj) const {
51 return *CardFromAddr(obj) == GC_CARD_DIRTY;
52 }
53
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070054 // Visit cards within memory range.
55 template <typename Visitor>
56 void VisitClear(const void* start, const void* end, const Visitor& visitor) {
57 byte* card_start = CardFromAddr(start);
58 byte* card_end = CardFromAddr(end);
59 for (byte* cur = card_start; cur != card_end; ++cur) {
60 if (*cur == GC_CARD_DIRTY) {
61 *cur = GC_CARD_CLEAN;
62 visitor(cur);
63 }
64 }
65 }
66
Ian Rogers30fab402012-01-23 15:43:46 -080067 // Returns a value that when added to a heap address >> GC_CARD_SHIFT will address the appropriate
68 // card table byte. For convenience this value is cached in every Thread
69 byte* GetBiasedBegin() const {
70 return biased_begin_;
jeffhao39da0352011-11-04 14:58:55 -070071 }
72
Ian Rogers30fab402012-01-23 15:43:46 -080073 // For every dirty card between begin and end invoke the visitor with the specified argument
74 typedef void Callback(Object* obj, void* arg);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070075 void Scan(HeapBitmap* bitmap, byte* begin, byte* end, Callback* visitor, void* arg) const;
Ian Rogers30fab402012-01-23 15:43:46 -080076
Ian Rogers30fab402012-01-23 15:43:46 -080077 // Assertion used to check the given address is covered by the card table
78 void CheckAddrIsInCardTable(const byte* addr) const;
79
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070080 // Resets all of the bytes in the card table to clean.
81 void ClearCardTable();
82
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070083 // Resets all of the bytes in the card table which do not map to the image space.
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070084 void ClearNonImageSpaceCards(Heap* heap);
Ian Rogers5d76c432011-10-31 21:42:49 -070085
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070086 // Returns the first address in the heap which maps to this card.
87 void* AddrFromCard(const byte *card_addr) const {
88 DCHECK(IsValidCard(card_addr))
89 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
90 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
91 << " end: " << reinterpret_cast<void*>(mem_map_->End());
92 uintptr_t offset = card_addr - biased_begin_;
93 return reinterpret_cast<void*>(offset << GC_CARD_SHIFT);
94 }
Elliott Hughesa168c832012-06-12 15:34:20 -070095 private:
96 CardTable(MemMap* begin, byte* biased_begin, size_t offset);
Ian Rogers5d76c432011-10-31 21:42:49 -070097
Ian Rogers30fab402012-01-23 15:43:46 -080098 // Returns the address of the relevant byte in the card table, given an address on the heap.
Ian Rogers5d76c432011-10-31 21:42:49 -070099 byte* CardFromAddr(const void *addr) const {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700100 byte *card_addr = biased_begin_ + ((uintptr_t)addr >> GC_CARD_SHIFT);
Ian Rogers30fab402012-01-23 15:43:46 -0800101 // Sanity check the caller was asking for address covered by the card table
Elliott Hughes24edeb52012-06-18 15:29:46 -0700102 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
103 << " card_addr: " << reinterpret_cast<void*>(card_addr);
104 return card_addr;
Ian Rogers5d76c432011-10-31 21:42:49 -0700105 }
106
Ian Rogers30fab402012-01-23 15:43:46 -0800107 // Returns true iff the card table address is within the bounds of the card table.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700108 bool IsValidCard(const byte* card_addr) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800109 byte* begin = mem_map_->Begin() + offset_;
110 byte* end = mem_map_->End();
Elliott Hughes24edeb52012-06-18 15:29:46 -0700111 return card_addr >= begin && card_addr < end;
Ian Rogers5d76c432011-10-31 21:42:49 -0700112 }
113
Ian Rogers30fab402012-01-23 15:43:46 -0800114 // Verifies that all gray objects are on a dirty card.
Ian Rogers5d76c432011-10-31 21:42:49 -0700115 void VerifyCardTable();
116
Ian Rogers30fab402012-01-23 15:43:46 -0800117 // Mmapped pages for the card table
Ian Rogers5d76c432011-10-31 21:42:49 -0700118 UniquePtr<MemMap> mem_map_;
Ian Rogers30fab402012-01-23 15:43:46 -0800119 // Value used to compute card table addresses from object addresses, see GetBiasedBegin
120 byte* const biased_begin_;
121 // Card table doesn't begin at the beginning of the mem_map_, instead it is displaced by offset
122 // to allow the byte value of biased_begin_ to equal GC_CARD_DIRTY
123 const size_t offset_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700124};
125
126} // namespace art
127#endif // DALVIK_ALLOC_CARDTABLE_H_