1 /*
   2  * Copyright (c) 2013, 2019, 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_SHENANDOAHHEAPREGIONSET_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGIONSET_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 class ShenandoahHeapRegionSet;
  34 
  35 class ShenandoahHeapRegionSetIterator : public StackObj {
  36 private:
  37   const ShenandoahHeapRegionSet* _set;
  38   ShenandoahHeap* const _heap;
  39 
  40   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile jint));
  41   volatile jint _current_index;
  42   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  43 
  44   // No implicit copying: iterators should be passed by reference to capture the state
  45   NONCOPYABLE(ShenandoahHeapRegionSetIterator);
  46 
  47 public:
  48   ShenandoahHeapRegionSetIterator(const ShenandoahHeapRegionSet* const set);
  49 
  50   // Reset existing iterator to new set
  51   void reset(const ShenandoahHeapRegionSet* const set);
  52 
  53   // MT version
  54   ShenandoahHeapRegion* claim_next();
  55 
  56   // Single-thread version
  57   ShenandoahHeapRegion* next();
  58 };
  59 
  60 class ShenandoahHeapRegionSet : public CHeapObj<mtGC> {
  61   friend class ShenandoahHeap;
  62 private:
  63   ShenandoahHeap* const _heap;
  64   size_t const          _map_size;
  65   size_t const          _region_size_bytes_shift;
  66   jbyte* const          _set_map;
  67   // Bias set map's base address for fast test if an oop is in set
  68   jbyte* const          _biased_set_map;
  69   size_t                _region_count;
  70 
  71 public:
  72   ShenandoahHeapRegionSet();
  73   ~ShenandoahHeapRegionSet();
  74 
  75   // Add region to set
  76   void add_region(ShenandoahHeapRegion* r);
  77   bool add_region_check_for_duplicates(ShenandoahHeapRegion* r);
  78 
  79   // Remove region from set
  80   void remove_region(ShenandoahHeapRegion* r);
  81 
  82   size_t count()  const { return _region_count; }
  83   bool is_empty() const { return _region_count == 0; }
  84 
  85   inline bool is_in(ShenandoahHeapRegion* r) const;
  86   inline bool is_in(size_t region_number)    const;
  87   inline bool is_in(HeapWord* p)             const;
  88 
  89   void print_on(outputStream* out) const;
  90 
  91   void clear();
  92 
  93 private:
  94   jbyte* biased_map_address() const {
  95     return _biased_set_map;
  96   }
  97 };
  98 
  99 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGIONSET_HPP