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 CMBitMapRO VALUE_OBJ_CLASS_SPEC {
  37  protected:
  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   BitMap    _bm;               // the bit map itself
  42 
  43  public:
  44   // constructor
  45   CMBitMapRO(int shifter);
  46 
  47   enum { do_yield = true };
  48 
  49   // inquiries
  50   HeapWord* startWord()   const { return _bmStartWord; }
  51   size_t    sizeInWords() const { return _bmWordSize;  }
  52   // the following is one past the last word in space
  53   HeapWord* endWord()     const { return _bmStartWord + _bmWordSize; }
  54 
  55   // read marks
  56 
  57   bool isMarked(HeapWord* addr) const {
  58     assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
  59            "outside underlying space?");
  60     return _bm.at(heapWordToOffset(addr));
  61   }
  62 
  63   // iteration
  64   inline bool iterate(BitMapClosure* cl, MemRegion mr);
  65   inline bool iterate(BitMapClosure* cl);
  66 
  67   // Return the address corresponding to the next marked bit at or after
  68   // "addr", and before "limit", if "limit" is non-NULL.  If there is no
  69   // such bit, returns "limit" if that is non-NULL, or else "endWord()".
  70   HeapWord* getNextMarkedWordAddress(const HeapWord* addr,
  71                                      const HeapWord* limit = NULL) const;
  72   // Return the address corresponding to the next unmarked bit at or after
  73   // "addr", and before "limit", if "limit" is non-NULL.  If there is no
  74   // such bit, returns "limit" if that is non-NULL, or else "endWord()".
  75   HeapWord* getNextUnmarkedWordAddress(const HeapWord* addr,
  76                                        const HeapWord* limit = NULL) const;
  77 
  78   // conversion utilities
  79   HeapWord* offsetToHeapWord(size_t offset) const {
  80     return _bmStartWord + (offset << _shifter);
  81   }
  82   size_t heapWordToOffset(const HeapWord* addr) const {
  83     return pointer_delta(addr, _bmStartWord) >> _shifter;
  84   }
  85   int heapWordDiffToOffsetDiff(size_t diff) const;
  86 
  87   // The argument addr should be the start address of a valid object
  88   HeapWord* nextObject(HeapWord* addr) {
  89     oop obj = (oop) addr;
  90     HeapWord* res =  addr + obj->size();
  91     assert(offsetToHeapWord(heapWordToOffset(res)) == res, "sanity");
  92     return res;
  93   }
  94 
  95   void print_on_error(outputStream* st, const char* prefix) const;
  96 
  97   // debugging
  98   NOT_PRODUCT(bool covers(MemRegion rs) const;)
  99 };
 100 
 101 class CMBitMap : public CMBitMapRO {
 102 
 103  public:
 104   static size_t compute_size(size_t heap_size);
 105   // Returns the amount of bytes on the heap between two marks in the bitmap.
 106   static size_t mark_distance();
 107   // Returns how many bytes (or bits) of the heap a single byte (or bit) of the
 108   // mark bitmap corresponds to. This is the same as the mark distance above.  static size_t heap_map_factor() {
 109   static size_t heap_map_factor() {
 110     return mark_distance();
 111   }
 112 
 113   CMBitMap() : CMBitMapRO(LogMinObjAlignment) {}
 114 
 115   // Initializes the underlying BitMap to cover the given area.
 116   void initialize(MemRegion heap, MemRegion bitmap);
 117 
 118   // Write marks.
 119   inline void mark(HeapWord* addr);
 120   inline void clear(HeapWord* addr);
 121   inline bool parMark(HeapWord* addr);
 122   inline bool parClear(HeapWord* addr);
 123 
 124   void markRange(MemRegion mr);
 125   void parMarkRange(MemRegion mr);
 126   void clearRange(MemRegion mr);
 127 
 128   // Starting at the bit corresponding to "addr" (inclusive), find the next
 129   // "1" bit, if any.  This bit starts some run of consecutive "1"'s; find
 130   // the end of this run (stopping at "end_addr").  Return the MemRegion
 131   // covering from the start of the region corresponding to the first bit
 132   // of the run to the end of the region corresponding to the last bit of
 133   // the run.  If there is no "1" bit at or after "addr", return an empty
 134   // MemRegion.
 135   MemRegion getAndClearMarkedRegion(HeapWord* addr, HeapWord* end_addr);
 136 
 137   // Clear the whole mark bitmap.
 138   virtual void clearAll();
 139 };
 140 
 141 #endif // SHARE_VM_GC_SHARED_CMBITMAP_HPP