359 oop MemAllocator::allocate() const {
360 oop obj = NULL;
361 {
362 Allocation allocation(*this, &obj);
363 HeapWord* mem = mem_allocate(allocation);
364 if (mem != NULL) {
365 obj = initialize(mem);
366 } else {
367 // The unhandled oop detector will poison local variable obj,
368 // so reset it to NULL if mem is NULL.
369 obj = NULL;
370 }
371 }
372 return obj;
373 }
374
375 void MemAllocator::mem_clear(HeapWord* mem) const {
376 assert(mem != NULL, "cannot initialize NULL object");
377 const size_t hs = oopDesc::header_size();
378 assert(_word_size >= hs, "unexpected object size");
379 oopDesc::set_klass_gap(mem, 0);
380 Copy::fill_to_aligned_words(mem + hs, _word_size - hs);
381 }
382
383 oop MemAllocator::finish(HeapWord* mem) const {
384 assert(mem != NULL, "NULL object pointer");
385 if (UseBiasedLocking) {
386 oopDesc::set_mark(mem, _klass->prototype_header());
387 } else {
388 // May be bootstrapping
389 oopDesc::set_mark(mem, markWord::prototype());
390 }
391 // Need a release store to ensure array/class length, mark word, and
392 // object zeroing are visible before setting the klass non-NULL, for
393 // concurrent collectors.
394 oopDesc::release_set_klass(mem, _klass);
395 return cast_to_oop(mem);
396 }
397
398 oop ObjAllocator::initialize(HeapWord* mem) const {
399 mem_clear(mem);
400 return finish(mem);
401 }
402
403 MemRegion ObjArrayAllocator::obj_memory_range(oop obj) const {
404 if (_do_zero) {
405 return MemAllocator::obj_memory_range(obj);
406 }
407 ArrayKlass* array_klass = ArrayKlass::cast(_klass);
408 const size_t hs = arrayOopDesc::header_size(array_klass->element_type());
409 return MemRegion(cast_from_oop<HeapWord*>(obj) + hs, _word_size - hs);
410 }
411
412 oop ObjArrayAllocator::initialize(HeapWord* mem) const {
413 // Set array length before setting the _klass field because a
414 // non-NULL klass field indicates that the object is parsable by
415 // concurrent GC.
416 assert(_length >= 0, "length should be non-negative");
417 if (_do_zero) {
418 mem_clear(mem);
419 }
420 arrayOopDesc::set_length(mem, _length);
421 return finish(mem);
422 }
423
424 oop ClassAllocator::initialize(HeapWord* mem) const {
425 // Set oop_size field before setting the _klass field because a
426 // non-NULL _klass field indicates that the object is parsable by
427 // concurrent GC.
428 assert(_word_size > 0, "oop_size must be positive.");
|
359 oop MemAllocator::allocate() const {
360 oop obj = NULL;
361 {
362 Allocation allocation(*this, &obj);
363 HeapWord* mem = mem_allocate(allocation);
364 if (mem != NULL) {
365 obj = initialize(mem);
366 } else {
367 // The unhandled oop detector will poison local variable obj,
368 // so reset it to NULL if mem is NULL.
369 obj = NULL;
370 }
371 }
372 return obj;
373 }
374
375 void MemAllocator::mem_clear(HeapWord* mem) const {
376 assert(mem != NULL, "cannot initialize NULL object");
377 const size_t hs = oopDesc::header_size();
378 assert(_word_size >= hs, "unexpected object size");
379 if (!UseCompactObjectHeaders) {
380 oopDesc::set_klass_gap(mem, 0);
381 }
382 Copy::fill_to_aligned_words(mem + hs, _word_size - hs);
383 }
384
385 oop MemAllocator::finish(HeapWord* mem) const {
386 assert(mem != NULL, "NULL object pointer");
387 // Need a release store to ensure array/class length, mark word, and
388 // object zeroing are visible before setting the klass non-NULL, for
389 // concurrent collectors.
390 if (UseCompactObjectHeaders) {
391 oopDesc::release_set_mark(mem, _klass->prototype_header());
392 } else {
393 oopDesc::set_mark(mem, markWord::prototype());
394 oopDesc::release_set_klass(mem, _klass);
395 }
396 return cast_to_oop(mem);
397 }
398
399 oop ObjAllocator::initialize(HeapWord* mem) const {
400 mem_clear(mem);
401 return finish(mem);
402 }
403
404 MemRegion ObjArrayAllocator::obj_memory_range(oop obj) const {
405 if (_do_zero) {
406 return MemAllocator::obj_memory_range(obj);
407 }
408 ArrayKlass* array_klass = ArrayKlass::cast(_klass);
409 const size_t hs = align_up(arrayOopDesc::base_offset_in_bytes(array_klass->element_type()), HeapWordSize) / HeapWordSize;
410 return MemRegion(cast_from_oop<HeapWord*>(obj) + hs, _word_size - hs);
411 }
412
413 oop ObjArrayAllocator::initialize(HeapWord* mem) const {
414 // Set array length before setting the _klass field because a
415 // non-NULL klass field indicates that the object is parsable by
416 // concurrent GC.
417 assert(_length >= 0, "length should be non-negative");
418 if (_do_zero) {
419 mem_clear(mem);
420 }
421 arrayOopDesc::set_length(mem, _length);
422 return finish(mem);
423 }
424
425 oop ClassAllocator::initialize(HeapWord* mem) const {
426 // Set oop_size field before setting the _klass field because a
427 // non-NULL _klass field indicates that the object is parsable by
428 // concurrent GC.
429 assert(_word_size > 0, "oop_size must be positive.");
|