1 /*
   2  * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates.
   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_SHENANDOAHHEAPREGION_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP
  26 
  27 #include "gc/shared/space.hpp"
  28 #include "memory/universe.hpp"
  29 #include "utilities/sizes.hpp"
  30 
  31 class ShenandoahHeapRegion : public ContiguousSpace {
  32 private:
  33   static Monitor _mem_protect_lock;
  34 
  35 public:
  36   static size_t RegionSizeBytes;
  37   static size_t RegionSizeShift;
  38 
  39 private:
  40   ShenandoahHeap* _heap;
  41   size_t _region_number;
  42   volatile jint _live_data;
  43   MemRegion reserved;
  44 
  45   bool _humongous_start;
  46   bool _humongous_continuation;
  47   bool _humongous_obj_array;
  48 
  49   bool _recycled;
  50   bool _root;
  51 
  52   HeapWord* _new_top;
  53 
  54   volatile jint _critical_pins;
  55 
  56 #ifdef ASSERT
  57   int _mem_protection_level;
  58 #endif
  59 
  60 public:
  61   ShenandoahHeapRegion(ShenandoahHeap* heap, HeapWord* start, size_t regionSize, size_t index);
  62 
  63   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
  64 
  65   size_t region_number() const;
  66 
  67   // Allocation (return NULL if full)
  68   inline HeapWord* allocate(size_t word_size);
  69 
  70   // Roll back the previous allocation of an object with specified size.
  71   // Returns TRUE when successful, FALSE if not successful or not supported.
  72   bool rollback_allocation(uint size);
  73 
  74   void clear_live_data();
  75   void set_live_data(size_t s);
  76   inline void increase_live_data_words(jint s);
  77 
  78   void set_recently_allocated(bool value);
  79   bool is_recently_allocated() const;
  80 
  81   bool has_live() const;
  82   size_t get_live_data_bytes() const;
  83   size_t get_live_data_words() const;
  84 
  85   void print_on(outputStream* st) const;
  86 
  87   size_t garbage() const;
  88 
  89   void recycle();
  90 
  91   void object_iterate_interruptible(ObjectClosure* blk, bool allow_cancel);
  92 
  93   HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
  94   void oop_iterate(ExtendedOopClosure* cl);
  95 
  96 private:
  97   void oop_iterate_objects(ExtendedOopClosure* cl);
  98   void oop_iterate_humongous(ExtendedOopClosure* cl);
  99 
 100 public:
 101   HeapWord* block_start_const(const void* p) const;
 102 
 103   // Just before GC we need to fill the current region.
 104   void fill_region();
 105 
 106   bool in_collection_set() const;
 107 
 108   void set_in_collection_set(bool b);
 109 
 110   void set_humongous_start(bool start);
 111   void set_humongous_continuation(bool continuation);
 112   void set_humongous_obj_array(bool obj_array);
 113 
 114   bool is_humongous() const;
 115   bool is_humongous_start() const;
 116   bool is_humongous_continuation() const;
 117   bool is_humongous_obj_array() const;
 118 
 119 #ifdef ASSERT
 120   void memProtectionOn();
 121   void memProtectionOff();
 122 #endif
 123 
 124   // The following are for humongous regions.  We need to save the
 125   markOop saved_mark_word;
 126   void save_mark_word(oop obj) {saved_mark_word = obj->mark();}
 127   markOop mark_word() {return saved_mark_word;}
 128 
 129   virtual CompactibleSpace* next_compaction_space() const;
 130 
 131   // Override for scan_and_forward support.
 132   void prepare_for_compaction(CompactPoint* cp);
 133   void adjust_pointers();
 134   void compact();
 135 
 136   void set_new_top(HeapWord* new_top) { _new_top = new_top; }
 137   HeapWord* new_top() const { return _new_top; }
 138 
 139   void pin();
 140   void unpin();
 141 
 142   bool is_pinned();
 143 
 144   void set_root(bool r) {
 145     _root = r;
 146   }
 147   bool is_root() {
 148     return _root;
 149   }
 150 };
 151 
 152 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP