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 
  31 class ShenandoahCollectionSet : public CHeapObj<mtGC> {
  32   friend class ShenandoahHeap;
  33 private:
  34   size_t const          _map_size;
  35   size_t const          _region_size_bytes_shift;
  36   jbyte* const          _cset_map;
  37   // Bias cset map's base address for fast test if an oop is in cset
  38   jbyte* const          _biased_cset_map;
  39 
  40   ShenandoahHeap* const _heap;
  41 
  42   size_t                _garbage;
  43   size_t                _live_data;
  44   size_t                _used;
  45   size_t                _region_count;
  46 
  47   char _pad0[DEFAULT_CACHE_LINE_SIZE];
  48   volatile jint         _current_index;
  49   char _pad1[DEFAULT_CACHE_LINE_SIZE];
  50 
  51 public:
  52   ShenandoahCollectionSet(ShenandoahHeap* heap, HeapWord* heap_base);
  53 
  54   // Add region to collection set
  55   void add_region(ShenandoahHeapRegion* r);
  56 
  57   // Bring per-region statuses to consistency with this collection.
  58   // TODO: This is a transitional interface that bridges the gap between
  59   // region statuses and this collection. Should go away after we merge them.
  60   void update_region_status();
  61 
  62   // Remove region from collection set
  63   void remove_region(ShenandoahHeapRegion* r);
  64 
  65   // MT version
  66   ShenandoahHeapRegion* claim_next();
  67 
  68   // Single-thread version
  69   ShenandoahHeapRegion* next();
  70 
  71   size_t count()  const { return _region_count; }
  72   bool is_empty() const { return _region_count == 0; }
  73 
  74   void clear_current_index() {
  75     _current_index = 0;
  76   }
  77 
  78   inline bool is_in(ShenandoahHeapRegion* r) const;
  79   inline bool is_in(size_t region_number)    const;
  80   inline bool is_in(HeapWord* p)             const;
  81 
  82   void print_on(outputStream* out) const;
  83 
  84   size_t used()      const { return _used; }
  85   size_t live_data() const { return _live_data; }
  86   size_t garbage()   const { return _garbage;   }
  87   void clear();
  88 
  89 private:
  90   jbyte* biased_map_address() const {
  91     return _biased_cset_map;
  92   }
  93 };
  94 
  95 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP