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