1 /*
   2  * Copyright (c) 2001, 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 #ifndef SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
  26 #define SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP
  27 
  28 #include "classfile/javaClasses.hpp"
  29 #include "gc/shared/allocTracer.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
  32 #include "memory/universe.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/jvmtiExport.hpp"
  36 #include "runtime/sharedRuntime.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 #include "services/lowMemoryDetector.hpp"
  40 #include "utilities/align.hpp"
  41 #include "utilities/copy.hpp"
  42 
  43 // Inline allocation implementations.
  44 
  45 void CollectedHeap::post_allocation_setup_common(Klass* klass,
  46                                                  HeapWord* obj_ptr) {
  47   post_allocation_setup_no_klass_install(klass, obj_ptr);
  48   oop obj = (oop)obj_ptr;
  49 #if (INCLUDE_G1GC || INCLUDE_CMSGC)
  50   // Need a release store to ensure array/class length, mark word, and
  51   // object zeroing are visible before setting the klass non-NULL, for
  52   // concurrent collectors.
  53   obj->release_set_klass(klass);
  54 #else
  55   obj->set_klass(klass);
  56 #endif
  57 }
  58 
  59 void CollectedHeap::post_allocation_setup_no_klass_install(Klass* klass,
  60                                                            HeapWord* obj_ptr) {
  61   oop obj = (oop)obj_ptr;
  62 
  63   assert(obj != NULL, "NULL object pointer");
  64   if (UseBiasedLocking && (klass != NULL)) {
  65     obj->set_mark_raw(klass->prototype_header());
  66   } else {
  67     // May be bootstrapping
  68     obj->set_mark_raw(markOopDesc::prototype());
  69   }
  70 }
  71 
  72 // Support for jvmti and dtrace
  73 inline void post_allocation_notify(Klass* klass, oop obj, int size) {
  74   // support low memory notifications (no-op if not enabled)
  75   LowMemoryDetector::detect_low_memory_for_collected_pools();
  76 
  77   // support for JVMTI VMObjectAlloc event (no-op if not enabled)
  78   JvmtiExport::vm_object_alloc_event_collector(obj);
  79 
  80   if (DTraceAllocProbes) {
  81     // support for Dtrace object alloc event (no-op most of the time)
  82     if (klass != NULL && klass->name() != NULL) {
  83       SharedRuntime::dtrace_object_alloc(obj, size);
  84     }
  85   }
  86 }
  87 
  88 void CollectedHeap::post_allocation_setup_obj(Klass* klass,
  89                                               HeapWord* obj_ptr,
  90                                               int size) {
  91   post_allocation_setup_common(klass, obj_ptr);
  92   oop obj = (oop)obj_ptr;
  93   assert(Universe::is_bootstrapping() ||
  94          !obj->is_array(), "must not be an array");
  95   // notify jvmti and dtrace
  96   post_allocation_notify(klass, obj, size);
  97 }
  98 
  99 void CollectedHeap::post_allocation_setup_class(Klass* klass,
 100                                                 HeapWord* obj_ptr,
 101                                                 int size) {
 102   // Set oop_size field before setting the _klass field because a
 103   // non-NULL _klass field indicates that the object is parsable by
 104   // concurrent GC.
 105   oop new_cls = (oop)obj_ptr;
 106   assert(size > 0, "oop_size must be positive.");
 107   java_lang_Class::set_oop_size(new_cls, size);
 108   post_allocation_setup_common(klass, obj_ptr);
 109   assert(Universe::is_bootstrapping() ||
 110          !new_cls->is_array(), "must not be an array");
 111   // notify jvmti and dtrace
 112   post_allocation_notify(klass, new_cls, size);
 113 }
 114 
 115 void CollectedHeap::post_allocation_setup_array(Klass* klass,
 116                                                 HeapWord* obj_ptr,
 117                                                 int length) {
 118   // Set array length before setting the _klass field because a
 119   // non-NULL klass field indicates that the object is parsable by
 120   // concurrent GC.
 121   assert(length >= 0, "length should be non-negative");
 122   ((arrayOop)obj_ptr)->set_length(length);
 123   post_allocation_setup_common(klass, obj_ptr);
 124   oop new_obj = (oop)obj_ptr;
 125   assert(new_obj->is_array(), "must be an array");
 126   // notify jvmti and dtrace (must be after length is set for dtrace)
 127   post_allocation_notify(klass, new_obj, new_obj->size());
 128 }
 129 
 130 HeapWord* CollectedHeap::common_mem_allocate_noinit(Klass* klass, size_t size, TRAPS) {
 131 
 132   // Clear unhandled oops for memory allocation.  Memory allocation might
 133   // not take out a lock if from tlab, so clear here.
 134   CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
 135 
 136   if (HAS_PENDING_EXCEPTION) {
 137     NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
 138     return NULL;  // caller does a CHECK_0 too
 139   }
 140 
 141   HeapWord* result = NULL;
 142   if (UseTLAB) {
 143     result = allocate_from_tlab(klass, THREAD, size);
 144     if (result != NULL) {
 145       assert(!HAS_PENDING_EXCEPTION,
 146              "Unexpected exception, will result in uninitialized storage");
 147       return result;
 148     }
 149   }
 150   bool gc_overhead_limit_was_exceeded = false;
 151   result = Universe::heap()->mem_allocate(size,
 152                                           &gc_overhead_limit_was_exceeded);
 153   if (result != NULL) {
 154     NOT_PRODUCT(Universe::heap()->
 155       check_for_non_bad_heap_word_value(result, size));
 156     assert(!HAS_PENDING_EXCEPTION,
 157            "Unexpected exception, will result in uninitialized storage");
 158     size_t size_in_bytes = size * HeapWordSize;
 159     THREAD->incr_allocated_bytes(size_in_bytes);
 160 
 161     AllocTracer::send_allocation_outside_tlab(klass, result, size_in_bytes, THREAD);
 162 
 163     if (ThreadHeapSampler::enabled()) {
 164       THREAD->heap_sampler().check_for_sampling(result, size_in_bytes);
 165     }
 166     return result;
 167   }
 168 
 169 
 170   if (!gc_overhead_limit_was_exceeded) {
 171     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 172     report_java_out_of_memory("Java heap space");
 173 
 174     if (JvmtiExport::should_post_resource_exhausted()) {
 175       JvmtiExport::post_resource_exhausted(
 176         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
 177         "Java heap space");
 178     }
 179 
 180     THROW_OOP_0(Universe::out_of_memory_error_java_heap());
 181   } else {
 182     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 183     report_java_out_of_memory("GC overhead limit exceeded");
 184 
 185     if (JvmtiExport::should_post_resource_exhausted()) {
 186       JvmtiExport::post_resource_exhausted(
 187         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
 188         "GC overhead limit exceeded");
 189     }
 190 
 191     THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
 192   }
 193 }
 194 
 195 HeapWord* CollectedHeap::common_mem_allocate_init(Klass* klass, size_t size, TRAPS) {
 196   HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
 197   init_obj(obj, size);
 198   return obj;
 199 }
 200 
 201 HeapWord* CollectedHeap::allocate_from_tlab(Klass* klass, Thread* thread, size_t size) {
 202   assert(UseTLAB, "should use UseTLAB");
 203 
 204   HeapWord* obj = thread->tlab().allocate(size);
 205   if (obj != NULL) {
 206     return obj;
 207   }
 208   // Otherwise...
 209   return allocate_from_tlab_slow(klass, thread, size);
 210 }
 211 
 212 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
 213   assert(obj != NULL, "cannot initialize NULL object");
 214   const size_t hs = oopDesc::header_size();
 215   assert(size >= hs, "unexpected object size");
 216   ((oop)obj)->set_klass_gap(0);
 217   Copy::fill_to_aligned_words(obj + hs, size - hs);
 218 }
 219 
 220 HeapWord* CollectedHeap::common_allocate_memory(Klass* klass, int size,
 221                                                 void (*post_setup)(Klass*, HeapWord*, int),
 222                                                 int size_for_post, bool init_memory,
 223                                                 TRAPS) {
 224   HeapWord* obj;
 225   if (init_memory) {
 226     obj = common_mem_allocate_init(klass, size, CHECK_NULL);
 227   } else {
 228     obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
 229   }
 230   post_setup(klass, obj, size_for_post);
 231   return obj;
 232 }
 233 
 234 HeapWord* CollectedHeap::allocate_memory(Klass* klass, int size,
 235                                          void (*post_setup)(Klass*, HeapWord*, int),
 236                                          int size_for_post, bool init_memory,
 237                                          TRAPS) {
 238   HeapWord* obj;
 239 
 240   assert(JavaThread::current()->heap_sampler().add_sampling_collector(),
 241          "Should never return false.");
 242 
 243   if (JvmtiExport::should_post_sampled_object_alloc()) {
 244     HandleMark hm(THREAD);
 245     Handle obj_h;
 246     {
 247       JvmtiSampledObjectAllocEventCollector collector;
 248       obj = common_allocate_memory(klass, size, post_setup, size_for_post,
 249                                    init_memory, CHECK_NULL);
 250       // If we want to be sampling, protect the allocated object with a Handle
 251       // before doing the callback. The callback is done in the destructor of
 252       // the JvmtiSampledObjectAllocEventCollector.
 253       obj_h = Handle(THREAD, (oop) obj);
 254     }
 255     obj = (HeapWord*) obj_h();
 256   } else {
 257     obj = common_allocate_memory(klass, size, post_setup, size_for_post,
 258                                  init_memory, CHECK_NULL);
 259   }
 260 
 261   assert(JavaThread::current()->heap_sampler().remove_sampling_collector(),
 262          "Should never return false.");
 263   return obj;
 264 }
 265 
 266 oop CollectedHeap::obj_allocate(Klass* klass, int size, TRAPS) {
 267   debug_only(check_for_valid_allocation_state());
 268   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 269   assert(size >= 0, "int won't convert to size_t");
 270   HeapWord* obj = allocate_memory(klass, size, post_allocation_setup_obj,
 271                                   size, true, CHECK_NULL);
 272   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 273   return (oop)obj;
 274 }
 275 
 276 oop CollectedHeap::class_allocate(Klass* klass, int size, TRAPS) {
 277   debug_only(check_for_valid_allocation_state());
 278   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 279   assert(size >= 0, "int won't convert to size_t");
 280   HeapWord* obj = allocate_memory(klass, size, post_allocation_setup_class,
 281                                   size, true, CHECK_NULL);
 282   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 283   return (oop)obj;
 284 }
 285 
 286 oop CollectedHeap::array_allocate(Klass* klass,
 287                                   int size,
 288                                   int length,
 289                                   TRAPS) {
 290   debug_only(check_for_valid_allocation_state());
 291   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 292   assert(size >= 0, "int won't convert to size_t");
 293   HeapWord* obj = allocate_memory(klass, size, post_allocation_setup_array,
 294                                   length, true, CHECK_NULL);
 295   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 296   return (oop)obj;
 297 }
 298 
 299 oop CollectedHeap::array_allocate_nozero(Klass* klass,
 300                                          int size,
 301                                          int length,
 302                                          TRAPS) {
 303   debug_only(check_for_valid_allocation_state());
 304   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 305   assert(size >= 0, "int won't convert to size_t");
 306 
 307   HeapWord* obj = allocate_memory(klass, size, post_allocation_setup_array,
 308                                   length, false, CHECK_NULL);
 309 #ifndef PRODUCT
 310   const size_t hs = oopDesc::header_size()+1;
 311   Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);
 312 #endif
 313   return (oop)obj;
 314 }
 315 
 316 inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,
 317                                                          HeapWord* end,
 318                                                          unsigned short alignment_in_bytes) {
 319   if (alignment_in_bytes <= ObjectAlignmentInBytes) {
 320     return addr;
 321   }
 322 
 323   assert(is_aligned(addr, HeapWordSize),
 324          "Address " PTR_FORMAT " is not properly aligned.", p2i(addr));
 325   assert(is_aligned(alignment_in_bytes, HeapWordSize),
 326          "Alignment size %u is incorrect.", alignment_in_bytes);
 327 
 328   HeapWord* new_addr = align_up(addr, alignment_in_bytes);
 329   size_t padding = pointer_delta(new_addr, addr);
 330 
 331   if (padding == 0) {
 332     return addr;
 333   }
 334 
 335   if (padding < CollectedHeap::min_fill_size()) {
 336     padding += alignment_in_bytes / HeapWordSize;
 337     assert(padding >= CollectedHeap::min_fill_size(),
 338            "alignment_in_bytes %u is expect to be larger "
 339            "than the minimum object size", alignment_in_bytes);
 340     new_addr = addr + padding;
 341   }
 342 
 343   assert(new_addr > addr, "Unexpected arithmetic overflow "
 344          PTR_FORMAT " not greater than " PTR_FORMAT, p2i(new_addr), p2i(addr));
 345   if(new_addr < end) {
 346     CollectedHeap::fill_with_object(addr, padding);
 347     return new_addr;
 348   } else {
 349     return NULL;
 350   }
 351 }
 352 
 353 #endif // SHARE_VM_GC_SHARED_COLLECTEDHEAP_INLINE_HPP