1 /*
   2  * Copyright (c) 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 #include "precompiled.hpp"
  25 #include "gc/z/zObjArrayAllocator.hpp"
  26 #include "gc/z/zUtils.inline.hpp"
  27 #include "memory/universe.hpp"
  28 #include "oops/arrayKlass.hpp"
  29 #include "runtime/interfaceSupport.inline.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "runtime/os.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 // To avoid delaying safepoints, clearing of arrays is split up in segments
  36 // with safepoint polling inbetween. However, we can't have a not-yet-cleared
  37 // array of oops on the heap when we safepoint since the GC will then stumble
  38 // across uninitialized oops. To avoid this we let an array of oops be an
  39 // array of a primitive type of the same size until the clearing has completed.
  40 // A max segment size of 64K was chosen because benchmarking suggests that is
  41 // offers a good trade-off between allocation time and time-to-safepoint.
  42 
  43 static Klass* substitute_object_array_klass(Klass* klass) {
  44   if (!klass->is_objArray_klass()) {
  45     return klass;
  46   }
  47 
  48   Klass* const substitute_klass = Universe::longArrayKlassObj();
  49   const BasicType type = ArrayKlass::cast(klass)->element_type();
  50   const BasicType substitute_type = ArrayKlass::cast(substitute_klass)->element_type();
  51   assert(type2aelembytes(type) == type2aelembytes(substitute_type), "Element size mismatch");
  52   return substitute_klass;
  53 }
  54 
  55 ZObjArrayAllocator::ZObjArrayAllocator(Klass* klass, size_t word_size, int length, Thread* thread) :
  56     ObjArrayAllocator(substitute_object_array_klass(klass), word_size, length, false /* do_zero */, thread),
  57     _final_klass(klass) {}
  58 
  59 oop ZObjArrayAllocator::finish(HeapWord* mem) const {
  60   HandleMark hm;
  61   Handle array(_thread, ObjArrayAllocator::finish(mem));
  62 
  63   // Clear array
  64   const size_t segment_max = ZUtils::bytes_to_words(64 * K);
  65   const size_t skip = arrayOopDesc::header_size(ArrayKlass::cast(_klass)->element_type());
  66   size_t remaining = _word_size - skip;
  67 
  68   while (remaining > 0) {
  69     // Clear segment
  70     const size_t segment = MIN2(remaining, segment_max);
  71     Copy::zero_to_words((HeapWord*)array() + (_word_size - remaining), segment);
  72     remaining -= segment;
  73 
  74     // Safepoint
  75     ThreadBlockInVM tbivm((JavaThread*)_thread);
  76   }
  77 
  78   if (_klass != _final_klass) {
  79     // Set final klass
  80     oopDesc::release_set_klass((HeapWord*)array(), _final_klass);
  81   }
  82 
  83   return array();
  84 }