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