collectedHeap.cpp revision 3157
0N/A/*
3157N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "gc_implementation/shared/vmGCOperations.hpp"
1879N/A#include "gc_interface/collectedHeap.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "oops/oop.inline.hpp"
2845N/A#include "oops/instanceMirrorKlass.hpp"
1879N/A#include "runtime/init.hpp"
1879N/A#include "services/heapDumper.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "thread_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "thread_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "thread_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "thread_bsd.inline.hpp"
2796N/A#endif
0N/A
0N/A
0N/A#ifdef ASSERT
0N/Aint CollectedHeap::_fire_out_of_memory_count = 0;
0N/A#endif
0N/A
481N/Asize_t CollectedHeap::_filler_array_max_size = 0;
481N/A
3157N/Atemplate <>
3157N/Avoid EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) {
3157N/A st->print_cr("GC heap %s", m.is_before ? "before" : "after");
3157N/A st->print_raw(m);
3157N/A}
3157N/A
3157N/Avoid GCHeapLog::log_heap(bool before) {
3157N/A if (!should_log()) {
3157N/A return;
3157N/A }
3157N/A
3157N/A jlong timestamp = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
3157N/A MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
3157N/A int index = compute_log_index();
3157N/A _records[index].thread = NULL; // Its the GC thread so it's not that interesting.
3157N/A _records[index].timestamp = timestamp;
3157N/A _records[index].data.is_before = before;
3157N/A stringStream st(_records[index].data.buffer(), _records[index].data.size());
3157N/A if (before) {
3157N/A Universe::print_heap_before_gc(&st);
3157N/A } else {
3157N/A Universe::print_heap_after_gc(&st);
3157N/A }
3157N/A}
3157N/A
0N/A// Memory state functions.
0N/A
1753N/A
1753N/ACollectedHeap::CollectedHeap() : _n_par_threads(0)
1753N/A
481N/A{
481N/A const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
481N/A const size_t elements_per_word = HeapWordSize / sizeof(jint);
481N/A _filler_array_max_size = align_object_size(filler_array_hdr_size() +
481N/A max_len * elements_per_word);
481N/A
481N/A _barrier_set = NULL;
481N/A _is_gc_active = false;
481N/A _total_collections = _total_full_collections = 0;
481N/A _gc_cause = _gc_lastcause = GCCause::_no_gc;
0N/A NOT_PRODUCT(_promotion_failure_alot_count = 0;)
0N/A NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
0N/A
0N/A if (UsePerfData) {
0N/A EXCEPTION_MARK;
0N/A
0N/A // create the gc cause jvmstat counters
0N/A _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
0N/A 80, GCCause::to_string(_gc_cause), CHECK);
0N/A
0N/A _perf_gc_lastcause =
0N/A PerfDataManager::create_string_variable(SUN_GC, "lastCause",
0N/A 80, GCCause::to_string(_gc_lastcause), CHECK);
0N/A }
1166N/A _defer_initial_card_mark = false; // strengthened by subclass in pre_initialize() below.
3157N/A // Create the ring log
3157N/A if (LogEvents) {
3157N/A _gc_heap_log = new GCHeapLog();
3157N/A } else {
3157N/A _gc_heap_log = NULL;
3157N/A }
0N/A}
0N/A
1166N/Avoid CollectedHeap::pre_initialize() {
1166N/A // Used for ReduceInitialCardMarks (when COMPILER2 is used);
1166N/A // otherwise remains unused.
1468N/A#ifdef COMPILER2
1194N/A _defer_initial_card_mark = ReduceInitialCardMarks && can_elide_tlab_store_barriers()
1194N/A && (DeferInitialCardMark || card_mark_must_follow_store());
1166N/A#else
1166N/A assert(_defer_initial_card_mark == false, "Who would set it?");
1166N/A#endif
1166N/A}
0N/A
0N/A#ifndef PRODUCT
0N/Avoid CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
0N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
0N/A for (size_t slot = 0; slot < size; slot += 1) {
0N/A assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
0N/A "Found badHeapWordValue in post-allocation check");
0N/A }
0N/A }
0N/A}
0N/A
2098N/Avoid CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
0N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
0N/A for (size_t slot = 0; slot < size; slot += 1) {
0N/A assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
0N/A "Found non badHeapWordValue in pre-allocation check");
0N/A }
0N/A }
0N/A}
0N/A#endif // PRODUCT
0N/A
0N/A#ifdef ASSERT
0N/Avoid CollectedHeap::check_for_valid_allocation_state() {
0N/A Thread *thread = Thread::current();
0N/A // How to choose between a pending exception and a potential
0N/A // OutOfMemoryError? Don't allow pending exceptions.
0N/A // This is a VM policy failure, so how do we exhaustively test it?
0N/A assert(!thread->has_pending_exception(),
0N/A "shouldn't be allocating with pending exception");
0N/A if (StrictSafepointChecks) {
0N/A assert(thread->allow_allocation(),
0N/A "Allocation done by thread for which allocation is blocked "
0N/A "by No_Allocation_Verifier!");
0N/A // Allocation of an oop can always invoke a safepoint,
0N/A // hence, the true argument
0N/A thread->check_for_valid_safepoint_state(true);
0N/A }
0N/A}
0N/A#endif
0N/A
0N/AHeapWord* CollectedHeap::allocate_from_tlab_slow(Thread* thread, size_t size) {
0N/A
0N/A // Retain tlab and allocate object in shared space if
0N/A // the amount free in the tlab is too large to discard.
0N/A if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
0N/A thread->tlab().record_slow_allocation(size);
0N/A return NULL;
0N/A }
0N/A
0N/A // Discard tlab and allocate a new one.
0N/A // To minimize fragmentation, the last TLAB may be smaller than the rest.
0N/A size_t new_tlab_size = thread->tlab().compute_size(size);
0N/A
0N/A thread->tlab().clear_before_allocation();
0N/A
0N/A if (new_tlab_size == 0) {
0N/A return NULL;
0N/A }
0N/A
0N/A // Allocate a new TLAB...
0N/A HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
0N/A if (obj == NULL) {
0N/A return NULL;
0N/A }
0N/A if (ZeroTLAB) {
0N/A // ..and clear it.
0N/A Copy::zero_to_words(obj, new_tlab_size);
0N/A } else {
2726N/A // ...and zap just allocated object.
2726N/A#ifdef ASSERT
2726N/A // Skip mangling the space corresponding to the object header to
2726N/A // ensure that the returned space is not considered parsable by
2726N/A // any concurrent GC thread.
2726N/A size_t hdr_size = oopDesc::header_size();
2726N/A Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal);
2726N/A#endif // ASSERT
0N/A }
0N/A thread->tlab().fill(obj, obj + size, new_tlab_size);
0N/A return obj;
0N/A}
0N/A
1027N/Avoid CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
1027N/A MemRegion deferred = thread->deferred_card_mark();
1027N/A if (!deferred.is_empty()) {
1166N/A assert(_defer_initial_card_mark, "Otherwise should be empty");
1027N/A {
1027N/A // Verify that the storage points to a parsable object in heap
1027N/A DEBUG_ONLY(oop old_obj = oop(deferred.start());)
1027N/A assert(is_in(old_obj), "Not in allocated heap");
1027N/A assert(!can_elide_initializing_store_barrier(old_obj),
1166N/A "Else should have been filtered in new_store_pre_barrier()");
1027N/A assert(!is_in_permanent(old_obj), "Sanity: not expected");
1027N/A assert(old_obj->is_oop(true), "Not an oop");
1027N/A assert(old_obj->is_parsable(), "Will not be concurrently parsable");
1027N/A assert(deferred.word_size() == (size_t)(old_obj->size()),
1027N/A "Mismatch: multiple objects?");
1027N/A }
1027N/A BarrierSet* bs = barrier_set();
1027N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
1027N/A bs->write_region(deferred);
1027N/A // "Clear" the deferred_card_mark field
1027N/A thread->set_deferred_card_mark(MemRegion());
1027N/A }
1027N/A assert(thread->deferred_card_mark().is_empty(), "invariant");
1027N/A}
1027N/A
1027N/A// Helper for ReduceInitialCardMarks. For performance,
1027N/A// compiled code may elide card-marks for initializing stores
1027N/A// to a newly allocated object along the fast-path. We
1027N/A// compensate for such elided card-marks as follows:
1027N/A// (a) Generational, non-concurrent collectors, such as
1027N/A// GenCollectedHeap(ParNew,DefNew,Tenured) and
1027N/A// ParallelScavengeHeap(ParallelGC, ParallelOldGC)
1027N/A// need the card-mark if and only if the region is
1027N/A// in the old gen, and do not care if the card-mark
1027N/A// succeeds or precedes the initializing stores themselves,
1027N/A// so long as the card-mark is completed before the next
1027N/A// scavenge. For all these cases, we can do a card mark
1027N/A// at the point at which we do a slow path allocation
1166N/A// in the old gen, i.e. in this call.
1027N/A// (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
1027N/A// in addition that the card-mark for an old gen allocated
1027N/A// object strictly follow any associated initializing stores.
1027N/A// In these cases, the memRegion remembered below is
1027N/A// used to card-mark the entire region either just before the next
1027N/A// slow-path allocation by this thread or just before the next scavenge or
1027N/A// CMS-associated safepoint, whichever of these events happens first.
1027N/A// (The implicit assumption is that the object has been fully
1027N/A// initialized by this point, a fact that we assert when doing the
1027N/A// card-mark.)
1027N/A// (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
1027N/A// G1 concurrent marking is in progress an SATB (pre-write-)barrier is
1027N/A// is used to remember the pre-value of any store. Initializing
1027N/A// stores will not need this barrier, so we need not worry about
1027N/A// compensating for the missing pre-barrier here. Turning now
1027N/A// to the post-barrier, we note that G1 needs a RS update barrier
1027N/A// which simply enqueues a (sequence of) dirty cards which may
1027N/A// optionally be refined by the concurrent update threads. Note
1027N/A// that this barrier need only be applied to a non-young write,
1027N/A// but, like in CMS, because of the presence of concurrent refinement
1027N/A// (much like CMS' precleaning), must strictly follow the oop-store.
1027N/A// Thus, using the same protocol for maintaining the intended
1166N/A// invariants turns out, serendepitously, to be the same for both
1166N/A// G1 and CMS.
1027N/A//
1166N/A// For any future collector, this code should be reexamined with
1166N/A// that specific collector in mind, and the documentation above suitably
1166N/A// extended and updated.
1166N/Aoop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
1027N/A // If a previous card-mark was deferred, flush it now.
1027N/A flush_deferred_store_barrier(thread);
1027N/A if (can_elide_initializing_store_barrier(new_obj)) {
1027N/A // The deferred_card_mark region should be empty
1027N/A // following the flush above.
1027N/A assert(thread->deferred_card_mark().is_empty(), "Error");
1027N/A } else {
1166N/A MemRegion mr((HeapWord*)new_obj, new_obj->size());
1166N/A assert(!mr.is_empty(), "Error");
1166N/A if (_defer_initial_card_mark) {
1166N/A // Defer the card mark
1166N/A thread->set_deferred_card_mark(mr);
1166N/A } else {
1166N/A // Do the card mark
1166N/A BarrierSet* bs = barrier_set();
1166N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
1166N/A bs->write_region(mr);
1166N/A }
1027N/A }
1027N/A return new_obj;
1027N/A}
1027N/A
481N/Asize_t CollectedHeap::filler_array_hdr_size() {
1491N/A return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
481N/A}
481N/A
481N/Asize_t CollectedHeap::filler_array_min_size() {
1491N/A return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
481N/A}
481N/A
481N/Asize_t CollectedHeap::filler_array_max_size() {
481N/A return _filler_array_max_size;
481N/A}
481N/A
481N/A#ifdef ASSERT
481N/Avoid CollectedHeap::fill_args_check(HeapWord* start, size_t words)
481N/A{
481N/A assert(words >= min_fill_size(), "too small to fill");
481N/A assert(words % MinObjAlignment == 0, "unaligned size");
481N/A assert(Universe::heap()->is_in_reserved(start), "not in heap");
481N/A assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
481N/A}
481N/A
1165N/Avoid CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
481N/A{
1165N/A if (ZapFillerObjects && zap) {
481N/A Copy::fill_to_words(start + filler_array_hdr_size(),
481N/A words - filler_array_hdr_size(), 0XDEAFBABE);
481N/A }
481N/A}
481N/A#endif // ASSERT
481N/A
481N/Avoid
1165N/ACollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
481N/A{
481N/A assert(words >= filler_array_min_size(), "too small for an array");
481N/A assert(words <= filler_array_max_size(), "too big for a single object");
481N/A
481N/A const size_t payload_size = words - filler_array_hdr_size();
481N/A const size_t len = payload_size * HeapWordSize / sizeof(jint);
481N/A
481N/A // Set the length first for concurrent GC.
481N/A ((arrayOop)start)->set_length((int)len);
494N/A post_allocation_setup_common(Universe::intArrayKlassObj(), start, words);
1165N/A DEBUG_ONLY(zap_filler_array(start, words, zap);)
481N/A}
481N/A
481N/Avoid
1165N/ACollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
481N/A{
481N/A assert(words <= filler_array_max_size(), "too big for a single object");
481N/A
481N/A if (words >= filler_array_min_size()) {
1165N/A fill_with_array(start, words, zap);
481N/A } else if (words > 0) {
481N/A assert(words == min_fill_size(), "unaligned size");
1142N/A post_allocation_setup_common(SystemDictionary::Object_klass(), start,
481N/A words);
481N/A }
481N/A}
481N/A
1165N/Avoid CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
481N/A{
481N/A DEBUG_ONLY(fill_args_check(start, words);)
481N/A HandleMark hm; // Free handles before leaving.
1165N/A fill_with_object_impl(start, words, zap);
481N/A}
481N/A
1165N/Avoid CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
481N/A{
481N/A DEBUG_ONLY(fill_args_check(start, words);)
481N/A HandleMark hm; // Free handles before leaving.
481N/A
1469N/A#ifdef _LP64
481N/A // A single array can fill ~8G, so multiple objects are needed only in 64-bit.
481N/A // First fill with arrays, ensuring that any remaining space is big enough to
481N/A // fill. The remainder is filled with a single object.
481N/A const size_t min = min_fill_size();
481N/A const size_t max = filler_array_max_size();
481N/A while (words > max) {
481N/A const size_t cur = words - max >= min ? max : max - min;
1165N/A fill_with_array(start, cur, zap);
481N/A start += cur;
481N/A words -= cur;
481N/A }
481N/A#endif
481N/A
1165N/A fill_with_object_impl(start, words, zap);
481N/A}
481N/A
0N/AHeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
0N/A guarantee(false, "thread-local allocation buffers not supported");
0N/A return NULL;
0N/A}
0N/A
0N/Avoid CollectedHeap::ensure_parsability(bool retire_tlabs) {
0N/A // The second disjunct in the assertion below makes a concession
0N/A // for the start-up verification done while the VM is being
0N/A // created. Callers be careful that you know that mutators
0N/A // aren't going to interfere -- for instance, this is permissible
0N/A // if we are still single-threaded and have either not yet
0N/A // started allocating (nothing much to verify) or we have
0N/A // started allocating but are now a full-fledged JavaThread
0N/A // (and have thus made our TLAB's) available for filling.
0N/A assert(SafepointSynchronize::is_at_safepoint() ||
0N/A !is_init_completed(),
0N/A "Should only be called at a safepoint or at start-up"
0N/A " otherwise concurrent mutator activity may make heap "
0N/A " unparsable again");
1166N/A const bool use_tlab = UseTLAB;
1166N/A const bool deferred = _defer_initial_card_mark;
1166N/A // The main thread starts allocating via a TLAB even before it
1166N/A // has added itself to the threads list at vm boot-up.
1166N/A assert(!use_tlab || Threads::first() != NULL,
1166N/A "Attempt to fill tlabs before main thread has been added"
1166N/A " to threads list is doomed to failure!");
1166N/A for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
1166N/A if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
1166N/A#ifdef COMPILER2
1166N/A // The deferred store barriers must all have been flushed to the
1166N/A // card-table (or other remembered set structure) before GC starts
1166N/A // processing the card-table (or other remembered set).
1166N/A if (deferred) flush_deferred_store_barrier(thread);
1166N/A#else
1166N/A assert(!deferred, "Should be false");
1166N/A assert(thread->deferred_card_mark().is_empty(), "Should be empty");
1166N/A#endif
0N/A }
0N/A}
0N/A
0N/Avoid CollectedHeap::accumulate_statistics_all_tlabs() {
0N/A if (UseTLAB) {
0N/A assert(SafepointSynchronize::is_at_safepoint() ||
0N/A !is_init_completed(),
0N/A "should only accumulate statistics on tlabs at safepoint");
0N/A
0N/A ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
0N/A }
0N/A}
0N/A
0N/Avoid CollectedHeap::resize_all_tlabs() {
0N/A if (UseTLAB) {
0N/A assert(SafepointSynchronize::is_at_safepoint() ||
0N/A !is_init_completed(),
0N/A "should only resize tlabs at safepoint");
0N/A
0N/A ThreadLocalAllocBuffer::resize_all_tlabs();
0N/A }
0N/A}
615N/A
615N/Avoid CollectedHeap::pre_full_gc_dump() {
615N/A if (HeapDumpBeforeFullGC) {
2697N/A TraceTime tt("Heap Dump (before full gc): ", PrintGCDetails, false, gclog_or_tty);
615N/A // We are doing a "major" collection and a heap dump before
615N/A // major collection has been requested.
615N/A HeapDumper::dump_heap();
615N/A }
615N/A if (PrintClassHistogramBeforeFullGC) {
2697N/A TraceTime tt("Class Histogram (before full gc): ", PrintGCDetails, true, gclog_or_tty);
615N/A VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
615N/A inspector.doit();
615N/A }
615N/A}
615N/A
615N/Avoid CollectedHeap::post_full_gc_dump() {
615N/A if (HeapDumpAfterFullGC) {
2697N/A TraceTime tt("Heap Dump (after full gc): ", PrintGCDetails, false, gclog_or_tty);
615N/A HeapDumper::dump_heap();
615N/A }
615N/A if (PrintClassHistogramAfterFullGC) {
2697N/A TraceTime tt("Class Histogram (after full gc): ", PrintGCDetails, true, gclog_or_tty);
615N/A VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
615N/A inspector.doit();
615N/A }
615N/A}
2845N/A
2845N/Aoop CollectedHeap::Class_obj_allocate(KlassHandle klass, int size, KlassHandle real_klass, TRAPS) {
2845N/A debug_only(check_for_valid_allocation_state());
2845N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
2845N/A assert(size >= 0, "int won't convert to size_t");
2845N/A HeapWord* obj;
2845N/A if (JavaObjectsInPerm) {
2845N/A obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
2845N/A } else {
2845N/A assert(ScavengeRootsInCode > 0, "must be");
2845N/A obj = common_mem_allocate_init(size, CHECK_NULL);
2845N/A }
2845N/A post_allocation_setup_common(klass, obj, size);
2845N/A assert(Universe::is_bootstrapping() ||
2845N/A !((oop)obj)->blueprint()->oop_is_array(), "must not be an array");
2845N/A NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
2845N/A oop mirror = (oop)obj;
2845N/A
2845N/A java_lang_Class::set_oop_size(mirror, size);
2845N/A
2845N/A // Setup indirections
2845N/A if (!real_klass.is_null()) {
2845N/A java_lang_Class::set_klass(mirror, real_klass());
2845N/A real_klass->set_java_mirror(mirror);
2845N/A }
2845N/A
2845N/A instanceMirrorKlass* mk = instanceMirrorKlass::cast(mirror->klass());
2845N/A assert(size == mk->instance_size(real_klass), "should have been set");
2845N/A
2845N/A // notify jvmti and dtrace
2845N/A post_allocation_notify(klass, (oop)obj);
2845N/A
2845N/A return mirror;
2845N/A}
2984N/A
2984N/A/////////////// Unit tests ///////////////
2984N/A
2984N/A#ifndef PRODUCT
2984N/Avoid CollectedHeap::test_is_in() {
2984N/A CollectedHeap* heap = Universe::heap();
2984N/A
3026N/A uintptr_t epsilon = (uintptr_t) MinObjAlignment;
3026N/A uintptr_t heap_start = (uintptr_t) heap->_reserved.start();
3026N/A uintptr_t heap_end = (uintptr_t) heap->_reserved.end();
3026N/A
2984N/A // Test that NULL is not in the heap.
2984N/A assert(!heap->is_in(NULL), "NULL is unexpectedly in the heap");
2984N/A
2984N/A // Test that a pointer to before the heap start is reported as outside the heap.
3026N/A assert(heap_start >= ((uintptr_t)NULL + epsilon), "sanity");
3026N/A void* before_heap = (void*)(heap_start - epsilon);
2984N/A assert(!heap->is_in(before_heap),
2984N/A err_msg("before_heap: " PTR_FORMAT " is unexpectedly in the heap", before_heap));
2984N/A
2984N/A // Test that a pointer to after the heap end is reported as outside the heap.
3026N/A assert(heap_end <= ((uintptr_t)-1 - epsilon), "sanity");
3026N/A void* after_heap = (void*)(heap_end + epsilon);
2984N/A assert(!heap->is_in(after_heap),
2984N/A err_msg("after_heap: " PTR_FORMAT " is unexpectedly in the heap", after_heap));
2984N/A}
2984N/A#endif