Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.3 KiB

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