LLVM 22.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
24/// Allocator for an owned mapped file region that supports thread-safe and
25/// process-safe bump pointer allocation.
26///
27/// This allocator is designed to create a sparse file when supported by the
28/// filesystem's \c ftruncate so that it can be used with a large maximum size.
29/// It will also attempt to shrink the underlying file down to its current
30/// allocation size when the last concurrent mapping is closed.
31///
32/// Process-safe. Uses file locks when resizing the file during initialization
33/// and destruction.
34///
35/// Thread-safe. Requires OS support thread-safe file lock.
36///
37/// Provides 8-byte alignment for all allocations.
39public:
41
42 /// Header for MappedFileRegionArena. It can be configured to be located
43 /// at any location within the file and the allocation will be appended after
44 /// the header.
45 struct Header {
46 // BumpPtr for new allocation.
47 std::atomic<uint64_t> BumpPtr;
48 // Allocated size on disk.
49 std::atomic<uint64_t> AllocatedSize;
50 // Capacity of the file.
51 std::atomic<uint64_t> Capacity;
52 // Offset from the beginning of the file to this header (for verification).
53 std::atomic<uint64_t> HeaderOffset;
54 };
55
56 /// Create a \c MappedFileRegionArena.
57 ///
58 /// \param Path the path to open the mapped region.
59 /// \param Capacity the maximum size for the mapped file region.
60 /// \param HeaderOffset the offset at which to store the header. This is so
61 /// that information can be stored before the header, like a file magic.
62 /// \param NewFileConstructor is for constructing new files. It has exclusive
63 /// access to the file. Must call \c initializeBumpPtr.
65 create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset,
66 function_ref<Error(MappedFileRegionArena &)> NewFileConstructor);
67
68 /// Minimum alignment for allocations, currently hardcoded to 8B.
69 static constexpr Align getAlign() {
70 // Trick Align into giving us '8' as a constexpr.
71 struct alignas(8) T {};
72 static_assert(alignof(T) == 8, "Tautology failed?");
73 return Align::Of<T>();
74 }
75
76 /// Allocate at least \p AllocSize. Rounds up to \a getAlign().
78 auto Offset = allocateOffset(AllocSize);
80 return Offset.takeError();
81 return data() + *Offset;
82 }
83 /// Allocate, returning the offset from \a data() instead of a pointer.
85
86 char *data() const { return Region.data(); }
87 uint64_t size() const { return H->BumpPtr; }
88 uint64_t capacity() const { return Region.size(); }
89
90 RegionT &getRegion() { return Region; }
91
92 ~MappedFileRegionArena() { destroyImpl(); }
93
97 destroyImpl();
98 moveImpl(RHS);
99 return *this;
100 }
101
104
105private:
106 // initialize header from offset.
107 void initializeHeader(uint64_t HeaderOffset);
108
109 void destroyImpl();
110 void moveImpl(MappedFileRegionArena &RHS) {
111 std::swap(Region, RHS.Region);
112 std::swap(H, RHS.H);
113 std::swap(Path, RHS.Path);
114 std::swap(FD, RHS.FD);
115 std::swap(SharedLockFD, RHS.SharedLockFD);
116 }
117
118private:
120 Header *H = nullptr;
121 std::string Path;
122 // File descriptor for the main storage file.
123 std::optional<int> FD;
124 // File descriptor for the file used as reader/writer lock.
125 std::optional<int> SharedLockFD;
126};
127
128} // namespace llvm::cas
129
130#endif // LLVM_CAS_MAPPEDFILEREGIONARENA_H
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define H(x, y, z)
Definition MD5.cpp:57
#define T
Value * RHS
Tagged union holding either a T or a Error.
Definition Error.h:485
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
MappedFileRegionArena & operator=(MappedFileRegionArena &&RHS)
MappedFileRegionArena(const MappedFileRegionArena &)=delete
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 Expected< MappedFileRegionArena > create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset, function_ref< Error(MappedFileRegionArena &)> NewFileConstructor)
Create a MappedFileRegionArena.
static constexpr Align getAlign()
Minimum alignment for allocations, currently hardcoded to 8B.
sys::fs::mapped_file_region RegionT
MappedFileRegionArena & operator=(const MappedFileRegionArena &)=delete
An efficient, type-erasing, non-owning reference to a callable.
This class represents a memory mapped file.
@ Offset
Definition DWP.cpp:477
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
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:102
Header for MappedFileRegionArena.