LLVM 22.0.0git
OnDiskDataAllocator.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 interface for OnDiskDataAllocator, a file backed data
11/// pool can be used to allocate space to store data packed in a single file. It
12/// is based on MappedFileRegionArena and includes a header in the beginning to
13/// provide metadata.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CAS_ONDISKDATAALLOCATOR_H
18#define LLVM_CAS_ONDISKDATAALLOCATOR_H
19
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/CAS/FileOffset.h"
22#include "llvm/Support/Error.h"
23
24namespace llvm::cas {
25
26/// Sink for data. Stores variable length data with 8-byte alignment. Does not
27/// track size of data, which is assumed to known from context, or embedded.
28/// Uses 0-padding but does not guarantee 0-termination.
30public:
32
33 /// A pointer to data stored on disk.
34 class OnDiskPtr {
35 public:
36 FileOffset getOffset() const { return Offset; }
37 explicit operator bool() const { return bool(getOffset()); }
38 const ValueProxy &operator*() const {
39 assert(Offset && "Null dereference");
40 return Value;
41 }
42 const ValueProxy *operator->() const {
43 assert(Offset && "Null dereference");
44 return &Value;
45 }
46
47 OnDiskPtr() = default;
48
49 private:
50 friend class OnDiskDataAllocator;
51 OnDiskPtr(FileOffset Offset, ValueProxy Value)
52 : Offset(Offset), Value(Value) {}
55 };
56
57 /// Get the data of \p Size stored at the given \p Offset. Note the allocator
58 /// doesn't keep track of the allocation size, thus \p Size doesn't need to
59 /// match the size of allocation but needs to be smaller.
61
62 /// Allocate at least \p Size with 8-byte alignment.
64
65 /// \returns the buffer that was allocated at \p create time, with size
66 /// \p UserHeaderSize.
68
69 size_t size() const;
70 size_t capacity() const;
71
73 create(const Twine &Path, const Twine &TableName, uint64_t MaxFileSize,
74 std::optional<uint64_t> NewFileInitialSize,
75 uint32_t UserHeaderSize = 0,
76 function_ref<void(void *)> UserHeaderInit = nullptr);
77
80
81 // No copy. Just call \a create() again.
84
86
87private:
88 struct ImplType;
89 explicit OnDiskDataAllocator(std::unique_ptr<ImplType> Impl);
90 std::unique_ptr<ImplType> Impl;
91};
92
93} // namespace llvm::cas
94
95#endif // LLVM_CAS_ONDISKDATAALLOCATOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares interface for FileOffset that represent stored data at an offset from the beginnin...
Value * RHS
Tagged union holding either a T or a Error.
Definition Error.h:485
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
FileOffset is a wrapper around uint64_t to represent the offset of data from the beginning of the fil...
Definition FileOffset.h:24
Expected< OnDiskPtr > allocate(size_t Size)
Allocate at least Size with 8-byte alignment.
OnDiskDataAllocator & operator=(const OnDiskDataAllocator &)=delete
Expected< ArrayRef< char > > get(FileOffset Offset, size_t Size) const
Get the data of Size stored at the given Offset.
static Expected< OnDiskDataAllocator > create(const Twine &Path, const Twine &TableName, uint64_t MaxFileSize, std::optional< uint64_t > NewFileInitialSize, uint32_t UserHeaderSize=0, function_ref< void(void *)> UserHeaderInit=nullptr)
MutableArrayRef< char > ValueProxy
OnDiskDataAllocator & operator=(OnDiskDataAllocator &&RHS)
OnDiskDataAllocator(OnDiskDataAllocator &&RHS)
OnDiskDataAllocator(const OnDiskDataAllocator &)=delete
MutableArrayRef< uint8_t > getUserHeader()
An efficient, type-erasing, non-owning reference to a callable.
@ Offset
Definition DWP.cpp:477