1 /*
   2  * Copyright (c) 2018, 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 "classfile/systemDictionary.hpp"
  27 #include "gc/shared/fill.hpp"
  28 #include "gc/shared/memAllocator.hpp"
  29 #include "utilities/copy.hpp"
  30 #include "utilities/debug.hpp"
  31 
  32 bool Fill::_disabled;
  33 size_t Fill::_extra_header_size;
  34 size_t Fill::_min_size;
  35 size_t Fill::_max_size;
  36 
  37 size_t Fill::object_header_size() {
  38   return oopDesc::header_size() + _extra_header_size;
  39 }
  40 
  41 size_t Fill::array_header_size() {
  42   return typeArrayOopDesc::header_size(T_INT) + _extra_header_size;
  43 }
  44 
  45 size_t Fill::array_max_payload_size() {
  46   return (typeArrayOopDesc::max_array_length(T_INT) * sizeof(jint)) / HeapWordSize;
  47 }
  48 
  49 size_t Fill::array_length(size_t size) {
  50   return (size - array_header_size()) * (HeapWordSize / sizeof(jint));
  51 }
  52 
  53 void Fill::initialize() {
  54   _disabled = false;
  55   set_extra_header_size(0);
  56 }
  57 
  58 void Fill::set_disabled() {
  59   _disabled = true;
  60 }
  61 
  62 void Fill::set_extra_header_size(size_t extra_header_size) {
  63   _extra_header_size = extra_header_size;
  64   _min_size = align_object_size(object_header_size());
  65   _max_size = align_object_size(array_header_size() + array_max_payload_size());
  66 }
  67 
  68 void Fill::set_max_size(size_t max_size) {
  69   _max_size = max_size;
  70 }
  71 
  72 size_t Fill::min_reserve_size() {
  73   const size_t object_size = object_header_size();
  74   return (object_size > (size_t)MinObjAlignment) ? align_object_size(object_size) : 0;
  75 }
  76 
  77 void Fill::fill_with_object(HeapWord* start) {
  78   const size_t size = object_header_size();
  79   ObjAllocator allocator(SystemDictionary::Object_klass(), size);
  80   allocator.initialize(start);
  81 }
  82 
  83 void Fill::fill_with_array(HeapWord* start, size_t size) {
  84   const size_t length = array_length(size);
  85   ObjArrayAllocator allocator(Universe::intArrayKlassObj(), size, (int)length, false /* do_zero */);
  86   allocator.initialize(start);
  87 
  88   if (ZapFillerObjects) {
  89     HeapWord* const payload_start = start + array_header_size();
  90     const size_t payload_size = size - array_header_size();
  91     Copy::fill_to_words(payload_start, payload_size, 0XDEAFBABE);
  92   }
  93 }
  94 
  95 void Fill::range(HeapWord* start, size_t size) {
  96   assert(is_object_aligned(start), "Misaligned start");
  97   assert(is_object_aligned(size), "Misaligned size");
  98   assert(is_object_aligned(min_size()), "Misaligned min size");
  99   assert(is_object_aligned(max_size()), "Misaligned max sizz");
 100   assert(min_size() <= max_size(), "Invalid min/max size");
 101 
 102   if (_disabled) {
 103     return;
 104   }
 105 
 106   const size_t min = min_size();
 107   const size_t max = max_size();
 108 
 109   while (size >= min) {
 110     if (size > max) {
 111       fill_with_array(start, max);
 112       start += max;
 113       size -= max;
 114     } else if (size > min) {
 115       fill_with_array(start, size);
 116       break;
 117     } else /* if (size == min) */ {
 118       fill_with_object(start);
 119       break;
 120     }
 121   }
 122 }
 123 
 124 void Fill::range(HeapWord* start, HeapWord* end) {
 125   Fill::range(start, pointer_delta(end, start));
 126 }