LLVM API Documentation

ObjectBuffer.h
Go to the documentation of this file.
00001 //===---- ObjectBuffer.h - Utility class to wrap object image memory -----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares a wrapper class to hold the memory into which an
00011 // object will be generated.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
00016 #define LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Support/MemoryBuffer.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 
00022 namespace llvm {
00023 
00024 /// This class acts as a container for the memory buffer used during generation
00025 /// and loading of executable objects using MCJIT and RuntimeDyld. The
00026 /// underlying memory for the object will be owned by the ObjectBuffer instance
00027 /// throughout its lifetime.
00028 class ObjectBuffer {
00029   virtual void anchor();
00030 public:
00031   ObjectBuffer() {}
00032   ObjectBuffer(std::unique_ptr<MemoryBuffer> Buf) : Buffer(std::move(Buf)) {}
00033   virtual ~ObjectBuffer() {}
00034 
00035   MemoryBufferRef getMemBuffer() const { return Buffer->getMemBufferRef(); }
00036 
00037   const char *getBufferStart() const { return Buffer->getBufferStart(); }
00038   size_t getBufferSize() const { return Buffer->getBufferSize(); }
00039   StringRef getBuffer() const { return Buffer->getBuffer(); }
00040   StringRef getBufferIdentifier() const {
00041     return Buffer->getBufferIdentifier();
00042   }
00043 
00044 protected:
00045   // The memory contained in an ObjectBuffer
00046   std::unique_ptr<MemoryBuffer> Buffer;
00047 };
00048 
00049 /// This class encapsulates the SmallVector and raw_svector_ostream needed to
00050 /// generate an object using MC code emission while providing a common
00051 /// ObjectBuffer interface for access to the memory once the object has been
00052 /// generated.
00053 class ObjectBufferStream : public ObjectBuffer {
00054   void anchor() override;
00055 public:
00056   ObjectBufferStream() : OS(SV) {}
00057   virtual ~ObjectBufferStream() {}
00058 
00059   raw_ostream &getOStream() { return OS; }
00060   void flush()
00061   {
00062     OS.flush();
00063 
00064     // Make the data accessible via the ObjectBuffer::Buffer
00065     Buffer =
00066         MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()), "", false);
00067   }
00068 
00069 protected:
00070   SmallVector<char, 4096> SV; // Working buffer into which we JIT.
00071   raw_svector_ostream     OS; // streaming wrapper
00072 };
00073 
00074 } // namespace llvm
00075 
00076 #endif