1 /*
   2  * Copyright (c) 2016, 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_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 
  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                _live_data;
  46   size_t                _used;
  47   size_t                _region_count;
  48 
  49   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  50   volatile jint         _current_index;
  51   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  52 
  53 public:
  54   ShenandoahCollectionSet(ShenandoahHeap* heap, char* heap_base, size_t size);
  55 
  56   // Add region to collection set
  57   void add_region(ShenandoahHeapRegion* r);
  58   bool add_region_check_for_duplicates(ShenandoahHeapRegion* r);
  59 
  60   // Bring per-region statuses to consistency with this collection.
  61   // TODO: This is a transitional interface that bridges the gap between
  62   // region statuses and this collection. Should go away after we merge them.
  63   void update_region_status();
  64 
  65   // Remove region from collection set
  66   void remove_region(ShenandoahHeapRegion* r);
  67 
  68   // MT version
  69   ShenandoahHeapRegion* claim_next();
  70 
  71   // Single-thread version
  72   ShenandoahHeapRegion* next();
  73 
  74   size_t count()  const { return _region_count; }
  75   bool is_empty() const { return _region_count == 0; }
  76 
  77   void clear_current_index() {
  78     _current_index = 0;
  79   }
  80 
  81   inline bool is_in(ShenandoahHeapRegion* r) const;
  82   inline bool is_in(size_t region_number)    const;
  83   inline bool is_in(oop obj)                 const;
  84 
  85   void print_on(outputStream* out) const;
  86 
  87   size_t used()      const { return _used; }
  88   size_t live_data() const { return _live_data; }
  89   size_t garbage()   const { return _garbage;   }
  90   void clear();
  91 
  92 private:
  93   char* map_address() const {
  94     return _cset_map;
  95   }
  96   char* biased_map_address() const {
  97     return _biased_cset_map;
  98   }
  99 };
 100 
 101 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP