FileOutputBuffer.h 3.4 KB

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