1 /*
   2  * Copyright (c) 2012, 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 #include "precompiled.hpp"
  26 #include "gc_implementation/g1/g1HeapSpanningTable.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 
  29 void G1HeapSpanningTableBase::clear(void* array) {
  30   memset(array, DefaultValue, array_size_bytes());
  31 }
  32 
  33 bool G1HeapSpanningTableBase::check_cleared(void* array) {
  34   char* carray = (char*) array;
  35   for (size_t i = 0; i < array_size_bytes(); i += 1) {
  36     if (carray[i] != (char) DefaultValue) {
  37       return false;
  38     }
  39   }
  40   return true;
  41 }
  42 
  43 void* G1HeapSpanningTableBase::create_new_array_base() {
  44   void* array = (void*) NEW_C_HEAP_ARRAY(char, array_size_bytes(), mtGC);
  45   clear(array);
  46   return array;
  47 }
  48 
  49 HeapWord* G1HeapSpanningTableBase::update_heap_end(HeapWord* new_end) {
  50   HeapWord* old_heap_end = _heap_end;
  51 
  52   // length_for() will verify new_end so we don't have to do it here too.
  53   uint new_length = length_for(new_end);
  54   _length = new_length;
  55   if (new_length > _length_high_watermark) {
  56     _length_high_watermark = new_length;
  57   }
  58   _heap_end = new_end;
  59 
  60   return old_heap_end;
  61 }
  62 
  63 #ifndef PRODUCT
  64 void G1HeapSpanningTableBase::verify_optional() {
  65   uint length_for_heap_end = length_for(_heap_end);
  66   guarantee(length_for_heap_end == _length,
  67             err_msg("invariant: _length: %u _heap_end: "PTR_FORMAT" %u",
  68                     _length, _heap_end, length_for_heap_end));
  69   guarantee(_length <= _length_high_watermark,
  70             err_msg("invariant: _length: %u _length_high_watermark: %u",
  71                     _length, _length_high_watermark));
  72   guarantee(_length_high_watermark <= _max_length,
  73             err_msg("invariant: _length_high_watermark: %u _max_length: %u",
  74                     _length_high_watermark, _max_length));
  75 }
  76 #endif // PRODUCT
  77 
  78 void G1HeapSpanningTableBase::initialize_base(HeapWord* bottom,
  79                                               HeapWord* max_end,
  80                                               size_t elem_size_bytes,
  81                                               uint shift_by) {
  82   _shift_by              = shift_by;
  83   _elem_size_bytes       = elem_size_bytes;
  84 
  85   _heap_bottom           = bottom;
  86   _heap_end              = bottom;
  87   _max_heap_end          = max_end;
  88 
  89   _length                = 0;
  90   _length_high_watermark = 0;
  91   _max_length            = length_for(max_end);
  92 }