g1PageBasedVirtualSpace.hpp 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1PAGEBASEDVIRTUALSPACE_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_G1PAGEBASEDVIRTUALSPACE_HPP

#include "memory/allocation.hpp"
#include "memory/memRegion.hpp"
#include "runtime/virtualspace.hpp"
#include "utilities/bitMap.hpp"

// Virtual space management helper for a virtual space with an OS page allocation
// granularity.
// (De-)Allocation requests are always OS page aligned by passing a page index
// and multiples of pages.
37 38 39 40 41 42
// For systems that only commits of memory in a given size (always greater than
// page size) the base address is required to be aligned to that page size.
// The actual size requested need not be aligned to that page size, but the size
// of the reservation passed may be rounded up to this page size. Any fragment
// (less than the page size) of the actual size at the tail of the request will
// be committed using OS small pages.
43 44 45 46 47 48 49 50 51
// The implementation gives an error when trying to commit or uncommit pages that
// have already been committed or uncommitted.
class G1PageBasedVirtualSpace VALUE_OBJ_CLASS_SPEC {
  friend class VMStructs;
 private:
  // Reserved area addresses.
  char* _low_boundary;
  char* _high_boundary;

52 53 54 55 56
  // The size of the tail in bytes of the handled space that needs to be committed
  // using small pages.
  size_t _tail_size;

  // The preferred page size used for commit/uncommit in bytes.
57 58 59 60 61
  size_t _page_size;

  // Bitmap used for verification of commit/uncommit operations.
  BitMap _committed;

62 63 64 65 66 67
  // Bitmap used to keep track of which pages are dirty or not for _special
  // spaces. This is needed because for those spaces the underlying memory
  // will only be zero filled the first time it is committed. Calls to commit
  // will use this bitmap and return whether or not the memory is zero filled.
  BitMap _dirty;

68 69 70 71 72 73 74
  // Indicates that the entire space has been committed and pinned in memory,
  // os::commit_memory() or os::uncommit_memory() have no function.
  bool _special;

  // Indicates whether the committed space should be executable.
  bool _executable;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  // Helper function for committing memory. Commit the given memory range by using
  // _page_size pages as much as possible and the remainder with small sized pages.
  void commit_internal(size_t start_page, size_t end_page);
  // Commit num_pages pages of _page_size size starting from start. All argument
  // checking has been performed.
  void commit_preferred_pages(size_t start_page, size_t end_page);
  // Commit space at the high end of the space that needs to be committed with small
  // sized pages.
  void commit_tail();

  // Uncommit the given memory range.
  void uncommit_internal(size_t start_page, size_t end_page);

  // Pretouch the given memory range.
  void pretouch_internal(size_t start_page, size_t end_page);

91 92 93
  // Returns the index of the page which contains the given address.
  uintptr_t  addr_to_page_index(char* addr) const;
  // Returns the address of the given page index.
94 95 96 97 98 99 100 101 102 103
  char*  page_start(size_t index) const;

  // Is the given page index the last page?
  bool is_last_page(size_t index) const { return index == (_committed.size() - 1); }
  // Is the given page index the first after last page?
  bool is_after_last_page(size_t index) const;
  // Is the last page only partially covered by this space?
  bool is_last_page_partial() const { return !is_ptr_aligned(_high_boundary, _page_size); }
  // Returns the end address of the given page bounded by the reserved space.
  char* bounded_end_addr(size_t end_page) const;
104 105

  // Returns true if the entire area is backed by committed memory.
106
  bool is_area_committed(size_t start_page, size_t size_in_pages) const;
107
  // Returns true if the entire area is not backed by committed memory.
108
  bool is_area_uncommitted(size_t start_page, size_t size_in_pages) const;
109

110
  void initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size);
111 112 113
 public:

  // Commit the given area of pages starting at start being size_in_pages large.
114
  // Returns true if the given area is zero filled upon completion.
115
  bool commit(size_t start_page, size_t size_in_pages);
116 117

  // Uncommit the given area of pages starting at start being size_in_pages large.
118
  void uncommit(size_t start_page, size_t size_in_pages);
119

120 121 122 123
  // Initialize the given reserved space with the given base address and the size
  // actually used.
  // Prefer to commit in page_size chunks.
  G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

  // Destruction
  ~G1PageBasedVirtualSpace();

  // Amount of reserved memory.
  size_t reserved_size() const;
  // Memory used in this virtual space.
  size_t committed_size() const;
  // Memory left to use/expand in this virtual space.
  size_t uncommitted_size() const;

  bool contains(const void* p) const;

  MemRegion reserved() {
    MemRegion x((HeapWord*)_low_boundary, reserved_size() / HeapWordSize);
    return x;
  }

  void release();

  void check_for_contiguity() PRODUCT_RETURN;

  // Debugging
  void print_on(outputStream* out) PRODUCT_RETURN;
  void print();
};

#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1PAGEBASEDVIRTUALSPACE_HPP