blob: fa79d5daefa6cd81414ef2f72fe281cad8b2a343 [file] [log] [blame]
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001/*
2 * Copyright (C) 2008 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#ifndef ART_SRC_SPACE_BITMAP_H_
18#define ART_SRC_SPACE_BITMAP_H_
19
20#include <limits.h>
21#include <stdint.h>
22#include <vector>
23
24#include "UniquePtr.h"
25#include "globals.h"
26#include "logging.h"
27#include "mem_map.h"
28#include "utils.h"
29
30namespace art {
31
32class Object;
33
34class SpaceBitmap {
35 public:
36 static const size_t kAlignment = 8;
37
38 typedef void Callback(Object* obj, void* arg);
39
40 typedef void ScanCallback(Object* obj, void* finger, void* arg);
41
42 typedef void SweepCallback(size_t ptr_count, Object** ptrs, void* arg);
43
44 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
45 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
46 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
47
48 ~SpaceBitmap();
49
50 // <offset> is the difference from .base to a pointer address.
51 // <index> is the index of .bits that contains the bit representing
52 // <offset>.
53 static size_t OffsetToIndex(size_t offset) {
54 return offset / kAlignment / kBitsPerWord;
55 }
56
57 static uintptr_t IndexToOffset(size_t index) {
58 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
59 }
60
61 // Pack the bits in backwards so they come out in address order when using CLZ.
62 static word OffsetToMask(uintptr_t offset_) {
63 return 1 << (sizeof(word) * 8 - 1 - (offset_ / kAlignment) % kBitsPerWord);
64 }
65
66 inline void Set(const Object* obj) {
67 Modify(obj, true);
68 }
69
70 inline void Clear(const Object* obj) {
71 Modify(obj, false);
72 }
73
74 void Clear();
75
76 inline bool Test(const Object* obj) const {
77 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
78 DCHECK(HasAddress(obj)) << obj;
79 DCHECK(bitmap_begin_ != NULL);
80 DCHECK_GE(addr, heap_begin_);
81 if (addr <= heap_end_) {
82 const uintptr_t offset = addr - heap_begin_;
83 return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0;
84 } else {
85 return false;
86 }
87 }
88
89 bool HasAddress(const void* addr) const;
90
91 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
92
93 class ClearVisitor {
94 public:
95 explicit ClearVisitor(SpaceBitmap* const bitmap)
96 : bitmap_(bitmap) {
97 }
98
99 void operator ()(Object* obj) const {
100 bitmap_->Clear(obj);
101 }
102 private:
103 SpaceBitmap* const bitmap_;
104 };
105
106 template <typename Visitor>
107 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
108 for (; visit_begin < visit_end; visit_begin += kAlignment ) {
109 visitor(reinterpret_cast<Object*>(visit_begin));
110 }
111 }
112
113 template <typename Visitor>
114 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
115 size_t start = OffsetToIndex(visit_begin - heap_begin_);
116 size_t end = OffsetToIndex(visit_end - heap_begin_ - 1);
117 for (size_t i = start; i <= end; i++) {
118 word w = bitmap_begin_[i];
119 if (w != 0) {
120 word high_bit = 1 << (kBitsPerWord - 1);
121 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
122 do {
123 const int shift = CLZ(w);
124 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
125 visitor(obj);
126 w &= ~(high_bit >> shift);
127 } while (w != 0);
128 }
129 }
130 }
131
132 void Walk(Callback* callback, void* arg);
133
134 void InOrderWalk(Callback* callback, void* arg);
135
136 void ScanWalk(uintptr_t base, uintptr_t max, ScanCallback* thunk, void* arg);
137
138 static void SweepWalk(const SpaceBitmap& live,
139 const SpaceBitmap& mark,
140 uintptr_t base, uintptr_t max,
141 SweepCallback* thunk, void* arg);
142
143 private:
144 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
145 // however, we document that this is expected on heap_end_
146 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
147 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
148 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)), heap_end_(heap_begin_ - 1),
149 name_(name) {}
150
151 inline void Modify(const Object* obj, bool do_set) {
152 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
153 DCHECK_GE(addr, heap_begin_);
154 const uintptr_t offset = addr - heap_begin_;
155 const size_t index = OffsetToIndex(offset);
156 const word mask = OffsetToMask(offset);
157 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
158 if (do_set) {
159 if (addr > heap_end_) {
160 heap_end_ = addr;
161 }
162 bitmap_begin_[index] |= mask;
163 } else {
164 bitmap_begin_[index] &= ~mask;
165 }
166 }
167
168 // Backing storage for bitmap.
169 UniquePtr<MemMap> mem_map_;
170
171 // This bitmap itself, word sized for efficiency in scanning.
172 word* const bitmap_begin_;
173
174 // Size of this bitmap.
175 const size_t bitmap_size_;
176
177 // The base address of the heap, which corresponds to the word containing the first bit in the
178 // bitmap.
179 const uintptr_t heap_begin_;
180
181 // The highest pointer value ever returned by an allocation from
182 // this heap. I.e., the highest address that may correspond to a
183 // set bit. If there are no bits set, (heap_end_ < heap_begin_).
184 uintptr_t heap_end_;
185
186 // Name of this bitmap.
187 std::string name_;
188};
189
190} // namespace art
191
192#endif // ART_SRC_SPACE_BITMAP_H_