1 /*
  2  * Copyright (c) 2015, 2026, 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 #ifndef SHARE_GC_Z_ZLIVEMAP_HPP
 25 #define SHARE_GC_Z_ZLIVEMAP_HPP
 26 
 27 #include "gc/z/zAddress.hpp"
 28 #include "gc/z/zBitMap.hpp"
 29 #include "gc/z/zGenerationId.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "runtime/atomic.hpp"
 32 
 33 class ObjectClosure;
 34 
 35 class ZLiveMap {
 36   friend class ZLiveMapTest;
 37 
 38 private:
 39   static const uint32_t NumSegments = 64;
 40   static const uint32_t BitsPerObject = 2;
 41 
 42   const uint32_t    _segment_size;
 43   const int         _segment_shift;
 44 
 45   Atomic<uint32_t>  _seqnum;
 46   Atomic<uint32_t>  _live_objects;
 47   Atomic<size_t>    _live_bytes;
 48   Atomic<uint32_t>  _will_expand_objects;
 49   BitMap::bm_word_t _segment_live_bits;
 50   BitMap::bm_word_t _segment_claim_bits;
 51   ZBitMap           _bitmap;
 52 
 53   const BitMapView segment_live_bits() const;
 54   const BitMapView segment_claim_bits() const;
 55 
 56   BitMapView segment_live_bits();
 57   BitMapView segment_claim_bits();
 58 
 59   BitMap::idx_t segment_start(BitMap::idx_t segment) const;
 60   BitMap::idx_t segment_end(BitMap::idx_t segment) const;
 61 
 62   bool is_segment_live(BitMap::idx_t segment) const;
 63   bool set_segment_live(BitMap::idx_t segment);
 64 
 65   BitMap::idx_t first_live_segment() const;
 66   BitMap::idx_t next_live_segment(BitMap::idx_t segment) const;
 67   BitMap::idx_t index_to_segment(BitMap::idx_t index) const;
 68 
 69   bool claim_segment(BitMap::idx_t segment);
 70 
 71   void initialize_bitmap();
 72 
 73   void reset(ZGenerationId id);
 74   void reset_segment(BitMap::idx_t segment);
 75 
 76   size_t do_object(ObjectClosure* cl, zaddress addr) const;
 77 
 78   template <typename Function>
 79   void iterate_segment(BitMap::idx_t segment, Function function);
 80 
 81 public:
 82   ZLiveMap(uint32_t object_max_count);
 83   ZLiveMap(const ZLiveMap& other) = delete;
 84 
 85   void reset();
 86 
 87   bool is_marked(ZGenerationId id) const;
 88 
 89   uint32_t live_objects() const;
 90   size_t live_bytes() const;
 91   uint32_t will_expand_objects() const;
 92 
 93   bool get(ZGenerationId id, BitMap::idx_t index) const;
 94   bool set(ZGenerationId id, BitMap::idx_t index, bool finalizable, bool& inc_live);
 95 
 96   void inc_live(uint32_t objects, size_t bytes);
 97   void inc_will_expand(uint32_t objects);
 98 
 99   template <typename Function>
100   void iterate(ZGenerationId id, Function function);
101 
102   BitMap::idx_t find_base_bit(BitMap::idx_t index);
103   BitMap::idx_t find_base_bit_in_segment(BitMap::idx_t start, BitMap::idx_t index);
104 };
105 
106 #endif // SHARE_GC_Z_ZLIVEMAP_HPP