FileOutputBuffer.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Utility for creating a in-memory buffer that will be written to a file.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
  18. #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/DataTypes.h"
  21. #include "llvm/Support/Error.h"
  22. namespace llvm {
  23. /// FileOutputBuffer - This interface provides simple way to create an in-memory
  24. /// buffer which will be written to a file. During the lifetime of these
  25. /// objects, the content or existence of the specified file is undefined. That
  26. /// is, creating an OutputBuffer for a file may immediately remove the file.
  27. /// If the FileOutputBuffer is committed, the target file's content will become
  28. /// the buffer content at the time of the commit. If the FileOutputBuffer is
  29. /// not committed, the file will be deleted in the FileOutputBuffer destructor.
  30. class FileOutputBuffer {
  31. public:
  32. enum {
  33. /// Set the 'x' bit on the resulting file.
  34. F_executable = 1,
  35. /// Don't use mmap and instead write an in-memory buffer to a file when this
  36. /// buffer is closed.
  37. F_no_mmap = 2,
  38. };
  39. /// Factory method to create an OutputBuffer object which manages a read/write
  40. /// buffer of the specified size. When committed, the buffer will be written
  41. /// to the file at the specified path.
  42. ///
  43. /// When F_modify is specified and \p FilePath refers to an existing on-disk
  44. /// file \p Size may be set to -1, in which case the entire file is used.
  45. /// Otherwise, the file shrinks or grows as necessary based on the value of
  46. /// \p Size. It is an error to specify F_modify and Size=-1 if \p FilePath
  47. /// does not exist.
  48. static Expected<std::unique_ptr<FileOutputBuffer>>
  49. create(StringRef FilePath, size_t Size, unsigned Flags = 0);
  50. /// Returns a pointer to the start of the buffer.
  51. virtual uint8_t *getBufferStart() const = 0;
  52. /// Returns a pointer to the end of the buffer.
  53. virtual uint8_t *getBufferEnd() const = 0;
  54. /// Returns size of the buffer.
  55. virtual size_t getBufferSize() const = 0;
  56. /// Returns path where file will show up if buffer is committed.
  57. StringRef getPath() const { return FinalPath; }
  58. /// Flushes the content of the buffer to its file and deallocates the
  59. /// buffer. If commit() is not called before this object's destructor
  60. /// is called, the file is deleted in the destructor. The optional parameter
  61. /// is used if it turns out you want the file size to be smaller than
  62. /// initially requested.
  63. virtual Error commit() = 0;
  64. /// If this object was previously committed, the destructor just deletes
  65. /// this object. If this object was not committed, the destructor
  66. /// deallocates the buffer and the target file is never written.
  67. virtual ~FileOutputBuffer() = default;
  68. /// This removes the temporary file (unless it already was committed)
  69. /// but keeps the memory mapping alive.
  70. virtual void discard() {}
  71. protected:
  72. FileOutputBuffer(StringRef Path) : FinalPath(Path) {}
  73. std::string FinalPath;
  74. };
  75. } // end namespace llvm
  76. #endif
  77. #ifdef __GNUC__
  78. #pragma GCC diagnostic pop
  79. #endif