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