< prev index next >

src/hotspot/share/gc/shared/collectedHeap.inline.hpp

Print this page
rev 50392 : JEP 331


  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/thread.inline.hpp"
  38 #include "services/lowMemoryDetector.hpp"
  39 #include "utilities/align.hpp"
  40 #include "utilities/copy.hpp"
  41 
  42 // Inline allocation implementations.
  43 
  44 void CollectedHeap::post_allocation_setup_common(Klass* klass,
  45                                                  HeapWord* obj_ptr) {
  46   post_allocation_setup_no_klass_install(klass, obj_ptr);
  47   oop obj = (oop)obj_ptr;
  48 #if (INCLUDE_G1GC || INCLUDE_CMSGC)
  49   // Need a release store to ensure array/class length, mark word, and
  50   // object zeroing are visible before setting the klass non-NULL, for
  51   // concurrent collectors.
  52   obj->release_set_klass(klass);
  53 #else
  54   obj->set_klass(klass);
  55 #endif
  56 }


 137     return NULL;  // caller does a CHECK_0 too
 138   }
 139 
 140   HeapWord* result = NULL;
 141   if (UseTLAB) {
 142     result = allocate_from_tlab(klass, THREAD, size);
 143     if (result != NULL) {
 144       assert(!HAS_PENDING_EXCEPTION,
 145              "Unexpected exception, will result in uninitialized storage");
 146       return result;
 147     }
 148   }
 149   bool gc_overhead_limit_was_exceeded = false;
 150   result = Universe::heap()->mem_allocate(size,
 151                                           &gc_overhead_limit_was_exceeded);
 152   if (result != NULL) {
 153     NOT_PRODUCT(Universe::heap()->
 154       check_for_non_bad_heap_word_value(result, size));
 155     assert(!HAS_PENDING_EXCEPTION,
 156            "Unexpected exception, will result in uninitialized storage");
 157     THREAD->incr_allocated_bytes(size * HeapWordSize);

 158 
 159     AllocTracer::send_allocation_outside_tlab(klass, result, size * HeapWordSize, THREAD);
 160 



 161     return result;
 162   }
 163 
 164 
 165   if (!gc_overhead_limit_was_exceeded) {
 166     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 167     report_java_out_of_memory("Java heap space");
 168 
 169     if (JvmtiExport::should_post_resource_exhausted()) {
 170       JvmtiExport::post_resource_exhausted(
 171         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
 172         "Java heap space");
 173     }
 174 
 175     THROW_OOP_0(Universe::out_of_memory_error_java_heap());
 176   } else {
 177     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
 178     report_java_out_of_memory("GC overhead limit exceeded");
 179 
 180     if (JvmtiExport::should_post_resource_exhausted()) {


 195 
 196 HeapWord* CollectedHeap::allocate_from_tlab(Klass* klass, Thread* thread, size_t size) {
 197   assert(UseTLAB, "should use UseTLAB");
 198 
 199   HeapWord* obj = thread->tlab().allocate(size);
 200   if (obj != NULL) {
 201     return obj;
 202   }
 203   // Otherwise...
 204   return allocate_from_tlab_slow(klass, thread, size);
 205 }
 206 
 207 void CollectedHeap::init_obj(HeapWord* obj, size_t size) {
 208   assert(obj != NULL, "cannot initialize NULL object");
 209   const size_t hs = oopDesc::header_size();
 210   assert(size >= hs, "unexpected object size");
 211   ((oop)obj)->set_klass_gap(0);
 212   Copy::fill_to_aligned_words(obj + hs, size - hs);
 213 }
 214 














































 215 oop CollectedHeap::obj_allocate(Klass* klass, int size, TRAPS) {
 216   debug_only(check_for_valid_allocation_state());
 217   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 218   assert(size >= 0, "int won't convert to size_t");
 219   HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
 220   post_allocation_setup_obj(klass, obj, size);
 221   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 222   return (oop)obj;
 223 }
 224 
 225 oop CollectedHeap::class_allocate(Klass* klass, int size, TRAPS) {
 226   debug_only(check_for_valid_allocation_state());
 227   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 228   assert(size >= 0, "int won't convert to size_t");
 229   HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
 230   post_allocation_setup_class(klass, obj, size); // set oop_size
 231   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 232   return (oop)obj;
 233 }
 234 
 235 oop CollectedHeap::array_allocate(Klass* klass,
 236                                   int size,
 237                                   int length,
 238                                   TRAPS) {
 239   debug_only(check_for_valid_allocation_state());
 240   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 241   assert(size >= 0, "int won't convert to size_t");
 242   HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
 243   post_allocation_setup_array(klass, obj, length);
 244   NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
 245   return (oop)obj;
 246 }
 247 
 248 oop CollectedHeap::array_allocate_nozero(Klass* klass,
 249                                          int size,
 250                                          int length,
 251                                          TRAPS) {
 252   debug_only(check_for_valid_allocation_state());
 253   assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
 254   assert(size >= 0, "int won't convert to size_t");
 255   HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
 256   ((oop)obj)->set_klass_gap(0);
 257   post_allocation_setup_array(klass, obj, length);
 258 #ifndef PRODUCT
 259   const size_t hs = oopDesc::header_size()+1;
 260   Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);
 261 #endif
 262   return (oop)obj;
 263 }
 264 
 265 inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,
 266                                                          HeapWord* end,
 267                                                          unsigned short alignment_in_bytes) {
 268   if (alignment_in_bytes <= ObjectAlignmentInBytes) {
 269     return addr;
 270   }
 271 
 272   assert(is_aligned(addr, HeapWordSize),
 273          "Address " PTR_FORMAT " is not properly aligned.", p2i(addr));
 274   assert(is_aligned(alignment_in_bytes, HeapWordSize),
 275          "Alignment size %u is incorrect.", alignment_in_bytes);
 276 
 277   HeapWord* new_addr = align_up(addr, alignment_in_bytes);




  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 }


 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()) {


 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);


< prev index next >