Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mapped_file_handle.ipp « windows « impl « detail « v2.0 « llfio « include - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20493a4547d3f88d1fa481963bd4d91adf92f81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* An mapped handle to a file
(C) 2017 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
File Created: Sept 2017


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Distributed under the Boost Software License, Version 1.0.
    (See accompanying file Licence.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt)
*/

#include "../../../mapped_file_handle.hpp"
#include "import.hpp"

LLFIO_V2_NAMESPACE_BEGIN

result<mapped_file_handle::size_type> mapped_file_handle::reserve(size_type reservation) noexcept
{
  LLFIO_LOG_FUNCTION_CALL(this);
  if(reservation == 0)
  {
    OUTCOME_TRY(length, underlying_file_maximum_extent());
    reservation = length;
  }
  reservation = utils::round_up_to_page_size(reservation);
  if(!_sh.is_valid())
  {
    // Section must match read/write of file, as otherwise map reservation doesn't work on Windows
    section_handle::flag sectionflags = section_handle::flag::singleton;
    if(this->is_readable())
      sectionflags |= section_handle::flag::read;
    if(this->is_writable())
      sectionflags |= section_handle::flag::write;
    OUTCOME_TRY(sh, section_handle::section(*this, 0, sectionflags));
    _sh = std::move(sh);
  }
  if(_mh.is_valid() && reservation == _mh.length())
  {
    return reservation;
  }
  section_handle::flag mapflags = section_handle::flag::read;
  // Reserve the full reservation in address space
  if(this->is_writable())
  {
    mapflags |= section_handle::flag::nocommit | section_handle::flag::write;
  }
  OUTCOME_TRYV(_mh.close());
  OUTCOME_TRY(mh, map_handle::map(_sh, reservation, 0, mapflags));
  _mh = std::move(mh);
  _reservation = reservation;
  return reservation;
}

result<void> mapped_file_handle::close() noexcept
{
  LLFIO_LOG_FUNCTION_CALL(this);
  if(_mh.is_valid())
  {
    OUTCOME_TRYV(_mh.close());
  }
  if(_sh.is_valid())
  {
    OUTCOME_TRYV(_sh.close());
  }
  return file_handle::close();
}
native_handle_type mapped_file_handle::release() noexcept
{
  LLFIO_LOG_FUNCTION_CALL(this);
  if(_mh.is_valid())
  {
    (void) _mh.close();
  }
  if(_sh.is_valid())
  {
    (void) _sh.close();
  }
  return file_handle::release();
}

result<mapped_file_handle::extent_type> mapped_file_handle::truncate(extent_type newsize) noexcept
{
  LLFIO_LOG_FUNCTION_CALL(this);
  // Release all maps and sections and truncate the backing file to zero
  if(newsize == 0)
  {
    OUTCOME_TRYV(_mh.close());
    OUTCOME_TRYV(_sh.close());
    return file_handle::truncate(newsize);
  }
  if(!_sh.is_valid())
  {
    OUTCOME_TRY(ret, file_handle::truncate(newsize));
    // Reserve now we have resized, it'll create a new section for the new size
    OUTCOME_TRYV(reserve(_reservation));
    return ret;
  }
  // Ask the section what size it is. If multiple LLFIO processes are using the same file,
  // because the section is a singleton based on the canonical path of the file, another
  // LLFIO in another process may have already resized the section for us in which case
  // we can skip doing work now.
  OUTCOME_TRY(size, _sh.length());
  if(size != newsize)
  {
    // If we are making this smaller, we must destroy the map and section first
    if(newsize < size)
    {
      OUTCOME_TRYV(_mh.close());
      OUTCOME_TRYV(_sh.close());
      // This will fail on Windows if any other processes are holding a section on this file
      OUTCOME_TRY(ret, file_handle::truncate(newsize));
      // Put the reservation and map back
      OUTCOME_TRYV(reserve(_reservation));
      return ret;
    }
    // Otherwise resize the file upwards, then the section.
    OUTCOME_TRYV(file_handle::truncate(newsize));
    // On Windows, resizing the section upwards maps the added extents into memory in all
    // processes using this singleton section
    OUTCOME_TRYV(_sh.truncate(newsize));
    // Have we exceeded the reservation? If so, reserve a new reservation which will recreate the map.
    if(newsize > _reservation)
    {
      OUTCOME_TRY(ret, reserve(newsize));
      return ret;
    }
    size = newsize;
  }
  // Adjust the map to reflect the new size of the section
  _mh._length = size;
  return newsize;
}

result<mapped_file_handle::extent_type> mapped_file_handle::update_map() noexcept
{
  OUTCOME_TRY(length, underlying_file_maximum_extent());
  if(length > _reservation)
  {
    // This API never exceeds the reservation
    length = _reservation;
  }
  if(length == 0)
  {
    OUTCOME_TRYV(_mh.close());
    OUTCOME_TRYV(_sh.close());
    return length;
  }
  if(!_sh.is_valid())
  {
    OUTCOME_TRYV(reserve(_reservation));
    return length;
  }
  OUTCOME_TRY(size, _sh.length());
  // Section may have become bigger than our reservation ...
  if(size >= length)
  {
    // Section is already the same size as the file, or is as big as it can go
    _mh._length = length;
    return length;
  }
  // Nobody appears to have extended the section to match the file yet
  OUTCOME_TRYV(_sh.truncate(length));
  // Adjust the map to reflect the new size of the section
  _mh._length = length;
  return length;
}

LLFIO_V2_NAMESPACE_END