Introduce a string_ostream string builder facilty
[oota-llvm.git] / include / llvm / ExecutionEngine / ObjectBuffer.h
1 //===---- ObjectBuffer.h - Utility class to wrap object image memory -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares a wrapper class to hold the memory into which an
11 // object will be generated.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
16 #define LLVM_EXECUTIONENGINE_OBJECTBUFFER_H
17
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21
22 namespace llvm {
23
24 /// ObjectBuffer - This class acts as a container for the memory buffer used during
25 /// generation 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 a
28 /// MemoryBuffer wrapper object instance to be owned by other classes (such as
29 /// ObjectFile) as needed, but the MemoryBuffer instance returned does not own the
30 /// actual memory it points to.
31 class ObjectBuffer {
32   virtual void anchor();
33 public:
34   ObjectBuffer() {}
35   ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}
36   virtual ~ObjectBuffer() {}
37
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);
43   }
44
45   const char *getBufferStart() const { return Buffer->getBufferStart(); }
46   size_t getBufferSize() const { return Buffer->getBufferSize(); }
47   StringRef getBuffer() const { return Buffer->getBuffer(); }
48
49 protected:
50   // The memory contained in an ObjectBuffer
51   std::unique_ptr<MemoryBuffer> Buffer;
52 };
53
54 /// ObjectBufferStream - This class encapsulates the SmallVector and
55 /// raw_svector_ostream needed to generate an object using MC code emission
56 /// while providing a common ObjectBuffer interface for access to the
57 /// memory once the object has been generated.
58 class ObjectBufferStream : public ObjectBuffer {
59   void anchor() override;
60 public:
61   ObjectBufferStream() {}
62   virtual ~ObjectBufferStream() {}
63
64   raw_ostream &getOStream() { return OS; }
65   void flush()
66   {
67     // Make the data accessible via the ObjectBuffer::Buffer
68     Buffer.reset(MemoryBuffer::getMemBuffer(OS.str(), "", false));
69   }
70
71 protected:
72   small_string_ostream<4096> OS; // Working buffer into which we JIT.
73 };
74
75 } // namespace llvm
76
77 #endif