|
| 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 | + |
| 18 | +#include "llvm/Support/Alignment.h" |
| 19 | +#include "llvm/Support/FileSystem.h" |
| 20 | +#include <atomic> |
| 21 | + |
| 22 | +namespace 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. |
| 38 | +class MappedFileRegionArena { |
| 39 | +public: |
| 40 | + using RegionT = sys::fs::mapped_file_region; |
| 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. |
| 64 | + static Expected<MappedFileRegionArena> |
| 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(). |
| 77 | + Expected<char *> allocate(uint64_t AllocSize) { |
| 78 | + auto Offset = allocateOffset(AllocSize); |
| 79 | + if (LLVM_UNLIKELY(!Offset)) |
| 80 | + return Offset.takeError(); |
| 81 | + return data() + *Offset; |
| 82 | + } |
| 83 | + /// Allocate, returning the offset from \a data() instead of a pointer. |
| 84 | + Expected<int64_t> allocateOffset(uint64_t AllocSize); |
| 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 | + |
| 94 | + MappedFileRegionArena() = default; |
| 95 | + MappedFileRegionArena(MappedFileRegionArena &&RHS) { moveImpl(RHS); } |
| 96 | + MappedFileRegionArena &operator=(MappedFileRegionArena &&RHS) { |
| 97 | + destroyImpl(); |
| 98 | + moveImpl(RHS); |
| 99 | + return *this; |
| 100 | + } |
| 101 | + |
| 102 | + MappedFileRegionArena(const MappedFileRegionArena &) = delete; |
| 103 | + MappedFileRegionArena &operator=(const MappedFileRegionArena &) = delete; |
| 104 | + |
| 105 | +private: |
| 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 | + |
| 118 | +private: |
| 119 | + RegionT Region; |
| 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 |
0 commit comments