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_SHENANDOAHFREESET_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHFREESET_HPP
  26 
  27 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 
  30 class ShenandoahFreeSet : public CHeapObj<mtGC> {
  31 private:
  32   ShenandoahHeap* const _heap;
  33   CHeapBitMap _mutator_free_bitmap;
  34   CHeapBitMap _collector_free_bitmap;
  35   size_t _max;
  36 
  37   // Left-most and right-most region indexes. There are no free regions outside
  38   // of [left-most; right-most] index intervals
  39   size_t _mutator_leftmost, _mutator_rightmost;
  40   size_t _collector_leftmost, _collector_rightmost;
  41 
  42   size_t _capacity;
  43   size_t _used;
  44 
  45   void assert_bounds() const PRODUCT_RETURN;
  46   void assert_heaplock_owned_by_current_thread() const PRODUCT_RETURN;
  47   void assert_heaplock_not_owned_by_current_thread() const PRODUCT_RETURN;
  48 
  49   bool is_mutator_free(size_t idx) const;
  50   bool is_collector_free(size_t idx) const;
  51 
  52   HeapWord* try_allocate_in(ShenandoahHeapRegion* region, ShenandoahAllocRequest& req, bool& in_new_region);
  53   HeapWord* allocate_single(ShenandoahAllocRequest& req, bool& in_new_region);
  54   HeapWord* allocate_contiguous(ShenandoahAllocRequest& req);
  55 
  56   void flip_to_gc(ShenandoahHeapRegion* r);
  57 
  58   void recompute_bounds();
  59   void adjust_bounds();
  60   bool touches_bounds(size_t num) const;
  61 
  62   void increase_used(size_t amount);
  63   void clear_internal();
  64 
  65   size_t collector_count() const { return _collector_free_bitmap.count_one_bits(); }
  66   size_t mutator_count()   const { return _mutator_free_bitmap.count_one_bits();   }
  67 
  68   void try_recycle_trashed(ShenandoahHeapRegion *r);
  69 
  70   bool is_empty_or_trash(ShenandoahHeapRegion *r);
  71   size_t alloc_capacity(ShenandoahHeapRegion *r);
  72   bool has_no_alloc_capacity(ShenandoahHeapRegion *r);
  73 
  74 public:
  75   ShenandoahFreeSet(ShenandoahHeap* heap, size_t max_regions);
  76 
  77   void clear();
  78   void rebuild();
  79 
  80   void recycle_trash();
  81 
  82   void log_status();
  83 
  84   size_t capacity()  const { return _capacity; }
  85   size_t used()      const { return _used;     }
  86   size_t available() const {
  87     assert(_used <= _capacity, "must use less than capacity");
  88     return _capacity - _used;
  89   }
  90 
  91   HeapWord* allocate(ShenandoahAllocRequest& req, bool& in_new_region);
  92   size_t unsafe_peek_free() const;
  93 
  94   void print_on(outputStream* out) const;
  95 };
  96 
  97 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHFREESET_HPP