5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
//===----------------------------------------------------------------------===//
8
+ //
9
+ /// \file
10
+ /// This file declares interface for MappedFileRegionBumpPtr, a bump pointer
11
+ /// allocator, backed by a memory-mapped file.
12
+ ///
13
+ //===----------------------------------------------------------------------===//
8
14
9
15
#ifndef LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H
10
16
#define LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H
11
17
12
- #include "llvm/Config/llvm-config.h"
13
18
#include "llvm/Support/Alignment.h"
14
19
#include "llvm/Support/FileSystem.h"
15
20
#include <atomic>
@@ -18,7 +23,7 @@ namespace llvm::cas {
18
23
19
24
namespace ondisk {
20
25
class OnDiskCASLogger;
21
- }
26
+ } // namespace ondisk
22
27
23
28
/// Allocator for an owned mapped file region that supports thread-safe and
24
29
/// process-safe bump pointer allocation.
@@ -31,33 +36,36 @@ class OnDiskCASLogger;
31
36
/// Process-safe. Uses file locks when resizing the file during initialization
32
37
/// and destruction.
33
38
///
34
- /// Thread-safe, assuming all threads use the same instance to talk to a given
35
- /// file/mapping. Unsafe to have multiple instances talking to the same file
36
- /// in the same process since file locks will misbehave. Clients should
37
- /// coordinate (somehow).
38
- ///
39
- /// \note Currently we allocate the whole file without sparseness on Windows.
39
+ /// Thread-safe. Requires OS support thread-safe file lock.
40
40
///
41
41
/// Provides 8-byte alignment for all allocations.
42
42
class MappedFileRegionBumpPtr {
43
43
public:
44
44
using RegionT = sys::fs::mapped_file_region;
45
45
46
+ /// Header for MappedFileRegionBumpPtr. It can be configured to be located
47
+ /// at any location within the file and the allocation will be appended after
48
+ /// the header.
49
+ struct Header {
50
+ std::atomic<uint64_t> BumpPtr;
51
+ std::atomic<uint64_t> AllocatedSize;
52
+ };
53
+
46
54
/// Create a \c MappedFileRegionBumpPtr.
47
55
///
48
56
/// \param Path the path to open the mapped region.
49
57
/// \param Capacity the maximum size for the mapped file region.
50
- /// \param BumpPtrOffset the offset at which to store the bump pointer.
58
+ /// \param HeaderOffset the offset at which to store the header. This is so
59
+ /// that information can be stored before the header, like a file magic.
51
60
/// \param NewFileConstructor is for constructing new files. It has exclusive
52
61
/// access to the file. Must call \c initializeBumpPtr.
53
62
static Expected<MappedFileRegionBumpPtr>
54
- create(const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset ,
63
+ create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset ,
55
64
std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
56
65
function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor);
57
66
58
- /// Finish initializing the bump pointer. Must be called by
59
- /// \c NewFileConstructor.
60
- void initializeBumpPtr(int64_t BumpPtrOffset);
67
+ /// Finish initializing the header. Must be called by \c NewFileConstructor.
68
+ void initializeHeader(uint64_t HeaderOffset);
61
69
62
70
/// Minimum alignment for allocations, currently hardcoded to 8B.
63
71
static constexpr Align getAlign() {
@@ -108,14 +116,12 @@ class MappedFileRegionBumpPtr {
108
116
}
109
117
110
118
private:
111
- struct Header {
112
- std::atomic<int64_t> BumpPtr;
113
- std::atomic<int64_t> AllocatedSize;
114
- };
115
119
RegionT Region;
116
120
Header *H = nullptr;
117
121
std::string Path;
122
+ // File descriptor for the main storage file.
118
123
std::optional<int> FD;
124
+ // File descriptor for the file used as reader/writer lock.
119
125
std::optional<int> SharedLockFD;
120
126
std::shared_ptr<ondisk::OnDiskCASLogger> Logger = nullptr;
121
127
};
0 commit comments