-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[CAS] Add MappedFileRegionArena #114099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cachemeifyoucan
merged 22 commits into
main
from
users/cachemeifyoucan/spr/cas-add-mappedfileregionbumpptr
Sep 15, 2025
Merged
[CAS] Add MappedFileRegionArena #114099
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0ae1094
[𝘀𝗽𝗿] changes to main this commit is based on
cachemeifyoucan a223045
[𝘀𝗽𝗿] initial version
cachemeifyoucan ec88f7d
[𝘀𝗽𝗿] changes introduced through rebase
zGoldthorpe 68bcf8d
rebase to main and add newest impl
cachemeifyoucan 9b7be34
Fix windows build failure and clang-format
cachemeifyoucan 2ac7fe0
try fix windows again
cachemeifyoucan 2bea345
add windows implementation
cachemeifyoucan 3bbd0c3
rebase to main wiht new FileLockKind
cachemeifyoucan fd9ca0b
update comments
cachemeifyoucan a958d65
Update comments and tests, plus some integer types
cachemeifyoucan bf9c66f
Fix a corner case
cachemeifyoucan c155ce4
Some review feedback
cachemeifyoucan 13892e4
unused variable warnings
cachemeifyoucan 949db8b
clang format
cachemeifyoucan bb29900
address review feedback
cachemeifyoucan 2255ae9
Update to a new implementation that address some existing FIXME
cachemeifyoucan 191f64a
clang format
cachemeifyoucan 6d40138
fix windows test
cachemeifyoucan 3151a35
update some comments.
cachemeifyoucan d725101
Rename to MappedFileRegionArena and update implementation
cachemeifyoucan dd58a5d
update comments
cachemeifyoucan c82b306
simplified the test a bit
cachemeifyoucan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//===- MappedFileRegionBumpPtr.h --------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
/// \file | ||
/// This file declares interface for MappedFileRegionBumpPtr, a bump pointer | ||
/// allocator, backed by a memory-mapped file. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H | ||
#define LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H | ||
|
||
#include "llvm/Support/Alignment.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include <atomic> | ||
|
||
namespace llvm::cas { | ||
|
||
/// Allocator for an owned mapped file region that supports thread-safe and | ||
/// process-safe bump pointer allocation. | ||
/// | ||
/// This allocator is designed to create a sparse file when supported by the | ||
/// filesystem's \c ftruncate so that it can be used with a large maximum size. | ||
/// It will also attempt to shrink the underlying file down to its current | ||
/// allocation size when the last concurrent mapping is closed. | ||
/// | ||
/// Process-safe. Uses file locks when resizing the file during initialization | ||
/// and destruction. | ||
/// | ||
/// Thread-safe, assuming all threads use the same instance to talk to a given | ||
/// file/mapping. Unsafe to have multiple instances talking to the same file | ||
/// in the same process since file locks will misbehave. Clients should | ||
/// coordinate (somehow). | ||
/// | ||
/// Provides 8-byte alignment for all allocations. | ||
class MappedFileRegionBumpPtr { | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public: | ||
using RegionT = sys::fs::mapped_file_region; | ||
|
||
/// Header for MappedFileRegionBumpPtr. It can be configured to be located | ||
/// at any location within the file and the allocation will be appended after | ||
/// the header. | ||
struct Header { | ||
std::atomic<uint64_t> BumpPtr; | ||
std::atomic<uint64_t> AllocatedSize; | ||
}; | ||
|
||
/// Create a \c MappedFileRegionBumpPtr. | ||
/// | ||
/// \param Path the path to open the mapped region. | ||
/// \param Capacity the maximum size for the mapped file region. | ||
/// \param HeaderOffset the offset at which to store the header. This is so | ||
/// that information can be stored before the header, like a file magic. | ||
/// \param NewFileConstructor is for constructing new files. It has exclusive | ||
/// access to the file. Must call \c initializeBumpPtr. | ||
static Expected<MappedFileRegionBumpPtr> | ||
create(const Twine &Path, uint64_t Capacity, uint64_t HeaderOffset, | ||
function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor); | ||
|
||
/// Finish initializing the header. Must be called by \c NewFileConstructor. | ||
void initializeHeader(uint64_t HeaderOffset); | ||
|
||
/// Minimum alignment for allocations, currently hardcoded to 8B. | ||
static constexpr Align getAlign() { | ||
// Trick Align into giving us '8' as a constexpr. | ||
struct alignas(8) T {}; | ||
static_assert(alignof(T) == 8, "Tautology failed?"); | ||
return Align::Of<T>(); | ||
} | ||
|
||
/// Allocate at least \p AllocSize. Rounds up to \a getAlign(). | ||
Expected<char *> allocate(uint64_t AllocSize) { | ||
auto Offset = allocateOffset(AllocSize); | ||
if (LLVM_UNLIKELY(!Offset)) | ||
return Offset.takeError(); | ||
return data() + *Offset; | ||
} | ||
/// Allocate, returning the offset from \a data() instead of a pointer. | ||
Expected<int64_t> allocateOffset(uint64_t AllocSize); | ||
|
||
char *data() const { return Region.data(); } | ||
uint64_t size() const { return H->BumpPtr; } | ||
uint64_t capacity() const { return Region.size(); } | ||
|
||
RegionT &getRegion() { return Region; } | ||
|
||
~MappedFileRegionBumpPtr() { destroyImpl(); } | ||
|
||
MappedFileRegionBumpPtr() = default; | ||
MappedFileRegionBumpPtr(MappedFileRegionBumpPtr &&RHS) { moveImpl(RHS); } | ||
MappedFileRegionBumpPtr &operator=(MappedFileRegionBumpPtr &&RHS) { | ||
destroyImpl(); | ||
moveImpl(RHS); | ||
return *this; | ||
} | ||
|
||
MappedFileRegionBumpPtr(const MappedFileRegionBumpPtr &) = delete; | ||
MappedFileRegionBumpPtr &operator=(const MappedFileRegionBumpPtr &) = delete; | ||
|
||
private: | ||
void destroyImpl(); | ||
void moveImpl(MappedFileRegionBumpPtr &RHS) { | ||
std::swap(Region, RHS.Region); | ||
std::swap(H, RHS.H); | ||
std::swap(Path, RHS.Path); | ||
std::swap(FD, RHS.FD); | ||
std::swap(SharedLockFD, RHS.SharedLockFD); | ||
} | ||
|
||
private: | ||
RegionT Region; | ||
Header *H = nullptr; | ||
std::string Path; | ||
// File descriptor for the main storage file. | ||
std::optional<int> FD; | ||
// File descriptor for the file used as reader/writer lock. | ||
std::optional<int> SharedLockFD; | ||
}; | ||
|
||
} // namespace llvm::cas | ||
|
||
#endif // LLVM_CAS_MAPPEDFILEREGIONBUMPPTR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.