]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/ExecutionEngine/ObjectBuffer.h
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / include / llvm / ExecutionEngine / ObjectBuffer.h
1 //===---- ObjectBuffer.h - Utility class to wrap object image memory -----===//\r
2 //\r
3 //                     The LLVM Compiler Infrastructure\r
4 //\r
5 // This file is distributed under the University of Illinois Open Source\r
6 // License. See LICENSE.TXT for details.\r
7 //\r
8 //===----------------------------------------------------------------------===//\r
9 //\r
10 // This file declares a wrapper class to hold the memory into which an\r
11 // object will be generated.\r
12 //\r
13 //===----------------------------------------------------------------------===//\r
14 \r
15 #ifndef LLVM_EXECUTIONENGINE_OBJECTBUFFER_H\r
16 #define LLVM_EXECUTIONENGINE_OBJECTBUFFER_H\r
17 \r
18 #include "llvm/ADT/SmallVector.h"\r
19 #include "llvm/ADT/OwningPtr.h"\r
20 #include "llvm/Support/raw_ostream.h"\r
21 #include "llvm/Support/MemoryBuffer.h"\r
22 \r
23 namespace llvm {\r
24 \r
25 /// ObjectBuffer - This class acts as a container for the memory buffer used during\r
26 /// generation and loading of executable objects using MCJIT and RuntimeDyld.  The\r
27 /// underlying memory for the object will be owned by the ObjectBuffer instance\r
28 /// throughout its lifetime.  The getMemBuffer() method provides a way to create a\r
29 /// MemoryBuffer wrapper object instance to be owned by other classes (such as\r
30 /// ObjectFile) as needed, but the MemoryBuffer instance returned does not own the\r
31 /// actual memory it points to.\r
32 class ObjectBuffer {\r
33 public:\r
34   ObjectBuffer() {}\r
35   ObjectBuffer(MemoryBuffer* Buf) : Buffer(Buf) {}\r
36   virtual ~ObjectBuffer() {}\r
37 \r
38   /// getMemBuffer - Like MemoryBuffer::getMemBuffer() this function\r
39   /// returns a pointer to an object that is owned by the caller. However,\r
40   /// the caller does not take ownership of the underlying memory.\r
41   MemoryBuffer *getMemBuffer() const {\r
42     return MemoryBuffer::getMemBuffer(Buffer->getBuffer(), "", false);\r
43   }\r
44 \r
45   const char *getBufferStart() const { return Buffer->getBufferStart(); }\r
46   size_t getBufferSize() const { return Buffer->getBufferSize(); }\r
47 \r
48 protected:\r
49   // The memory contained in an ObjectBuffer\r
50   OwningPtr<MemoryBuffer> Buffer;\r
51 };\r
52 \r
53 /// ObjectBufferStream - This class encapsulates the SmallVector and\r
54 /// raw_svector_ostream needed to generate an object using MC code emission\r
55 /// while providing a common ObjectBuffer interface for access to the\r
56 /// memory once the object has been generated.\r
57 class ObjectBufferStream : public ObjectBuffer {\r
58 public:\r
59   ObjectBufferStream() : OS(SV) {}\r
60   virtual ~ObjectBufferStream() {}\r
61 \r
62   raw_ostream &getOStream() { return OS; }\r
63   void flush()\r
64   {\r
65     OS.flush();\r
66 \r
67     // Make the data accessible via the ObjectBuffer::Buffer\r
68     Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),\r
69                                             "",\r
70                                             false));\r
71   }\r
72 \r
73 protected:\r
74   SmallVector<char, 4096> SV; // Working buffer into which we JIT.\r
75   raw_svector_ostream     OS; // streaming wrapper\r
76 };\r
77 \r
78 } // namespace llvm\r
79 \r
80 #endif\r