1 /*
  2  * Copyright (c) 2018, 2023, 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_GC_SHARED_MARKBITMAP_HPP
 26 #define SHARE_GC_SHARED_MARKBITMAP_HPP
 27 
 28 #include "memory/memRegion.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "utilities/bitMap.hpp"
 31 
 32 // A generic mark bitmap for concurrent marking.  This is essentially a wrapper
 33 // around the BitMap class that is based on HeapWords, with one bit per (1 << _shifter) HeapWords.
 34 class MarkBitMap {
 35 protected:
 36   MemRegion _covered;    // The heap area covered by this bitmap.
 37 
 38   const int _shifter;    // Shift amount from heap index to bit index in the bitmap.
 39 
 40   BitMapView _bm;        // The actual bitmap.
 41 
 42   virtual void check_mark(HeapWord* addr) NOT_DEBUG_RETURN;
 43 
 44   // Convert from bit offset to address.
 45   HeapWord* offset_to_addr(size_t offset) const {
 46     return _covered.start() + (offset << _shifter);
 47   }
 48   // Convert from address to bit offset.
 49   size_t addr_to_offset(const HeapWord* addr) const {
 50     return pointer_delta(addr, _covered.start()) >> _shifter;
 51   }
 52 
 53   // Clear bitmap range
 54   void do_clear(MemRegion mr, bool large);
 55 
 56   // Returns the amount of bytes on the heap between two marks in the bitmap.
 57   static size_t mark_distance();
 58 
 59 public:
 60   static size_t compute_size(size_t heap_size);
 61   // Returns how many bytes (or bits) of the heap a single byte (or bit) of the
 62   // mark bitmap corresponds to. This is the same as the mark distance above.
 63   static size_t heap_map_factor() {
 64     return mark_distance();
 65   }
 66 
 67   MarkBitMap() : _covered(), _shifter(LogMinObjAlignment), _bm() {}
 68 
 69   // Initializes the underlying BitMap to cover the given area.
 70   void initialize(MemRegion heap, MemRegion storage);
 71 
 72   // Read marks
 73   bool is_marked(oop obj) const;
 74   bool is_marked(HeapWord* addr) const {
 75     assert(_covered.contains(addr),
 76            "Address " PTR_FORMAT " is outside underlying space from " PTR_FORMAT " to " PTR_FORMAT,
 77            p2i(addr), p2i(_covered.start()), p2i(_covered.end()));
 78     return _bm.at(addr_to_offset(addr));
 79   }
 80 
 81   // Return the address corresponding to the next marked bit at or after
 82   // "addr", and before "limit", if "limit" is non-null.  If there is no
 83   // such bit, returns "limit" if that is non-null, or else "endWord()".
 84   inline HeapWord* get_next_marked_addr(const HeapWord* addr,
 85                                         HeapWord* limit) const;
 86 
 87   void print_on_error(outputStream* st, const char* prefix) const;
 88 
 89   // Write marks.
 90   inline void mark(HeapWord* addr);
 91   inline void mark(oop obj);
 92   inline void clear(HeapWord* addr);
 93   inline void clear(oop obj);
 94   inline bool par_mark(HeapWord* addr);
 95   inline bool par_mark(oop obj);
 96 
 97   // Clear bitmap.
 98   void clear()                         { do_clear(_covered, true); }
 99   void clear_range(MemRegion mr)       { do_clear(mr, false);      }
100   void clear_range_large(MemRegion mr) { do_clear(mr, true);       }
101 
102   size_t count_marked(MemRegion mr);
103 };
104 
105 #endif // SHARE_GC_SHARED_MARKBITMAP_HPP