| 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 Implements OnDiskDataAllocator. |
| 10 | /// |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/CAS/OnDiskDataAllocator.h" |
| 14 | #include "DatabaseFile.h" |
| 15 | #include "llvm/CAS/OnDiskCASLogger.h" |
| 16 | #include "llvm/Config/llvm-config.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::cas; |
| 20 | using namespace llvm::cas::ondisk; |
| 21 | |
| 22 | #if LLVM_ENABLE_ONDISK_CAS |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // DataAllocator data structures. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
| 29 | /// DataAllocator table layout: |
| 30 | /// - [8-bytes: Generic table header] |
| 31 | /// - 8-bytes: AllocatorOffset (reserved for implementing free lists) |
| 32 | /// - 8-bytes: Size for user data header |
| 33 | /// - <user data buffer> |
| 34 | /// |
| 35 | /// Record layout: |
| 36 | /// - <data> |
| 37 | class DataAllocatorHandle { |
| 38 | public: |
| 39 | static constexpr TableHandle::TableKind Kind = |
| 40 | TableHandle::TableKind::DataAllocator; |
| 41 | |
| 42 | struct Header { |
| 43 | TableHandle::Header GenericHeader; |
| 44 | std::atomic<int64_t> AllocatorOffset; |
| 45 | const uint64_t UserHeaderSize; |
| 46 | }; |
| 47 | |
| 48 | operator TableHandle() const { |
| 49 | if (!H) |
| 50 | return TableHandle(); |
| 51 | return TableHandle(*Region, H->GenericHeader); |
| 52 | } |
| 53 | |
| 54 | Expected<MutableArrayRef<char>> allocate(MappedFileRegionArena &Alloc, |
| 55 | size_t DataSize) { |
| 56 | assert(&Alloc.getRegion() == Region); |
| 57 | auto Ptr = Alloc.allocate(AllocSize: DataSize); |
| 58 | if (LLVM_UNLIKELY(!Ptr)) |
| 59 | return Ptr.takeError(); |
| 60 | return MutableArrayRef(*Ptr, DataSize); |
| 61 | } |
| 62 | |
| 63 | explicit operator bool() const { return H; } |
| 64 | const Header &getHeader() const { return *H; } |
| 65 | MappedFileRegion &getRegion() const { return *Region; } |
| 66 | |
| 67 | MutableArrayRef<uint8_t> getUserHeader() { |
| 68 | return MutableArrayRef(reinterpret_cast<uint8_t *>(H + 1), |
| 69 | H->UserHeaderSize); |
| 70 | } |
| 71 | |
| 72 | static Expected<DataAllocatorHandle> |
| 73 | create(MappedFileRegionArena &Alloc, StringRef Name, uint32_t ); |
| 74 | |
| 75 | DataAllocatorHandle() = default; |
| 76 | DataAllocatorHandle(MappedFileRegion &Region, Header &H) |
| 77 | : Region(&Region), H(&H) {} |
| 78 | DataAllocatorHandle(MappedFileRegion &Region, intptr_t ) |
| 79 | : DataAllocatorHandle( |
| 80 | Region, *reinterpret_cast<Header *>(Region.data() + HeaderOffset)) { |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | MappedFileRegion *Region = nullptr; |
| 85 | Header *H = nullptr; |
| 86 | }; |
| 87 | |
| 88 | } // end anonymous namespace |
| 89 | |
| 90 | struct OnDiskDataAllocator::ImplType { |
| 91 | DatabaseFile File; |
| 92 | DataAllocatorHandle Store; |
| 93 | }; |
| 94 | |
| 95 | Expected<DataAllocatorHandle> |
| 96 | DataAllocatorHandle::create(MappedFileRegionArena &Alloc, StringRef Name, |
| 97 | uint32_t ) { |
| 98 | // Allocate. |
| 99 | auto Offset = |
| 100 | Alloc.allocateOffset(AllocSize: sizeof(Header) + UserHeaderSize + Name.size() + 1); |
| 101 | if (LLVM_UNLIKELY(!Offset)) |
| 102 | return Offset.takeError(); |
| 103 | |
| 104 | // Construct the header and the name. |
| 105 | assert(Name.size() <= UINT16_MAX && "Expected smaller table name" ); |
| 106 | auto *H = new (Alloc.getRegion().data() + *Offset) |
| 107 | Header{.GenericHeader: {.Kind: TableHandle::TableKind::DataAllocator, |
| 108 | .NameSize: static_cast<uint16_t>(Name.size()), |
| 109 | .NameRelOffset: static_cast<int32_t>(sizeof(Header) + UserHeaderSize)}, |
| 110 | /*AllocatorOffset=*/{0}, |
| 111 | /*UserHeaderSize=*/UserHeaderSize}; |
| 112 | // Memset UserHeader. |
| 113 | char * = reinterpret_cast<char *>(H + 1); |
| 114 | memset(s: UserHeader, c: 0, n: UserHeaderSize); |
| 115 | // Write database file name (null-terminated). |
| 116 | char *NameStorage = UserHeader + UserHeaderSize; |
| 117 | llvm::copy(Range&: Name, Out: NameStorage); |
| 118 | NameStorage[Name.size()] = 0; |
| 119 | return DataAllocatorHandle(Alloc.getRegion(), *H); |
| 120 | } |
| 121 | |
| 122 | Expected<OnDiskDataAllocator> OnDiskDataAllocator::create( |
| 123 | const Twine &PathTwine, const Twine &TableNameTwine, uint64_t MaxFileSize, |
| 124 | std::optional<uint64_t> NewFileInitialSize, uint32_t , |
| 125 | std::shared_ptr<ondisk::OnDiskCASLogger> Logger, |
| 126 | function_ref<void(void *)> ) { |
| 127 | assert(!UserHeaderSize || UserHeaderInit); |
| 128 | SmallString<128> PathStorage; |
| 129 | StringRef Path = PathTwine.toStringRef(Out&: PathStorage); |
| 130 | SmallString<128> TableNameStorage; |
| 131 | StringRef TableName = TableNameTwine.toStringRef(Out&: TableNameStorage); |
| 132 | |
| 133 | // Constructor for if the file doesn't exist. |
| 134 | auto NewDBConstructor = [&](DatabaseFile &DB) -> Error { |
| 135 | auto Store = |
| 136 | DataAllocatorHandle::create(Alloc&: DB.getAlloc(), Name: TableName, UserHeaderSize); |
| 137 | if (LLVM_UNLIKELY(!Store)) |
| 138 | return Store.takeError(); |
| 139 | |
| 140 | if (auto E = DB.addTable(Table: *Store)) |
| 141 | return E; |
| 142 | |
| 143 | if (UserHeaderSize) |
| 144 | UserHeaderInit(Store->getUserHeader().data()); |
| 145 | return Error::success(); |
| 146 | }; |
| 147 | |
| 148 | // Get or create the file. |
| 149 | Expected<DatabaseFile> File = |
| 150 | DatabaseFile::create(Path, Capacity: MaxFileSize, Logger, NewDBConstructor); |
| 151 | if (!File) |
| 152 | return File.takeError(); |
| 153 | |
| 154 | // Find the table and validate it. |
| 155 | std::optional<TableHandle> Table = File->findTable(Name: TableName); |
| 156 | if (!Table) |
| 157 | return createTableConfigError(ErrC: std::errc::argument_out_of_domain, Path, |
| 158 | TableName, Msg: "table not found" ); |
| 159 | if (Error E = checkTable(Label: "table kind" , Expected: (size_t)DataAllocatorHandle::Kind, |
| 160 | Observed: (size_t)Table->getHeader().Kind, Path, TrieName: TableName)) |
| 161 | return std::move(E); |
| 162 | auto Store = Table->cast<DataAllocatorHandle>(); |
| 163 | assert(Store && "Already checked the kind" ); |
| 164 | |
| 165 | // Success. |
| 166 | OnDiskDataAllocator::ImplType Impl{.File: DatabaseFile(std::move(*File)), .Store: Store}; |
| 167 | return OnDiskDataAllocator(std::make_unique<ImplType>(args: std::move(Impl))); |
| 168 | } |
| 169 | |
| 170 | Expected<OnDiskDataAllocator::OnDiskPtr> |
| 171 | OnDiskDataAllocator::allocate(size_t Size) { |
| 172 | auto Data = Impl->Store.allocate(Alloc&: Impl->File.getAlloc(), DataSize: Size); |
| 173 | if (LLVM_UNLIKELY(!Data)) |
| 174 | return Data.takeError(); |
| 175 | |
| 176 | return OnDiskPtr(FileOffset(Data->data() - Impl->Store.getRegion().data()), |
| 177 | *Data); |
| 178 | } |
| 179 | |
| 180 | Expected<ArrayRef<char>> OnDiskDataAllocator::get(FileOffset Offset, |
| 181 | size_t Size) const { |
| 182 | assert(Offset); |
| 183 | assert(Impl); |
| 184 | if (Offset.get() + Size >= Impl->File.getAlloc().size()) |
| 185 | return createStringError(EC: make_error_code(e: std::errc::protocol_error), |
| 186 | S: "requested size too large in allocator" ); |
| 187 | return ArrayRef<char>{Impl->File.getRegion().data() + Offset.get(), Size}; |
| 188 | } |
| 189 | |
| 190 | MutableArrayRef<uint8_t> OnDiskDataAllocator::() const { |
| 191 | return Impl->Store.getUserHeader(); |
| 192 | } |
| 193 | |
| 194 | size_t OnDiskDataAllocator::size() const { return Impl->File.size(); } |
| 195 | size_t OnDiskDataAllocator::capacity() const { |
| 196 | return Impl->File.getRegion().size(); |
| 197 | } |
| 198 | |
| 199 | OnDiskDataAllocator::OnDiskDataAllocator(std::unique_ptr<ImplType> Impl) |
| 200 | : Impl(std::move(Impl)) {} |
| 201 | |
| 202 | #else // !LLVM_ENABLE_ONDISK_CAS |
| 203 | |
| 204 | struct OnDiskDataAllocator::ImplType {}; |
| 205 | |
| 206 | Expected<OnDiskDataAllocator> OnDiskDataAllocator::create( |
| 207 | const Twine &Path, const Twine &TableName, uint64_t MaxFileSize, |
| 208 | std::optional<uint64_t> NewFileInitialSize, uint32_t UserHeaderSize, |
| 209 | std::shared_ptr<ondisk::OnDiskCASLogger> Logger, |
| 210 | function_ref<void(void *)> UserHeaderInit) { |
| 211 | return createStringError(make_error_code(std::errc::not_supported), |
| 212 | "OnDiskDataAllocator is not supported" ); |
| 213 | } |
| 214 | |
| 215 | Expected<OnDiskDataAllocator::OnDiskPtr> |
| 216 | OnDiskDataAllocator::allocate(size_t Size) { |
| 217 | return createStringError(make_error_code(std::errc::not_supported), |
| 218 | "OnDiskDataAllocator is not supported" ); |
| 219 | } |
| 220 | |
| 221 | Expected<ArrayRef<char>> OnDiskDataAllocator::get(FileOffset Offset, |
| 222 | size_t Size) const { |
| 223 | return createStringError(make_error_code(std::errc::not_supported), |
| 224 | "OnDiskDataAllocator is not supported" ); |
| 225 | } |
| 226 | |
| 227 | MutableArrayRef<uint8_t> OnDiskDataAllocator::getUserHeader() const { |
| 228 | return {}; |
| 229 | } |
| 230 | |
| 231 | size_t OnDiskDataAllocator::size() const { return 0; } |
| 232 | size_t OnDiskDataAllocator::capacity() const { return 0; } |
| 233 | |
| 234 | #endif // LLVM_ENABLE_ONDISK_CAS |
| 235 | |
| 236 | OnDiskDataAllocator::OnDiskDataAllocator(OnDiskDataAllocator &&RHS) = default; |
| 237 | OnDiskDataAllocator & |
| 238 | OnDiskDataAllocator::operator=(OnDiskDataAllocator &&RHS) = default; |
| 239 | OnDiskDataAllocator::~OnDiskDataAllocator() = default; |
| 240 | |