1 /*
   2  * Copyright (c) 2018, 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 #include "precompiled.hpp"
  26 #include "gc/parallel/psFileBackedVirtualspace.hpp"
  27 #include "memory/virtualspace.hpp"
  28 #include "runtime/os.hpp"
  29 
  30 PSFileBackedVirtualSpace::PSFileBackedVirtualSpace(ReservedSpace rs, size_t alignment, const char* path) : PSVirtualSpace(rs, alignment),
  31                                                    _file_path(path), _fd(-1), _mapping_succeeded(false) {
  32   assert(!rs.special(), "ReservedSpace passed to PSFileBackedVirtualSpace cannot be special");
  33 }
  34 
  35 bool PSFileBackedVirtualSpace::initialize() {
  36   _fd = os::create_file_for_heap(_file_path);
  37   if (_fd == -1) {
  38     return false;
  39   }
  40   // We map the reserved space to a file at initialization.
  41   char* ret = os::replace_existing_mapping_with_file_mapping(reserved_low_addr(), reserved_size(), _fd);
  42   if (ret != reserved_low_addr()) {
  43     return false;
  44   }
  45   // _mapping_succeeded is false if we return before this point.
  46   // expand calls later check value of this flag and return error if it is false.
  47   _mapping_succeeded = true;
  48   _special = true;
  49   return true;
  50 }
  51 
  52 PSFileBackedVirtualSpace::PSFileBackedVirtualSpace(ReservedSpace rs, const char* path) {
  53   PSFileBackedVirtualSpace(rs, os::vm_page_size(), path);
  54 }
  55 
  56 bool PSFileBackedVirtualSpace::expand_by(size_t bytes) {
  57   assert(special(), "Since entire space is committed at initialization, _special should always be true for PSFileBackedVirtualSpace");
  58 
  59   // if mapping did not succeed during intialization return false
  60   if (!_mapping_succeeded) {
  61     return false;
  62   }
  63   return PSVirtualSpace::expand_by(bytes);
  64 
  65 }
  66 
  67 bool PSFileBackedVirtualSpace::shrink_by(size_t bytes) {
  68   assert(special(), "Since entire space is committed at initialization, _special should always be true for PSFileBackedVirtualSpace");
  69   return PSVirtualSpace::shrink_by(bytes);
  70 }
  71 
  72 size_t PSFileBackedVirtualSpace::expand_into(PSVirtualSpace* space, size_t bytes) {
  73   // not supported. Since doing this will change page mapping which will lead to large TLB penalties.
  74   assert(false, "expand_into() should not be called for PSFileBackedVirtualSpace");
  75   return 0;
  76 }
  77 
  78 void PSFileBackedVirtualSpace::release() {
  79   os::close(_fd);
  80   _fd = -1;
  81   _file_path = NULL;
  82 
  83   PSVirtualSpace::release();
  84 }
  85