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 public:
  71   static size_t RegionSizeBytes;
  72   static size_t RegionSizeShift;
  73 
  74 private:
  75   int _region_number;
  76   volatile size_t liveData;
  77   MemRegion reserved;
  78   bool _is_in_collection_set;
  79 
  80   bool _humongous_start;
  81   bool _humongous_continuation;
  82 
  83 #ifdef ASSERT
  84   int _mem_protection_level;
  85 #endif
  86 
  87 public:
  88   static void setup_heap_region_size(size_t initial_heap_size, size_t max_heap_size);
  89 
  90   jint initialize_heap_region(HeapWord* start, size_t regionSize, int index);
  91 
  92 
  93   int region_number() const;
  94 
  95   // Roll back the previous allocation of an object with specified size.
  96   // Returns TRUE when successful, FALSE if not successful or not supported.
  97   bool rollback_allocation(uint size);
  98 
  99   void clearLiveData();
 100   void setLiveData(size_t s);
 101   void increase_live_data(size_t s);
 102 
 103   size_t getLiveData() const;
 104 
 105   void print_on(outputStream* st) const;
 106 
 107   size_t garbage() const;
 108 
 109   void recycle();
 110   void reset();
 111 
 112   void oop_iterate_skip_unreachable(ExtendedOopClosure* cl, bool skip_unreachable_objects);
 113 
 114   void object_iterate_interruptible(ObjectClosure* blk, bool allow_cancel);
 115 
 116   HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
 117 
 118   HeapWord* block_start_const(const void* p) const;
 119 
 120   // Just before GC we need to fill the current region.
 121   void fill_region();
 122 
 123   bool is_in_collection_set() const;
 124 
 125   void set_is_in_collection_set(bool b);
 126 
 127   void set_humongous_start(bool start);
 128   void set_humongous_continuation(bool continuation);
 129 
 130   bool is_humongous() const;
 131   bool is_humongous_start() const;
 132   bool is_humongous_continuation() const;
 133 
 134 #ifdef ASSERT
 135   void memProtectionOn();
 136   void memProtectionOff();
 137 #endif
 138 
 139   static ByteSize is_in_collection_set_offset();
 140   // The following are for humongous regions.  We need to save the
 141   markOop saved_mark_word;
 142   void save_mark_word(oop obj) {saved_mark_word = obj->mark();}
 143   markOop mark_word() {return saved_mark_word;}
 144 
 145   virtual CompactibleSpace* next_compaction_space() const;
 146 
 147   // Override for scan_and_forward support.
 148   void prepare_for_compaction(CompactPoint* cp);
 149   void adjust_pointers();
 150   void compact();
 151 
 152   virtual oop compact_oop(HeapWord* addr) const {
 153     return oop(addr + 1);
 154   }
 155 private:
 156   void do_reset();
 157 
 158 };
 159 
 160 
 161 
 162 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_HPP