1 /*
   2  * Copyright (c) 2015, 2018, 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_SHARED_CMBITMAP_HPP
  26 #define SHARE_VM_GC_SHARED_CMBITMAP_HPP
  27 
  28 #include "memory/memRegion.hpp"
  29 #include "utilities/bitMap.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 // A generic CM bit map.  This is essentially a wrapper around the BitMap
  33 // class, with one bit per (1<<_shifter) HeapWords.
  34 
  35 class MarkBitMapRO {
  36  protected:
  37   MemRegion _covered;      // The heap area covered by this bitmap.
  38   HeapWord* _bmStartWord;  // base address of range covered by map
  39   size_t    _bmWordSize;   // map size (in #HeapWords covered)
  40   const int _shifter;      // map to char or bit
  41   BitMapView _bm;          // the bit map itself
  42 
  43  public:
  44   // constructor
  45   MarkBitMapRO(int shifter);
  46 
  47   // inquiries
  48   HeapWord* startWord()   const { return _bmStartWord; }
  49   // the following is one past the last word in space
  50   HeapWord* endWord()     const { return _bmStartWord + _bmWordSize; }
  51 
  52   // read marks
  53 
  54   bool isMarked(HeapWord* addr) const {
  55     assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
  56            "outside underlying space?");
  57     return _bm.at(heapWordToOffset(addr));
  58   }
  59 
  60   // iteration
  61   inline bool iterate(BitMapClosure* cl, MemRegion mr);
  62 
  63   // Return the address corresponding to the next marked bit at or after
  64   // "addr", and before "limit", if "limit" is non-NULL.  If there is no
  65   // such bit, returns "limit" if that is non-NULL, or else "endWord()".
  66   inline HeapWord* getNextMarkedWordAddress(const HeapWord* addr,
  67                                      const HeapWord* limit = NULL) const;
  68 
  69   // conversion utilities
  70   HeapWord* offsetToHeapWord(size_t offset) const {
  71     return _bmStartWord + (offset << _shifter);
  72   }
  73   size_t heapWordToOffset(const HeapWord* addr) const {
  74     return pointer_delta(addr, _bmStartWord) >> _shifter;
  75   }
  76 
  77   // The argument addr should be the start address of a valid object
  78   inline HeapWord* nextObject(HeapWord* addr);
  79 
  80   void print_on_error(outputStream* st, const char* prefix) const;
  81 
  82   // debugging
  83   NOT_PRODUCT(bool covers(MemRegion rs) const;)
  84 };
  85 
  86 class MarkBitMap : public MarkBitMapRO {
  87  private:
  88   // Clear bitmap range
  89   void do_clear(MemRegion mr, bool large);
  90 
  91  public:
  92   static size_t compute_size(size_t heap_size);
  93   // Returns the amount of bytes on the heap between two marks in the bitmap.
  94   static size_t mark_distance();
  95   // Returns how many bytes (or bits) of the heap a single byte (or bit) of the
  96   // mark bitmap corresponds to. This is the same as the mark distance above.  static size_t heap_map_factor() {
  97   static size_t heap_map_factor() {
  98     return mark_distance();
  99   }
 100 
 101   MarkBitMap() : MarkBitMapRO(LogMinObjAlignment) {}
 102 
 103   // Initializes the underlying BitMap to cover the given area.
 104   void initialize(MemRegion heap, MemRegion bitmap);
 105 
 106   // Write marks.
 107   inline void mark(HeapWord* addr);
 108   inline void clear(HeapWord* addr);
 109   inline bool parMark(HeapWord* addr);
 110 
 111   // Clear range. For larger regions, use *_large.
 112   void clear()                         { do_clear(_covered, true); }
 113   void clear_range(MemRegion mr)       { do_clear(mr, false);      }
 114   void clear_range_large(MemRegion mr) { do_clear(mr, true);       }
 115 };
 116 
 117 #endif // SHARE_VM_GC_SHARED_CMBITMAP_HPP