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/memRegion.hpp"
  30 #include "oops/oopsHierarchy.hpp"
  31 #include "utilities/bitMap.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 class G1CMBitMap;
  36 class G1CMTask;
  37 class G1ConcurrentMark;
  38 class HeapRegion;
  39 
  40 // Closure for iteration over bitmaps
  41 class G1CMBitMapClosure {
  42 private:
  43   G1ConcurrentMark* const _cm;
  44   G1CMTask* const _task;
  45 public:
  46   G1CMBitMapClosure(G1CMTask *task, G1ConcurrentMark* cm) : _task(task), _cm(cm) { }
  47 
  48   bool do_addr(HeapWord* const addr);
  49 };
  50 
  51 class G1CMBitMapMappingChangedListener : public G1MappingChangedListener {
  52  private:
  53   G1CMBitMap* _bm;
  54  public:
  55   G1CMBitMapMappingChangedListener() : _bm(NULL) {}
  56 
  57   void set_bitmap(G1CMBitMap* bm) { _bm = bm; }
  58 
  59   virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled);
  60 };
  61 
  62 // A generic mark bitmap for concurrent marking.  This is essentially a wrapper
  63 // around the BitMap class that is based on HeapWords, with one bit per (1 << _shifter) HeapWords.
  64 class G1CMBitMap {
  65 private:
  66   MemRegion _covered;    // The heap area covered by this bitmap.
  67 
  68   const int _shifter;    // Shift amount from heap index to bit index in the bitmap.
  69 
  70   BitMapView _bm;        // The actual bitmap.
  71 
  72   G1CMBitMapMappingChangedListener _listener;
  73 
  74   inline void check_mark(HeapWord* addr) NOT_DEBUG_RETURN;
  75 
  76   // Convert from bit offset to address.
  77   HeapWord* offset_to_addr(size_t offset) const {
  78     return _covered.start() + (offset << _shifter);
  79   }
  80   // Convert from address to bit offset.
  81   size_t addr_to_offset(const HeapWord* addr) const {
  82     return pointer_delta(addr, _covered.start()) >> _shifter;
  83   }
  84 public:
  85   static size_t compute_size(size_t heap_size);
  86   // Returns the amount of bytes on the heap between two marks in the bitmap.
  87   static size_t mark_distance();
  88   // Returns how many bytes (or bits) of the heap a single byte (or bit) of the
  89   // mark bitmap corresponds to. This is the same as the mark distance above.
  90   static size_t heap_map_factor() {
  91     return mark_distance();
  92   }
  93 
  94   G1CMBitMap() : _covered(), _bm(), _shifter(LogMinObjAlignment), _listener() { _listener.set_bitmap(this); }
  95 
  96   // Initializes the underlying BitMap to cover the given area.
  97   void initialize(MemRegion heap, G1RegionToSpaceMapper* storage);
  98 
  99   // Read marks
 100   bool is_marked(oop obj) const;
 101   bool is_marked(HeapWord* addr) const {
 102     assert(_covered.contains(addr),
 103            "Address " PTR_FORMAT " is outside underlying space from " PTR_FORMAT " to " PTR_FORMAT,
 104            p2i(addr), p2i(_covered.start()), p2i(_covered.end()));
 105     return _bm.at(addr_to_offset(addr));
 106   }
 107 
 108   // Apply the closure to the addresses that correspond to marked bits in the bitmap.
 109   inline bool iterate(G1CMBitMapClosure* cl, MemRegion mr);
 110 
 111   // Return the address corresponding to the next marked bit at or after
 112   // "addr", and before "limit", if "limit" is non-NULL.  If there is no
 113   // such bit, returns "limit" if that is non-NULL, or else "endWord()".
 114   inline HeapWord* get_next_marked_addr(const HeapWord* addr,
 115                                         const HeapWord* limit) const;
 116 
 117   // The argument addr should be the start address of a valid object
 118   inline HeapWord* addr_after_obj(HeapWord* addr);
 119 
 120   void print_on_error(outputStream* st, const char* prefix) const;
 121 
 122   // Write marks.
 123   inline void mark(HeapWord* addr);
 124   inline void clear(HeapWord* addr);
 125   inline void clear(oop obj);
 126   inline bool par_mark(HeapWord* addr);
 127   inline bool par_mark(oop obj);
 128 
 129   void clear_range(MemRegion mr);
 130   void clear_region(HeapRegion* hr);
 131 };
 132 
 133 #endif // SHARE_VM_GC_G1_G1CONCURRENTMARKBITMAP_HPP