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   BitMap::bm_word_t _segment_live_bits;
 49   BitMap::bm_word_t _segment_claim_bits;
 50   ZBitMap           _bitmap;
 51 
 52   const BitMapView segment_live_bits() const;
 53   const BitMapView segment_claim_bits() const;
 54 
 55   BitMapView segment_live_bits();
 56   BitMapView segment_claim_bits();
 57 
 58   BitMap::idx_t segment_start(BitMap::idx_t segment) const;
 59   BitMap::idx_t segment_end(BitMap::idx_t segment) const;
 60 
 61   bool is_segment_live(BitMap::idx_t segment) const;
 62   bool set_segment_live(BitMap::idx_t segment);
 63 
 64   BitMap::idx_t first_live_segment() const;
 65   BitMap::idx_t next_live_segment(BitMap::idx_t segment) const;
 66   BitMap::idx_t index_to_segment(BitMap::idx_t index) const;
 67 
 68   bool claim_segment(BitMap::idx_t segment);
 69 
 70   void initialize_bitmap();
 71 
 72   void reset(ZGenerationId id);
 73   void reset_segment(BitMap::idx_t segment);
 74 
 75   size_t do_object(ObjectClosure* cl, zaddress addr) const;
 76 
 77   template <typename Function>
 78   void iterate_segment(BitMap::idx_t segment, Function function);
 79 
 80 public:
 81   ZLiveMap(uint32_t object_max_count);
 82   ZLiveMap(const ZLiveMap& other) = delete;
 83 
 84   void reset();
 85 
 86   bool is_marked(ZGenerationId id) const;
 87 
 88   uint32_t live_objects() const;
 89   size_t live_bytes() const;
 90 
 91   bool get(ZGenerationId id, BitMap::idx_t index) const;
 92   bool set(ZGenerationId id, BitMap::idx_t index, bool finalizable, bool& inc_live);
 93 
 94   void inc_live(uint32_t objects, size_t bytes);
 95 
 96   template <typename Function>
 97   void iterate(ZGenerationId id, Function function);
 98 
 99   BitMap::idx_t find_base_bit(BitMap::idx_t index);
100   BitMap::idx_t find_base_bit_in_segment(BitMap::idx_t start, BitMap::idx_t index);
101 };
102 
103 #endif // SHARE_GC_Z_ZLIVEMAP_HPP