1 //===---- ObjectBuffer.h - Utility class to wrap object image memory -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares a wrapper class to hold the memory into which an
11 // object will be generated.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
16 #define LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
24 /// This class acts as a container for the memory buffer used during generation
25 /// and loading of executable objects using MCJIT and RuntimeDyld. The
26 /// underlying memory for the object will be owned by the ObjectBuffer instance
27 /// throughout its lifetime. The getMemBuffer() method provides a way to create
28 /// a MemoryBuffer wrapper object instance to be owned by other classes (such as
29 /// ObjectFile) as needed, but the MemoryBuffer instance returned does not own
30 /// the actual memory it points to.
32 virtual void anchor();
35 ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}
36 virtual ~ObjectBuffer() {}
38 /// Like MemoryBuffer::getMemBuffer() this function returns a pointer to an
39 /// object that is owned by the caller. However, the caller does not take
40 /// ownership of the underlying memory.
41 std::unique_ptr<MemoryBuffer> getMemBuffer() const {
42 return std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(
43 Buffer->getBuffer(), Buffer->getBufferIdentifier(), false));
46 const char *getBufferStart() const { return Buffer->getBufferStart(); }
47 size_t getBufferSize() const { return Buffer->getBufferSize(); }
48 StringRef getBuffer() const { return Buffer->getBuffer(); }
49 StringRef getBufferIdentifier() const {
50 return Buffer->getBufferIdentifier();
54 // The memory contained in an ObjectBuffer
55 std::unique_ptr<MemoryBuffer> Buffer;
58 /// This class encapsulates the SmallVector and raw_svector_ostream needed to
59 /// generate an object using MC code emission while providing a common
60 /// ObjectBuffer interface for access to the memory once the object has been
62 class ObjectBufferStream : public ObjectBuffer {
63 void anchor() override;
65 ObjectBufferStream() : OS(SV) {}
66 virtual ~ObjectBufferStream() {}
68 raw_ostream &getOStream() { return OS; }
73 // Make the data accessible via the ObjectBuffer::Buffer
74 Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
80 SmallVector<char, 4096> SV; // Working buffer into which we JIT.
81 raw_svector_ostream OS; // streaming wrapper