Path: blob/master/src/hotspot/share/memory/virtualspace.hpp
40949 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_VIRTUALSPACE_HPP25#define SHARE_MEMORY_VIRTUALSPACE_HPP2627#include "memory/memRegion.hpp"28#include "utilities/globalDefinitions.hpp"2930class outputStream;3132// ReservedSpace is a data structure for reserving a contiguous address range.3334class ReservedSpace {35friend class VMStructs;36protected:37char* _base;38size_t _size;39size_t _noaccess_prefix;40size_t _alignment;41size_t _page_size;42bool _special;43int _fd_for_heap;44private:45bool _executable;4647// ReservedSpace48ReservedSpace(char* base, size_t size, size_t alignment,49size_t page_size, bool special, bool executable);50protected:51// Helpers to clear and set members during initialization. Two members52// require special treatment:53// * _fd_for_heap - The fd is set once and should not be cleared54// even if the reservation has to be retried.55// * _noaccess_prefix - Used for compressed heaps and updated after56// the reservation is initialized. Always set to57// 0 during initialization.58void clear_members();59void initialize_members(char* base, size_t size, size_t alignment,60size_t page_size, bool special, bool executable);6162void initialize(size_t size, size_t alignment, size_t page_size,63char* requested_address, bool executable);6465void reserve(size_t size, size_t alignment, size_t page_size,66char* requested_address, bool executable);67public:68// Constructor69ReservedSpace();70// Initialize the reserved space with the given size. Depending on the size71// a suitable page size and alignment will be used.72explicit ReservedSpace(size_t size);73// Initialize the reserved space with the given size. The preferred_page_size74// is used as the minimum page size/alignment. This may waste some space if75// the given size is not aligned to that value, as the reservation will be76// aligned up to the final alignment in this case.77ReservedSpace(size_t size, size_t preferred_page_size);78ReservedSpace(size_t size, size_t alignment, size_t page_size,79char* requested_address = NULL);8081// Accessors82char* base() const { return _base; }83size_t size() const { return _size; }84char* end() const { return _base + _size; }85size_t alignment() const { return _alignment; }86size_t page_size() const { return _page_size; }87bool special() const { return _special; }88bool executable() const { return _executable; }89size_t noaccess_prefix() const { return _noaccess_prefix; }90bool is_reserved() const { return _base != NULL; }91void release();9293// Splitting94// This splits the space into two spaces, the first part of which will be returned.95ReservedSpace first_part(size_t partition_size, size_t alignment);96ReservedSpace last_part (size_t partition_size, size_t alignment);9798// These simply call the above using the default alignment.99inline ReservedSpace first_part(size_t partition_size);100inline ReservedSpace last_part (size_t partition_size);101102// Alignment103static size_t page_align_size_up(size_t size);104static size_t page_align_size_down(size_t size);105static size_t allocation_align_size_up(size_t size);106bool contains(const void* p) const {107return (base() <= ((char*)p)) && (((char*)p) < (base() + size()));108}109};110111ReservedSpace112ReservedSpace::first_part(size_t partition_size)113{114return first_part(partition_size, alignment());115}116117ReservedSpace ReservedSpace::last_part(size_t partition_size)118{119return last_part(partition_size, alignment());120}121122// Class encapsulating behavior specific of memory space reserved for Java heap.123class ReservedHeapSpace : public ReservedSpace {124private:125void try_reserve_heap(size_t size, size_t alignment, size_t page_size,126char *requested_address);127void try_reserve_range(char *highest_start, char *lowest_start,128size_t attach_point_alignment, char *aligned_HBMA,129char *upper_bound, size_t size, size_t alignment, size_t page_size);130void initialize_compressed_heap(const size_t size, size_t alignment, size_t page_size);131// Create protection page at the beginning of the space.132void establish_noaccess_prefix();133public:134// Constructor. Tries to find a heap that is good for compressed oops.135// heap_allocation_directory is the path to the backing memory for Java heap. When set, Java heap will be allocated136// on the device which is managed by the file system where the directory resides.137ReservedHeapSpace(size_t size, size_t forced_base_alignment, size_t page_size, const char* heap_allocation_directory = NULL);138// Returns the base to be used for compression, i.e. so that null can be139// encoded safely and implicit null checks can work.140char *compressed_oop_base() const { return _base - _noaccess_prefix; }141MemRegion region() const;142};143144// Class encapsulating behavior specific memory space for Code145class ReservedCodeSpace : public ReservedSpace {146public:147// Constructor148ReservedCodeSpace(size_t r_size, size_t rs_align, size_t page_size);149};150151// VirtualSpace is data structure for committing a previously reserved address range in smaller chunks.152153class VirtualSpace {154friend class VMStructs;155private:156// Reserved area157char* _low_boundary;158char* _high_boundary;159160// Committed area161char* _low;162char* _high;163164// The entire space has been committed and pinned in memory, no165// os::commit_memory() or os::uncommit_memory().166bool _special;167168// Need to know if commit should be executable.169bool _executable;170171// MPSS Support172// Each virtualspace region has a lower, middle, and upper region.173// Each region has an end boundary and a high pointer which is the174// high water mark for the last allocated byte.175// The lower and upper unaligned to LargePageSizeInBytes uses default page.176// size. The middle region uses large page size.177char* _lower_high;178char* _middle_high;179char* _upper_high;180181char* _lower_high_boundary;182char* _middle_high_boundary;183char* _upper_high_boundary;184185size_t _lower_alignment;186size_t _middle_alignment;187size_t _upper_alignment;188189// MPSS Accessors190char* lower_high() const { return _lower_high; }191char* middle_high() const { return _middle_high; }192char* upper_high() const { return _upper_high; }193194char* lower_high_boundary() const { return _lower_high_boundary; }195char* middle_high_boundary() const { return _middle_high_boundary; }196char* upper_high_boundary() const { return _upper_high_boundary; }197198size_t lower_alignment() const { return _lower_alignment; }199size_t middle_alignment() const { return _middle_alignment; }200size_t upper_alignment() const { return _upper_alignment; }201202public:203// Committed area204char* low() const { return _low; }205char* high() const { return _high; }206207// Reserved area208char* low_boundary() const { return _low_boundary; }209char* high_boundary() const { return _high_boundary; }210211bool special() const { return _special; }212213public:214// Initialization215VirtualSpace();216bool initialize_with_granularity(ReservedSpace rs, size_t committed_byte_size, size_t max_commit_ganularity);217bool initialize(ReservedSpace rs, size_t committed_byte_size);218219// Destruction220~VirtualSpace();221222// Reserved memory223size_t reserved_size() const;224// Actually committed OS memory225size_t actual_committed_size() const;226// Memory used/expanded in this virtual space227size_t committed_size() const;228// Memory left to use/expand in this virtual space229size_t uncommitted_size() const;230231bool contains(const void* p) const;232233// Operations234// returns true on success, false otherwise235bool expand_by(size_t bytes, bool pre_touch = false);236void shrink_by(size_t bytes);237void release();238239void check_for_contiguity() PRODUCT_RETURN;240241// Debugging242void print_on(outputStream* out) PRODUCT_RETURN;243void print();244};245246#endif // SHARE_MEMORY_VIRTUALSPACE_HPP247248249