1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Errno.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
28 #include <sys/types.h>
29 #include <system_error>
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
41 MemoryBuffer::~MemoryBuffer() { }
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
45 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
46 bool RequiresNullTerminator) {
47 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
48 "Buffer is not null terminated!");
49 BufferStart = BufStart;
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
57 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
58 /// null-terminates it.
59 static void CopyStringRef(char *Memory, StringRef Data) {
60 memcpy(Memory, Data.data(), Data.size());
61 Memory[Data.size()] = 0; // Null terminate string.
65 struct NamedBufferAlloc {
67 NamedBufferAlloc(const Twine &Name) : Name(Name) {}
71 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
72 SmallString<256> NameBuf;
73 StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
75 char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
76 CopyStringRef(Mem + N, NameRef);
81 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
82 class MemoryBufferMem : public MemoryBuffer {
84 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
85 init(InputData.begin(), InputData.end(), RequiresNullTerminator);
88 const char *getBufferIdentifier() const override {
89 // The name is stored after the class itself.
90 return reinterpret_cast<const char*>(this + 1);
93 BufferKind getBufferKind() const override {
94 return MemoryBuffer_Malloc;
99 static ErrorOr<std::unique_ptr<MemoryBuffer>>
100 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
101 uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize);
103 std::unique_ptr<MemoryBuffer>
104 MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
105 bool RequiresNullTerminator) {
106 auto *Ret = new (NamedBufferAlloc(BufferName))
107 MemoryBufferMem(InputData, RequiresNullTerminator);
108 return std::unique_ptr<MemoryBuffer>(Ret);
111 std::unique_ptr<MemoryBuffer>
112 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
113 return std::unique_ptr<MemoryBuffer>(getMemBuffer(
114 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
117 std::unique_ptr<MemoryBuffer>
118 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
119 std::unique_ptr<MemoryBuffer> Buf =
120 getNewUninitMemBuffer(InputData.size(), BufferName);
123 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
128 std::unique_ptr<MemoryBuffer>
129 MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
130 // Allocate space for the MemoryBuffer, the data and the name. It is important
131 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
132 // TODO: Is 16-byte alignment enough? We copy small object files with large
133 // alignment expectations into this buffer.
134 SmallString<256> NameBuf;
135 StringRef NameRef = BufferName.toStringRef(NameBuf);
136 size_t AlignedStringLen =
137 RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
138 size_t RealLen = AlignedStringLen + Size + 1;
139 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
143 // The name is stored after the class itself.
144 CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
146 // The buffer begins after the name and must be aligned.
147 char *Buf = Mem + AlignedStringLen;
148 Buf[Size] = 0; // Null terminate buffer.
150 auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
151 return std::unique_ptr<MemoryBuffer>(Ret);
154 std::unique_ptr<MemoryBuffer>
155 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
156 std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
159 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
163 ErrorOr<std::unique_ptr<MemoryBuffer>>
164 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) {
165 SmallString<256> NameBuf;
166 StringRef NameRef = Filename.toStringRef(NameBuf);
170 return getFile(Filename, FileSize);
173 ErrorOr<std::unique_ptr<MemoryBuffer>>
174 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
176 return getFileAux(FilePath, -1, MapSize, Offset, false, false);
180 //===----------------------------------------------------------------------===//
181 // MemoryBuffer::getFile implementation.
182 //===----------------------------------------------------------------------===//
185 /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
187 /// This handles converting the offset into a legal offset on the platform.
188 class MemoryBufferMMapFile : public MemoryBuffer {
189 sys::fs::mapped_file_region MFR;
191 static uint64_t getLegalMapOffset(uint64_t Offset) {
192 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
195 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
196 return Len + (Offset - getLegalMapOffset(Offset));
199 const char *getStart(uint64_t Len, uint64_t Offset) {
200 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
204 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
205 uint64_t Offset, std::error_code &EC)
206 : MFR(FD, sys::fs::mapped_file_region::readonly,
207 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
209 const char *Start = getStart(Len, Offset);
210 init(Start, Start + Len, RequiresNullTerminator);
214 const char *getBufferIdentifier() const override {
215 // The name is stored after the class itself.
216 return reinterpret_cast<const char *>(this + 1);
219 BufferKind getBufferKind() const override {
220 return MemoryBuffer_MMap;
225 static ErrorOr<std::unique_ptr<MemoryBuffer>>
226 getMemoryBufferForStream(int FD, const Twine &BufferName) {
227 const ssize_t ChunkSize = 4096*4;
228 SmallString<ChunkSize> Buffer;
230 // Read into Buffer until we hit EOF.
232 Buffer.reserve(Buffer.size() + ChunkSize);
233 ReadBytes = read(FD, Buffer.end(), ChunkSize);
234 if (ReadBytes == -1) {
235 if (errno == EINTR) continue;
236 return std::error_code(errno, std::generic_category());
238 Buffer.set_size(Buffer.size() + ReadBytes);
239 } while (ReadBytes != 0);
241 return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
245 ErrorOr<std::unique_ptr<MemoryBuffer>>
246 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
247 bool RequiresNullTerminator, bool IsVolatileSize) {
248 return getFileAux(Filename, FileSize, FileSize, 0,
249 RequiresNullTerminator, IsVolatileSize);
252 static ErrorOr<std::unique_ptr<MemoryBuffer>>
253 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
254 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
255 bool IsVolatileSize);
257 static ErrorOr<std::unique_ptr<MemoryBuffer>>
258 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
259 uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) {
261 std::error_code EC = sys::fs::openFileForRead(Filename, FD);
265 ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
266 getOpenFileImpl(FD, Filename, FileSize, MapSize, Offset,
267 RequiresNullTerminator, IsVolatileSize);
272 static bool shouldUseMmap(int FD,
276 bool RequiresNullTerminator,
278 bool IsVolatileSize) {
279 // mmap may leave the buffer without null terminator if the file size changed
280 // by the time the last page is mapped in, so avoid it if the file size is
285 // We don't use mmap for small files because this can severely fragment our
287 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
290 if (!RequiresNullTerminator)
294 // If we don't know the file size, use fstat to find out. fstat on an open
295 // file descriptor is cheaper than stat on a random path.
296 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
297 // RequiresNullTerminator = false and MapSize != -1.
298 if (FileSize == size_t(-1)) {
299 sys::fs::file_status Status;
300 if (sys::fs::status(FD, Status))
302 FileSize = Status.getSize();
305 // If we need a null terminator and the end of the map is inside the file,
306 // we cannot use mmap.
307 size_t End = Offset + MapSize;
308 assert(End <= FileSize);
312 // Don't try to map files that are exactly a multiple of the system page size
313 // if we need a null terminator.
314 if ((FileSize & (PageSize -1)) == 0)
317 #if defined(__CYGWIN__)
318 // Don't try to map files that are exactly a multiple of the physical page size
319 // if we need a null terminator.
320 // FIXME: We should reorganize again getPageSize() on Win32.
321 if ((FileSize & (4096 - 1)) == 0)
328 static ErrorOr<std::unique_ptr<MemoryBuffer>>
329 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
330 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
331 bool IsVolatileSize) {
332 static int PageSize = sys::Process::getPageSize();
334 // Default is to map the full file.
335 if (MapSize == uint64_t(-1)) {
336 // If we don't know the file size, use fstat to find out. fstat on an open
337 // file descriptor is cheaper than stat on a random path.
338 if (FileSize == uint64_t(-1)) {
339 sys::fs::file_status Status;
340 std::error_code EC = sys::fs::status(FD, Status);
344 // If this not a file or a block device (e.g. it's a named pipe
345 // or character device), we can't trust the size. Create the memory
346 // buffer by copying off the stream.
347 sys::fs::file_type Type = Status.type();
348 if (Type != sys::fs::file_type::regular_file &&
349 Type != sys::fs::file_type::block_file)
350 return getMemoryBufferForStream(FD, Filename);
352 FileSize = Status.getSize();
357 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
358 PageSize, IsVolatileSize)) {
360 std::unique_ptr<MemoryBuffer> Result(
361 new (NamedBufferAlloc(Filename))
362 MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC));
364 return std::move(Result);
367 std::unique_ptr<MemoryBuffer> Buf =
368 MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
370 // Failed to create a buffer. The only way it can fail is if
371 // new(std::nothrow) returns 0.
372 return make_error_code(errc::not_enough_memory);
375 char *BufPtr = const_cast<char *>(Buf->getBufferStart());
377 size_t BytesLeft = MapSize;
379 if (lseek(FD, Offset, SEEK_SET) == -1)
380 return std::error_code(errno, std::generic_category());
385 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
387 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
392 // Error while reading.
393 return std::error_code(errno, std::generic_category());
396 memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
399 BytesLeft -= NumRead;
403 return std::move(Buf);
406 ErrorOr<std::unique_ptr<MemoryBuffer>>
407 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
408 bool RequiresNullTerminator, bool IsVolatileSize) {
409 return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
410 RequiresNullTerminator, IsVolatileSize);
413 ErrorOr<std::unique_ptr<MemoryBuffer>>
414 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
416 assert(MapSize != uint64_t(-1));
417 return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
418 /*IsVolatileSize*/ false);
421 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
422 // Read in all of the data from stdin, we cannot mmap stdin.
424 // FIXME: That isn't necessarily true, we should try to mmap stdin and
425 // fallback if it fails.
426 sys::ChangeStdinToBinary();
428 return getMemoryBufferForStream(0, "<stdin>");
431 MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
432 StringRef Data = getBuffer();
433 StringRef Identifier = getBufferIdentifier();
434 return MemoryBufferRef(Data, Identifier);