FileOutputBuffer.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
  14. #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/FileSystem.h"
  19. namespace llvm {
  20. /// FileOutputBuffer - This interface provides simple way to create an in-memory
  21. /// buffer which will be written to a file. During the lifetime of these
  22. /// objects, the content or existence of the specified file is undefined. That
  23. /// is, creating an OutputBuffer for a file may immediately remove the file.
  24. /// If the FileOutputBuffer is committed, the target file's content will become
  25. /// the buffer content at the time of the commit. If the FileOutputBuffer is
  26. /// not committed, the file will be deleted in the FileOutputBuffer destructor.
  27. class FileOutputBuffer {
  28. public:
  29. enum {
  30. F_executable = 1 /// set the 'x' bit on the resulting file
  31. };
  32. /// Factory method to create an OutputBuffer object which manages a read/write
  33. /// buffer of the specified size. When committed, the buffer will be written
  34. /// to the file at the specified path.
  35. static std::error_code create(StringRef FilePath, size_t Size,
  36. std::unique_ptr<FileOutputBuffer> &Result,
  37. unsigned Flags = 0);
  38. /// Returns a pointer to the start of the buffer.
  39. uint8_t *getBufferStart() {
  40. return (uint8_t*)Region->data();
  41. }
  42. /// Returns a pointer to the end of the buffer.
  43. uint8_t *getBufferEnd() {
  44. return (uint8_t*)Region->data() + Region->size();
  45. }
  46. /// Returns size of the buffer.
  47. size_t getBufferSize() const {
  48. return Region->size();
  49. }
  50. /// Returns path where file will show up if buffer is committed.
  51. StringRef getPath() const {
  52. return FinalPath;
  53. }
  54. /// Flushes the content of the buffer to its file and deallocates the
  55. /// buffer. If commit() is not called before this object's destructor
  56. /// is called, the file is deleted in the destructor. The optional parameter
  57. /// is used if it turns out you want the file size to be smaller than
  58. /// initially requested.
  59. std::error_code commit();
  60. /// If this object was previously committed, the destructor just deletes
  61. /// this object. If this object was not committed, the destructor
  62. /// deallocates the buffer and the target file is never written.
  63. ~FileOutputBuffer();
  64. private:
  65. FileOutputBuffer(const FileOutputBuffer &) = delete;
  66. FileOutputBuffer &operator=(const FileOutputBuffer &) = delete;
  67. FileOutputBuffer(std::unique_ptr<llvm::sys::fs::mapped_file_region> R,
  68. StringRef Path, StringRef TempPath);
  69. std::unique_ptr<llvm::sys::fs::mapped_file_region> Region;
  70. SmallString<128> FinalPath;
  71. SmallString<128> TempPath;
  72. };
  73. } // end namespace llvm
  74. #endif