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