LLVM 23.0.0git
ContiguousBlobAccumulator.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 defines ContiguousBlobAccumulator, the size-limited output buffer
11/// shared by the yaml2obj emitters.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OBJECTYAML_CONTIGUOUSBLOBACCUMULATOR_H
16#define LLVM_OBJECTYAML_CONTIGUOUSBLOBACCUMULATOR_H
17
19#include "llvm/Support/Error.h"
20
21namespace llvm {
22namespace yaml {
23
24class BinaryRef;
25
26// This class is used to build up a contiguous binary blob while keeping
27// track of an offset in the output (which notionally begins at
28// `InitialOffset`).
29// The blob might be limited to an arbitrary size. All attempts to write data
30// are ignored and the error condition is remembered once the limit is reached.
31// Such an approach allows us to simplify the code by delaying error reporting
32// and doing it at a convenient time.
34 const uint64_t InitialOffset;
35 const uint64_t MaxSize;
36
39 Error ReachedLimitErr = Error::success();
40
41 LLVM_ABI bool checkLimit(uint64_t Size);
42
43public:
45 : InitialOffset(BaseOffset), MaxSize(SizeLimit), OS(Buf) {}
46
47 uint64_t tell() const { return OS.tell(); }
48 uint64_t getOffset() const { return InitialOffset + OS.tell(); }
49 void writeBlobToStream(raw_ostream &Out) const { Out << OS.str(); }
50
52 // Request to write 0 bytes to check we did not reach the limit.
53 checkLimit(0);
54 return std::move(ReachedLimitErr);
55 }
56
57 /// \returns The new offset.
59
61 if (checkLimit(Size))
62 return &OS;
63 return nullptr;
64 }
65
67
68 void writeZeros(uint64_t Num) {
69 if (checkLimit(Num))
70 OS.write_zeros(Num);
71 }
72
73 void write(const char *Ptr, size_t Size) {
74 if (checkLimit(Size))
75 OS.write(Ptr, Size);
76 }
77
78 void write(unsigned char C) {
79 if (checkLimit(1))
80 OS.write(C);
81 }
82
83 LLVM_ABI unsigned writeULEB128(uint64_t Val);
84
85 LLVM_ABI unsigned writeSLEB128(int64_t Val);
86
87 template <typename T> void write(T Val, llvm::endianness E) {
88 if (checkLimit(sizeof(T)))
90 }
91
92 LLVM_ABI void updateDataAt(uint64_t Pos, void *Data, size_t Size);
93};
94
95} // end namespace yaml
96} // end namespace llvm
97
98#endif // LLVM_OBJECTYAML_CONTIGUOUSBLOBACCUMULATOR_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
static cl::opt< unsigned > SizeLimit("eif-limit", cl::init(6), cl::Hidden, cl::desc("Size limit in Hexagon early if-conversion"))
#define T
ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
Specialized YAMLIO scalar type for representing a binary blob.
Definition YAML.h:64
LLVM_ABI uint64_t padToAlignment(unsigned Align)
LLVM_ABI void writeAsBinary(const BinaryRef &Bin, uint64_t N=UINT64_MAX)
void write(const char *Ptr, size_t Size)
ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
LLVM_ABI void updateDataAt(uint64_t Pos, void *Data, size_t Size)
#define UINT64_MAX
Definition DataTypes.h:77
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:96
This is an optimization pass for GlobalISel generic memory operations.
endianness
Definition bit.h:71
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39