duke@435: /* trims@1907: * Copyright (c) 1997, 2009, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * 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) { coleenp@1091: initialize(size, 0, false, NULL, 0, false); duke@435: } duke@435: duke@435: ReservedSpace::ReservedSpace(size_t size, size_t alignment, coleenp@672: bool large, coleenp@672: char* requested_address, coleenp@672: const size_t noaccess_prefix) { coleenp@672: initialize(size+noaccess_prefix, alignment, large, requested_address, coleenp@1091: noaccess_prefix, false); coleenp@1091: } coleenp@1091: coleenp@1091: ReservedSpace::ReservedSpace(size_t size, size_t alignment, coleenp@1091: bool large, coleenp@1091: bool executable) { coleenp@1091: initialize(size, alignment, large, NULL, 0, executable); 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: kvn@1973: // Helper method. kvn@1973: static bool failed_to_reserve_as_requested(char* base, char* requested_address, kvn@1973: const size_t size, bool special) kvn@1973: { kvn@1973: if (base == requested_address || requested_address == NULL) kvn@1973: return false; // did not fail kvn@1973: kvn@1973: if (base != NULL) { kvn@1973: // Different reserve address may be acceptable in other cases kvn@1973: // but for compressed oops heap should be at requested address. kvn@1973: assert(UseCompressedOops, "currently requested address used only for compressed oops"); kvn@1973: if (PrintCompressedOopsMode) { kvn@1973: tty->cr(); kvn@1973: tty->print_cr("Reserved memory at not requested address: " PTR_FORMAT " vs " PTR_FORMAT, base, requested_address); kvn@1973: } kvn@1973: // OS ignored requested address. Try different address. kvn@1973: if (special) { kvn@1973: if (!os::release_memory_special(base, size)) { kvn@1973: fatal("os::release_memory_special failed"); kvn@1973: } kvn@1973: } else { kvn@1973: if (!os::release_memory(base, size)) { kvn@1973: fatal("os::release_memory failed"); kvn@1973: } kvn@1973: } kvn@1973: } kvn@1973: return true; kvn@1973: } kvn@1973: duke@435: ReservedSpace::ReservedSpace(const size_t prefix_size, duke@435: const size_t prefix_align, duke@435: const size_t suffix_size, coleenp@672: const size_t suffix_align, kvn@1077: char* requested_address, coleenp@672: const size_t noaccess_prefix) 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: kvn@1973: // Assert that if noaccess_prefix is used, it is the same as prefix_align. kvn@1973: assert(noaccess_prefix == 0 || kvn@1973: noaccess_prefix == prefix_align, "noaccess prefix wrong"); kvn@1973: coleenp@672: // Add in noaccess_prefix to prefix_size; coleenp@672: const size_t adjusted_prefix_size = prefix_size + noaccess_prefix; coleenp@672: const size_t size = adjusted_prefix_size + suffix_size; coleenp@672: 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) { coleenp@1091: initialize(size, prefix_align, true, requested_address, noaccess_prefix, coleenp@1091: false); duke@435: return; duke@435: } duke@435: duke@435: _base = NULL; duke@435: _size = 0; duke@435: _alignment = 0; duke@435: _special = false; coleenp@672: _noaccess_prefix = 0; coleenp@1091: _executable = false; coleenp@672: duke@435: // Optimistically try to reserve the exact size needed. kvn@1077: char* addr; kvn@1077: if (requested_address != 0) { kvn@1973: requested_address -= noaccess_prefix; // adjust address kvn@1973: assert(requested_address != NULL, "huge noaccess prefix?"); kvn@1973: addr = os::attempt_reserve_memory_at(size, requested_address); kvn@1973: if (failed_to_reserve_as_requested(addr, requested_address, size, false)) { kvn@1973: // OS ignored requested address. Try different address. kvn@1973: addr = NULL; kvn@1973: } kvn@1077: } else { kvn@1077: addr = os::reserve_memory(size, NULL, prefix_align); kvn@1077: } 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). coleenp@672: const size_t ofs = size_t(addr) + adjusted_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); coleenp@672: addr = reserve_and_align(size + extra, adjusted_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. coleenp@672: addr = reserve_and_align(size + suffix_align, adjusted_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; coleenp@672: _noaccess_prefix = noaccess_prefix; duke@435: } duke@435: duke@435: void ReservedSpace::initialize(size_t size, size_t alignment, bool large, coleenp@672: char* requested_address, coleenp@1091: const size_t noaccess_prefix, coleenp@1091: bool executable) { 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; coleenp@1091: _executable = executable; duke@435: _alignment = 0; coleenp@672: _noaccess_prefix = 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: kvn@1973: if (requested_address != 0) { kvn@1973: requested_address -= noaccess_prefix; // adjust requested address kvn@1973: assert(requested_address != NULL, "huge noaccess prefix?"); kvn@1973: } kvn@1973: duke@435: if (special) { duke@435: coleenp@1091: base = os::reserve_memory_special(size, requested_address, executable); duke@435: duke@435: if (base != NULL) { kvn@1973: if (failed_to_reserve_as_requested(base, requested_address, size, true)) { kvn@1973: // OS ignored requested address. Try different address. kvn@1973: return; kvn@1973: } 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 kvn@1973: if (UseLargePages && (!FLAG_IS_DEFAULT(UseLargePages) || kvn@1973: !FLAG_IS_DEFAULT(LargePageSizeInBytes))) { kvn@1973: if (PrintCompressedOopsMode) { kvn@1973: tty->cr(); kvn@1973: tty->print_cr("Reserve regular memory without large pages."); kvn@1973: } kvn@1973: } 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) { kvn@1973: base = os::attempt_reserve_memory_at(size, requested_address); kvn@1973: if (failed_to_reserve_as_requested(base, requested_address, size, false)) { kvn@1973: // OS ignored requested address. Try different address. kvn@1973: base = NULL; kvn@1973: } 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; ysr@777: do { ysr@777: char* extra_base = os::reserve_memory(extra_size, NULL, alignment); ysr@777: if (extra_base == NULL) return; ysr@777: // Do manual alignement ysr@777: base = (char*) align_size_up((uintptr_t) extra_base, alignment); ysr@777: assert(base >= extra_base, "just checking"); ysr@777: // Re-reserve the region at the aligned base address. ysr@777: os::release_memory(extra_base, extra_size); ysr@777: base = os::reserve_memory(size, base); ysr@777: } while (base == NULL); 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()); coleenp@672: _noaccess_prefix = noaccess_prefix; coleenp@672: coleenp@672: // Assert that if noaccess_prefix is used, it is the same as alignment. coleenp@672: assert(noaccess_prefix == 0 || coleenp@672: noaccess_prefix == _alignment, "noaccess prefix wrong"); 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, coleenp@1091: bool special, bool executable) { 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; coleenp@672: _noaccess_prefix = 0; duke@435: _special = special; coleenp@1091: _executable = executable; 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) { coleenp@1091: os::split_reserved_memory(base(), size(), partition_size, realloc); duke@435: } coleenp@1091: ReservedSpace result(base(), partition_size, alignment, special(), coleenp@1091: executable()); 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, coleenp@1091: alignment, special(), executable()); 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()) { coleenp@672: char *real_base = _base - _noaccess_prefix; coleenp@672: const size_t real_size = _size + _noaccess_prefix; duke@435: if (special()) { coleenp@672: os::release_memory_special(real_base, real_size); duke@435: } else{ coleenp@672: os::release_memory(real_base, real_size); duke@435: } duke@435: _base = NULL; duke@435: _size = 0; coleenp@672: _noaccess_prefix = 0; duke@435: _special = false; coleenp@1091: _executable = false; duke@435: } duke@435: } duke@435: coleenp@672: void ReservedSpace::protect_noaccess_prefix(const size_t size) { kvn@1973: assert( (_noaccess_prefix != 0) == (UseCompressedOops && _base != NULL && kvn@1973: (size_t(_base + _size) > OopEncodingHeapMax) && kvn@1973: Universe::narrow_oop_use_implicit_null_checks()), kvn@1973: "noaccess_prefix should be used only with non zero based compressed oops"); kvn@1973: kvn@1973: // If there is no noaccess prefix, return. coleenp@672: if (_noaccess_prefix == 0) return; coleenp@672: coleenp@672: assert(_noaccess_prefix >= (size_t)os::vm_page_size(), coleenp@672: "must be at least page size big"); coleenp@672: coleenp@672: // Protect memory at the base of the allocated region. coleenp@672: // If special, the page was committed (only matters on windows) coleenp@672: if (!os::protect_memory(_base, _noaccess_prefix, os::MEM_PROT_NONE, coleenp@672: _special)) { coleenp@672: fatal("cannot protect protection page"); coleenp@672: } kvn@1973: if (PrintCompressedOopsMode) { kvn@1973: tty->cr(); kvn@1973: tty->print_cr("Protected page at the reserved heap base: " PTR_FORMAT " / " INTX_FORMAT " bytes", _base, _noaccess_prefix); kvn@1973: } coleenp@672: coleenp@672: _base += _noaccess_prefix; coleenp@672: _size -= _noaccess_prefix; coleenp@672: assert((size == _size) && ((uintptr_t)_base % _alignment == 0), coleenp@672: "must be exactly of required size and alignment"); coleenp@672: } coleenp@672: coleenp@672: ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment, coleenp@672: bool large, char* requested_address) : coleenp@672: ReservedSpace(size, alignment, large, coleenp@672: requested_address, kvn@1077: (UseCompressedOops && (Universe::narrow_oop_base() != NULL) && kvn@1077: Universe::narrow_oop_use_implicit_null_checks()) ? coleenp@760: lcm(os::vm_page_size(), alignment) : 0) { coleenp@672: // Only reserved space for the java heap should have a noaccess_prefix coleenp@672: // if using compressed oops. coleenp@672: protect_noaccess_prefix(size); coleenp@672: } coleenp@672: coleenp@672: ReservedHeapSpace::ReservedHeapSpace(const size_t prefix_size, coleenp@672: const size_t prefix_align, coleenp@672: const size_t suffix_size, kvn@1077: const size_t suffix_align, kvn@1077: char* requested_address) : coleenp@672: ReservedSpace(prefix_size, prefix_align, suffix_size, suffix_align, kvn@1077: requested_address, kvn@1077: (UseCompressedOops && (Universe::narrow_oop_base() != NULL) && kvn@1077: Universe::narrow_oop_use_implicit_null_checks()) ? coleenp@760: lcm(os::vm_page_size(), prefix_align) : 0) { coleenp@672: protect_noaccess_prefix(prefix_size+suffix_size); coleenp@672: } duke@435: coleenp@1091: // Reserve space for code segment. Same as Java heap only we mark this as coleenp@1091: // executable. coleenp@1091: ReservedCodeSpace::ReservedCodeSpace(size_t r_size, coleenp@1091: size_t rs_align, coleenp@1091: bool large) : coleenp@1091: ReservedSpace(r_size, rs_align, large, /*executable*/ true) { coleenp@1091: } coleenp@1091: 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; coleenp@672: _special = false; coleenp@1091: _executable = false; 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(); coleenp@1091: _executable = rs.executable(); 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() { coleenp@672: // This does not release memory it never reserved. coleenp@672: // Caller must release via rs.release(); 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; coleenp@1091: _executable = 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"); coleenp@1091: if (!os::commit_memory(lower_high(), lower_needs, _executable)) { 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"); coleenp@1091: if (!os::commit_memory(middle_high(), middle_needs, middle_alignment(), coleenp@1091: _executable)) { 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"); coleenp@1091: if (!os::commit_memory(upper_high(), upper_needs, _executable)) { 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