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(size_t size);
  94   ReservedSpace(size_t size, size_t alignment, bool large,
  95                 char* requested_address = NULL,
  96                 const size_t noaccess_prefix = 0);
  97   ReservedSpace(const size_t prefix_size, const size_t prefix_align,
  98                 const size_t suffix_size, const size_t suffix_align,
  99                 char* requested_address,
 100                 const size_t noaccess_prefix = 0);
 101   ReservedSpace(size_t size, size_t alignment, bool large, bool executable);
 102 
 103   // Accessors
 104   char*  base()            const { return _base;      }
 105   size_t size()            const { return _size;      }
 106   size_t alignment()       const { return _alignment; }
 107   bool   special()         const { return _special;   }
 108   bool   executable()      const { return _executable;   }
 109   size_t noaccess_prefix() const { return _noaccess_prefix;   }
 110   bool is_reserved()       const { return _base != NULL; }
 111   void release();
 112 
 113   // Splitting
 114   ReservedSpace first_part(size_t partition_size, size_t alignment,
 115                            bool split = false, bool realloc = true);
 116   ReservedSpace last_part (size_t partition_size, size_t alignment);
 117 
 118   // These simply call the above using the default alignment.
 119   inline ReservedSpace first_part(size_t partition_size,
 120                                   bool split = false, bool realloc = true);
 121   inline ReservedSpace last_part (size_t partition_size);
 122 
 123   // Alignment
 124   static size_t page_align_size_up(size_t size);
 125   static size_t page_align_size_down(size_t size);
 126   static size_t allocation_align_size_up(size_t size);
 127   static size_t allocation_align_size_down(size_t size);
 128 };
 129 
 130 ReservedSpace
 131 ReservedSpace::first_part(size_t partition_size, bool split, bool realloc)
 132 {
 133   return first_part(partition_size, alignment(), split, realloc);
 134 }
 135 
 136 ReservedSpace ReservedSpace::last_part(size_t partition_size)
 137 {
 138   return last_part(partition_size, alignment());
 139 }
 140 
 141 // Class encapsulating behavior specific of memory space reserved for Java heap
 142 class ReservedHeapSpace : public ReservedSpace {
 143 public:
 144   // Constructor
 145   ReservedHeapSpace(size_t size, size_t forced_base_alignment,
 146                     bool large, char* requested_address);
 147   ReservedHeapSpace(const size_t prefix_size, const size_t prefix_align,
 148                     const size_t suffix_size, const size_t suffix_align,
 149                     char* requested_address);
 150 };
 151 
 152 // Class encapsulating behavior specific memory space for Code
 153 class ReservedCodeSpace : public ReservedSpace {
 154  public:
 155   // Constructor
 156   ReservedCodeSpace(size_t r_size, size_t rs_align, bool large);
 157 };
 158 
 159 // VirtualSpace is data structure for committing a previously reserved address range in smaller chunks.
 160 
 161 class VirtualSpace VALUE_OBJ_CLASS_SPEC {
 162   friend class VMStructs;
 163  private:
 164   // Reserved area
 165   char* _low_boundary;
 166   char* _high_boundary;
 167 
 168   // Committed area
 169   char* _low;
 170   char* _high;
 171 
 172   // The entire space has been committed and pinned in memory, no
 173   // os::commit_memory() or os::uncommit_memory().
 174   bool _special;
 175 
 176   // Need to know if commit should be executable.
 177   bool   _executable;
 178 
 179   // MPSS Support
 180   // Each virtualspace region has a lower, middle, and upper region.
 181   // Each region has an end boundary and a high pointer which is the
 182   // high water mark for the last allocated byte.
 183   // The lower and upper unaligned to LargePageSizeInBytes uses default page.
 184   // size.  The middle region uses large page size.
 185   char* _lower_high;
 186   char* _middle_high;
 187   char* _upper_high;
 188 
 189   char* _lower_high_boundary;
 190   char* _middle_high_boundary;
 191   char* _upper_high_boundary;
 192 
 193   size_t _lower_alignment;
 194   size_t _middle_alignment;
 195   size_t _upper_alignment;
 196 
 197   // MPSS Accessors
 198   char* lower_high() const { return _lower_high; }
 199   char* middle_high() const { return _middle_high; }
 200   char* upper_high() const { return _upper_high; }
 201 
 202   char* lower_high_boundary() const { return _lower_high_boundary; }
 203   char* middle_high_boundary() const { return _middle_high_boundary; }
 204   char* upper_high_boundary() const { return _upper_high_boundary; }
 205 
 206   size_t lower_alignment() const { return _lower_alignment; }
 207   size_t middle_alignment() const { return _middle_alignment; }
 208   size_t upper_alignment() const { return _upper_alignment; }
 209 
 210  public:
 211   // Committed area
 212   char* low()  const { return _low; }
 213   char* high() const { return _high; }
 214 
 215   // Reserved area
 216   char* low_boundary()  const { return _low_boundary; }
 217   char* high_boundary() const { return _high_boundary; }
 218 
 219   bool special() const { return _special; }
 220 
 221  public:
 222   // Initialization
 223   VirtualSpace();
 224   bool initialize(ReservedSpace rs, size_t committed_byte_size);
 225 
 226   // Destruction
 227   ~VirtualSpace();
 228 
 229   // Testers (all sizes are byte sizes)
 230   size_t committed_size()   const;
 231   size_t reserved_size()    const;
 232   size_t uncommitted_size() const;
 233   bool   contains(const void* p)  const;
 234 
 235   // Operations
 236   // returns true on success, false otherwise
 237   bool expand_by(size_t bytes, bool pre_touch = false);
 238   void shrink_by(size_t bytes);
 239   void release();
 240 
 241   void check_for_contiguity() PRODUCT_RETURN;
 242 
 243   // Debugging
 244   void print() PRODUCT_RETURN;
 245 };
 246 
 247 #endif // SHARE_VM_RUNTIME_VIRTUALSPACE_HPP