1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. 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_VM_GC_G1_G1BLOCKOFFSETTABLE_HPP
  26 #define SHARE_VM_GC_G1_G1BLOCKOFFSETTABLE_HPP
  27 
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "memory/virtualspace.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 // Forward declarations
  34 class G1BlockOffsetTable;
  35 class G1ContiguousSpace;
  36 
  37 // This implementation of "G1BlockOffsetTable" divides the covered region
  38 // into "N"-word subregions (where "N" = 2^"LogN".  An array with an entry
  39 // for each such subregion indicates how far back one must go to find the
  40 // start of the chunk that includes the first word of the subregion.
  41 //
  42 // Each G1BlockOffsetTablePart is owned by a G1ContiguousSpace.
  43 
  44 class G1BlockOffsetTable: public CHeapObj<mtGC> {
  45   friend class G1BlockOffsetTablePart;
  46   friend class VMStructs;
  47 
  48 private:
  49   // The reserved region covered by the table.
  50   MemRegion _reserved;
  51 
  52   // Array for keeping offsets for retrieving object start fast given an
  53   // address.
  54   u_char* _offset_array;          // byte array keeping backwards offsets
  55 
  56   void check_offset(size_t offset, const char* msg) const {
  57     assert(offset <= N_words,
  58            "%s - offset: " SIZE_FORMAT ", N_words: %u",
  59            msg, offset, (uint)N_words);
  60   }
  61 
  62   // Bounds checking accessors:
  63   // For performance these have to devolve to array accesses in product builds.
  64   inline u_char offset_array(size_t index) const;
  65 
  66   void set_offset_array_raw(size_t index, u_char offset) {
  67     _offset_array[index] = offset;
  68   }
  69 
  70   inline void set_offset_array(size_t index, u_char offset);
  71 
  72   inline void set_offset_array(size_t index, HeapWord* high, HeapWord* low);
  73 
  74   inline void set_offset_array(size_t left, size_t right, u_char offset);
  75 
  76   bool is_card_boundary(HeapWord* p) const;
  77 
  78   void check_index(size_t index, const char* msg) const NOT_DEBUG_RETURN;
  79 
  80 public:
  81 
  82   // Return the number of slots needed for an offset array
  83   // that covers mem_region_words words.
  84   static size_t compute_size(size_t mem_region_words) {
  85     size_t number_of_slots = (mem_region_words / N_words);
  86     return ReservedSpace::allocation_align_size_up(number_of_slots);
  87   }
  88 
  89   // Returns how many bytes of the heap a single byte of the BOT corresponds to.
  90   static size_t heap_map_factor() {
  91     return N_bytes;
  92   }
  93 
  94   enum SomePublicConstants {
  95     LogN = 9,
  96     LogN_words = LogN - LogHeapWordSize,
  97     N_bytes = 1 << LogN,
  98     N_words = 1 << LogN_words
  99   };
 100 
 101   // Initialize the table to cover from "base" to (at least)
 102   // "base + init_word_size".  In the future, the table may be expanded
 103   // (see "resize" below) up to the size of "_reserved" (which must be at
 104   // least "init_word_size".) The contents of the initial table are
 105   // undefined; it is the responsibility of the constituent
 106   // G1BlockOffsetTable(s) to initialize cards.
 107   G1BlockOffsetTable(MemRegion heap, G1RegionToSpaceMapper* storage);
 108 
 109   // Return the appropriate index into "_offset_array" for "p".
 110   inline size_t index_for(const void* p) const;
 111   inline size_t index_for_raw(const void* p) const;
 112 
 113   // Return the address indicating the start of the region corresponding to
 114   // "index" in "_offset_array".
 115   inline HeapWord* address_for_index(size_t index) const;
 116   // Variant of address_for_index that does not check the index for validity.
 117   inline HeapWord* address_for_index_raw(size_t index) const {
 118     return _reserved.start() + (index << LogN_words);
 119   }
 120 };
 121 
 122 class G1BlockOffsetTablePart VALUE_OBJ_CLASS_SPEC {
 123   friend class G1BlockOffsetTable;
 124   friend class VMStructs;
 125 private:
 126   enum SomePrivateConstants {
 127     N_words = G1BlockOffsetTable::N_words,
 128     LogN    = G1BlockOffsetTable::LogN
 129   };
 130 
 131   // allocation boundary at which offset array must be updated
 132   HeapWord* _next_offset_threshold;
 133   size_t    _next_offset_index;      // index corresponding to that boundary
 134 
 135   // This is the global BlockOffsetTable.
 136   G1BlockOffsetTable* _bot;
 137 
 138   // The space that owns this subregion.
 139   G1ContiguousSpace* _space;
 140 
 141   // Sets the entries
 142   // corresponding to the cards starting at "start" and ending at "end"
 143   // to point back to the card before "start": the interval [start, end)
 144   // is right-open.
 145   void set_remainder_to_point_to_start(HeapWord* start, HeapWord* end);
 146   // Same as above, except that the args here are a card _index_ interval
 147   // that is closed: [start_index, end_index]
 148   void set_remainder_to_point_to_start_incl(size_t start, size_t end);
 149 
 150   // Zero out the entry for _bottom (offset will be zero). Does not check for availability of the
 151   // memory first.
 152   void zero_bottom_entry_raw();
 153   // Variant of initialize_threshold that does not check for availability of the
 154   // memory first.
 155   HeapWord* initialize_threshold_raw();
 156 
 157   inline size_t block_size(const HeapWord* p) const;
 158 
 159   // Returns the address of a block whose start is at most "addr".
 160   // If "has_max_index" is true, "assumes "max_index" is the last valid one
 161   // in the array.
 162   inline HeapWord* block_at_or_preceding(const void* addr,
 163                                          bool has_max_index,
 164                                          size_t max_index) const;
 165 
 166   // "q" is a block boundary that is <= "addr"; "n" is the address of the
 167   // next block (or the end of the space.)  Return the address of the
 168   // beginning of the block that contains "addr".  Does so without side
 169   // effects (see, e.g., spec of  block_start.)
 170   inline HeapWord* forward_to_block_containing_addr_const(HeapWord* q, HeapWord* n,
 171                                                           const void* addr) const;
 172 
 173   // "q" is a block boundary that is <= "addr"; return the address of the
 174   // beginning of the block that contains "addr".  May have side effects
 175   // on "this", by updating imprecise entries.
 176   inline HeapWord* forward_to_block_containing_addr(HeapWord* q,
 177                                                     const void* addr);
 178 
 179   // "q" is a block boundary that is <= "addr"; "n" is the address of the
 180   // next block (or the end of the space.)  Return the address of the
 181   // beginning of the block that contains "addr".  May have side effects
 182   // on "this", by updating imprecise entries.
 183   HeapWord* forward_to_block_containing_addr_slow(HeapWord* q,
 184                                                   HeapWord* n,
 185                                                   const void* addr);
 186 
 187   // Requires that "*threshold_" be the first array entry boundary at or
 188   // above "blk_start", and that "*index_" be the corresponding array
 189   // index.  If the block starts at or crosses "*threshold_", records
 190   // "blk_start" as the appropriate block start for the array index
 191   // starting at "*threshold_", and for any other indices crossed by the
 192   // block.  Updates "*threshold_" and "*index_" to correspond to the first
 193   // index after the block end.
 194   void alloc_block_work(HeapWord** threshold_, size_t* index_,
 195                         HeapWord* blk_start, HeapWord* blk_end);
 196 
 197   void check_all_cards(size_t left_card, size_t right_card) const;
 198 
 199 public:
 200   //  The elements of the array are initialized to zero.
 201   G1BlockOffsetTablePart(G1BlockOffsetTable* array, G1ContiguousSpace* gsp);
 202 
 203   void verify() const;
 204 
 205   // Returns the address of the start of the block containing "addr", or
 206   // else "null" if it is covered by no block.  (May have side effects,
 207   // namely updating of shared array entries that "point" too far
 208   // backwards.  This can occur, for example, when lab allocation is used
 209   // in a space covered by the table.)
 210   inline HeapWord* block_start(const void* addr);
 211   // Same as above, but does not have any of the possible side effects
 212   // discussed above.
 213   inline HeapWord* block_start_const(const void* addr) const;
 214 
 215   // Initialize the threshold to reflect the first boundary after the
 216   // bottom of the covered region.
 217   HeapWord* initialize_threshold();
 218 
 219   void reset_bot() {
 220     zero_bottom_entry_raw();
 221     initialize_threshold_raw();
 222   }
 223 
 224   // Return the next threshold, the point at which the table should be
 225   // updated.
 226   HeapWord* threshold() const { return _next_offset_threshold; }
 227 
 228   // These must be guaranteed to work properly (i.e., do nothing)
 229   // when "blk_start" ("blk" for second version) is "NULL".  In this
 230   // implementation, that's true because NULL is represented as 0, and thus
 231   // never exceeds the "_next_offset_threshold".
 232   void alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
 233     if (blk_end > _next_offset_threshold) {
 234       alloc_block_work(&_next_offset_threshold, &_next_offset_index, blk_start, blk_end);
 235     }
 236   }
 237   void alloc_block(HeapWord* blk, size_t size) {
 238     alloc_block(blk, blk+size);
 239   }
 240 
 241   void set_for_starts_humongous(HeapWord* obj_top, size_t fill_size);
 242 
 243   void print_on(outputStream* out) PRODUCT_RETURN;
 244 };
 245 
 246 #endif // SHARE_VM_GC_G1_G1BLOCKOFFSETTABLE_HPP