667 assert(MemRegion(bottom(), end()).contains(p),
668 err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
669 p, bottom(), end()));
670 HeapWord* current_top = top();
671 assert(p <= current_top,
672 err_msg("p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
673 p, current_top));
674 assert(p == current_top || oop(p)->is_oop(),
675 err_msg("p (" PTR_FORMAT ") is not a block start - "
676 "current_top: " PTR_FORMAT ", is_oop: %s",
677 p, current_top, BOOL_TO_STR(oop(p)->is_oop())));
678 if (p < current_top) {
679 return oop(p)->size();
680 } else {
681 assert(p == current_top, "just checking");
682 return pointer_delta(end(), (HeapWord*) p);
683 }
684 }
685
686 // This version requires locking.
687 inline HeapWord* ContiguousSpace::allocate_impl(size_t size,
688 HeapWord* const end_value) {
689 assert(Heap_lock->owned_by_self() ||
690 (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()),
691 "not locked");
692 HeapWord* obj = top();
693 if (pointer_delta(end_value, obj) >= size) {
694 HeapWord* new_top = obj + size;
695 set_top(new_top);
696 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
697 return obj;
698 } else {
699 return NULL;
700 }
701 }
702
703 // This version is lock-free.
704 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size,
705 HeapWord* const end_value) {
706 do {
707 HeapWord* obj = top();
708 if (pointer_delta(end_value, obj) >= size) {
709 HeapWord* new_top = obj + size;
710 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
711 // result can be one of two:
712 // the old top value: the exchange succeeded
713 // otherwise: the new value of the top is returned.
714 if (result == obj) {
715 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
716 return obj;
717 }
718 } else {
719 return NULL;
720 }
721 } while (true);
722 }
723
724 HeapWord* ContiguousSpace::allocate_aligned(size_t size) {
725 assert(Heap_lock->owned_by_self() || (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()), "not locked");
726 HeapWord* end_value = end();
727
728 HeapWord* obj = CollectedHeap::align_allocation_or_fail(top(), end_value, SurvivorAlignmentInBytes);
729 if (obj == NULL) {
730 return NULL;
731 }
732
733 if (pointer_delta(end_value, obj) >= size) {
734 HeapWord* new_top = obj + size;
735 set_top(new_top);
736 assert(is_ptr_aligned(obj, SurvivorAlignmentInBytes) && is_aligned(new_top),
737 "checking alignment");
738 return obj;
739 } else {
740 set_top(obj);
741 return NULL;
742 }
743 }
744
745 // Requires locking.
746 HeapWord* ContiguousSpace::allocate(size_t size) {
747 return allocate_impl(size, end());
748 }
749
750 // Lock-free.
751 HeapWord* ContiguousSpace::par_allocate(size_t size) {
752 return par_allocate_impl(size, end());
753 }
754
755 void ContiguousSpace::allocate_temporary_filler(int factor) {
756 // allocate temporary type array decreasing free size with factor 'factor'
757 assert(factor >= 0, "just checking");
758 size_t size = pointer_delta(end(), top());
759
760 // if space is full, return
761 if (size == 0) return;
762
763 if (factor > 0) {
764 size -= size/factor;
765 }
766 size = align_object_size(size);
767
768 const size_t array_header_size = typeArrayOopDesc::header_size(T_INT);
769 if (size >= (size_t)align_object_size(array_header_size)) {
770 size_t length = (size - array_header_size) * (HeapWordSize / sizeof(jint));
771 // allocate uninitialized int array
772 typeArrayOop t = (typeArrayOop) allocate(size);
773 assert(t != NULL, "allocation should succeed");
774 t->set_mark(markOopDesc::prototype());
775 t->set_klass(Universe::intArrayKlassObj());
776 t->set_length((int)length);
777 } else {
778 assert(size == CollectedHeap::min_fill_size(),
779 "size for smallest fake object doesn't match");
780 instanceOop obj = (instanceOop) allocate(size);
781 obj->set_mark(markOopDesc::prototype());
782 obj->set_klass_gap(0);
783 obj->set_klass(SystemDictionary::Object_klass());
784 }
785 }
786
787 void EdenSpace::clear(bool mangle_space) {
788 ContiguousSpace::clear(mangle_space);
789 set_soft_end(end());
790 }
791
792 // Requires locking.
793 HeapWord* EdenSpace::allocate(size_t size) {
794 return allocate_impl(size, soft_end());
795 }
796
797 // Lock-free.
798 HeapWord* EdenSpace::par_allocate(size_t size) {
799 return par_allocate_impl(size, soft_end());
800 }
801
802 HeapWord* ConcEdenSpace::par_allocate(size_t size)
803 {
804 do {
805 // The invariant is top() should be read before end() because
806 // top() can't be greater than end(), so if an update of _soft_end
807 // occurs between 'end_val = end();' and 'top_val = top();' top()
808 // also can grow up to the new end() and the condition
809 // 'top_val > end_val' is true. To ensure the loading order
810 // OrderAccess::loadload() is required after top() read.
811 HeapWord* obj = top();
812 OrderAccess::loadload();
813 if (pointer_delta(*soft_end_addr(), obj) >= size) {
814 HeapWord* new_top = obj + size;
815 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
816 // result can be one of two:
817 // the old top value: the exchange succeeded
818 // otherwise: the new value of the top is returned.
819 if (result == obj) {
820 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
821 return obj;
822 }
823 } else {
824 return NULL;
825 }
826 } while (true);
827 }
828
829
830 HeapWord* OffsetTableContigSpace::initialize_threshold() {
831 return _offsets.initialize_threshold();
832 }
833
834 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
835 _offsets.alloc_block(start, end);
836 return _offsets.threshold();
837 }
838
839 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
840 MemRegion mr) :
841 _offsets(sharedOffsetArray, mr),
842 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
843 {
844 _offsets.set_contig_space(this);
845 initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
846 }
847
848 #define OBJ_SAMPLE_INTERVAL 0
|
667 assert(MemRegion(bottom(), end()).contains(p),
668 err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
669 p, bottom(), end()));
670 HeapWord* current_top = top();
671 assert(p <= current_top,
672 err_msg("p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
673 p, current_top));
674 assert(p == current_top || oop(p)->is_oop(),
675 err_msg("p (" PTR_FORMAT ") is not a block start - "
676 "current_top: " PTR_FORMAT ", is_oop: %s",
677 p, current_top, BOOL_TO_STR(oop(p)->is_oop())));
678 if (p < current_top) {
679 return oop(p)->size();
680 } else {
681 assert(p == current_top, "just checking");
682 return pointer_delta(end(), (HeapWord*) p);
683 }
684 }
685
686 // This version requires locking.
687 inline HeapWord* ContiguousSpace::allocate_impl(size_t size) {
688 assert(Heap_lock->owned_by_self() ||
689 (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()),
690 "not locked");
691 HeapWord* obj = top();
692 if (pointer_delta(end(), obj) >= size) {
693 HeapWord* new_top = obj + size;
694 set_top(new_top);
695 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
696 return obj;
697 } else {
698 return NULL;
699 }
700 }
701
702 // This version is lock-free.
703 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size) {
704 do {
705 HeapWord* obj = top();
706 if (pointer_delta(end(), obj) >= size) {
707 HeapWord* new_top = obj + size;
708 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
709 // result can be one of two:
710 // the old top value: the exchange succeeded
711 // otherwise: the new value of the top is returned.
712 if (result == obj) {
713 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
714 return obj;
715 }
716 } else {
717 return NULL;
718 }
719 } while (true);
720 }
721
722 HeapWord* ContiguousSpace::allocate_aligned(size_t size) {
723 assert(Heap_lock->owned_by_self() || (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()), "not locked");
724 HeapWord* end_value = end();
725
726 HeapWord* obj = CollectedHeap::align_allocation_or_fail(top(), end_value, SurvivorAlignmentInBytes);
727 if (obj == NULL) {
728 return NULL;
729 }
730
731 if (pointer_delta(end_value, obj) >= size) {
732 HeapWord* new_top = obj + size;
733 set_top(new_top);
734 assert(is_ptr_aligned(obj, SurvivorAlignmentInBytes) && is_aligned(new_top),
735 "checking alignment");
736 return obj;
737 } else {
738 set_top(obj);
739 return NULL;
740 }
741 }
742
743 // Requires locking.
744 HeapWord* ContiguousSpace::allocate(size_t size) {
745 return allocate_impl(size);
746 }
747
748 // Lock-free.
749 HeapWord* ContiguousSpace::par_allocate(size_t size) {
750 return par_allocate_impl(size);
751 }
752
753 void ContiguousSpace::allocate_temporary_filler(int factor) {
754 // allocate temporary type array decreasing free size with factor 'factor'
755 assert(factor >= 0, "just checking");
756 size_t size = pointer_delta(end(), top());
757
758 // if space is full, return
759 if (size == 0) return;
760
761 if (factor > 0) {
762 size -= size/factor;
763 }
764 size = align_object_size(size);
765
766 const size_t array_header_size = typeArrayOopDesc::header_size(T_INT);
767 if (size >= (size_t)align_object_size(array_header_size)) {
768 size_t length = (size - array_header_size) * (HeapWordSize / sizeof(jint));
769 // allocate uninitialized int array
770 typeArrayOop t = (typeArrayOop) allocate(size);
771 assert(t != NULL, "allocation should succeed");
772 t->set_mark(markOopDesc::prototype());
773 t->set_klass(Universe::intArrayKlassObj());
774 t->set_length((int)length);
775 } else {
776 assert(size == CollectedHeap::min_fill_size(),
777 "size for smallest fake object doesn't match");
778 instanceOop obj = (instanceOop) allocate(size);
779 obj->set_mark(markOopDesc::prototype());
780 obj->set_klass_gap(0);
781 obj->set_klass(SystemDictionary::Object_klass());
782 }
783 }
784
785 HeapWord* OffsetTableContigSpace::initialize_threshold() {
786 return _offsets.initialize_threshold();
787 }
788
789 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
790 _offsets.alloc_block(start, end);
791 return _offsets.threshold();
792 }
793
794 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
795 MemRegion mr) :
796 _offsets(sharedOffsetArray, mr),
797 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
798 {
799 _offsets.set_contig_space(this);
800 initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
801 }
802
803 #define OBJ_SAMPLE_INTERVAL 0
|