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