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/OwningPtr.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
25 /// ObjectBuffer - This class acts as a container for the memory buffer used during
26 /// generation and loading of executable objects using MCJIT and RuntimeDyld. The
27 /// underlying memory for the object will be owned by the ObjectBuffer instance
28 /// throughout its lifetime. The getMemBuffer() method provides a way to create a
29 /// MemoryBuffer wrapper object instance to be owned by other classes (such as
30 /// ObjectFile) as needed, but the MemoryBuffer instance returned does not own the
31 /// actual memory it points to.
35 ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}
36 virtual ~ObjectBuffer() {}
38 /// getMemBuffer - Like MemoryBuffer::getMemBuffer() this function
39 /// returns a pointer to an object that is owned by the caller. However,
40 /// the caller does not take ownership of the underlying memory.
41 MemoryBuffer *getMemBuffer() const {
42 return MemoryBuffer::getMemBuffer(Buffer->getBuffer(), "", false);
45 const char *getBufferStart() const { return Buffer->getBufferStart(); }
46 size_t getBufferSize() const { return Buffer->getBufferSize(); }
49 // The memory contained in an ObjectBuffer
50 OwningPtr<MemoryBuffer> Buffer;
53 /// ObjectBufferStream - This class encapsulates the SmallVector and
54 /// raw_svector_ostream needed to generate an object using MC code emission
55 /// while providing a common ObjectBuffer interface for access to the
56 /// memory once the object has been generated.
57 class ObjectBufferStream : public ObjectBuffer {
59 ObjectBufferStream() : OS(SV) {}
60 virtual ~ObjectBufferStream() {}
62 raw_ostream &getOStream() { return OS; }
67 // Make the data accessible via the ObjectBuffer::Buffer
68 Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
74 SmallVector<char, 4096> SV; // Working buffer into which we JIT.
75 raw_svector_ostream OS; // streaming wrapper