blob: cab45c736b51e5fe585d208a87aea26fb3809272 [file] [log] [blame]
Chris Lattneree2d1f12007-04-29 06:58:52 +00001//===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattneree2d1f12007-04-29 06:58:52 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MemoryBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/MemoryBuffer.h"
Chris Lattnera5425182008-04-01 06:05:21 +000015#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/SmallString.h"
Benjamin Kramere1effb02011-11-22 12:31:53 +000017#include "llvm/Config/config.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Errno.h"
Kaelyn Uhrain23fb5c32012-06-20 20:21:33 +000019#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000021#include "llvm/Support/Path.h"
22#include "llvm/Support/Process.h"
23#include "llvm/Support/Program.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000024#include "llvm/Support/system_error.h"
Jeff Cohen50b2d2c62007-04-29 14:21:44 +000025#include <cassert>
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include <cerrno>
Chris Lattneree2d1f12007-04-29 06:58:52 +000027#include <cstdio>
28#include <cstring>
Nick Lewycky0de20af2010-12-19 20:43:38 +000029#include <new>
Chris Lattnera5425182008-04-01 06:05:21 +000030#include <sys/stat.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include <sys/types.h>
Chris Lattnera5425182008-04-01 06:05:21 +000032#if !defined(_MSC_VER) && !defined(__MINGW32__)
33#include <unistd.h>
Chris Lattnera5425182008-04-01 06:05:21 +000034#else
35#include <io.h>
Rafael Espindolad8830872013-06-12 14:11:15 +000036// Simplistic definitinos of these macros for use in getOpenFile.
Dan Gohman3e172942013-02-19 22:38:58 +000037#ifndef S_ISREG
38#define S_ISREG(x) (1)
39#endif
40#ifndef S_ISBLK
41#define S_ISBLK(x) (0)
Daniel Dunbare2d25c22012-11-06 17:08:09 +000042#endif
Chris Lattnera5425182008-04-01 06:05:21 +000043#endif
Chris Lattneree2d1f12007-04-29 06:58:52 +000044using namespace llvm;
45
46//===----------------------------------------------------------------------===//
47// MemoryBuffer implementation itself.
48//===----------------------------------------------------------------------===//
49
Benjamin Kramerce2a9222010-06-25 11:50:40 +000050MemoryBuffer::~MemoryBuffer() { }
Chris Lattneree2d1f12007-04-29 06:58:52 +000051
52/// init - Initialize this MemoryBuffer as a reference to externally allocated
53/// memory, memory that we know is already null terminated.
Rafael Espindola258a6052011-03-10 18:33:29 +000054void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
55 bool RequiresNullTerminator) {
Rafael Espindola7c9cc462011-03-18 02:55:51 +000056 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
Rafael Espindola258a6052011-03-10 18:33:29 +000057 "Buffer is not null terminated!");
Chris Lattneree2d1f12007-04-29 06:58:52 +000058 BufferStart = BufStart;
59 BufferEnd = BufEnd;
Chris Lattneree2d1f12007-04-29 06:58:52 +000060}
61
62//===----------------------------------------------------------------------===//
63// MemoryBufferMem implementation.
64//===----------------------------------------------------------------------===//
65
Benjamin Kramerce2a9222010-06-25 11:50:40 +000066/// CopyStringRef - Copies contents of a StringRef into a block of memory and
67/// null-terminates it.
68static void CopyStringRef(char *Memory, StringRef Data) {
69 memcpy(Memory, Data.data(), Data.size());
70 Memory[Data.size()] = 0; // Null terminate string.
71}
72
Benjamin Kramera73cc5e2013-03-30 15:23:08 +000073namespace {
Michael J. Spencer2343a742013-03-12 19:28:19 +000074struct NamedBufferAlloc {
75 StringRef Name;
76 NamedBufferAlloc(StringRef Name) : Name(Name) {}
77};
Benjamin Kramera73cc5e2013-03-30 15:23:08 +000078}
Michael J. Spencer2343a742013-03-12 19:28:19 +000079
80void *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);
83 return Mem;
Benjamin Kramerce2a9222010-06-25 11:50:40 +000084}
85
Chris Lattneree2d1f12007-04-29 06:58:52 +000086namespace {
Benjamin Kramerce2a9222010-06-25 11:50:40 +000087/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
Chris Lattneree2d1f12007-04-29 06:58:52 +000088class MemoryBufferMem : public MemoryBuffer {
Chris Lattneree2d1f12007-04-29 06:58:52 +000089public:
Rafael Espindola258a6052011-03-10 18:33:29 +000090 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
91 init(InputData.begin(), InputData.end(), RequiresNullTerminator);
Chris Lattneree2d1f12007-04-29 06:58:52 +000092 }
Benjamin Kramerce2a9222010-06-25 11:50:40 +000093
Craig Topper3186c012012-09-23 02:12:10 +000094 virtual const char *getBufferIdentifier() const LLVM_OVERRIDE {
Benjamin Kramerce2a9222010-06-25 11:50:40 +000095 // The name is stored after the class itself.
96 return reinterpret_cast<const char*>(this + 1);
Chris Lattneree2d1f12007-04-29 06:58:52 +000097 }
Craig Topper3186c012012-09-23 02:12:10 +000098
99 virtual BufferKind getBufferKind() const LLVM_OVERRIDE {
Ted Kremeneke203bbb2011-04-28 20:34:18 +0000100 return MemoryBuffer_Malloc;
101 }
Chris Lattneree2d1f12007-04-29 06:58:52 +0000102};
103}
104
105/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
Chris Lattner60789262011-05-22 00:50:53 +0000106/// that InputData must be a null terminated if RequiresNullTerminator is true!
Chris Lattner0e45d242010-04-05 22:42:30 +0000107MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
Rafael Espindolaab959a22011-03-17 22:18:42 +0000108 StringRef BufferName,
109 bool RequiresNullTerminator) {
Michael J. Spencer2343a742013-03-12 19:28:19 +0000110 return new (NamedBufferAlloc(BufferName))
111 MemoryBufferMem(InputData, RequiresNullTerminator);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000112}
113
Chris Lattnerf5ea3862007-10-09 21:46:38 +0000114/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
115/// copying the contents and taking ownership of it. This has no requirements
116/// on EndPtr[0].
Chris Lattner0e45d242010-04-05 22:42:30 +0000117MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000118 StringRef BufferName) {
119 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
120 if (!Buf) return 0;
121 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
122 InputData.size());
123 return Buf;
Chris Lattnerf5ea3862007-10-09 21:46:38 +0000124}
125
Chris Lattneree2d1f12007-04-29 06:58:52 +0000126/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000127/// that is not initialized. Note that the caller should initialize the
128/// memory allocated by this method. The memory is owned by the MemoryBuffer
129/// object.
Evan Cheng86cb3182008-05-05 18:30:58 +0000130MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000131 StringRef BufferName) {
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000132 // 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 size_t AlignedStringLen =
135 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1,
136 sizeof(void*)); // TODO: Is sizeof(void*) enough?
137 size_t RealLen = AlignedStringLen + Size + 1;
138 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
139 if (!Mem) return 0;
140
141 // The name is stored after the class itself.
142 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
143
144 // The buffer begins after the name and must be aligned.
145 char *Buf = Mem + AlignedStringLen;
146 Buf[Size] = 0; // Null terminate buffer.
147
Rafael Espindola258a6052011-03-10 18:33:29 +0000148 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000149}
150
151/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
152/// is completely initialized to zeros. Note that the caller should
153/// initialize the memory allocated by this method. The memory is owned by
154/// the MemoryBuffer object.
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000155MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
Chris Lattneree2d1f12007-04-29 06:58:52 +0000156 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
Evan Cheng333db7a2009-02-13 07:54:34 +0000157 if (!SB) return 0;
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000158 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000159 return SB;
160}
161
162
Chris Lattner44158472007-11-18 18:52:28 +0000163/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
164/// if the Filename is "-". If an error occurs, this returns null and fills
165/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
166/// returns an empty buffer.
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000167error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
168 OwningPtr<MemoryBuffer> &result,
169 int64_t FileSize) {
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000170 if (Filename == "-")
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000171 return getSTDIN(result);
172 return getFile(Filename, result, FileSize);
Chris Lattner44158472007-11-18 18:52:28 +0000173}
174
Chris Lattneree2d1f12007-04-29 06:58:52 +0000175//===----------------------------------------------------------------------===//
Chris Lattneree2d1f12007-04-29 06:58:52 +0000176// MemoryBuffer::getFile implementation.
177//===----------------------------------------------------------------------===//
178
Chris Lattnera5425182008-04-01 06:05:21 +0000179namespace {
Michael J. Spencer2343a742013-03-12 19:28:19 +0000180/// \brief Memorry maps a file descriptor using sys::fs::mapped_file_region.
181///
182/// This handles converting the offset into a legal offset on the platform.
183class MemoryBufferMMapFile : public MemoryBuffer {
184 sys::fs::mapped_file_region MFR;
185
186 static uint64_t getLegalMapOffset(uint64_t Offset) {
187 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
188 }
189
190 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
191 return Len + (Offset - getLegalMapOffset(Offset));
192 }
193
194 const char *getStart(uint64_t Len, uint64_t Offset) {
195 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
196 }
197
Chris Lattnera5425182008-04-01 06:05:21 +0000198public:
Michael J. Spencer2343a742013-03-12 19:28:19 +0000199 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
200 uint64_t Offset, error_code EC)
Michael J. Spencer42ad29f2013-03-14 00:20:10 +0000201 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
Michael J. Spencer2343a742013-03-12 19:28:19 +0000202 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
203 if (!EC) {
204 const char *Start = getStart(Len, Offset);
205 init(Start, Start + Len, RequiresNullTerminator);
206 }
207 }
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000208
Michael J. Spencer2343a742013-03-12 19:28:19 +0000209 virtual const char *getBufferIdentifier() const LLVM_OVERRIDE {
210 // The name is stored after the class itself.
211 return reinterpret_cast<const char *>(this + 1);
Chris Lattnera5425182008-04-01 06:05:21 +0000212 }
Craig Topper3186c012012-09-23 02:12:10 +0000213
214 virtual BufferKind getBufferKind() const LLVM_OVERRIDE {
Ted Kremeneke203bbb2011-04-28 20:34:18 +0000215 return MemoryBuffer_MMap;
216 }
Chris Lattnera5425182008-04-01 06:05:21 +0000217};
218}
219
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000220static error_code getMemoryBufferForStream(int FD,
221 StringRef BufferName,
222 OwningPtr<MemoryBuffer> &result) {
223 const ssize_t ChunkSize = 4096*4;
224 SmallString<ChunkSize> Buffer;
225 ssize_t ReadBytes;
226 // Read into Buffer until we hit EOF.
227 do {
228 Buffer.reserve(Buffer.size() + ChunkSize);
229 ReadBytes = read(FD, Buffer.end(), ChunkSize);
230 if (ReadBytes == -1) {
231 if (errno == EINTR) continue;
232 return error_code(errno, posix_category());
233 }
234 Buffer.set_size(Buffer.size() + ReadBytes);
235 } while (ReadBytes != 0);
236
237 result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
238 return error_code::success();
239}
240
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000241error_code MemoryBuffer::getFile(StringRef Filename,
242 OwningPtr<MemoryBuffer> &result,
Rafael Espindola2475adc2011-03-22 19:20:47 +0000243 int64_t FileSize,
244 bool RequiresNullTerminator) {
Chris Lattner6bf4e6d2010-11-23 22:20:27 +0000245 // Ensure the path is null terminated.
Dan Gohmanb377e282010-06-24 16:25:50 +0000246 SmallString<256> PathBuf(Filename.begin(), Filename.end());
Rafael Espindola2475adc2011-03-22 19:20:47 +0000247 return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize,
248 RequiresNullTerminator);
Dan Gohmanb377e282010-06-24 16:25:50 +0000249}
250
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000251static error_code getOpenFileImpl(int FD, const char *Filename,
252 OwningPtr<MemoryBuffer> &Result,
253 uint64_t FileSize, uint64_t MapSize,
254 int64_t Offset, bool RequiresNullTerminator);
255
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000256error_code MemoryBuffer::getFile(const char *Filename,
257 OwningPtr<MemoryBuffer> &result,
Rafael Espindola2475adc2011-03-22 19:20:47 +0000258 int64_t FileSize,
259 bool RequiresNullTerminator) {
Rafael Espindola6d354812013-07-16 19:44:17 +0000260 int FD;
261 error_code EC = sys::fs::openFileForRead(Filename, FD);
262 if (EC)
263 return EC;
Chris Lattner60789262011-05-22 00:50:53 +0000264
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000265 error_code ret = getOpenFileImpl(FD, Filename, result, FileSize, FileSize, 0,
266 RequiresNullTerminator);
Michael J. Spencer42ad29f2013-03-14 00:20:10 +0000267 close(FD);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000268 return ret;
Chris Lattner6bf4e6d2010-11-23 22:20:27 +0000269}
Michael J. Spencer447762d2010-11-29 18:16:10 +0000270
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000271static bool shouldUseMmap(int FD,
272 size_t FileSize,
Rafael Espindola258a6052011-03-10 18:33:29 +0000273 size_t MapSize,
274 off_t Offset,
275 bool RequiresNullTerminator,
276 int PageSize) {
277 // We don't use mmap for small files because this can severely fragment our
278 // address space.
279 if (MapSize < 4096*4)
280 return false;
281
282 if (!RequiresNullTerminator)
283 return true;
284
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000285
286 // If we don't know the file size, use fstat to find out. fstat on an open
287 // file descriptor is cheaper than stat on a random path.
288 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
289 // RequiresNullTerminator = false and MapSize != -1.
290 if (FileSize == size_t(-1)) {
Rafael Espindola4d105872013-07-18 03:04:20 +0000291 sys::fs::file_status Status;
292 error_code EC = sys::fs::status(FD, Status);
293 if (EC)
294 return EC;
295 FileSize = Status.getSize();
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000296 }
297
Rafael Espindola258a6052011-03-10 18:33:29 +0000298 // If we need a null terminator and the end of the map is inside the file,
299 // we cannot use mmap.
300 size_t End = Offset + MapSize;
301 assert(End <= FileSize);
302 if (End != FileSize)
303 return false;
304
305 // Don't try to map files that are exactly a multiple of the system page size
306 // if we need a null terminator.
307 if ((FileSize & (PageSize -1)) == 0)
308 return false;
309
310 return true;
311}
312
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000313static error_code getOpenFileImpl(int FD, const char *Filename,
314 OwningPtr<MemoryBuffer> &result,
315 uint64_t FileSize, uint64_t MapSize,
316 int64_t Offset, bool RequiresNullTerminator) {
Chandler Carruthacd64be2012-12-31 23:31:56 +0000317 static int PageSize = sys::process::get_self()->page_size();
Rafael Espindola258a6052011-03-10 18:33:29 +0000318
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000319 // Default is to map the full file.
Ivan Krasin639222d2011-09-15 23:13:00 +0000320 if (MapSize == uint64_t(-1)) {
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000321 // If we don't know the file size, use fstat to find out. fstat on an open
322 // file descriptor is cheaper than stat on a random path.
Ivan Krasin639222d2011-09-15 23:13:00 +0000323 if (FileSize == uint64_t(-1)) {
Rafael Espindola4d105872013-07-18 03:04:20 +0000324 sys::fs::file_status Status;
325 error_code EC = sys::fs::status(FD, Status);
326 if (EC)
327 return EC;
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000328
Dan Gohman782609e2013-02-19 19:36:55 +0000329 // If this not a file or a block device (e.g. it's a named pipe
330 // or character device), we can't trust the size. Create the memory
331 // buffer by copying off the stream.
Rafael Espindola4d105872013-07-18 03:04:20 +0000332 sys::fs::file_type Type = Status.type();
333 if (Type != sys::fs::file_type::regular_file &&
334 Type != sys::fs::file_type::block_file)
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000335 return getMemoryBufferForStream(FD, Filename, result);
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000336
Rafael Espindola4d105872013-07-18 03:04:20 +0000337 FileSize = Status.getSize();
Chris Lattnera5425182008-04-01 06:05:21 +0000338 }
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000339 MapSize = FileSize;
Chris Lattnera5425182008-04-01 06:05:21 +0000340 }
Michael J. Spencer447762d2010-11-29 18:16:10 +0000341
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000342 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
Rafael Espindola258a6052011-03-10 18:33:29 +0000343 PageSize)) {
Michael J. Spencer2343a742013-03-12 19:28:19 +0000344 error_code EC;
345 result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
346 RequiresNullTerminator, FD, MapSize, Offset, EC));
347 if (!EC)
David Blaikie18544b92012-02-09 19:24:12 +0000348 return error_code::success();
Chris Lattnera5425182008-04-01 06:05:21 +0000349 }
Evan Cheng333db7a2009-02-13 07:54:34 +0000350
Rafael Espindola258a6052011-03-10 18:33:29 +0000351 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
Evan Cheng333db7a2009-02-13 07:54:34 +0000352 if (!Buf) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000353 // Failed to create a buffer. The only way it can fail is if
354 // new(std::nothrow) returns 0.
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000355 return make_error_code(errc::not_enough_memory);
Evan Cheng333db7a2009-02-13 07:54:34 +0000356 }
357
358 OwningPtr<MemoryBuffer> SB(Buf);
Chris Lattnera5425182008-04-01 06:05:21 +0000359 char *BufPtr = const_cast<char*>(SB->getBufferStart());
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000360
Rafael Espindola258a6052011-03-10 18:33:29 +0000361 size_t BytesLeft = MapSize;
Benjamin Kramere1effb02011-11-22 12:31:53 +0000362#ifndef HAVE_PREAD
Rafael Espindola258a6052011-03-10 18:33:29 +0000363 if (lseek(FD, Offset, SEEK_SET) == -1)
364 return error_code(errno, posix_category());
Benjamin Kramere1effb02011-11-22 12:31:53 +0000365#endif
Rafael Espindola258a6052011-03-10 18:33:29 +0000366
Chris Lattneree2d1f12007-04-29 06:58:52 +0000367 while (BytesLeft) {
Benjamin Kramere1effb02011-11-22 12:31:53 +0000368#ifdef HAVE_PREAD
369 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
370#else
Chris Lattneree2d1f12007-04-29 06:58:52 +0000371 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
Benjamin Kramere1effb02011-11-22 12:31:53 +0000372#endif
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000373 if (NumRead == -1) {
374 if (errno == EINTR)
375 continue;
376 // Error while reading.
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000377 return error_code(errno, posix_category());
Chris Lattneree2d1f12007-04-29 06:58:52 +0000378 }
Argyrios Kyrtzidis3dc531e2012-03-13 20:18:42 +0000379 if (NumRead == 0) {
380 assert(0 && "We got inaccurate FileSize value or fstat reported an "
381 "invalid file size.");
Argyrios Kyrtzidisef9092652012-04-05 04:23:56 +0000382 *BufPtr = '\0'; // null-terminate at the actual size.
Argyrios Kyrtzidis3dc531e2012-03-13 20:18:42 +0000383 break;
384 }
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000385 BytesLeft -= NumRead;
386 BufPtr += NumRead;
Chris Lattneree2d1f12007-04-29 06:58:52 +0000387 }
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000388
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000389 result.swap(SB);
David Blaikie18544b92012-02-09 19:24:12 +0000390 return error_code::success();
Chris Lattneree2d1f12007-04-29 06:58:52 +0000391}
392
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000393error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
394 OwningPtr<MemoryBuffer> &Result,
395 uint64_t FileSize,
396 bool RequiresNullTerminator) {
397 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
398 RequiresNullTerminator);
399}
400
401error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
402 OwningPtr<MemoryBuffer> &Result,
403 uint64_t MapSize, int64_t Offset) {
404 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
405}
406
Chris Lattneree2d1f12007-04-29 06:58:52 +0000407//===----------------------------------------------------------------------===//
408// MemoryBuffer::getSTDIN implementation.
409//===----------------------------------------------------------------------===//
410
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +0000411error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
Chris Lattneree2d1f12007-04-29 06:58:52 +0000412 // Read in all of the data from stdin, we cannot mmap stdin.
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000413 //
414 // FIXME: That isn't necessarily true, we should try to mmap stdin and
415 // fallback if it fails.
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000416 sys::ChangeStdinToBinary();
Benjamin Kramer58e6c2e2010-06-25 16:07:18 +0000417
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000418 return getMemoryBufferForStream(0, "<stdin>", result);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000419}