LLVM 23.0.0git
MappedFileRegionArena.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This file declares interface for MappedFileRegionArena, a bump pointer
11/// allocator, backed by a memory-mapped file.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CAS_MAPPEDFILEREGIONARENA_H
16#define LLVM_CAS_MAPPEDFILEREGIONARENA_H
17
20#include <atomic>
21
22namespace llvm::cas {
23
24namespace ondisk {
25class OnDiskCASLogger;
26} // namespace ondisk
27
28/// Allocator for an owned mapped file region that supports thread-safe and
29/// process-safe bump pointer allocation.
30///
31/// This allocator is designed to create a sparse file when supported by the
32/// filesystem's \c ftruncate so that it can be used with a large maximum size.
33/// It will also attempt to shrink the underlying file down to its current
34/// allocation size when the last concurrent mapping is closed.
35///
36/// Process-safe. Uses file locks when resizing the file during initialization
37/// and destruction.
38///
39/// Thread-safe. Requires OS support thread-safe file lock.
40///
41/// Provides 8-byte alignment for all allocations.
43public:
45
46 /// Header for MappedFileRegionArena. 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 // BumpPtr for new allocation.
51 std::atomic<uint64_t> BumpPtr;
52 // Allocated size on disk.
53 std::atomic<uint64_t> AllocatedSize;
54 // Capacity of the file.
55 std::atomic<uint64_t> Capacity;
56 // Offset from the beginning of the file to this header (for verification).
57 std::atomic<uint64_t> HeaderOffset;
58 };
59
60 /// Create a \c MappedFileRegionArena.
61 ///
62 /// \param Path the path to open the mapped region.
63 /// \param Capacity the maximum size for the mapped file region.
64 /// \param HeaderOffset the offset at which to store the header. This is so
65 /// that information can be stored before the header, like a file magic.
66 /// \param NewFileConstructor is for constructing new files. It has exclusive
67 /// access to the file. Must call \c initializeBumpPtr.
69 create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset,
70 std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
71 function_ref<Error(MappedFileRegionArena &)> NewFileConstructor);
72
73 /// Minimum alignment for allocations, currently hardcoded to 8B.
74 static constexpr Align getAlign() {
75 // Trick Align into giving us '8' as a constexpr.
76 struct alignas(8) T {};
77 static_assert(alignof(T) == 8, "Tautology failed?");
78 return Align::Of<T>();
79 }
80
81 /// Allocate at least \p AllocSize. Rounds up to \a getAlign().
83 auto Offset = allocateOffset(AllocSize);
85 return Offset.takeError();
86 return data() + *Offset;
87 }
88 /// Allocate, returning the offset from \a data() instead of a pointer.
90
91 char *data() const { return Region.data(); }
92 uint64_t size() const { return H->BumpPtr; }
93 uint64_t capacity() const { return Region.size(); }
94
95 RegionT &getRegion() { return Region; }
96
97 ~MappedFileRegionArena() { destroyImpl(); }
98
102 destroyImpl();
103 moveImpl(RHS);
104 return *this;
105 }
106
109
110private:
111 // initialize header from offset.
112 void initializeHeader(uint64_t HeaderOffset);
113
114 LLVM_ABI_FOR_TEST void destroyImpl();
115 void moveImpl(MappedFileRegionArena &RHS) {
116 std::swap(Region, RHS.Region);
117 std::swap(H, RHS.H);
118 std::swap(Path, RHS.Path);
119 std::swap(FD, RHS.FD);
120 std::swap(SharedLockFD, RHS.SharedLockFD);
121 std::swap(Logger, RHS.Logger);
122 }
123
124private:
126 Header *H = nullptr;
127 std::string Path;
128 // File descriptor for the main storage file.
129 std::optional<int> FD;
130 // File descriptor for the file used as reader/writer lock.
131 std::optional<int> SharedLockFD;
132 std::shared_ptr<ondisk::OnDiskCASLogger> Logger = nullptr;
133};
134
135} // namespace llvm::cas
136
137#endif // LLVM_CAS_MAPPEDFILEREGIONARENA_H
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
#define H(x, y, z)
Definition MD5.cpp:56
#define T
Value * RHS
Tagged union holding either a T or a Error.
Definition Error.h:485
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI_FOR_TEST Expected< MappedFileRegionArena > create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset, std::shared_ptr< ondisk::OnDiskCASLogger > Logger, function_ref< Error(MappedFileRegionArena &)> NewFileConstructor)
Create a MappedFileRegionArena.
MappedFileRegionArena & operator=(MappedFileRegionArena &&RHS)
MappedFileRegionArena(const MappedFileRegionArena &)=delete
LLVM_ABI_FOR_TEST Expected< int64_t > allocateOffset(uint64_t AllocSize)
Allocate, returning the offset from data() instead of a pointer.
MappedFileRegionArena(MappedFileRegionArena &&RHS)
Expected< char * > allocate(uint64_t AllocSize)
Allocate at least AllocSize. Rounds up to getAlign().
static constexpr Align getAlign()
Minimum alignment for allocations, currently hardcoded to 8B.
sys::fs::mapped_file_region RegionT
MappedFileRegionArena & operator=(const MappedFileRegionArena &)=delete
Interface for logging low-level on-disk cas operations.
An efficient, type-erasing, non-owning reference to a callable.
This class represents a memory mapped file.
@ Offset
Definition DWP.cpp:532
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static constexpr Align Of()
Allow constructions of constexpr Align from types.
Definition Alignment.h:94
Header for MappedFileRegionArena.