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 
  33   // Allow scan_and_forward to call (private) overrides for auxiliary functions on this class
  34   template <typename SpaceType>
  35   friend void CompactibleSpace::scan_and_forward(SpaceType* space, CompactPoint* cp);
  36   template <typename SpaceType>
  37   friend void CompactibleSpace::scan_and_adjust_pointers(SpaceType* space);
  38   template <typename SpaceType>
  39   friend void CompactibleSpace::scan_and_compact(SpaceType* space);
  40 
  41 private:
  42   // Auxiliary functions for scan_and_forward support.
  43   // See comments for CompactibleSpace for more information.
  44   inline HeapWord* scan_limit() const {
  45     return top();
  46   }
  47 
  48   inline bool scanned_block_is_obj(const HeapWord* addr) const {
  49     return true; // Always true, since scan_limit is top
  50   }
  51 
  52   inline size_t scanned_block_size(const HeapWord* addr) const {
  53     oop obj = oop(addr+1);
  54     size_t size = obj->size() + 1;
  55     return size;
  56   }
  57 
  58     // Auxiliary functions for scan_and_{forward,adjust_pointers,compact} support.
  59   inline size_t adjust_obj_size(size_t size) const {
  60     return size + 1;
  61   }
  62 
  63   inline size_t obj_size(const HeapWord* addr) const {
  64     return oop(addr+1)->size() + 1;
  65   }
  66 
  67   inline oop make_oop(HeapWord* addr) const {
  68     return oop(addr+1);
  69   }
  70 
  71   inline oop compact_oop(HeapWord* addr) const {
  72     return oop(addr + 1);
  73   }
  74 
  75 public:
  76   static size_t RegionSizeBytes;
  77   static size_t RegionSizeShift;
  78 
  79 private:
  80   int _region_number;
  81   volatile size_t liveData;
  82   MemRegion reserved;
  83   bool _is_in_collection_set;
  84 
  85   bool _humongous_start;
  86   bool _humongous_continuation;
  87 
  88 #ifdef ASSERT
  89   int _mem_protection_level;
  90 #endif
  91 
  92 public:
  93   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
  94 
  95   jint initialize_heap_region(HeapWord* start, size_t regionSize, int index);
  96 
  97 
  98   int region_number() const;
  99 
 100   // Roll back the previous allocation of an object with specified size.
 101   // Returns TRUE when successful, FALSE if not successful or not supported.
 102   bool rollback_allocation(uint size);
 103 
 104   void clearLiveData();
 105   void setLiveData(size_t s);
 106   void increase_live_data(size_t s);
 107 
 108   size_t getLiveData() const;
 109 
 110   void print_on(outputStream* st) const;
 111 
 112   size_t garbage() const;
 113 
 114   void recycle();
 115   void reset();
 116 
 117   void oop_iterate_skip_unreachable(ExtendedOopClosure* cl, bool skip_unreachable_objects);
 118 
 119   void object_iterate_interruptible(ObjectClosure* blk, bool allow_cancel);
 120 
 121   HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
 122 
 123   HeapWord* block_start_const(const void* p) const;
 124 
 125   // Just before GC we need to fill the current region.
 126   void fill_region();
 127 
 128   bool is_in_collection_set() const;
 129 
 130   void set_is_in_collection_set(bool b);
 131 
 132   void set_humongous_start(bool start);
 133   void set_humongous_continuation(bool continuation);
 134 
 135   bool is_humongous() const;
 136   bool is_humongous_start() const;
 137   bool is_humongous_continuation() const;
 138 
 139 #ifdef ASSERT
 140   void memProtectionOn();
 141   void memProtectionOff();
 142 #endif
 143 
 144   static ByteSize is_in_collection_set_offset();
 145   // The following are for humongous regions.  We need to save the
 146   markOop saved_mark_word;
 147   void save_mark_word(oop obj) {saved_mark_word = obj->mark();}
 148   markOop mark_word() {return saved_mark_word;}
 149 
 150   virtual CompactibleSpace* next_compaction_space() const;
 151 
 152   // Override for scan_and_forward support.
 153   void prepare_for_compaction(CompactPoint* cp);
 154   void adjust_pointers();
 155   void compact();
 156 
 157 private:
 158   void do_reset();
 159 
 160 };
 161 
 162 
 163 
 164 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP