MemoryBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- 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. // This file defines the MemoryBuffer interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
  18. #define LLVM_SUPPORT_MEMORYBUFFER_H
  19. #include "llvm-c/Types.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/Support/Alignment.h"
  24. #include "llvm/Support/CBindingWrapping.h"
  25. #include "llvm/Support/ErrorOr.h"
  26. #include "llvm/Support/MemoryBufferRef.h"
  27. #include <cstddef>
  28. #include <cstdint>
  29. #include <memory>
  30. namespace llvm {
  31. namespace sys {
  32. namespace fs {
  33. // Duplicated from FileSystem.h to avoid a dependency.
  34. #if defined(_WIN32)
  35. // A Win32 HANDLE is a typedef of void*
  36. using file_t = void *;
  37. #else
  38. using file_t = int;
  39. #endif
  40. } // namespace fs
  41. } // namespace sys
  42. /// This interface provides simple read-only access to a block of memory, and
  43. /// provides simple methods for reading files and standard input into a memory
  44. /// buffer. In addition to basic access to the characters in the file, this
  45. /// interface guarantees you can read one character past the end of the file,
  46. /// and that this character will read as '\0'.
  47. ///
  48. /// The '\0' guarantee is needed to support an optimization -- it's intended to
  49. /// be more efficient for clients which are reading all the data to stop
  50. /// reading when they encounter a '\0' than to continually check the file
  51. /// position to see if it has reached the end of the file.
  52. class MemoryBuffer {
  53. const char *BufferStart; // Start of the buffer.
  54. const char *BufferEnd; // End of the buffer.
  55. protected:
  56. MemoryBuffer() = default;
  57. void init(const char *BufStart, const char *BufEnd,
  58. bool RequiresNullTerminator);
  59. public:
  60. MemoryBuffer(const MemoryBuffer &) = delete;
  61. MemoryBuffer &operator=(const MemoryBuffer &) = delete;
  62. virtual ~MemoryBuffer();
  63. const char *getBufferStart() const { return BufferStart; }
  64. const char *getBufferEnd() const { return BufferEnd; }
  65. size_t getBufferSize() const { return BufferEnd-BufferStart; }
  66. StringRef getBuffer() const {
  67. return StringRef(BufferStart, getBufferSize());
  68. }
  69. /// Return an identifier for this buffer, typically the filename it was read
  70. /// from.
  71. virtual StringRef getBufferIdentifier() const { return "Unknown buffer"; }
  72. /// For read-only MemoryBuffer_MMap, mark the buffer as unused in the near
  73. /// future and the kernel can free resources associated with it. Further
  74. /// access is supported but may be expensive. This calls
  75. /// madvise(MADV_DONTNEED) on read-only file mappings on *NIX systems. This
  76. /// function should not be called on a writable buffer.
  77. virtual void dontNeedIfMmap() {}
  78. /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
  79. /// if successful, otherwise returning null.
  80. ///
  81. /// \param IsText Set to true to indicate that the file should be read in
  82. /// text mode.
  83. ///
  84. /// \param IsVolatile Set to true to indicate that the contents of the file
  85. /// can change outside the user's control, e.g. when libclang tries to parse
  86. /// while the user is editing/updating the file or if the file is on an NFS.
  87. ///
  88. /// \param Alignment Set to indicate that the buffer should be aligned to at
  89. /// least the specified alignment.
  90. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  91. getFile(const Twine &Filename, bool IsText = false,
  92. bool RequiresNullTerminator = true, bool IsVolatile = false,
  93. std::optional<Align> Alignment = std::nullopt);
  94. /// Read all of the specified file into a MemoryBuffer as a stream
  95. /// (i.e. until EOF reached). This is useful for special files that
  96. /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
  97. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  98. getFileAsStream(const Twine &Filename);
  99. /// Given an already-open file descriptor, map some slice of it into a
  100. /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
  101. /// Since this is in the middle of a file, the buffer is not null terminated.
  102. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  103. getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
  104. int64_t Offset, bool IsVolatile = false,
  105. std::optional<Align> Alignment = std::nullopt);
  106. /// Given an already-open file descriptor, read the file and return a
  107. /// MemoryBuffer.
  108. ///
  109. /// \param IsVolatile Set to true to indicate that the contents of the file
  110. /// can change outside the user's control, e.g. when libclang tries to parse
  111. /// while the user is editing/updating the file or if the file is on an NFS.
  112. ///
  113. /// \param Alignment Set to indicate that the buffer should be aligned to at
  114. /// least the specified alignment.
  115. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  116. getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
  117. bool RequiresNullTerminator = true, bool IsVolatile = false,
  118. std::optional<Align> Alignment = std::nullopt);
  119. /// Open the specified memory range as a MemoryBuffer. Note that InputData
  120. /// must be null terminated if RequiresNullTerminator is true.
  121. static std::unique_ptr<MemoryBuffer>
  122. getMemBuffer(StringRef InputData, StringRef BufferName = "",
  123. bool RequiresNullTerminator = true);
  124. static std::unique_ptr<MemoryBuffer>
  125. getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
  126. /// Open the specified memory range as a MemoryBuffer, copying the contents
  127. /// and taking ownership of it. InputData does not have to be null terminated.
  128. static std::unique_ptr<MemoryBuffer>
  129. getMemBufferCopy(StringRef InputData, const Twine &BufferName = "");
  130. /// Read all of stdin into a file buffer, and return it.
  131. static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();
  132. /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
  133. /// is "-".
  134. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  135. getFileOrSTDIN(const Twine &Filename, bool IsText = false,
  136. bool RequiresNullTerminator = true,
  137. std::optional<Align> Alignment = std::nullopt);
  138. /// Map a subrange of the specified file as a MemoryBuffer.
  139. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  140. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
  141. bool IsVolatile = false,
  142. std::optional<Align> Alignment = std::nullopt);
  143. //===--------------------------------------------------------------------===//
  144. // Provided for performance analysis.
  145. //===--------------------------------------------------------------------===//
  146. /// The kind of memory backing used to support the MemoryBuffer.
  147. enum BufferKind {
  148. MemoryBuffer_Malloc,
  149. MemoryBuffer_MMap
  150. };
  151. /// Return information on the memory mechanism used to support the
  152. /// MemoryBuffer.
  153. virtual BufferKind getBufferKind() const = 0;
  154. MemoryBufferRef getMemBufferRef() const;
  155. };
  156. /// This class is an extension of MemoryBuffer, which allows copy-on-write
  157. /// access to the underlying contents. It only supports creation methods that
  158. /// are guaranteed to produce a writable buffer. For example, mapping a file
  159. /// read-only is not supported.
  160. class WritableMemoryBuffer : public MemoryBuffer {
  161. protected:
  162. WritableMemoryBuffer() = default;
  163. public:
  164. using MemoryBuffer::getBuffer;
  165. using MemoryBuffer::getBufferEnd;
  166. using MemoryBuffer::getBufferStart;
  167. // const_cast is well-defined here, because the underlying buffer is
  168. // guaranteed to have been initialized with a mutable buffer.
  169. char *getBufferStart() {
  170. return const_cast<char *>(MemoryBuffer::getBufferStart());
  171. }
  172. char *getBufferEnd() {
  173. return const_cast<char *>(MemoryBuffer::getBufferEnd());
  174. }
  175. MutableArrayRef<char> getBuffer() {
  176. return {getBufferStart(), getBufferEnd()};
  177. }
  178. static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
  179. getFile(const Twine &Filename, bool IsVolatile = false,
  180. std::optional<Align> Alignment = std::nullopt);
  181. /// Map a subrange of the specified file as a WritableMemoryBuffer.
  182. static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
  183. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
  184. bool IsVolatile = false,
  185. std::optional<Align> Alignment = std::nullopt);
  186. /// Allocate a new MemoryBuffer of the specified size that is not initialized.
  187. /// Note that the caller should initialize the memory allocated by this
  188. /// method. The memory is owned by the MemoryBuffer object.
  189. ///
  190. /// \param Alignment Set to indicate that the buffer should be aligned to at
  191. /// least the specified alignment.
  192. static std::unique_ptr<WritableMemoryBuffer>
  193. getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "",
  194. std::optional<Align> Alignment = std::nullopt);
  195. /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
  196. /// that the caller need not initialize the memory allocated by this method.
  197. /// The memory is owned by the MemoryBuffer object.
  198. static std::unique_ptr<WritableMemoryBuffer>
  199. getNewMemBuffer(size_t Size, const Twine &BufferName = "");
  200. private:
  201. // Hide these base class factory function so one can't write
  202. // WritableMemoryBuffer::getXXX()
  203. // and be surprised that he got a read-only Buffer.
  204. using MemoryBuffer::getFileAsStream;
  205. using MemoryBuffer::getFileOrSTDIN;
  206. using MemoryBuffer::getMemBuffer;
  207. using MemoryBuffer::getMemBufferCopy;
  208. using MemoryBuffer::getOpenFile;
  209. using MemoryBuffer::getOpenFileSlice;
  210. using MemoryBuffer::getSTDIN;
  211. };
  212. /// This class is an extension of MemoryBuffer, which allows write access to
  213. /// the underlying contents and committing those changes to the original source.
  214. /// It only supports creation methods that are guaranteed to produce a writable
  215. /// buffer. For example, mapping a file read-only is not supported.
  216. class WriteThroughMemoryBuffer : public MemoryBuffer {
  217. protected:
  218. WriteThroughMemoryBuffer() = default;
  219. public:
  220. using MemoryBuffer::getBuffer;
  221. using MemoryBuffer::getBufferEnd;
  222. using MemoryBuffer::getBufferStart;
  223. // const_cast is well-defined here, because the underlying buffer is
  224. // guaranteed to have been initialized with a mutable buffer.
  225. char *getBufferStart() {
  226. return const_cast<char *>(MemoryBuffer::getBufferStart());
  227. }
  228. char *getBufferEnd() {
  229. return const_cast<char *>(MemoryBuffer::getBufferEnd());
  230. }
  231. MutableArrayRef<char> getBuffer() {
  232. return {getBufferStart(), getBufferEnd()};
  233. }
  234. static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
  235. getFile(const Twine &Filename, int64_t FileSize = -1);
  236. /// Map a subrange of the specified file as a ReadWriteMemoryBuffer.
  237. static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
  238. getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset);
  239. private:
  240. // Hide these base class factory function so one can't write
  241. // WritableMemoryBuffer::getXXX()
  242. // and be surprised that he got a read-only Buffer.
  243. using MemoryBuffer::getFileAsStream;
  244. using MemoryBuffer::getFileOrSTDIN;
  245. using MemoryBuffer::getMemBuffer;
  246. using MemoryBuffer::getMemBufferCopy;
  247. using MemoryBuffer::getOpenFile;
  248. using MemoryBuffer::getOpenFileSlice;
  249. using MemoryBuffer::getSTDIN;
  250. };
  251. // Create wrappers for C Binding types (see CBindingWrapping.h).
  252. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef)
  253. } // end namespace llvm
  254. #endif // LLVM_SUPPORT_MEMORYBUFFER_H
  255. #ifdef __GNUC__
  256. #pragma GCC diagnostic pop
  257. #endif