1 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
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 // Utility for creating a in-memory buffer that will be written to a file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
16 
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 
21 namespace llvm {
22 
23 class error_code;
24 template<class T> class OwningPtr;
25 
26 /// FileOutputBuffer - This interface provides simple way to create an in-memory
27 /// buffer which will be written to a file. During the lifetime of these
28 /// objects, the content or existence of the specified file is undefined. That
29 /// is, creating an OutputBuffer for a file may immediately remove the file.
30 /// If the FileOutputBuffer is committed, the target file's content will become
31 /// the buffer content at the time of the commit.  If the FileOutputBuffer is
32 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
33 class FileOutputBuffer {
34 public:
35 
36   enum  {
37     F_executable = 1  /// set the 'x' bit on the resulting file
38   };
39 
40   /// Factory method to create an OutputBuffer object which manages a read/write
41   /// buffer of the specified size. When committed, the buffer will be written
42   /// to the file at the specified path.
43   static error_code create(StringRef FilePath, size_t Size,
44                            OwningPtr<FileOutputBuffer> &Result,
45                            unsigned Flags=0);
46 
47 
48   /// Returns a pointer to the start of the buffer.
49   uint8_t *getBufferStart() const {
50     return BufferStart;
51   }
52 
53   /// Returns a pointer to the end of the buffer.
54   uint8_t *getBufferEnd() const {
55     return BufferEnd;
56   }
57 
58   /// Returns size of the buffer.
59   size_t getBufferSize() const {
60     return BufferEnd - BufferStart;
61   }
62 
63   /// Returns path where file will show up if buffer is committed.
64   StringRef getPath() const {
65     return FinalPath;
66   }
67 
68   /// Flushes the content of the buffer to its file and deallocates the
69   /// buffer.  If commit() is not called before this object's destructor
70   /// is called, the file is deleted in the destructor. The optional parameter
71   /// is used if it turns out you want the file size to be smaller than
72   /// initially requested.
73   error_code commit(int64_t NewSmallerSize = -1);
74 
75   /// If this object was previously committed, the destructor just deletes
76   /// this object.  If this object was not committed, the destructor
77   /// deallocates the buffer and the target file is never written.
78   ~FileOutputBuffer();
79 
80 
81 private:
82   FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
83   FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
84 protected:
85   FileOutputBuffer(uint8_t *Start, uint8_t *End,
86                     StringRef Path, StringRef TempPath);
87 
88   uint8_t            *BufferStart;
89   uint8_t            *BufferEnd;
90   SmallString<128>    FinalPath;
91   SmallString<128>    TempPath;
92 };
93 
94 
95 
96 } // end namespace llvm
97 
98 #endif
99