1 /*
   2  * Copyright (c) 1997, 2010, 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_RUNTIME_VIRTUALSPACE_HPP
  26 #define SHARE_VM_RUNTIME_VIRTUALSPACE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 
  30 // ReservedSpace is a data structure for reserving a contiguous address range.
  31 
  32 class ReservedSpace VALUE_OBJ_CLASS_SPEC {
  33   friend class VMStructs;
  34  private:
  35   char*  _base;
  36   size_t _size;
  37   size_t _noaccess_prefix;
  38 
  39   // The base and size prior to any alignment done by this class; used only on
  40   // systems that cannot release part of a region.
  41   char*  _raw_base;
  42   size_t _raw_size;
  43 
  44   size_t _alignment;
  45   bool   _special;
  46   bool   _executable;
  47 
  48   // ReservedSpace
  49   ReservedSpace(char* base, size_t size, size_t alignment, bool special,
  50                 bool executable);
  51 
  52   bool failed_to_reserve_as_requested(char* base, char* requested_address,
  53                                       const size_t size, bool special);
  54   void initialize(size_t size, size_t alignment, bool large,
  55                   char* requested_address,
  56                   const size_t noaccess_prefix,
  57                   bool executable);
  58 
  59   inline void set_raw_base_and_size(char * const raw_base, size_t raw_size);
  60 
  61   // Release virtual address space.  If alignment was done, use the saved
  62   // address and size when releasing.
  63   void release_memory(char * default_addr, size_t default_size);
  64 
  65   // Release parts of an already-reserved memory region [addr, addr + len) to
  66   // get a new region that has "compound alignment."  Return the start of the
  67   // resulting region, or NULL on failure.
  68   //
  69   // The region is logically divided into a prefix and a suffix.  The prefix
  70   // starts at the result address, which is aligned to prefix_align.  The suffix
  71   // starts at result address + prefix_size, which is aligned to suffix_align.
  72   // The total size of the result region is size prefix_size + suffix_size.
  73   char* align_reserved_region(char* addr, const size_t len,
  74                               const size_t prefix_size,
  75                               const size_t prefix_align,
  76                               const size_t suffix_size,
  77                               const size_t suffix_align);
  78 
  79   // Reserve memory, call align_reserved_region() to alignment it and return the
  80   // result.
  81   char* reserve_and_align(const size_t reserve_size,
  82                           const size_t prefix_size,
  83                           const size_t prefix_align,
  84                           const size_t suffix_size,
  85                           const size_t suffix_align);
  86 
  87  protected:
  88   // Create protection page at the beginning of the space.
  89   void protect_noaccess_prefix(const size_t size);
  90 
  91  public:
  92   // Constructor
  93   ReservedSpace();
  94   ReservedSpace(size_t size);
  95   ReservedSpace(size_t size, size_t alignment, bool large,
  96                 char* requested_address = NULL,
  97                 const size_t noaccess_prefix = 0);
  98   ReservedSpace(const size_t prefix_size, const size_t prefix_align,
  99                 const size_t suffix_size, const size_t suffix_align,
 100                 char* requested_address,
 101                 const size_t noaccess_prefix = 0);
 102   ReservedSpace(size_t size, size_t alignment, bool large, bool executable);
 103 
 104   // Accessors
 105   char*  base()            const { return _base;      }
 106   size_t size()            const { return _size;      }
 107   size_t alignment()       const { return _alignment; }
 108   bool   special()         const { return _special;   }
 109   bool   executable()      const { return _executable;   }
 110   size_t noaccess_prefix() const { return _noaccess_prefix;   }
 111   bool is_reserved()       const { return _base != NULL; }
 112   void release();
 113 
 114   // Splitting
 115   ReservedSpace first_part(size_t partition_size, size_t alignment,
 116                            bool split = false, bool realloc = true);
 117   ReservedSpace last_part (size_t partition_size, size_t alignment);
 118 
 119   // These simply call the above using the default alignment.
 120   inline ReservedSpace first_part(size_t partition_size,
 121                                   bool split = false, bool realloc = true);
 122   inline ReservedSpace last_part (size_t partition_size);
 123 
 124   // Alignment
 125   static size_t page_align_size_up(size_t size);
 126   static size_t page_align_size_down(size_t size);
 127   static size_t allocation_align_size_up(size_t size);
 128   static size_t allocation_align_size_down(size_t size);
 129 };
 130 
 131 ReservedSpace
 132 ReservedSpace::first_part(size_t partition_size, bool split, bool realloc)
 133 {
 134   return first_part(partition_size, alignment(), split, realloc);
 135 }
 136 
 137 ReservedSpace ReservedSpace::last_part(size_t partition_size)
 138 {
 139   return last_part(partition_size, alignment());
 140 }
 141 
 142 // Class encapsulating behavior specific of memory space reserved for Java heap
 143 class ReservedHeapSpace : public ReservedSpace {
 144 public:
 145   // Constructor
 146   ReservedHeapSpace(size_t size, size_t forced_base_alignment,
 147                     bool large, char* requested_address);
 148   ReservedHeapSpace(const size_t prefix_size, const size_t prefix_align,
 149                     const size_t suffix_size, const size_t suffix_align,
 150                     char* requested_address);
 151 };
 152 
 153 // Class encapsulating behavior specific memory space for Code
 154 class ReservedCodeSpace : public ReservedSpace {
 155  public:
 156   // Constructor
 157   ReservedCodeSpace(size_t r_size, size_t rs_align, bool large);
 158 };
 159 
 160 // VirtualSpace is data structure for committing a previously reserved address range in smaller chunks.
 161 
 162 class VirtualSpace VALUE_OBJ_CLASS_SPEC {
 163   friend class VMStructs;
 164  private:
 165   // Reserved area
 166   char* _low_boundary;
 167   char* _high_boundary;
 168 
 169   // Committed area
 170   char* _low;
 171   char* _high;
 172 
 173   // The entire space has been committed and pinned in memory, no
 174   // os::commit_memory() or os::uncommit_memory().
 175   bool _special;
 176 
 177   // Need to know if commit should be executable.
 178   bool   _executable;
 179 
 180   // MPSS Support
 181   // Each virtualspace region has a lower, middle, and upper region.
 182   // Each region has an end boundary and a high pointer which is the
 183   // high water mark for the last allocated byte.
 184   // The lower and upper unaligned to LargePageSizeInBytes uses default page.
 185   // size.  The middle region uses large page size.
 186   char* _lower_high;
 187   char* _middle_high;
 188   char* _upper_high;
 189 
 190   char* _lower_high_boundary;
 191   char* _middle_high_boundary;
 192   char* _upper_high_boundary;
 193 
 194   size_t _lower_alignment;
 195   size_t _middle_alignment;
 196   size_t _upper_alignment;
 197 
 198   // MPSS Accessors
 199   char* lower_high() const { return _lower_high; }
 200   char* middle_high() const { return _middle_high; }
 201   char* upper_high() const { return _upper_high; }
 202 
 203   char* lower_high_boundary() const { return _lower_high_boundary; }
 204   char* middle_high_boundary() const { return _middle_high_boundary; }
 205   char* upper_high_boundary() const { return _upper_high_boundary; }
 206 
 207   size_t lower_alignment() const { return _lower_alignment; }
 208   size_t middle_alignment() const { return _middle_alignment; }
 209   size_t upper_alignment() const { return _upper_alignment; }
 210 
 211  public:
 212   // Committed area
 213   char* low()  const { return _low; }
 214   char* high() const { return _high; }
 215 
 216   // Reserved area
 217   char* low_boundary()  const { return _low_boundary; }
 218   char* high_boundary() const { return _high_boundary; }
 219 
 220   bool special() const { return _special; }
 221 
 222  public:
 223   // Initialization
 224   VirtualSpace();
 225   bool initialize(ReservedSpace rs, size_t committed_byte_size);
 226 
 227   // Destruction
 228   ~VirtualSpace();
 229 
 230   // Testers (all sizes are byte sizes)
 231   size_t committed_size()   const;
 232   size_t reserved_size()    const;
 233   size_t uncommitted_size() const;
 234   bool   contains(const void* p)  const;
 235 
 236   // Operations
 237   // returns true on success, false otherwise
 238   bool expand_by(size_t bytes, bool pre_touch = false);
 239   void shrink_by(size_t bytes);
 240   void release();
 241 
 242   void check_for_contiguity() PRODUCT_RETURN;
 243 
 244   // Debugging
 245   void print() PRODUCT_RETURN;
 246 };
 247 
 248 #endif // SHARE_VM_RUNTIME_VIRTUALSPACE_HPP