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