1//===- SmallVectorMemoryBuffer.h --------------------------------*- C++ -*-===//
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// This file declares a wrapper class to hold the memory into which an
10// object will be generated.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_SMALLVECTORMEMORYBUFFER_H
15#define LLVM_SUPPORT_SMALLVECTORMEMORYBUFFER_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/raw_ostream.h"
20
21namespace llvm {
22
23/// SmallVector-backed MemoryBuffer instance.
24///
25/// This class enables efficient construction of MemoryBuffers from SmallVector
26/// instances. This is useful for MCJIT and Orc, where object files are streamed
27/// into SmallVectors, then inspected using ObjectFile (which takes a
28/// MemoryBuffer).
29class SmallVectorMemoryBuffer : public MemoryBuffer {
30public:
31 /// Construct a SmallVectorMemoryBuffer from the given SmallVector r-value.
32 SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV,
33 bool RequiresNullTerminator = true)
34 : SmallVectorMemoryBuffer(std::move(SV), "<in-memory object>",
35 RequiresNullTerminator) {}
36
37 /// Construct a named SmallVectorMemoryBuffer from the given SmallVector
38 /// r-value and StringRef.
39 SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name,
40 bool RequiresNullTerminator = true)
41 : SV(std::move(SV)), BufferName(std::string(Name)) {
42 if (RequiresNullTerminator) {
43 this->SV.push_back(Elt: '\0');
44 this->SV.pop_back();
45 }
46 init(BufStart: this->SV.begin(), BufEnd: this->SV.end(), RequiresNullTerminator: false);
47 }
48
49 // Key function.
50 ~SmallVectorMemoryBuffer() override;
51
52 StringRef getBufferIdentifier() const override { return BufferName; }
53
54 BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
55
56private:
57 SmallVector<char, 0> SV;
58 std::string BufferName;
59};
60
61} // namespace llvm
62
63#endif
64

source code of llvm/include/llvm/Support/SmallVectorMemoryBuffer.h