1 /*
   2  * Copyright (c) 2016, 2020, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  31 #include "gc/shenandoah/shenandoahPadding.hpp"
  32 
  33 class ShenandoahCollectionSet : public CHeapObj<mtGC> {
  34   friend class ShenandoahHeap;
  35 private:
  36   size_t const          _map_size;
  37   size_t const          _region_size_bytes_shift;
  38   ReservedSpace         _map_space;
  39   char* const           _cset_map;
  40   // Bias cset map's base address for fast test if an oop is in cset
  41   char* const           _biased_cset_map;
  42 
  43   ShenandoahHeap* const _heap;
  44 
  45   size_t                _garbage;
  46   size_t                _used;
  47   size_t                _region_count;
  48 
  49   shenandoah_padding(0);
  50   volatile jint         _current_index;
  51   shenandoah_padding(1);
  52 
  53 public:
  54   ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base);
  55 
  56   // Add region to collection set
  57   void add_region(ShenandoahHeapRegion* r);
  58 
  59   // MT version
  60   ShenandoahHeapRegion* claim_next();
  61 
  62   // Single-thread version
  63   ShenandoahHeapRegion* next();
  64 
  65   size_t count()  const { return _region_count; }
  66   bool is_empty() const { return _region_count == 0; }
  67 
  68   void clear_current_index() {
  69     _current_index = 0;
  70   }
  71 
  72   inline bool is_in(ShenandoahHeapRegion* r) const;
  73   inline bool is_in(size_t region_idx)       const;
  74   inline bool is_in(oop obj)                 const;
  75   inline bool is_in_loc(void* loc)           const;
  76 
  77   void print_on(outputStream* out) const;
  78 
  79   size_t used()      const { return _used; }
  80   size_t garbage()   const { return _garbage;   }
  81   void clear();
  82 
  83 private:
  84   char* map_address() const {
  85     return _cset_map;
  86   }
  87   char* biased_map_address() const {
  88     return _biased_cset_map;
  89   }
  90 };
  91 
  92 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP