xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/SmallVectorMemoryBuffer.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/Compiler.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 namespace llvm {
23 
24 /// SmallVector-backed MemoryBuffer instance.
25 ///
26 /// This class enables efficient construction of MemoryBuffers from SmallVector
27 /// instances. This is useful for MCJIT and Orc, where object files are streamed
28 /// into SmallVectors, then inspected using ObjectFile (which takes a
29 /// MemoryBuffer).
30 class LLVM_ABI SmallVectorMemoryBuffer : public MemoryBuffer {
31 public:
32   /// Construct a SmallVectorMemoryBuffer from the given SmallVector r-value.
33   SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV,
34                           bool RequiresNullTerminator = true)
35       : SmallVectorMemoryBuffer(std::move(SV), "<in-memory object>",
36                                 RequiresNullTerminator) {}
37 
38   /// Construct a named SmallVectorMemoryBuffer from the given SmallVector
39   /// r-value and StringRef.
40   SmallVectorMemoryBuffer(SmallVectorImpl<char> &&SV, StringRef Name,
41                           bool RequiresNullTerminator = true)
SV(std::move (SV))42       : SV(std::move(SV)), BufferName(std::string(Name)) {
43     if (RequiresNullTerminator) {
44       this->SV.push_back('\0');
45       this->SV.pop_back();
46     }
47     init(this->SV.begin(), this->SV.end(), false);
48   }
49 
50   // Key function.
51   ~SmallVectorMemoryBuffer() override;
52 
getBufferIdentifier()53   StringRef getBufferIdentifier() const override { return BufferName; }
54 
getBufferKind()55   BufferKind getBufferKind() const override { return MemoryBuffer_Malloc; }
56 
57 private:
58   SmallVector<char, 0> SV;
59   std::string BufferName;
60 };
61 
62 } // namespace llvm
63 
64 #endif
65