• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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_RUNTIME_GC_ACCOUNTING_BITMAP_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_BITMAP_H_
19 
20 #include <limits.h>
21 #include <stdint.h>
22 #include <memory>
23 #include <set>
24 #include <vector>
25 
26 #include "base/locks.h"
27 #include "base/mem_map.h"
28 #include "runtime_globals.h"
29 
30 namespace art {
31 
32 namespace gc {
33 namespace accounting {
34 
35 // TODO: Use this code to implement SpaceBitmap.
36 class Bitmap {
37  public:
38   // Create and initialize a bitmap with size num_bits. Storage is allocated with a MemMap.
39   static Bitmap* Create(const std::string& name, size_t num_bits);
40 
41   // Initialize a space bitmap using the provided mem_map as the live bits. Takes ownership of the
42   // mem map. The address range covered starts at heap_begin and is of size equal to heap_capacity.
43   // Objects are kAlignement-aligned.
44   static Bitmap* CreateFromMemMap(MemMap&& mem_map, size_t num_bits);
45 
46   // offset is the difference from base to a index.
BitIndexToWordIndex(uintptr_t offset)47   static ALWAYS_INLINE constexpr size_t BitIndexToWordIndex(uintptr_t offset) {
48     return offset / kBitsPerBitmapWord;
49   }
50 
51   template<typename T>
WordIndexToBitIndex(T word_index)52   static ALWAYS_INLINE constexpr T WordIndexToBitIndex(T word_index) {
53     return static_cast<T>(word_index * kBitsPerBitmapWord);
54   }
55 
BitIndexToMask(uintptr_t bit_index)56   static ALWAYS_INLINE constexpr uintptr_t BitIndexToMask(uintptr_t bit_index) {
57     return static_cast<uintptr_t>(1) << (bit_index % kBitsPerBitmapWord);
58   }
59 
SetBit(size_t bit_index)60   ALWAYS_INLINE bool SetBit(size_t bit_index) {
61     return ModifyBit<true>(bit_index);
62   }
63 
ClearBit(size_t bit_index)64   ALWAYS_INLINE bool ClearBit(size_t bit_index) {
65     return ModifyBit<false>(bit_index);
66   }
67 
68   ALWAYS_INLINE bool TestBit(size_t bit_index) const;
69 
70   // Returns true if the bit_index was previously set.
71   ALWAYS_INLINE bool AtomicTestAndSetBit(size_t bit_index);
72 
73   // Fill the bitmap with zeroes.  Returns the bitmap's memory to the system as a side-effect.
74   void Clear();
75 
76   // Visit the all the set bits range [visit_begin, visit_end) where visit_begin and visit_end are
77   // bit indices visitor is called with the index of each set bit.
78   template <typename Visitor>
79   void VisitSetBits(uintptr_t visit_begin, size_t visit_end, const Visitor& visitor) const;
80 
81   void CopyFrom(Bitmap* source_bitmap);
82 
83   // Starting address of our internal storage.
Begin()84   uintptr_t* Begin() const {
85     return bitmap_begin_;
86   }
87 
88   // Size of our bitmap in bits.
BitmapSize()89   size_t BitmapSize() const {
90     return bitmap_size_;
91   }
92 
93   // Check that a bit index is valid with a DCHECK.
CheckValidBitIndex(size_t bit_index)94   ALWAYS_INLINE void CheckValidBitIndex(size_t bit_index) const {
95     DCHECK_LT(bit_index, BitmapSize());
96   }
97 
98   std::string Dump() const;
99 
100  protected:
101   static constexpr size_t kBitsPerBitmapWord = kBitsPerIntPtrT;
102 
103   Bitmap(MemMap&& mem_map, size_t bitmap_size);
104   ~Bitmap();
105 
106   // Allocate the mem-map for a bitmap based on how many bits are required.
107   static MemMap AllocateMemMap(const std::string& name, size_t num_bits);
108 
109   template<bool kSetBit>
110   ALWAYS_INLINE bool ModifyBit(uintptr_t bit_index);
111 
112   // Backing storage for bitmap. This is interpreted as an array of
113   // kBitsPerBitmapWord-sized integers, with bits assigned in each word little
114   // endian first.
115   MemMap mem_map_;
116 
117   // This bitmap itself, word sized for efficiency in scanning.
118   uintptr_t* const bitmap_begin_;
119 
120   // Number of bits in the bitmap.
121   const size_t bitmap_size_;
122 
123  private:
124   DISALLOW_IMPLICIT_CONSTRUCTORS(Bitmap);
125 };
126 
127 // One bit per kAlignment in range [start, end)
128 template<size_t kAlignment>
129 class MemoryRangeBitmap : public Bitmap {
130  public:
131   static MemoryRangeBitmap* Create(
132       const std::string& name, uintptr_t cover_begin, uintptr_t cover_end);
133   static MemoryRangeBitmap* CreateFromMemMap(
134       MemMap&& mem_map, uintptr_t cover_begin, size_t num_bits);
135 
136   // Beginning of the memory range that the bitmap covers.
CoverBegin()137   ALWAYS_INLINE uintptr_t CoverBegin() const {
138     return cover_begin_;
139   }
140 
141   // End of the memory range that the bitmap covers.
CoverEnd()142   ALWAYS_INLINE uintptr_t CoverEnd() const {
143     return cover_begin_ + kAlignment * BitmapSize();
144   }
145 
146   // Return the address associated with a bit index.
AddrFromBitIndex(size_t bit_index)147   ALWAYS_INLINE uintptr_t AddrFromBitIndex(size_t bit_index) const {
148     const uintptr_t addr = CoverBegin() + bit_index * kAlignment;
149     DCHECK_EQ(BitIndexFromAddr(addr), bit_index);
150     return addr;
151   }
152 
153   // Return the bit index associated with an address .
BitIndexFromAddr(uintptr_t addr)154   ALWAYS_INLINE uintptr_t BitIndexFromAddr(uintptr_t addr) const {
155     uintptr_t result = (addr - CoverBegin()) / kAlignment;
156     DCHECK(result < BitmapSize()) << CoverBegin() << " <= " <<  addr << " < " << CoverEnd();
157     return result;
158   }
159 
HasAddress(const uintptr_t addr)160   ALWAYS_INLINE bool HasAddress(const uintptr_t addr) const {
161     // Don't use BitIndexFromAddr() here as the addr passed to this function
162     // could be outside the range. If addr < cover_begin_, then the result
163     // underflows to some very large value past the end of the bitmap.
164     // Therefore, all operations are unsigned here.
165     bool ret = (addr - CoverBegin()) / kAlignment < BitmapSize();
166     if (ret) {
167       DCHECK(CoverBegin() <= addr && addr < CoverEnd())
168           << CoverBegin() << " <= " <<  addr << " < " << CoverEnd();
169     }
170     return ret;
171   }
172 
Set(uintptr_t addr)173   ALWAYS_INLINE bool Set(uintptr_t addr) {
174     return SetBit(BitIndexFromAddr(addr));
175   }
176 
Clear(uintptr_t addr)177   ALWAYS_INLINE bool Clear(uintptr_t addr) {
178     return ClearBit(BitIndexFromAddr(addr));
179   }
180 
Test(uintptr_t addr)181   ALWAYS_INLINE bool Test(uintptr_t addr) const {
182     return TestBit(BitIndexFromAddr(addr));
183   }
184 
185   // Returns true if the object was previously set.
AtomicTestAndSet(uintptr_t addr)186   ALWAYS_INLINE bool AtomicTestAndSet(uintptr_t addr) {
187     return AtomicTestAndSetBit(BitIndexFromAddr(addr));
188   }
189 
190  private:
MemoryRangeBitmap(MemMap && mem_map,uintptr_t begin,size_t num_bits)191   MemoryRangeBitmap(MemMap&& mem_map, uintptr_t begin, size_t num_bits)
192       : Bitmap(std::move(mem_map), num_bits),
193         cover_begin_(begin) {}
194 
195   uintptr_t const cover_begin_;
196 
197   DISALLOW_IMPLICIT_CONSTRUCTORS(MemoryRangeBitmap);
198 };
199 
200 }  // namespace accounting
201 }  // namespace gc
202 }  // namespace art
203 
204 #endif  // ART_RUNTIME_GC_ACCOUNTING_BITMAP_H_
205