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 size_t Size) const;
62
63 /// Allocate at least \p Size with 8-byte alignment.
65
66 /// \returns the buffer that was allocated at \p create time, with size
67 /// \p UserHeaderSize.
69
70 LLVM_ABI_FOR_TEST size_t size() const;
71 LLVM_ABI_FOR_TEST size_t capacity() const;
72
74 create(const Twine &Path, const Twine &TableName, uint64_t MaxFileSize,
75 std::optional<uint64_t> NewFileInitialSize,
76 uint32_t UserHeaderSize = 0,
77 function_ref<void(void *)> UserHeaderInit = nullptr);
78
81
82 // No copy. Just call \a create() again.
85
87
88private:
89 struct ImplType;
90 explicit OnDiskDataAllocator(std::unique_ptr<ImplType> Impl);
91 std::unique_ptr<ImplType> Impl;
92};
93
94} // namespace llvm::cas
95
96#endif // LLVM_CAS_ONDISKDATAALLOCATOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
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:298
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
LLVM_ABI_FOR_TEST ~OnDiskDataAllocator()
MutableArrayRef< uint8_t > getUserHeader() const
LLVM_ABI_FOR_TEST Expected< OnDiskPtr > allocate(size_t Size)
Allocate at least Size with 8-byte alignment.
OnDiskDataAllocator & operator=(const OnDiskDataAllocator &)=delete
LLVM_ABI_FOR_TEST Expected< ArrayRef< char > > get(FileOffset Offset, size_t Size) const
Get the data of Size stored at the given Offset.
static LLVM_ABI_FOR_TEST 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)
LLVM_ABI_FOR_TEST size_t size() const
MutableArrayRef< char > ValueProxy
LLVM_ABI_FOR_TEST OnDiskDataAllocator & operator=(OnDiskDataAllocator &&RHS)
LLVM_ABI_FOR_TEST OnDiskDataAllocator(OnDiskDataAllocator &&RHS)
OnDiskDataAllocator(const OnDiskDataAllocator &)=delete
LLVM_ABI_FOR_TEST size_t capacity() const
An efficient, type-erasing, non-owning reference to a callable.
@ Offset
Definition DWP.cpp:532