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