1 /*
   2  * Copyright (c) 2013, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGIONSET_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGIONSET_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeapRegion.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahPadding.hpp"
  31 
  32 class ShenandoahHeapRegionSet;
  33 
  34 class ShenandoahHeapRegionSetIterator : public StackObj {
  35 private:
  36   const ShenandoahHeapRegionSet* _set;
  37   ShenandoahHeap* const _heap;
  38 
  39   shenandoah_padding(0);
  40   volatile jint _current_index;
  41   shenandoah_padding(1);
  42 
  43   // No implicit copying: iterators should be passed by reference to capture the state
  44   ShenandoahHeapRegionSetIterator(const ShenandoahHeapRegionSetIterator& that);
  45   ShenandoahHeapRegionSetIterator& operator=(const ShenandoahHeapRegionSetIterator& o);
  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_idx)       const;
  87   inline bool is_in(oop 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_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGIONSET_HPP