1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_G1_G1CONCURRENTMARKBITMAP_HPP
  26 #define SHARE_VM_GC_G1_G1CONCURRENTMARKBITMAP_HPP
  27 
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "oops/oopsHierarchy.hpp"
  32 #include "utilities/bitMap.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 class G1CMBitMap;
  37 class G1CMTask;
  38 class G1ConcurrentMark;
  39 class HeapRegion;
  40 
  41 // Closure for iteration over bitmaps
  42 class G1CMBitMapClosure VALUE_OBJ_CLASS_SPEC {
  43 private:
  44   G1ConcurrentMark* const _cm;
  45   G1CMTask* const _task;
  46 public:
  47   G1CMBitMapClosure(G1CMTask *task, G1ConcurrentMark* cm) : _task(task), _cm(cm) { }
  48 
  49   bool do_addr(HeapWord* const addr);
  50 };
  51 
  52 class G1CMBitMapMappingChangedListener : public G1MappingChangedListener {
  53  private:
  54   G1CMBitMap* _bm;
  55  public:
  56   G1CMBitMapMappingChangedListener() : _bm(NULL) {}
  57 
  58   void set_bitmap(G1CMBitMap* bm) { _bm = bm; }
  59 
  60   virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled);
  61 };
  62 
  63 // A generic mark bitmap for concurrent marking.  This is essentially a wrapper
  64 // around the BitMap class that is based on HeapWords, with one bit per (1 << _shifter) HeapWords.
  65 class G1CMBitMap VALUE_OBJ_CLASS_SPEC {
  66 private:
  67   MemRegion _covered;    // The heap area covered by this bitmap.
  68 
  69   const int _shifter;    // Shift amount from heap index to bit index in the bitmap.
  70 
  71   BitMapView _bm;        // The actual bitmap.
  72 
  73   G1CMBitMapMappingChangedListener _listener;
  74 
  75   inline void check_mark(HeapWord* addr) NOT_DEBUG_RETURN;
  76 
  77   // Convert from bit offset to address.
  78   HeapWord* offset_to_addr(size_t offset) const {
  79     return _covered.start() + (offset << _shifter);
  80   }
  81   // Convert from address to bit offset.
  82   size_t addr_to_offset(const HeapWord* addr) const {
  83     return pointer_delta(addr, _covered.start()) >> _shifter;
  84   }
  85 public:
  86   static size_t compute_size(size_t heap_size);
  87   // Returns the amount of bytes on the heap between two marks in the bitmap.
  88   static size_t mark_distance();
  89   // Returns how many bytes (or bits) of the heap a single byte (or bit) of the
  90   // mark bitmap corresponds to. This is the same as the mark distance above.
  91   static size_t heap_map_factor() {
  92     return mark_distance();
  93   }
  94 
  95   G1CMBitMap() : _covered(), _bm(), _shifter(LogMinObjAlignment), _listener() { _listener.set_bitmap(this); }
  96 
  97   // Initializes the underlying BitMap to cover the given area.
  98   void initialize(MemRegion heap, G1RegionToSpaceMapper* storage);
  99 
 100   // Read marks
 101   bool is_marked(oop obj) const;
 102   bool is_marked(HeapWord* addr) const {
 103     assert(_covered.contains(addr),
 104            "Address " PTR_FORMAT " is outside underlying space from " PTR_FORMAT " to " PTR_FORMAT,
 105            p2i(addr), p2i(_covered.start()), p2i(_covered.end()));
 106     return _bm.at(addr_to_offset(addr));
 107   }
 108 
 109   // Apply the closure to the addresses that correspond to marked bits in the bitmap.
 110   inline bool iterate(G1CMBitMapClosure* cl, MemRegion mr);
 111 
 112   // Return the address corresponding to the next marked bit at or after
 113   // "addr", and before "limit", if "limit" is non-NULL.  If there is no
 114   // such bit, returns "limit" if that is non-NULL, or else "endWord()".
 115   inline HeapWord* get_next_marked_addr(const HeapWord* addr,
 116                                         const HeapWord* limit) const;
 117 
 118   // The argument addr should be the start address of a valid object
 119   inline HeapWord* addr_after_obj(HeapWord* addr);
 120 
 121   void print_on_error(outputStream* st, const char* prefix) const;
 122 
 123   // Write marks.
 124   inline void mark(HeapWord* addr);
 125   inline void clear(HeapWord* addr);
 126   inline void clear(oop obj);
 127   inline bool par_mark(HeapWord* addr);
 128   inline bool par_mark(oop obj);
 129 
 130   void clear_range(MemRegion mr);
 131   void clear_region(HeapRegion* hr);
 132 };
 133 
 134 #endif // SHARE_VM_GC_G1_G1CONCURRENTMARKBITMAP_HPP