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.
29 virtual void anchor();
32 ObjectBuffer(std::unique_ptr<MemoryBuffer> Buf) : Buffer(std::move(Buf)) {}
33 virtual ~ObjectBuffer() {}
35 MemoryBufferRef getMemBuffer() const { return Buffer->getMemBufferRef(); }
37 const char *getBufferStart() const { return Buffer->getBufferStart(); }
38 size_t getBufferSize() const { return Buffer->getBufferSize(); }
39 StringRef getBuffer() const { return Buffer->getBuffer(); }
40 StringRef getBufferIdentifier() const {
41 return Buffer->getBufferIdentifier();
45 // The memory contained in an ObjectBuffer
46 std::unique_ptr<MemoryBuffer> Buffer;
49 /// This class encapsulates the SmallVector and raw_svector_ostream needed to
50 /// generate an object using MC code emission while providing a common
51 /// ObjectBuffer interface for access to the memory once the object has been
53 class ObjectBufferStream : public ObjectBuffer {
54 void anchor() override;
56 ObjectBufferStream() : OS(SV) {}
57 virtual ~ObjectBufferStream() {}
59 raw_ostream &getOStream() { return OS; }
64 // Make the data accessible via the ObjectBuffer::Buffer
66 MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()), "", false);
70 SmallVector<char, 4096> SV; // Working buffer into which we JIT.
71 raw_svector_ostream OS; // streaming wrapper