1 /*
 2  * Copyright (c) 2018, 2019, 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.inline.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     os::close(_fd);
44     return false;
45   }
46   // _mapping_succeeded is false if we return before this point.
47   // expand calls later check value of this flag and return error if it is false.
48   _mapping_succeeded = true;
49   _special = true;
50   os::close(_fd);
51   return true;
52 }
53 
54 bool PSFileBackedVirtualSpace::expand_by(size_t bytes) {
55   assert(special(), "Since entire space is committed at initialization, _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(), "Since entire space is committed at initialization, _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 }
83