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