1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_GC_G1_G1PAGEBASEDVIRTUALSPACE_HPP 26 #define SHARE_VM_GC_G1_G1PAGEBASEDVIRTUALSPACE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/memRegion.hpp" 30 #include "memory/virtualspace.hpp" 31 #include "utilities/align.hpp" 32 #include "utilities/bitMap.hpp" 33 34 class WorkGang; 35 36 // Virtual space management helper for a virtual space with an OS page allocation 37 // granularity. 38 // (De-)Allocation requests are always OS page aligned by passing a page index 39 // and multiples of pages. 40 // For systems that only commits of memory in a given size (always greater than 41 // page size) the base address is required to be aligned to that page size. 42 // The actual size requested need not be aligned to that page size, but the size 43 // of the reservation passed may be rounded up to this page size. Any fragment 44 // (less than the page size) of the actual size at the tail of the request will 45 // be committed using OS small pages. 46 // The implementation gives an error when trying to commit or uncommit pages that 47 // have already been committed or uncommitted. 48 class G1PageBasedVirtualSpace VALUE_OBJ_CLASS_SPEC { 49 friend class VMStructs; 50 private: 51 // Reserved area addresses. 52 char* _low_boundary; 53 char* _high_boundary; 54 55 // The size of the tail in bytes of the handled space that needs to be committed 56 // using small pages. 57 size_t _tail_size; 58 59 // The preferred page size used for commit/uncommit in bytes. 60 size_t _page_size; 61 62 // Bitmap used for verification of commit/uncommit operations. 63 CHeapBitMap _committed; 64 65 // Bitmap used to keep track of which pages are dirty or not for _special 66 // spaces. This is needed because for those spaces the underlying memory 67 // will only be zero filled the first time it is committed. Calls to commit 68 // will use this bitmap and return whether or not the memory is zero filled. 69 CHeapBitMap _dirty; 70 71 // Indicates that the entire space has been committed and pinned in memory, 72 // os::commit_memory() or os::uncommit_memory() have no function. 73 bool _special; 74 75 // Indicates whether the committed space should be executable. 76 bool _executable; 77 78 // Helper function for committing memory. Commit the given memory range by using 79 // _page_size pages as much as possible and the remainder with small sized pages. 80 void commit_internal(size_t start_page, size_t end_page); 81 // Commit num_pages pages of _page_size size starting from start. All argument 82 // checking has been performed. 83 void commit_preferred_pages(size_t start_page, size_t end_page); 84 // Commit space at the high end of the space that needs to be committed with small 85 // sized pages. 86 void commit_tail(); 87 88 // Uncommit the given memory range. 89 void uncommit_internal(size_t start_page, size_t end_page); 90 91 // Pretouch the given memory range. 92 void pretouch_internal(size_t start_page, size_t end_page); 93 94 // Returns the index of the page which contains the given address. 95 uintptr_t addr_to_page_index(char* addr) const; 96 // Returns the address of the given page index. 97 char* page_start(size_t index) const; 98 99 // Is the given page index the last page? 100 bool is_last_page(size_t index) const { return index == (_committed.size() - 1); } 101 // Is the given page index the first after last page? 102 bool is_after_last_page(size_t index) const; 103 // Is the last page only partially covered by this space? 104 bool is_last_page_partial() const { return !is_aligned(_high_boundary, _page_size); } 105 // Returns the end address of the given page bounded by the reserved space. 106 char* bounded_end_addr(size_t end_page) const; 107 108 // Returns true if the entire area is backed by committed memory. 109 bool is_area_committed(size_t start_page, size_t size_in_pages) const; 110 // Returns true if the entire area is not backed by committed memory. 111 bool is_area_uncommitted(size_t start_page, size_t size_in_pages) const; 112 113 void initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size); 114 public: 115 116 // Commit the given area of pages starting at start being size_in_pages large. 117 // Returns true if the given area is zero filled upon completion. 118 bool commit(size_t start_page, size_t size_in_pages); 119 120 // Uncommit the given area of pages starting at start being size_in_pages large. 121 void uncommit(size_t start_page, size_t size_in_pages); 122 123 void pretouch(size_t start_page, size_t size_in_pages, WorkGang* pretouch_gang = NULL); 124 125 // Initialize the given reserved space with the given base address and the size 126 // actually used. 127 // Prefer to commit in page_size chunks. 128 G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size); 129 130 // Destruction 131 ~G1PageBasedVirtualSpace(); 132 133 // Amount of reserved memory. 134 size_t reserved_size() const; 135 // Memory used in this virtual space. 136 size_t committed_size() const; 137 // Memory left to use/expand in this virtual space. 138 size_t uncommitted_size() const; 139 140 bool contains(const void* p) const; 141 142 MemRegion reserved() { 143 MemRegion x((HeapWord*)_low_boundary, reserved_size() / HeapWordSize); 144 return x; 145 } 146 147 void check_for_contiguity() PRODUCT_RETURN; 148 149 // Debugging 150 void print_on(outputStream* out) PRODUCT_RETURN; 151 void print(); 152 }; 153 154 #endif // SHARE_VM_GC_G1_G1PAGEBASEDVIRTUALSPACE_HPP