1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===// 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 implements the MemoryBuffer interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/MemoryBuffer.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Support/Alignment.h" 18 #include "llvm/Support/Errc.h" 19 #include "llvm/Support/Error.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/MathExtras.h" 23 #include "llvm/Support/Process.h" 24 #include "llvm/Support/Program.h" 25 #include "llvm/Support/SmallVectorMemoryBuffer.h" 26 #include <algorithm> 27 #include <cassert> 28 #include <cstring> 29 #include <new> 30 #include <sys/types.h> 31 #include <system_error> 32 #if !defined(_MSC_VER) && !defined(__MINGW32__) 33 #include <unistd.h> 34 #else 35 #include <io.h> 36 #endif 37 38 #ifdef __MVS__ 39 #include "llvm/Support/AutoConvert.h" 40 #endif 41 using namespace llvm; 42 43 //===----------------------------------------------------------------------===// 44 // MemoryBuffer implementation itself. 45 //===----------------------------------------------------------------------===// 46 47 MemoryBuffer::~MemoryBuffer() = default; 48 49 /// init - Initialize this MemoryBuffer as a reference to externally allocated 50 /// memory, memory that we know is already null terminated. 51 void MemoryBuffer::init(const char *BufStart, const char *BufEnd, 52 bool RequiresNullTerminator) { 53 assert((!RequiresNullTerminator || BufEnd[0] == 0) && 54 "Buffer is not null terminated!"); 55 BufferStart = BufStart; 56 BufferEnd = BufEnd; 57 } 58 59 //===----------------------------------------------------------------------===// 60 // MemoryBufferMem implementation. 61 //===----------------------------------------------------------------------===// 62 63 /// CopyStringRef - Copies contents of a StringRef into a block of memory and 64 /// null-terminates it. 65 static void CopyStringRef(char *Memory, StringRef Data) { 66 if (!Data.empty()) 67 memcpy(Memory, Data.data(), Data.size()); 68 Memory[Data.size()] = 0; // Null terminate string. 69 } 70 71 namespace { 72 struct NamedBufferAlloc { 73 const Twine &Name; 74 NamedBufferAlloc(const Twine &Name) : Name(Name) {} 75 }; 76 } // namespace 77 78 void *operator new(size_t N, const NamedBufferAlloc &Alloc) { 79 SmallString<256> NameBuf; 80 StringRef NameRef = Alloc.Name.toStringRef(NameBuf); 81 82 char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) + 83 NameRef.size() + 1)); 84 *reinterpret_cast<size_t *>(Mem + N) = NameRef.size(); 85 CopyStringRef(Mem + N + sizeof(size_t), NameRef); 86 return Mem; 87 } 88 89 namespace { 90 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory. 91 template<typename MB> 92 class MemoryBufferMem : public MB { 93 public: 94 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) { 95 MemoryBuffer::init(InputData.begin(), InputData.end(), 96 RequiresNullTerminator); 97 } 98 99 /// Disable sized deallocation for MemoryBufferMem, because it has 100 /// tail-allocated data. 101 void operator delete(void *p) { std::free(p); } 102 103 StringRef getBufferIdentifier() const override { 104 // The name is stored after the class itself. 105 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t), 106 *reinterpret_cast<const size_t *>(this + 1)); 107 } 108 109 MemoryBuffer::BufferKind getBufferKind() const override { 110 return MemoryBuffer::MemoryBuffer_Malloc; 111 } 112 }; 113 } // namespace 114 115 template <typename MB> 116 static ErrorOr<std::unique_ptr<MB>> 117 getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, 118 bool IsText, bool RequiresNullTerminator, bool IsVolatile, 119 std::optional<Align> Alignment); 120 121 std::unique_ptr<MemoryBuffer> 122 MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName, 123 bool RequiresNullTerminator) { 124 auto *Ret = new (NamedBufferAlloc(BufferName)) 125 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator); 126 return std::unique_ptr<MemoryBuffer>(Ret); 127 } 128 129 std::unique_ptr<MemoryBuffer> 130 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) { 131 return std::unique_ptr<MemoryBuffer>(getMemBuffer( 132 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator)); 133 } 134 135 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 136 getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) { 137 auto Buf = 138 WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName); 139 if (!Buf) 140 return make_error_code(errc::not_enough_memory); 141 // Calling memcpy with null src/dst is UB, and an empty StringRef is 142 // represented with {nullptr, 0}. 143 llvm::copy(InputData, Buf->getBufferStart()); 144 return std::move(Buf); 145 } 146 147 std::unique_ptr<MemoryBuffer> 148 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) { 149 auto Buf = getMemBufferCopyImpl(InputData, BufferName); 150 if (Buf) 151 return std::move(*Buf); 152 return nullptr; 153 } 154 155 ErrorOr<std::unique_ptr<MemoryBuffer>> 156 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText, 157 bool RequiresNullTerminator, 158 std::optional<Align> Alignment) { 159 SmallString<256> NameBuf; 160 StringRef NameRef = Filename.toStringRef(NameBuf); 161 162 if (NameRef == "-") 163 return getSTDIN(); 164 return getFile(Filename, IsText, RequiresNullTerminator, 165 /*IsVolatile=*/false, Alignment); 166 } 167 168 ErrorOr<std::unique_ptr<MemoryBuffer>> 169 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize, 170 uint64_t Offset, bool IsVolatile, 171 std::optional<Align> Alignment) { 172 return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false, 173 /*RequiresNullTerminator=*/false, IsVolatile, 174 Alignment); 175 } 176 177 //===----------------------------------------------------------------------===// 178 // MemoryBuffer::getFile implementation. 179 //===----------------------------------------------------------------------===// 180 181 namespace { 182 183 template <typename MB> 184 constexpr sys::fs::mapped_file_region::mapmode Mapmode = 185 sys::fs::mapped_file_region::readonly; 186 template <> 187 constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> = 188 sys::fs::mapped_file_region::readonly; 189 template <> 190 constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> = 191 sys::fs::mapped_file_region::priv; 192 template <> 193 constexpr sys::fs::mapped_file_region::mapmode 194 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite; 195 196 /// Memory maps a file descriptor using sys::fs::mapped_file_region. 197 /// 198 /// This handles converting the offset into a legal offset on the platform. 199 template<typename MB> 200 class MemoryBufferMMapFile : public MB { 201 sys::fs::mapped_file_region MFR; 202 203 static uint64_t getLegalMapOffset(uint64_t Offset) { 204 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1); 205 } 206 207 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) { 208 return Len + (Offset - getLegalMapOffset(Offset)); 209 } 210 211 const char *getStart(uint64_t Len, uint64_t Offset) { 212 return MFR.const_data() + (Offset - getLegalMapOffset(Offset)); 213 } 214 215 public: 216 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len, 217 uint64_t Offset, std::error_code &EC) 218 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset), 219 getLegalMapOffset(Offset), EC) { 220 if (!EC) { 221 const char *Start = getStart(Len, Offset); 222 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator); 223 } 224 } 225 226 /// Disable sized deallocation for MemoryBufferMMapFile, because it has 227 /// tail-allocated data. 228 void operator delete(void *p) { ::operator delete(p); } 229 230 StringRef getBufferIdentifier() const override { 231 // The name is stored after the class itself. 232 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t), 233 *reinterpret_cast<const size_t *>(this + 1)); 234 } 235 236 MemoryBuffer::BufferKind getBufferKind() const override { 237 return MemoryBuffer::MemoryBuffer_MMap; 238 } 239 240 void dontNeedIfMmap() override { MFR.dontNeed(); } 241 }; 242 } // namespace 243 244 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 245 getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName) { 246 SmallString<sys::fs::DefaultReadChunkSize> Buffer; 247 if (Error E = sys::fs::readNativeFileToEOF(FD, Buffer)) 248 return errorToErrorCode(std::move(E)); 249 return getMemBufferCopyImpl(Buffer, BufferName); 250 } 251 252 ErrorOr<std::unique_ptr<MemoryBuffer>> 253 MemoryBuffer::getFile(const Twine &Filename, bool IsText, 254 bool RequiresNullTerminator, bool IsVolatile, 255 std::optional<Align> Alignment) { 256 return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0, 257 IsText, RequiresNullTerminator, IsVolatile, 258 Alignment); 259 } 260 261 template <typename MB> 262 static ErrorOr<std::unique_ptr<MB>> 263 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, 264 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, 265 bool IsVolatile, std::optional<Align> Alignment); 266 267 template <typename MB> 268 static ErrorOr<std::unique_ptr<MB>> 269 getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, 270 bool IsText, bool RequiresNullTerminator, bool IsVolatile, 271 std::optional<Align> Alignment) { 272 Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead( 273 Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None); 274 if (!FDOrErr) 275 return errorToErrorCode(FDOrErr.takeError()); 276 sys::fs::file_t FD = *FDOrErr; 277 auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset, 278 RequiresNullTerminator, IsVolatile, Alignment); 279 sys::fs::closeFile(FD); 280 return Ret; 281 } 282 283 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 284 WritableMemoryBuffer::getFile(const Twine &Filename, bool IsVolatile, 285 std::optional<Align> Alignment) { 286 return getFileAux<WritableMemoryBuffer>( 287 Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false, 288 /*RequiresNullTerminator=*/false, IsVolatile, Alignment); 289 } 290 291 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 292 WritableMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize, 293 uint64_t Offset, bool IsVolatile, 294 std::optional<Align> Alignment) { 295 return getFileAux<WritableMemoryBuffer>( 296 Filename, MapSize, Offset, /*IsText=*/false, 297 /*RequiresNullTerminator=*/false, IsVolatile, Alignment); 298 } 299 300 std::unique_ptr<WritableMemoryBuffer> 301 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, 302 const Twine &BufferName, 303 std::optional<Align> Alignment) { 304 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>; 305 306 // Use 16-byte alignment if no alignment is specified. 307 Align BufAlign = Alignment.value_or(Align(16)); 308 309 // Allocate space for the MemoryBuffer, the data and the name. It is important 310 // that MemoryBuffer and data are aligned so PointerIntPair works with them. 311 SmallString<256> NameBuf; 312 StringRef NameRef = BufferName.toStringRef(NameBuf); 313 314 size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1; 315 size_t RealLen = StringLen + Size + 1 + BufAlign.value(); 316 if (RealLen <= Size) // Check for rollover. 317 return nullptr; 318 // We use a call to malloc() rather than a call to a non-throwing operator 319 // new() because LLVM unconditionally installs an out of memory new handler 320 // when exceptions are disabled. This new handler intentionally crashes to 321 // aid with debugging, but that makes non-throwing new calls unhelpful. 322 // See MemoryBufferMem::operator delete() for the paired call to free(), and 323 // llvm::install_out_of_memory_new_handler() for the installation of the 324 // custom new handler. 325 char *Mem = static_cast<char *>(std::malloc(RealLen)); 326 if (!Mem) 327 return nullptr; 328 329 // The name is stored after the class itself. 330 *reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size(); 331 CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef); 332 333 // The buffer begins after the name and must be aligned. 334 char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign); 335 Buf[Size] = 0; // Null terminate buffer. 336 337 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true); 338 return std::unique_ptr<WritableMemoryBuffer>(Ret); 339 } 340 341 std::unique_ptr<WritableMemoryBuffer> 342 WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) { 343 auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName); 344 if (!SB) 345 return nullptr; 346 memset(SB->getBufferStart(), 0, Size); 347 return SB; 348 } 349 350 static bool shouldUseMmap(sys::fs::file_t FD, 351 size_t FileSize, 352 size_t MapSize, 353 off_t Offset, 354 bool RequiresNullTerminator, 355 int PageSize, 356 bool IsVolatile) { 357 // mmap may leave the buffer without null terminator if the file size changed 358 // by the time the last page is mapped in, so avoid it if the file size is 359 // likely to change. 360 if (IsVolatile && RequiresNullTerminator) 361 return false; 362 363 // We don't use mmap for small files because this can severely fragment our 364 // address space. 365 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize) 366 return false; 367 368 if (!RequiresNullTerminator) 369 return true; 370 371 // If we don't know the file size, use fstat to find out. fstat on an open 372 // file descriptor is cheaper than stat on a random path. 373 // FIXME: this chunk of code is duplicated, but it avoids a fstat when 374 // RequiresNullTerminator = false and MapSize != -1. 375 if (FileSize == size_t(-1)) { 376 sys::fs::file_status Status; 377 if (sys::fs::status(FD, Status)) 378 return false; 379 FileSize = Status.getSize(); 380 } 381 382 // If we need a null terminator and the end of the map is inside the file, 383 // we cannot use mmap. 384 size_t End = Offset + MapSize; 385 assert(End <= FileSize); 386 if (End != FileSize) 387 return false; 388 389 // Don't try to map files that are exactly a multiple of the system page size 390 // if we need a null terminator. 391 if ((FileSize & (PageSize -1)) == 0) 392 return false; 393 394 #if defined(__CYGWIN__) 395 // Don't try to map files that are exactly a multiple of the physical page size 396 // if we need a null terminator. 397 // FIXME: We should reorganize again getPageSize() on Win32. 398 if ((FileSize & (4096 - 1)) == 0) 399 return false; 400 #endif 401 402 return true; 403 } 404 405 static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 406 getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, 407 uint64_t Offset) { 408 Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForReadWrite( 409 Filename, sys::fs::CD_OpenExisting, sys::fs::OF_None); 410 if (!FDOrErr) 411 return errorToErrorCode(FDOrErr.takeError()); 412 sys::fs::file_t FD = *FDOrErr; 413 414 // Default is to map the full file. 415 if (MapSize == uint64_t(-1)) { 416 // If we don't know the file size, use fstat to find out. fstat on an open 417 // file descriptor is cheaper than stat on a random path. 418 if (FileSize == uint64_t(-1)) { 419 sys::fs::file_status Status; 420 std::error_code EC = sys::fs::status(FD, Status); 421 if (EC) 422 return EC; 423 424 // If this not a file or a block device (e.g. it's a named pipe 425 // or character device), we can't mmap it, so error out. 426 sys::fs::file_type Type = Status.type(); 427 if (Type != sys::fs::file_type::regular_file && 428 Type != sys::fs::file_type::block_file) 429 return make_error_code(errc::invalid_argument); 430 431 FileSize = Status.getSize(); 432 } 433 MapSize = FileSize; 434 } 435 436 std::error_code EC; 437 std::unique_ptr<WriteThroughMemoryBuffer> Result( 438 new (NamedBufferAlloc(Filename)) 439 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize, 440 Offset, EC)); 441 if (EC) 442 return EC; 443 return std::move(Result); 444 } 445 446 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 447 WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) { 448 return getReadWriteFile(Filename, FileSize, FileSize, 0); 449 } 450 451 /// Map a subrange of the specified file as a WritableMemoryBuffer. 452 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 453 WriteThroughMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize, 454 uint64_t Offset) { 455 return getReadWriteFile(Filename, -1, MapSize, Offset); 456 } 457 458 template <typename MB> 459 static ErrorOr<std::unique_ptr<MB>> 460 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, 461 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, 462 bool IsVolatile, std::optional<Align> Alignment) { 463 static int PageSize = sys::Process::getPageSizeEstimate(); 464 465 // Default is to map the full file. 466 if (MapSize == uint64_t(-1)) { 467 // If we don't know the file size, use fstat to find out. fstat on an open 468 // file descriptor is cheaper than stat on a random path. 469 if (FileSize == uint64_t(-1)) { 470 sys::fs::file_status Status; 471 std::error_code EC = sys::fs::status(FD, Status); 472 if (EC) 473 return EC; 474 475 // If this not a file or a block device (e.g. it's a named pipe 476 // or character device), we can't trust the size. Create the memory 477 // buffer by copying off the stream. 478 sys::fs::file_type Type = Status.type(); 479 if (Type != sys::fs::file_type::regular_file && 480 Type != sys::fs::file_type::block_file) 481 return getMemoryBufferForStream(FD, Filename); 482 483 FileSize = Status.getSize(); 484 } 485 MapSize = FileSize; 486 } 487 488 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, 489 PageSize, IsVolatile)) { 490 std::error_code EC; 491 std::unique_ptr<MB> Result( 492 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>( 493 RequiresNullTerminator, FD, MapSize, Offset, EC)); 494 if (!EC) 495 return std::move(Result); 496 } 497 498 #ifdef __MVS__ 499 // Set codepage auto-conversion for z/OS. 500 if (auto EC = llvm::enableAutoConversion(FD)) 501 return EC; 502 #endif 503 504 auto Buf = 505 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename, Alignment); 506 if (!Buf) { 507 // Failed to create a buffer. The only way it can fail is if 508 // new(std::nothrow) returns 0. 509 return make_error_code(errc::not_enough_memory); 510 } 511 512 // Read until EOF, zero-initialize the rest. 513 MutableArrayRef<char> ToRead = Buf->getBuffer(); 514 while (!ToRead.empty()) { 515 Expected<size_t> ReadBytes = 516 sys::fs::readNativeFileSlice(FD, ToRead, Offset); 517 if (!ReadBytes) 518 return errorToErrorCode(ReadBytes.takeError()); 519 if (*ReadBytes == 0) { 520 std::memset(ToRead.data(), 0, ToRead.size()); 521 break; 522 } 523 ToRead = ToRead.drop_front(*ReadBytes); 524 Offset += *ReadBytes; 525 } 526 527 return std::move(Buf); 528 } 529 530 ErrorOr<std::unique_ptr<MemoryBuffer>> 531 MemoryBuffer::getOpenFile(sys::fs::file_t FD, const Twine &Filename, 532 uint64_t FileSize, bool RequiresNullTerminator, 533 bool IsVolatile, std::optional<Align> Alignment) { 534 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0, 535 RequiresNullTerminator, IsVolatile, 536 Alignment); 537 } 538 539 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getOpenFileSlice( 540 sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, 541 bool IsVolatile, std::optional<Align> Alignment) { 542 assert(MapSize != uint64_t(-1)); 543 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false, 544 IsVolatile, Alignment); 545 } 546 547 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() { 548 // Read in all of the data from stdin, we cannot mmap stdin. 549 // 550 // FIXME: That isn't necessarily true, we should try to mmap stdin and 551 // fallback if it fails. 552 sys::ChangeStdinMode(sys::fs::OF_Text); 553 554 return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>"); 555 } 556 557 ErrorOr<std::unique_ptr<MemoryBuffer>> 558 MemoryBuffer::getFileAsStream(const Twine &Filename) { 559 Expected<sys::fs::file_t> FDOrErr = 560 sys::fs::openNativeFileForRead(Filename, sys::fs::OF_None); 561 if (!FDOrErr) 562 return errorToErrorCode(FDOrErr.takeError()); 563 sys::fs::file_t FD = *FDOrErr; 564 ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = 565 getMemoryBufferForStream(FD, Filename); 566 sys::fs::closeFile(FD); 567 return Ret; 568 } 569 570 MemoryBufferRef MemoryBuffer::getMemBufferRef() const { 571 StringRef Data = getBuffer(); 572 StringRef Identifier = getBufferIdentifier(); 573 return MemoryBufferRef(Data, Identifier); 574 } 575 576 SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer() = default; 577