blob: afe670f4afe10551757f0af6cb086b2753f1cb84 [file] [log] [blame]
Stephen Hines87f34652014-02-14 18:00:16 -08001//===- 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//===----------------------------------------------------------------------===//
Stephen Hines37b74a32014-11-26 18:48:20 -08009#ifndef MCLD_SUPPORT_FILEOUTPUTBUFFER_H_
10#define MCLD_SUPPORT_FILEOUTPUTBUFFER_H_
Stephen Hines87f34652014-02-14 18:00:16 -080011
Stephen Hines37b74a32014-11-26 18:48:20 -080012#include "mcld/Support/MemoryRegion.h"
13
Stephen Hines87f34652014-02-14 18:00:16 -080014#include <llvm/ADT/StringRef.h>
15#include <llvm/Support/DataTypes.h>
16#include <llvm/Support/FileSystem.h>
Stephen Hines37b74a32014-11-26 18:48:20 -080017
Stephen Hines0dea6bc2014-07-15 18:33:32 -070018#include <system_error>
Stephen Hines87f34652014-02-14 18:00:16 -080019
20namespace mcld {
21
22class FileHandle;
23
24/// FileOutputBuffer - This interface is borrowed from llvm bassically, and we
25/// may use ostream to emit output later.
26class FileOutputBuffer {
Stephen Hines37b74a32014-11-26 18:48:20 -080027 public:
Stephen Hines87f34652014-02-14 18:00:16 -080028 /// Factory method to create an OutputBuffer object which manages a read/write
29 /// buffer of the specified size. When committed, the buffer will be written
30 /// to the file at the specified path.
Stephen Hines0dea6bc2014-07-15 18:33:32 -070031 static std::error_code create(FileHandle& pFileHandle,
32 size_t pSize,
33 std::unique_ptr<FileOutputBuffer>& pResult);
Stephen Hines87f34652014-02-14 18:00:16 -080034
35 /// Returns a pointer to the start of the buffer.
36 uint8_t* getBufferStart() {
Stephen Hines37b74a32014-11-26 18:48:20 -080037 return reinterpret_cast<uint8_t*>(m_pRegion->data());
Stephen Hines87f34652014-02-14 18:00:16 -080038 }
39
40 /// Returns a pointer to the end of the buffer.
41 uint8_t* getBufferEnd() {
Stephen Hines37b74a32014-11-26 18:48:20 -080042 return reinterpret_cast<uint8_t*>(m_pRegion->data()) + m_pRegion->size();
Stephen Hines87f34652014-02-14 18:00:16 -080043 }
44
45 /// Returns size of the buffer.
Stephen Hines37b74a32014-11-26 18:48:20 -080046 size_t getBufferSize() const { return m_pRegion->size(); }
Stephen Hines87f34652014-02-14 18:00:16 -080047
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
Stephen Hines37b74a32014-11-26 18:48:20 -080055 private:
56 FileOutputBuffer(const FileOutputBuffer&);
57 FileOutputBuffer& operator=(const FileOutputBuffer&);
Stephen Hines87f34652014-02-14 18:00:16 -080058
59 FileOutputBuffer(llvm::sys::fs::mapped_file_region* pRegion,
60 FileHandle& pFileHandle);
61
Stephen Hines0dea6bc2014-07-15 18:33:32 -070062 std::unique_ptr<llvm::sys::fs::mapped_file_region> m_pRegion;
Stephen Hines87f34652014-02-14 18:00:16 -080063 FileHandle& m_FileHandle;
64};
65
Stephen Hines37b74a32014-11-26 18:48:20 -080066} // namespace mcld
Stephen Hines87f34652014-02-14 18:00:16 -080067
Stephen Hines37b74a32014-11-26 18:48:20 -080068#endif // MCLD_SUPPORT_FILEOUTPUTBUFFER_H_