duke@435: /* duke@435: * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_virtualspace.cpp.incl" duke@435: duke@435: duke@435: // ReservedSpace duke@435: ReservedSpace::ReservedSpace(size_t size) { duke@435: initialize(size, 0, false, NULL); duke@435: } duke@435: duke@435: ReservedSpace::ReservedSpace(size_t size, size_t alignment, duke@435: bool large, char* requested_address) { duke@435: initialize(size, alignment, large, requested_address); duke@435: } duke@435: duke@435: char * duke@435: ReservedSpace::align_reserved_region(char* addr, const size_t len, duke@435: const size_t prefix_size, duke@435: const size_t prefix_align, duke@435: const size_t suffix_size, duke@435: const size_t suffix_align) duke@435: { duke@435: assert(addr != NULL, "sanity"); duke@435: const size_t required_size = prefix_size + suffix_size; duke@435: assert(len >= required_size, "len too small"); duke@435: duke@435: const size_t s = size_t(addr); duke@435: const size_t beg_ofs = s + prefix_size & suffix_align - 1; duke@435: const size_t beg_delta = beg_ofs == 0 ? 0 : suffix_align - beg_ofs; duke@435: duke@435: if (len < beg_delta + required_size) { duke@435: return NULL; // Cannot do proper alignment. duke@435: } duke@435: const size_t end_delta = len - (beg_delta + required_size); duke@435: duke@435: if (beg_delta != 0) { duke@435: os::release_memory(addr, beg_delta); duke@435: } duke@435: duke@435: if (end_delta != 0) { duke@435: char* release_addr = (char*) (s + beg_delta + required_size); duke@435: os::release_memory(release_addr, end_delta); duke@435: } duke@435: duke@435: return (char*) (s + beg_delta); duke@435: } duke@435: duke@435: char* ReservedSpace::reserve_and_align(const size_t reserve_size, duke@435: const size_t prefix_size, duke@435: const size_t prefix_align, duke@435: const size_t suffix_size, duke@435: const size_t suffix_align) duke@435: { duke@435: assert(reserve_size > prefix_size + suffix_size, "should not be here"); duke@435: duke@435: char* raw_addr = os::reserve_memory(reserve_size, NULL, prefix_align); duke@435: if (raw_addr == NULL) return NULL; duke@435: duke@435: char* result = align_reserved_region(raw_addr, reserve_size, prefix_size, duke@435: prefix_align, suffix_size, duke@435: suffix_align); duke@435: if (result == NULL && !os::release_memory(raw_addr, reserve_size)) { duke@435: fatal("os::release_memory failed"); duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: if (result != NULL) { duke@435: const size_t raw = size_t(raw_addr); duke@435: const size_t res = size_t(result); duke@435: assert(res >= raw, "alignment decreased start addr"); duke@435: assert(res + prefix_size + suffix_size <= raw + reserve_size, duke@435: "alignment increased end addr"); duke@435: assert((res & prefix_align - 1) == 0, "bad alignment of prefix"); duke@435: assert((res + prefix_size & suffix_align - 1) == 0, duke@435: "bad alignment of suffix"); duke@435: } duke@435: #endif duke@435: duke@435: return result; duke@435: } duke@435: duke@435: ReservedSpace::ReservedSpace(const size_t prefix_size, duke@435: const size_t prefix_align, duke@435: const size_t suffix_size, duke@435: const size_t suffix_align) duke@435: { duke@435: assert(prefix_size != 0, "sanity"); duke@435: assert(prefix_align != 0, "sanity"); duke@435: assert(suffix_size != 0, "sanity"); duke@435: assert(suffix_align != 0, "sanity"); duke@435: assert((prefix_size & prefix_align - 1) == 0, duke@435: "prefix_size not divisible by prefix_align"); duke@435: assert((suffix_size & suffix_align - 1) == 0, duke@435: "suffix_size not divisible by suffix_align"); duke@435: assert((suffix_align & prefix_align - 1) == 0, duke@435: "suffix_align not divisible by prefix_align"); duke@435: duke@435: // On systems where the entire region has to be reserved and committed up duke@435: // front, the compound alignment normally done by this method is unnecessary. duke@435: const bool try_reserve_special = UseLargePages && duke@435: prefix_align == os::large_page_size(); duke@435: if (!os::can_commit_large_page_memory() && try_reserve_special) { duke@435: initialize(prefix_size + suffix_size, prefix_align, true); duke@435: return; duke@435: } duke@435: duke@435: _base = NULL; duke@435: _size = 0; duke@435: _alignment = 0; duke@435: _special = false; duke@435: duke@435: // Optimistically try to reserve the exact size needed. duke@435: const size_t size = prefix_size + suffix_size; duke@435: char* addr = os::reserve_memory(size, NULL, prefix_align); duke@435: if (addr == NULL) return; duke@435: duke@435: // Check whether the result has the needed alignment (unlikely unless duke@435: // prefix_align == suffix_align). duke@435: const size_t ofs = size_t(addr) + prefix_size & suffix_align - 1; duke@435: if (ofs != 0) { duke@435: // Wrong alignment. Release, allocate more space and do manual alignment. duke@435: // duke@435: // On most operating systems, another allocation with a somewhat larger size duke@435: // will return an address "close to" that of the previous allocation. The duke@435: // result is often the same address (if the kernel hands out virtual duke@435: // addresses from low to high), or an address that is offset by the increase duke@435: // in size. Exploit that to minimize the amount of extra space requested. duke@435: if (!os::release_memory(addr, size)) { duke@435: fatal("os::release_memory failed"); duke@435: } duke@435: duke@435: const size_t extra = MAX2(ofs, suffix_align - ofs); duke@435: addr = reserve_and_align(size + extra, prefix_size, prefix_align, duke@435: suffix_size, suffix_align); duke@435: if (addr == NULL) { duke@435: // Try an even larger region. If this fails, address space is exhausted. duke@435: addr = reserve_and_align(size + suffix_align, prefix_size, duke@435: prefix_align, suffix_size, suffix_align); duke@435: } duke@435: } duke@435: duke@435: _base = addr; duke@435: _size = size; duke@435: _alignment = prefix_align; duke@435: } duke@435: duke@435: void ReservedSpace::initialize(size_t size, size_t alignment, bool large, duke@435: char* requested_address) { duke@435: const size_t granularity = os::vm_allocation_granularity(); duke@435: assert((size & granularity - 1) == 0, duke@435: "size not aligned to os::vm_allocation_granularity()"); duke@435: assert((alignment & granularity - 1) == 0, duke@435: "alignment not aligned to os::vm_allocation_granularity()"); duke@435: assert(alignment == 0 || is_power_of_2((intptr_t)alignment), duke@435: "not a power of 2"); duke@435: duke@435: _base = NULL; duke@435: _size = 0; duke@435: _special = false; duke@435: _alignment = 0; duke@435: if (size == 0) { duke@435: return; duke@435: } duke@435: duke@435: // If OS doesn't support demand paging for large page memory, we need duke@435: // to use reserve_memory_special() to reserve and pin the entire region. duke@435: bool special = large && !os::can_commit_large_page_memory(); duke@435: char* base = NULL; duke@435: duke@435: if (special) { duke@435: // It's not hard to implement reserve_memory_special() such that it can duke@435: // allocate at fixed address, but there seems no use of this feature duke@435: // for now, so it's not implemented. duke@435: assert(requested_address == NULL, "not implemented"); duke@435: duke@435: base = os::reserve_memory_special(size); duke@435: duke@435: if (base != NULL) { duke@435: // Check alignment constraints duke@435: if (alignment > 0) { duke@435: assert((uintptr_t) base % alignment == 0, duke@435: "Large pages returned a non-aligned address"); duke@435: } duke@435: _special = true; duke@435: } else { duke@435: // failed; try to reserve regular memory below duke@435: } duke@435: } duke@435: duke@435: if (base == NULL) { duke@435: // Optimistically assume that the OSes returns an aligned base pointer. duke@435: // When reserving a large address range, most OSes seem to align to at duke@435: // least 64K. duke@435: duke@435: // If the memory was requested at a particular address, use duke@435: // os::attempt_reserve_memory_at() to avoid over mapping something duke@435: // important. If available space is not detected, return NULL. duke@435: duke@435: if (requested_address != 0) { duke@435: base = os::attempt_reserve_memory_at(size, requested_address); duke@435: } else { duke@435: base = os::reserve_memory(size, NULL, alignment); duke@435: } duke@435: duke@435: if (base == NULL) return; duke@435: duke@435: // Check alignment constraints duke@435: if (alignment > 0 && ((size_t)base & alignment - 1) != 0) { duke@435: // Base not aligned, retry duke@435: if (!os::release_memory(base, size)) fatal("os::release_memory failed"); duke@435: // Reserve size large enough to do manual alignment and duke@435: // increase size to a multiple of the desired alignment duke@435: size = align_size_up(size, alignment); duke@435: size_t extra_size = size + alignment; duke@435: char* extra_base = os::reserve_memory(extra_size, NULL, alignment); duke@435: if (extra_base == NULL) return; duke@435: // Do manual alignement duke@435: base = (char*) align_size_up((uintptr_t) extra_base, alignment); duke@435: assert(base >= extra_base, "just checking"); duke@435: // Release unused areas duke@435: size_t unused_bottom_size = base - extra_base; duke@435: size_t unused_top_size = extra_size - size - unused_bottom_size; duke@435: assert(unused_bottom_size % os::vm_allocation_granularity() == 0, duke@435: "size not allocation aligned"); duke@435: assert(unused_top_size % os::vm_allocation_granularity() == 0, duke@435: "size not allocation aligned"); duke@435: if (unused_bottom_size > 0) { duke@435: os::release_memory(extra_base, unused_bottom_size); duke@435: } duke@435: if (unused_top_size > 0) { duke@435: os::release_memory(base + size, unused_top_size); duke@435: } duke@435: } duke@435: } duke@435: // Done duke@435: _base = base; duke@435: _size = size; duke@435: _alignment = MAX2(alignment, (size_t) os::vm_page_size()); duke@435: duke@435: assert(markOopDesc::encode_pointer_as_mark(_base)->decode_pointer() == _base, duke@435: "area must be distinguisable from marks for mark-sweep"); duke@435: assert(markOopDesc::encode_pointer_as_mark(&_base[size])->decode_pointer() == &_base[size], duke@435: "area must be distinguisable from marks for mark-sweep"); duke@435: } duke@435: duke@435: duke@435: ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment, duke@435: bool special) { duke@435: assert((size % os::vm_allocation_granularity()) == 0, duke@435: "size not allocation aligned"); duke@435: _base = base; duke@435: _size = size; duke@435: _alignment = alignment; duke@435: _special = special; duke@435: } duke@435: duke@435: duke@435: ReservedSpace ReservedSpace::first_part(size_t partition_size, size_t alignment, duke@435: bool split, bool realloc) { duke@435: assert(partition_size <= size(), "partition failed"); duke@435: if (split) { duke@435: os::split_reserved_memory(_base, _size, partition_size, realloc); duke@435: } duke@435: ReservedSpace result(base(), partition_size, alignment, special()); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: ReservedSpace duke@435: ReservedSpace::last_part(size_t partition_size, size_t alignment) { duke@435: assert(partition_size <= size(), "partition failed"); duke@435: ReservedSpace result(base() + partition_size, size() - partition_size, duke@435: alignment, special()); duke@435: return result; duke@435: } duke@435: duke@435: duke@435: size_t ReservedSpace::page_align_size_up(size_t size) { duke@435: return align_size_up(size, os::vm_page_size()); duke@435: } duke@435: duke@435: duke@435: size_t ReservedSpace::page_align_size_down(size_t size) { duke@435: return align_size_down(size, os::vm_page_size()); duke@435: } duke@435: duke@435: duke@435: size_t ReservedSpace::allocation_align_size_up(size_t size) { duke@435: return align_size_up(size, os::vm_allocation_granularity()); duke@435: } duke@435: duke@435: duke@435: size_t ReservedSpace::allocation_align_size_down(size_t size) { duke@435: return align_size_down(size, os::vm_allocation_granularity()); duke@435: } duke@435: duke@435: duke@435: void ReservedSpace::release() { duke@435: if (is_reserved()) { duke@435: if (special()) { duke@435: os::release_memory_special(_base, _size); duke@435: } else{ duke@435: os::release_memory(_base, _size); duke@435: } duke@435: _base = NULL; duke@435: _size = 0; duke@435: _special = false; duke@435: } duke@435: } duke@435: duke@435: duke@435: // VirtualSpace duke@435: duke@435: VirtualSpace::VirtualSpace() { duke@435: _low_boundary = NULL; duke@435: _high_boundary = NULL; duke@435: _low = NULL; duke@435: _high = NULL; duke@435: _lower_high = NULL; duke@435: _middle_high = NULL; duke@435: _upper_high = NULL; duke@435: _lower_high_boundary = NULL; duke@435: _middle_high_boundary = NULL; duke@435: _upper_high_boundary = NULL; duke@435: _lower_alignment = 0; duke@435: _middle_alignment = 0; duke@435: _upper_alignment = 0; duke@435: } duke@435: duke@435: duke@435: bool VirtualSpace::initialize(ReservedSpace rs, size_t committed_size) { duke@435: if(!rs.is_reserved()) return false; // allocation failed. duke@435: assert(_low_boundary == NULL, "VirtualSpace already initialized"); duke@435: _low_boundary = rs.base(); duke@435: _high_boundary = low_boundary() + rs.size(); duke@435: duke@435: _low = low_boundary(); duke@435: _high = low(); duke@435: duke@435: _special = rs.special(); duke@435: duke@435: // When a VirtualSpace begins life at a large size, make all future expansion duke@435: // and shrinking occur aligned to a granularity of large pages. This avoids duke@435: // fragmentation of physical addresses that inhibits the use of large pages duke@435: // by the OS virtual memory system. Empirically, we see that with a 4MB duke@435: // page size, the only spaces that get handled this way are codecache and duke@435: // the heap itself, both of which provide a substantial performance duke@435: // boost in many benchmarks when covered by large pages. duke@435: // duke@435: // No attempt is made to force large page alignment at the very top and duke@435: // bottom of the space if they are not aligned so already. duke@435: _lower_alignment = os::vm_page_size(); duke@435: _middle_alignment = os::page_size_for_region(rs.size(), rs.size(), 1); duke@435: _upper_alignment = os::vm_page_size(); duke@435: duke@435: // End of each region duke@435: _lower_high_boundary = (char*) round_to((intptr_t) low_boundary(), middle_alignment()); duke@435: _middle_high_boundary = (char*) round_down((intptr_t) high_boundary(), middle_alignment()); duke@435: _upper_high_boundary = high_boundary(); duke@435: duke@435: // High address of each region duke@435: _lower_high = low_boundary(); duke@435: _middle_high = lower_high_boundary(); duke@435: _upper_high = middle_high_boundary(); duke@435: duke@435: // commit to initial size duke@435: if (committed_size > 0) { duke@435: if (!expand_by(committed_size)) { duke@435: return false; duke@435: } duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: VirtualSpace::~VirtualSpace() { duke@435: release(); duke@435: } duke@435: duke@435: duke@435: void VirtualSpace::release() { duke@435: (void)os::release_memory(low_boundary(), reserved_size()); duke@435: _low_boundary = NULL; duke@435: _high_boundary = NULL; duke@435: _low = NULL; duke@435: _high = NULL; duke@435: _lower_high = NULL; duke@435: _middle_high = NULL; duke@435: _upper_high = NULL; duke@435: _lower_high_boundary = NULL; duke@435: _middle_high_boundary = NULL; duke@435: _upper_high_boundary = NULL; duke@435: _lower_alignment = 0; duke@435: _middle_alignment = 0; duke@435: _upper_alignment = 0; duke@435: _special = false; duke@435: } duke@435: duke@435: duke@435: size_t VirtualSpace::committed_size() const { duke@435: return pointer_delta(high(), low(), sizeof(char)); duke@435: } duke@435: duke@435: duke@435: size_t VirtualSpace::reserved_size() const { duke@435: return pointer_delta(high_boundary(), low_boundary(), sizeof(char)); duke@435: } duke@435: duke@435: duke@435: size_t VirtualSpace::uncommitted_size() const { duke@435: return reserved_size() - committed_size(); duke@435: } duke@435: duke@435: duke@435: bool VirtualSpace::contains(const void* p) const { duke@435: return low() <= (const char*) p && (const char*) p < high(); duke@435: } duke@435: duke@435: /* duke@435: First we need to determine if a particular virtual space is using large duke@435: pages. This is done at the initialize function and only virtual spaces duke@435: that are larger than LargePageSizeInBytes use large pages. Once we duke@435: have determined this, all expand_by and shrink_by calls must grow and duke@435: shrink by large page size chunks. If a particular request duke@435: is within the current large page, the call to commit and uncommit memory duke@435: can be ignored. In the case that the low and high boundaries of this duke@435: space is not large page aligned, the pages leading to the first large duke@435: page address and the pages after the last large page address must be duke@435: allocated with default pages. duke@435: */ duke@435: bool VirtualSpace::expand_by(size_t bytes, bool pre_touch) { duke@435: if (uncommitted_size() < bytes) return false; duke@435: duke@435: if (special()) { duke@435: // don't commit memory if the entire space is pinned in memory duke@435: _high += bytes; duke@435: return true; duke@435: } duke@435: duke@435: char* previous_high = high(); duke@435: char* unaligned_new_high = high() + bytes; duke@435: assert(unaligned_new_high <= high_boundary(), duke@435: "cannot expand by more than upper boundary"); duke@435: duke@435: // Calculate where the new high for each of the regions should be. If duke@435: // the low_boundary() and high_boundary() are LargePageSizeInBytes aligned duke@435: // then the unaligned lower and upper new highs would be the duke@435: // lower_high() and upper_high() respectively. duke@435: char* unaligned_lower_new_high = duke@435: MIN2(unaligned_new_high, lower_high_boundary()); duke@435: char* unaligned_middle_new_high = duke@435: MIN2(unaligned_new_high, middle_high_boundary()); duke@435: char* unaligned_upper_new_high = duke@435: MIN2(unaligned_new_high, upper_high_boundary()); duke@435: duke@435: // Align the new highs based on the regions alignment. lower and upper duke@435: // alignment will always be default page size. middle alignment will be duke@435: // LargePageSizeInBytes if the actual size of the virtual space is in duke@435: // fact larger than LargePageSizeInBytes. duke@435: char* aligned_lower_new_high = duke@435: (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment()); duke@435: char* aligned_middle_new_high = duke@435: (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment()); duke@435: char* aligned_upper_new_high = duke@435: (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment()); duke@435: duke@435: // Determine which regions need to grow in this expand_by call. duke@435: // If you are growing in the lower region, high() must be in that duke@435: // region so calcuate the size based on high(). For the middle and duke@435: // upper regions, determine the starting point of growth based on the duke@435: // location of high(). By getting the MAX of the region's low address duke@435: // (or the prevoius region's high address) and high(), we can tell if it duke@435: // is an intra or inter region growth. duke@435: size_t lower_needs = 0; duke@435: if (aligned_lower_new_high > lower_high()) { duke@435: lower_needs = duke@435: pointer_delta(aligned_lower_new_high, lower_high(), sizeof(char)); duke@435: } duke@435: size_t middle_needs = 0; duke@435: if (aligned_middle_new_high > middle_high()) { duke@435: middle_needs = duke@435: pointer_delta(aligned_middle_new_high, middle_high(), sizeof(char)); duke@435: } duke@435: size_t upper_needs = 0; duke@435: if (aligned_upper_new_high > upper_high()) { duke@435: upper_needs = duke@435: pointer_delta(aligned_upper_new_high, upper_high(), sizeof(char)); duke@435: } duke@435: duke@435: // Check contiguity. duke@435: assert(low_boundary() <= lower_high() && duke@435: lower_high() <= lower_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(lower_high_boundary() <= middle_high() && duke@435: middle_high() <= middle_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(middle_high_boundary() <= upper_high() && duke@435: upper_high() <= upper_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: duke@435: // Commit regions duke@435: if (lower_needs > 0) { duke@435: assert(low_boundary() <= lower_high() && duke@435: lower_high() + lower_needs <= lower_high_boundary(), duke@435: "must not expand beyond region"); duke@435: if (!os::commit_memory(lower_high(), lower_needs)) { duke@435: debug_only(warning("os::commit_memory failed")); duke@435: return false; duke@435: } else { duke@435: _lower_high += lower_needs; duke@435: } duke@435: } duke@435: if (middle_needs > 0) { duke@435: assert(lower_high_boundary() <= middle_high() && duke@435: middle_high() + middle_needs <= middle_high_boundary(), duke@435: "must not expand beyond region"); duke@435: if (!os::commit_memory(middle_high(), middle_needs, middle_alignment())) { duke@435: debug_only(warning("os::commit_memory failed")); duke@435: return false; duke@435: } duke@435: _middle_high += middle_needs; duke@435: } duke@435: if (upper_needs > 0) { duke@435: assert(middle_high_boundary() <= upper_high() && duke@435: upper_high() + upper_needs <= upper_high_boundary(), duke@435: "must not expand beyond region"); duke@435: if (!os::commit_memory(upper_high(), upper_needs)) { duke@435: debug_only(warning("os::commit_memory failed")); duke@435: return false; duke@435: } else { duke@435: _upper_high += upper_needs; duke@435: } duke@435: } duke@435: duke@435: if (pre_touch || AlwaysPreTouch) { duke@435: int vm_ps = os::vm_page_size(); duke@435: for (char* curr = previous_high; duke@435: curr < unaligned_new_high; duke@435: curr += vm_ps) { duke@435: // Note the use of a write here; originally we tried just a read, but duke@435: // since the value read was unused, the optimizer removed the read. duke@435: // If we ever have a concurrent touchahead thread, we'll want to use duke@435: // a read, to avoid the potential of overwriting data (if a mutator duke@435: // thread beats the touchahead thread to a page). There are various duke@435: // ways of making sure this read is not optimized away: for example, duke@435: // generating the code for a read procedure at runtime. duke@435: *curr = 0; duke@435: } duke@435: } duke@435: duke@435: _high += bytes; duke@435: return true; duke@435: } duke@435: duke@435: // A page is uncommitted if the contents of the entire page is deemed unusable. duke@435: // Continue to decrement the high() pointer until it reaches a page boundary duke@435: // in which case that particular page can now be uncommitted. duke@435: void VirtualSpace::shrink_by(size_t size) { duke@435: if (committed_size() < size) duke@435: fatal("Cannot shrink virtual space to negative size"); duke@435: duke@435: if (special()) { duke@435: // don't uncommit if the entire space is pinned in memory duke@435: _high -= size; duke@435: return; duke@435: } duke@435: duke@435: char* unaligned_new_high = high() - size; duke@435: assert(unaligned_new_high >= low_boundary(), "cannot shrink past lower boundary"); duke@435: duke@435: // Calculate new unaligned address duke@435: char* unaligned_upper_new_high = duke@435: MAX2(unaligned_new_high, middle_high_boundary()); duke@435: char* unaligned_middle_new_high = duke@435: MAX2(unaligned_new_high, lower_high_boundary()); duke@435: char* unaligned_lower_new_high = duke@435: MAX2(unaligned_new_high, low_boundary()); duke@435: duke@435: // Align address to region's alignment duke@435: char* aligned_upper_new_high = duke@435: (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment()); duke@435: char* aligned_middle_new_high = duke@435: (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment()); duke@435: char* aligned_lower_new_high = duke@435: (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment()); duke@435: duke@435: // Determine which regions need to shrink duke@435: size_t upper_needs = 0; duke@435: if (aligned_upper_new_high < upper_high()) { duke@435: upper_needs = duke@435: pointer_delta(upper_high(), aligned_upper_new_high, sizeof(char)); duke@435: } duke@435: size_t middle_needs = 0; duke@435: if (aligned_middle_new_high < middle_high()) { duke@435: middle_needs = duke@435: pointer_delta(middle_high(), aligned_middle_new_high, sizeof(char)); duke@435: } duke@435: size_t lower_needs = 0; duke@435: if (aligned_lower_new_high < lower_high()) { duke@435: lower_needs = duke@435: pointer_delta(lower_high(), aligned_lower_new_high, sizeof(char)); duke@435: } duke@435: duke@435: // Check contiguity. duke@435: assert(middle_high_boundary() <= upper_high() && duke@435: upper_high() <= upper_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(lower_high_boundary() <= middle_high() && duke@435: middle_high() <= middle_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(low_boundary() <= lower_high() && duke@435: lower_high() <= lower_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: duke@435: // Uncommit duke@435: if (upper_needs > 0) { duke@435: assert(middle_high_boundary() <= aligned_upper_new_high && duke@435: aligned_upper_new_high + upper_needs <= upper_high_boundary(), duke@435: "must not shrink beyond region"); duke@435: if (!os::uncommit_memory(aligned_upper_new_high, upper_needs)) { duke@435: debug_only(warning("os::uncommit_memory failed")); duke@435: return; duke@435: } else { duke@435: _upper_high -= upper_needs; duke@435: } duke@435: } duke@435: if (middle_needs > 0) { duke@435: assert(lower_high_boundary() <= aligned_middle_new_high && duke@435: aligned_middle_new_high + middle_needs <= middle_high_boundary(), duke@435: "must not shrink beyond region"); duke@435: if (!os::uncommit_memory(aligned_middle_new_high, middle_needs)) { duke@435: debug_only(warning("os::uncommit_memory failed")); duke@435: return; duke@435: } else { duke@435: _middle_high -= middle_needs; duke@435: } duke@435: } duke@435: if (lower_needs > 0) { duke@435: assert(low_boundary() <= aligned_lower_new_high && duke@435: aligned_lower_new_high + lower_needs <= lower_high_boundary(), duke@435: "must not shrink beyond region"); duke@435: if (!os::uncommit_memory(aligned_lower_new_high, lower_needs)) { duke@435: debug_only(warning("os::uncommit_memory failed")); duke@435: return; duke@435: } else { duke@435: _lower_high -= lower_needs; duke@435: } duke@435: } duke@435: duke@435: _high -= size; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void VirtualSpace::check_for_contiguity() { duke@435: // Check contiguity. duke@435: assert(low_boundary() <= lower_high() && duke@435: lower_high() <= lower_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(lower_high_boundary() <= middle_high() && duke@435: middle_high() <= middle_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(middle_high_boundary() <= upper_high() && duke@435: upper_high() <= upper_high_boundary(), duke@435: "high address must be contained within the region"); duke@435: assert(low() >= low_boundary(), "low"); duke@435: assert(low_boundary() <= lower_high_boundary(), "lower high boundary"); duke@435: assert(upper_high_boundary() <= high_boundary(), "upper high boundary"); duke@435: assert(high() <= upper_high(), "upper high"); duke@435: } duke@435: duke@435: void VirtualSpace::print() { duke@435: tty->print ("Virtual space:"); duke@435: if (special()) tty->print(" (pinned in memory)"); duke@435: tty->cr(); duke@435: tty->print_cr(" - committed: %ld", committed_size()); duke@435: tty->print_cr(" - reserved: %ld", reserved_size()); duke@435: tty->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low(), high()); duke@435: tty->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low_boundary(), high_boundary()); duke@435: } duke@435: duke@435: #endif