|
| 1 | +//===- MappedFileRegionBumpPtr.h --------------------------------*- C++ -*-===// |
| 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 | +#ifndef LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H |
| 10 | +#define LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H |
| 11 | + |
| 12 | +#include "llvm/Config/llvm-config.h" |
| 13 | +#include "llvm/Support/Alignment.h" |
| 14 | +#include "llvm/Support/FileSystem.h" |
| 15 | +#include <atomic> |
| 16 | + |
| 17 | +namespace llvm::cas { |
| 18 | + |
| 19 | +/// Allocator for an owned mapped file region that supports thread-safe and |
| 20 | +/// process-safe bump pointer allocation. |
| 21 | +/// |
| 22 | +/// This allocator is designed to create a sparse file when supported by the |
| 23 | +/// filesystem's \c ftruncate so that it can be used with a large maximum size. |
| 24 | +/// It will also attempt to shrink the underlying file down to its current |
| 25 | +/// allocation size when the last concurrent mapping is closed. |
| 26 | +/// |
| 27 | +/// Process-safe. Uses file locks when resizing the file during initialization |
| 28 | +/// and destruction. |
| 29 | +/// |
| 30 | +/// Thread-safe, assuming all threads use the same instance to talk to a given |
| 31 | +/// file/mapping. Unsafe to have multiple instances talking to the same file |
| 32 | +/// in the same process since file locks will misbehave. Clients should |
| 33 | +/// coordinate (somehow). |
| 34 | +/// |
| 35 | +/// \note Currently we allocate the whole file without sparseness on Windows. |
| 36 | +/// |
| 37 | +/// Provides 8-byte alignment for all allocations. |
| 38 | +class MappedFileRegionBumpPtr { |
| 39 | +public: |
| 40 | + using RegionT = sys::fs::mapped_file_region; |
| 41 | + |
| 42 | + /// Create a \c MappedFileRegionBumpPtr. |
| 43 | + /// |
| 44 | + /// \param Path the path to open the mapped region. |
| 45 | + /// \param Capacity the maximum size for the mapped file region. |
| 46 | + /// \param BumpPtrOffset the offset at which to store the bump pointer. |
| 47 | + /// \param NewFileConstructor is for constructing new files. It has exclusive |
| 48 | + /// access to the file. Must call \c initializeBumpPtr. |
| 49 | + static Expected<MappedFileRegionBumpPtr> |
| 50 | + create(const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset, |
| 51 | + function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor); |
| 52 | + |
| 53 | + /// Create a \c MappedFileRegionBumpPtr., shared across the process via a |
| 54 | + /// singleton map. |
| 55 | + /// |
| 56 | + /// FIXME: Singleton map should be based on sys::fs::UniqueID, but currently |
| 57 | + /// it is just based on \p Path. |
| 58 | + /// |
| 59 | + /// \param Path the path to open the mapped region. |
| 60 | + /// \param Capacity the maximum size for the mapped file region. |
| 61 | + /// \param BumpPtrOffset the offset at which to store the bump pointer. |
| 62 | + /// \param NewFileConstructor is for constructing new files. It has exclusive |
| 63 | + /// access to the file. Must call \c initializeBumpPtr. |
| 64 | + static Expected<std::shared_ptr<MappedFileRegionBumpPtr>> createShared( |
| 65 | + const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset, |
| 66 | + function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor); |
| 67 | + |
| 68 | + /// Finish initializing the bump pointer. Must be called by |
| 69 | + /// \c NewFileConstructor. |
| 70 | + void initializeBumpPtr(int64_t BumpPtrOffset); |
| 71 | + |
| 72 | + /// Minimum alignment for allocations, currently hardcoded to 8B. |
| 73 | + static constexpr Align getAlign() { |
| 74 | + // Trick Align into giving us '8' as a constexpr. |
| 75 | + struct alignas(8) T {}; |
| 76 | + static_assert(alignof(T) == 8, "Tautology failed?"); |
| 77 | + return Align::Of<T>(); |
| 78 | + } |
| 79 | + |
| 80 | + /// Allocate at least \p AllocSize. Rounds up to \a getAlign(). |
| 81 | + char *allocate(uint64_t AllocSize) { |
| 82 | + return data() + allocateOffset(AllocSize); |
| 83 | + } |
| 84 | + /// Allocate, returning the offset from \a data() instead of a pointer. |
| 85 | + int64_t allocateOffset(uint64_t AllocSize); |
| 86 | + |
| 87 | + char *data() const { return Region.data(); } |
| 88 | + uint64_t size() const { return *BumpPtr; } |
| 89 | + uint64_t capacity() const { return Region.size(); } |
| 90 | + |
| 91 | + RegionT &getRegion() { return Region; } |
| 92 | + |
| 93 | + ~MappedFileRegionBumpPtr() { destroyImpl(); } |
| 94 | + |
| 95 | + MappedFileRegionBumpPtr() = default; |
| 96 | + MappedFileRegionBumpPtr(MappedFileRegionBumpPtr &&RHS) { moveImpl(RHS); } |
| 97 | + MappedFileRegionBumpPtr &operator=(MappedFileRegionBumpPtr &&RHS) { |
| 98 | + destroyImpl(); |
| 99 | + moveImpl(RHS); |
| 100 | + return *this; |
| 101 | + } |
| 102 | + |
| 103 | + MappedFileRegionBumpPtr(const MappedFileRegionBumpPtr &) = delete; |
| 104 | + MappedFileRegionBumpPtr &operator=(const MappedFileRegionBumpPtr &) = delete; |
| 105 | + |
| 106 | +private: |
| 107 | + void destroyImpl(); |
| 108 | + void moveImpl(MappedFileRegionBumpPtr &RHS) { |
| 109 | + std::swap(Region, RHS.Region); |
| 110 | + std::swap(BumpPtr, RHS.BumpPtr); |
| 111 | + std::swap(Path, RHS.Path); |
| 112 | + std::swap(FD, RHS.FD); |
| 113 | + std::swap(SharedLockFD, RHS.SharedLockFD); |
| 114 | + } |
| 115 | + |
| 116 | +private: |
| 117 | + RegionT Region; |
| 118 | + std::atomic<int64_t> *BumpPtr = nullptr; |
| 119 | + std::string Path; |
| 120 | + std::optional<int> FD; |
| 121 | + std::optional<int> SharedLockFD; |
| 122 | +}; |
| 123 | + |
| 124 | +} // namespace llvm::cas |
| 125 | + |
| 126 | +#endif // LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H |
0 commit comments