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/OwningPtr.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Config/config.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"
24 #include "llvm/Support/system_error.h"
31 #include <sys/types.h>
32 #if !defined(_MSC_VER) && !defined(__MINGW32__)
36 // Simplistic definitinos of these macros for use in getOpenFile.
38 #define S_ISREG(x) (1)
41 #define S_ISBLK(x) (0)
46 //===----------------------------------------------------------------------===//
47 // MemoryBuffer implementation itself.
48 //===----------------------------------------------------------------------===//
50 MemoryBuffer::~MemoryBuffer() { }
52 /// init - Initialize this MemoryBuffer as a reference to externally allocated
53 /// memory, memory that we know is already null terminated.
54 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
55 bool RequiresNullTerminator) {
56 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
57 "Buffer is not null terminated!");
58 BufferStart = BufStart;
62 //===----------------------------------------------------------------------===//
63 // MemoryBufferMem implementation.
64 //===----------------------------------------------------------------------===//
66 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
67 /// null-terminates it.
68 static void CopyStringRef(char *Memory, StringRef Data) {
69 memcpy(Memory, Data.data(), Data.size());
70 Memory[Data.size()] = 0; // Null terminate string.
74 struct NamedBufferAlloc {
76 NamedBufferAlloc(StringRef Name) : Name(Name) {}
80 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
81 char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1));
82 CopyStringRef(Mem + N, Alloc.Name);
87 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
88 class MemoryBufferMem : public MemoryBuffer {
90 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
91 init(InputData.begin(), InputData.end(), RequiresNullTerminator);
94 virtual const char *getBufferIdentifier() const override {
95 // The name is stored after the class itself.
96 return reinterpret_cast<const char*>(this + 1);
99 virtual BufferKind getBufferKind() const override {
100 return MemoryBuffer_Malloc;
105 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
106 /// that InputData must be a null terminated if RequiresNullTerminator is true!
107 MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
108 StringRef BufferName,
109 bool RequiresNullTerminator) {
110 return new (NamedBufferAlloc(BufferName))
111 MemoryBufferMem(InputData, RequiresNullTerminator);
114 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
115 /// copying the contents and taking ownership of it. This has no requirements
117 MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
118 StringRef BufferName) {
119 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
121 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
126 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
127 /// that is not initialized. Note that the caller should initialize the
128 /// memory allocated by this method. The memory is owned by the MemoryBuffer
130 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
131 StringRef BufferName) {
132 // Allocate space for the MemoryBuffer, the data and the name. It is important
133 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
134 // TODO: Is 16-byte alignment enough? We copy small object files with large
135 // alignment expectations into this buffer.
136 size_t AlignedStringLen =
137 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
138 size_t RealLen = AlignedStringLen + Size + 1;
139 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
142 // The name is stored after the class itself.
143 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
145 // The buffer begins after the name and must be aligned.
146 char *Buf = Mem + AlignedStringLen;
147 Buf[Size] = 0; // Null terminate buffer.
149 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
152 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
153 /// is completely initialized to zeros. Note that the caller should
154 /// initialize the memory allocated by this method. The memory is owned by
155 /// the MemoryBuffer object.
156 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
157 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
159 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
164 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
165 /// if the Filename is "-". If an error occurs, this returns null and fills
166 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
167 /// returns an empty buffer.
168 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
169 std::unique_ptr<MemoryBuffer> &Result,
172 return getSTDIN(Result);
173 return getFile(Filename, Result, FileSize);
176 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
177 OwningPtr<MemoryBuffer> &Result,
179 std::unique_ptr<MemoryBuffer> MB;
180 error_code ec = getFileOrSTDIN(Filename, MB, FileSize);
181 Result = std::move(MB);
186 //===----------------------------------------------------------------------===//
187 // MemoryBuffer::getFile implementation.
188 //===----------------------------------------------------------------------===//
191 /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
193 /// This handles converting the offset into a legal offset on the platform.
194 class MemoryBufferMMapFile : public MemoryBuffer {
195 sys::fs::mapped_file_region MFR;
197 static uint64_t getLegalMapOffset(uint64_t Offset) {
198 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
201 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
202 return Len + (Offset - getLegalMapOffset(Offset));
205 const char *getStart(uint64_t Len, uint64_t Offset) {
206 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
210 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
211 uint64_t Offset, error_code EC)
212 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
213 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
215 const char *Start = getStart(Len, Offset);
216 init(Start, Start + Len, RequiresNullTerminator);
220 virtual const char *getBufferIdentifier() const override {
221 // The name is stored after the class itself.
222 return reinterpret_cast<const char *>(this + 1);
225 virtual BufferKind getBufferKind() const override {
226 return MemoryBuffer_MMap;
231 static error_code getMemoryBufferForStream(int FD,
232 StringRef BufferName,
233 std::unique_ptr<MemoryBuffer> &Result) {
234 const ssize_t ChunkSize = 4096*4;
235 SmallString<ChunkSize> Buffer;
237 // Read into Buffer until we hit EOF.
239 Buffer.reserve(Buffer.size() + ChunkSize);
240 ReadBytes = read(FD, Buffer.end(), ChunkSize);
241 if (ReadBytes == -1) {
242 if (errno == EINTR) continue;
243 return error_code(errno, posix_category());
245 Buffer.set_size(Buffer.size() + ReadBytes);
246 } while (ReadBytes != 0);
248 Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
249 return error_code::success();
252 static error_code getFileAux(const char *Filename,
253 std::unique_ptr<MemoryBuffer> &Result,
255 bool RequiresNullTerminator);
257 error_code MemoryBuffer::getFile(Twine Filename,
258 std::unique_ptr<MemoryBuffer> &Result,
260 bool RequiresNullTerminator) {
261 // Ensure the path is null terminated.
262 SmallString<256> PathBuf;
263 StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
264 return getFileAux(NullTerminatedName.data(), Result, FileSize,
265 RequiresNullTerminator);
268 error_code MemoryBuffer::getFile(Twine Filename,
269 OwningPtr<MemoryBuffer> &Result,
271 bool RequiresNullTerminator) {
272 std::unique_ptr<MemoryBuffer> MB;
273 error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator);
274 Result = std::move(MB);
278 static error_code getOpenFileImpl(int FD, const char *Filename,
279 std::unique_ptr<MemoryBuffer> &Result,
280 uint64_t FileSize, uint64_t MapSize,
281 int64_t Offset, bool RequiresNullTerminator);
283 static error_code getFileAux(const char *Filename,
284 std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
285 bool RequiresNullTerminator) {
287 error_code EC = sys::fs::openFileForRead(Filename, FD);
291 error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
292 RequiresNullTerminator);
297 static bool shouldUseMmap(int FD,
301 bool RequiresNullTerminator,
303 // We don't use mmap for small files because this can severely fragment our
305 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
308 if (!RequiresNullTerminator)
312 // If we don't know the file size, use fstat to find out. fstat on an open
313 // file descriptor is cheaper than stat on a random path.
314 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
315 // RequiresNullTerminator = false and MapSize != -1.
316 if (FileSize == size_t(-1)) {
317 sys::fs::file_status Status;
318 error_code EC = sys::fs::status(FD, Status);
321 FileSize = Status.getSize();
324 // If we need a null terminator and the end of the map is inside the file,
325 // we cannot use mmap.
326 size_t End = Offset + MapSize;
327 assert(End <= FileSize);
331 #if defined(_WIN32) || defined(__CYGWIN__)
332 // Don't peek the next page if file is multiple of *physical* pagesize(4k)
333 // but is not multiple of AllocationGranularity(64k),
334 // when a null terminator is required.
335 // FIXME: It's not good to hardcode 4096 here. dwPageSize shows 4096.
336 if ((FileSize & (4096 - 1)) == 0)
340 // Don't try to map files that are exactly a multiple of the system page size
341 // if we need a null terminator.
342 if ((FileSize & (PageSize -1)) == 0)
348 static error_code getOpenFileImpl(int FD, const char *Filename,
349 std::unique_ptr<MemoryBuffer> &Result,
350 uint64_t FileSize, uint64_t MapSize,
351 int64_t Offset, bool RequiresNullTerminator) {
352 static int PageSize = sys::process::get_self()->page_size();
354 // Default is to map the full file.
355 if (MapSize == uint64_t(-1)) {
356 // If we don't know the file size, use fstat to find out. fstat on an open
357 // file descriptor is cheaper than stat on a random path.
358 if (FileSize == uint64_t(-1)) {
359 sys::fs::file_status Status;
360 error_code EC = sys::fs::status(FD, Status);
364 // If this not a file or a block device (e.g. it's a named pipe
365 // or character device), we can't trust the size. Create the memory
366 // buffer by copying off the stream.
367 sys::fs::file_type Type = Status.type();
368 if (Type != sys::fs::file_type::regular_file &&
369 Type != sys::fs::file_type::block_file)
370 return getMemoryBufferForStream(FD, Filename, Result);
372 FileSize = Status.getSize();
377 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
380 Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
381 RequiresNullTerminator, FD, MapSize, Offset, EC));
383 return error_code::success();
386 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
388 // Failed to create a buffer. The only way it can fail is if
389 // new(std::nothrow) returns 0.
390 return make_error_code(errc::not_enough_memory);
393 std::unique_ptr<MemoryBuffer> SB(Buf);
394 char *BufPtr = const_cast<char*>(SB->getBufferStart());
396 size_t BytesLeft = MapSize;
398 if (lseek(FD, Offset, SEEK_SET) == -1)
399 return error_code(errno, posix_category());
404 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
406 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
411 // Error while reading.
412 return error_code(errno, posix_category());
415 assert(0 && "We got inaccurate FileSize value or fstat reported an "
416 "invalid file size.");
417 *BufPtr = '\0'; // null-terminate at the actual size.
420 BytesLeft -= NumRead;
425 return error_code::success();
428 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
429 std::unique_ptr<MemoryBuffer> &Result,
431 bool RequiresNullTerminator) {
432 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
433 RequiresNullTerminator);
436 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
437 OwningPtr<MemoryBuffer> &Result,
439 bool RequiresNullTerminator) {
440 std::unique_ptr<MemoryBuffer> MB;
441 error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
442 RequiresNullTerminator);
443 Result = std::move(MB);
447 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
448 std::unique_ptr<MemoryBuffer> &Result,
449 uint64_t MapSize, int64_t Offset) {
450 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
453 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
454 OwningPtr<MemoryBuffer> &Result,
455 uint64_t MapSize, int64_t Offset) {
456 std::unique_ptr<MemoryBuffer> MB;
457 error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false);
458 Result = std::move(MB);
462 //===----------------------------------------------------------------------===//
463 // MemoryBuffer::getSTDIN implementation.
464 //===----------------------------------------------------------------------===//
466 error_code MemoryBuffer::getSTDIN(std::unique_ptr<MemoryBuffer> &Result) {
467 // Read in all of the data from stdin, we cannot mmap stdin.
469 // FIXME: That isn't necessarily true, we should try to mmap stdin and
470 // fallback if it fails.
471 sys::ChangeStdinToBinary();
473 return getMemoryBufferForStream(0, "<stdin>", Result);
476 error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &Result) {
477 std::unique_ptr<MemoryBuffer> MB;
478 error_code ec = getSTDIN(MB);
479 Result = std::move(MB);