FileOutputBuffer.h revision a790f0a8f3175183bea088389b3e4ae41813e192
1//===- FileOutputBuffer.h -------------------------------------------------===//
2//
3//                     the mclinker project
4//
5// this file is distributed under the university of illinois open source
6// license. see license.txt for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_SUPPORT_FILEOUTPUTBUFFER_H
10#define MCLD_SUPPORT_FILEOUTPUTBUFFER_H
11
12#include <mcld/Support/MemoryRegion.h>
13#include <llvm/ADT/StringRef.h>
14#include <llvm/Support/DataTypes.h>
15#include <llvm/Support/FileSystem.h>
16#include <system_error>
17
18namespace mcld {
19
20class FileHandle;
21
22/// FileOutputBuffer - This interface is borrowed from llvm bassically, and we
23/// may use ostream to emit output later.
24class FileOutputBuffer {
25public:
26  /// Factory method to create an OutputBuffer object which manages a read/write
27  /// buffer of the specified size. When committed, the buffer will be written
28  /// to the file at the specified path.
29  static std::error_code create(FileHandle& pFileHandle,
30                                size_t pSize,
31                                std::unique_ptr<FileOutputBuffer>& pResult);
32
33  /// Returns a pointer to the start of the buffer.
34  uint8_t* getBufferStart() {
35    return (uint8_t*)m_pRegion->data();
36  }
37
38  /// Returns a pointer to the end of the buffer.
39  uint8_t* getBufferEnd() {
40    return (uint8_t*)m_pRegion->data() + m_pRegion->size();
41  }
42
43  /// Returns size of the buffer.
44  size_t getBufferSize() const {
45    return m_pRegion->size();
46  }
47
48  MemoryRegion request(size_t pOffset, size_t pLength);
49
50  /// Returns path where file will show up if buffer is committed.
51  llvm::StringRef getPath() const;
52
53  ~FileOutputBuffer();
54
55private:
56  FileOutputBuffer(const FileOutputBuffer &);
57  FileOutputBuffer &operator=(const FileOutputBuffer &);
58
59  FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion,
60                   FileHandle& pFileHandle);
61
62  std::unique_ptr<llvm::sys::fs::mapped_file_region> m_pRegion;
63  FileHandle& m_FileHandle;
64};
65
66} // namespace mcld
67
68#endif
69