LLVM 22.0.0git
DatabaseFile.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 the common interface for a DatabaseFile that is used to
11/// implement OnDiskCAS.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_CAS_DATABASEFILE_H
16#define LLVM_LIB_CAS_DATABASEFILE_H
17
18#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Error.h"
21
23
25
26/// Generic handle for a table.
27///
28/// Generic table header layout:
29/// - 2-bytes: TableKind
30/// - 2-bytes: TableNameSize
31/// - 4-bytes: TableNameRelOffset (relative to header)
33public:
34 enum class TableKind : uint16_t {
37 };
38 struct Header {
41 int32_t NameRelOffset; ///< Relative to Header.
42 };
43
44 explicit operator bool() const { return H; }
45 const Header &getHeader() const { return *H; }
46 MappedFileRegion &getRegion() const { return *Region; }
47
48 template <class T> static void check() {
49 static_assert(
50 std::is_same<decltype(T::Header::GenericHeader), Header>::value,
51 "T::GenericHeader should be of type TableHandle::Header");
52 static_assert(offsetof(typename T::Header, GenericHeader) == 0,
53 "T::GenericHeader must be the head of T::Header");
54 }
55 template <class T> bool is() const { return T::Kind == H->Kind; }
56 template <class T> T dyn_cast() const {
57 check<T>();
58 if (is<T>())
59 return T(*Region, *reinterpret_cast<typename T::Header *>(H));
60 return T();
61 }
62 template <class T> T cast() const {
63 assert(is<T>());
64 return dyn_cast<T>();
65 }
66
68 auto *Begin = reinterpret_cast<const char *>(H) + H->NameRelOffset;
69 return StringRef(Begin, H->NameSize);
70 }
71
72 TableHandle() = default;
73 TableHandle(MappedFileRegion &Region, Header &H) : Region(&Region), H(&H) {}
74 TableHandle(MappedFileRegion &Region, intptr_t HeaderOffset)
75 : TableHandle(Region,
76 *reinterpret_cast<Header *>(Region.data() + HeaderOffset)) {
77 }
78
79private:
80 MappedFileRegion *Region = nullptr;
81 Header *H = nullptr;
82};
83
84/// Encapsulate a database file, which:
85/// - Sets/checks magic.
86/// - Sets/checks version.
87/// - Points at an arbitrary root table.
88/// - Sets up a MappedFileRegionArena for allocation.
89///
90/// Top-level layout:
91/// - 4-bytes: Magic
92/// - 4-bytes: Version
93/// - 8-bytes: RootTableOffset (16-bits: Kind; 48-bits: Offset)
94/// - 8-bytes: BumpPtr from MappedFileRegionArena
95class DatabaseFile {
96public:
97 static constexpr uint32_t getMagic() { return 0xDA7ABA53UL; }
98 static constexpr uint32_t getVersion() { return 1UL; }
99 struct Header {
102 std::atomic<int64_t> RootTableOffset;
103 };
104
105 const Header &getHeader() { return *H; }
106 MappedFileRegionArena &getAlloc() { return Alloc; }
107 MappedFileRegion &getRegion() { return Alloc.getRegion(); }
108
109 /// Add a table. This is currently not thread safe and should be called inside
110 /// NewDBConstructor.
112
113 /// Find a table. May return null.
114 std::optional<TableHandle> findTable(StringRef Name);
115
116 /// Create the DatabaseFile at Path with Capacity.
118 create(const Twine &Path, uint64_t Capacity,
119 function_ref<Error(DatabaseFile &)> NewDBConstructor);
120
121 size_t size() const { return Alloc.size(); }
122
123private:
125 get(std::unique_ptr<MappedFileRegionArena> Alloc) {
126 if (Error E = validate(Alloc->getRegion()))
127 return std::move(E);
128 return DatabaseFile(std::move(Alloc));
129 }
130
131 static Error validate(MappedFileRegion &Region);
132
133 DatabaseFile(MappedFileRegionArena &Alloc)
134 : H(reinterpret_cast<Header *>(Alloc.data())), Alloc(Alloc) {}
135 DatabaseFile(std::unique_ptr<MappedFileRegionArena> Alloc)
136 : DatabaseFile(*Alloc) {
137 OwnedAlloc = std::move(Alloc);
138 }
139
140 Header *H = nullptr;
141 MappedFileRegionArena &Alloc;
142 std::unique_ptr<MappedFileRegionArena> OwnedAlloc;
143};
144
145Error createTableConfigError(std::errc ErrC, StringRef Path,
146 StringRef TableName, const Twine &Msg);
147
148Error checkTable(StringRef Label, size_t Expected, size_t Observed,
149 StringRef Path, StringRef TrieName);
150
151} // namespace llvm::cas::ondisk
152
153#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Prepare AGPR Alloc
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define offsetof(TYPE, MEMBER)
#define H(x, y, z)
Definition MD5.cpp:57
This file declares interface for MappedFileRegionArena, a bump pointer allocator, backed by a memory-...
#define T
static Split data
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Allocator for an owned mapped file region that supports thread-safe and process-safe bump pointer all...
sys::fs::mapped_file_region RegionT
Encapsulate a database file, which:
static Expected< DatabaseFile > create(const Twine &Path, uint64_t Capacity, function_ref< Error(DatabaseFile &)> NewDBConstructor)
Create the DatabaseFile at Path with Capacity.
std::optional< TableHandle > findTable(StringRef Name)
Find a table. May return null.
static constexpr uint32_t getVersion()
MappedFileRegionArena & getAlloc()
static constexpr uint32_t getMagic()
MappedFileRegion & getRegion()
Error addTable(TableHandle Table)
Add a table.
Generic handle for a table.
TableHandle(MappedFileRegion &Region, Header &H)
const Header & getHeader() const
MappedFileRegion & getRegion() const
TableHandle(MappedFileRegion &Region, intptr_t HeaderOffset)
An efficient, type-erasing, non-owning reference to a callable.
Error createTableConfigError(std::errc ErrC, StringRef Path, StringRef TableName, const Twine &Msg)
MappedFileRegionArena::RegionT MappedFileRegion
Error checkTable(StringRef Label, size_t Expected, size_t Observed, StringRef Path, StringRef TrieName)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
int32_t NameRelOffset
Relative to Header.