blob: abf4f60b27a61487fd8c9b1e0e270ddab9f42770 [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/SmallString.h"
Benjamin Kramere1effb02011-11-22 12:31:53 +000016#include "llvm/Config/config.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000017#include "llvm/Support/Errc.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"
Jeff Cohen50b2d2c62007-04-29 14:21:44 +000024#include <cassert>
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include <cerrno>
Chris Lattneree2d1f12007-04-29 06:58:52 +000026#include <cstdio>
27#include <cstring>
Nick Lewycky0de20af2010-12-19 20:43:38 +000028#include <new>
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include <sys/types.h>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000030#include <system_error>
Chris Lattnera5425182008-04-01 06:05:21 +000031#if !defined(_MSC_VER) && !defined(__MINGW32__)
32#include <unistd.h>
Chris Lattnera5425182008-04-01 06:05:21 +000033#else
34#include <io.h>
35#endif
Chris Lattneree2d1f12007-04-29 06:58:52 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// MemoryBuffer implementation itself.
40//===----------------------------------------------------------------------===//
41
Benjamin Kramerce2a9222010-06-25 11:50:40 +000042MemoryBuffer::~MemoryBuffer() { }
Chris Lattneree2d1f12007-04-29 06:58:52 +000043
44/// init - Initialize this MemoryBuffer as a reference to externally allocated
45/// memory, memory that we know is already null terminated.
Rafael Espindola258a6052011-03-10 18:33:29 +000046void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
47 bool RequiresNullTerminator) {
Rafael Espindola7c9cc462011-03-18 02:55:51 +000048 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
Rafael Espindola258a6052011-03-10 18:33:29 +000049 "Buffer is not null terminated!");
Chris Lattneree2d1f12007-04-29 06:58:52 +000050 BufferStart = BufStart;
51 BufferEnd = BufEnd;
Chris Lattneree2d1f12007-04-29 06:58:52 +000052}
53
54//===----------------------------------------------------------------------===//
55// MemoryBufferMem implementation.
56//===----------------------------------------------------------------------===//
57
Benjamin Kramerce2a9222010-06-25 11:50:40 +000058/// CopyStringRef - Copies contents of a StringRef into a block of memory and
59/// null-terminates it.
60static void CopyStringRef(char *Memory, StringRef Data) {
61 memcpy(Memory, Data.data(), Data.size());
62 Memory[Data.size()] = 0; // Null terminate string.
63}
64
Benjamin Kramera73cc5e2013-03-30 15:23:08 +000065namespace {
Michael J. Spencer2343a742013-03-12 19:28:19 +000066struct NamedBufferAlloc {
67 StringRef Name;
68 NamedBufferAlloc(StringRef Name) : Name(Name) {}
69};
Benjamin Kramera73cc5e2013-03-30 15:23:08 +000070}
Michael J. Spencer2343a742013-03-12 19:28:19 +000071
72void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
73 char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1));
74 CopyStringRef(Mem + N, Alloc.Name);
75 return Mem;
Benjamin Kramerce2a9222010-06-25 11:50:40 +000076}
77
Chris Lattneree2d1f12007-04-29 06:58:52 +000078namespace {
Benjamin Kramerce2a9222010-06-25 11:50:40 +000079/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
Chris Lattneree2d1f12007-04-29 06:58:52 +000080class MemoryBufferMem : public MemoryBuffer {
Chris Lattneree2d1f12007-04-29 06:58:52 +000081public:
Rafael Espindola258a6052011-03-10 18:33:29 +000082 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
83 init(InputData.begin(), InputData.end(), RequiresNullTerminator);
Chris Lattneree2d1f12007-04-29 06:58:52 +000084 }
Benjamin Kramerce2a9222010-06-25 11:50:40 +000085
Craig Topper6ff5aa72014-03-10 03:53:12 +000086 const char *getBufferIdentifier() const override {
Benjamin Kramerce2a9222010-06-25 11:50:40 +000087 // The name is stored after the class itself.
88 return reinterpret_cast<const char*>(this + 1);
Chris Lattneree2d1f12007-04-29 06:58:52 +000089 }
Craig Topper3186c012012-09-23 02:12:10 +000090
Craig Topper6ff5aa72014-03-10 03:53:12 +000091 BufferKind getBufferKind() const override {
Ted Kremeneke203bbb2011-04-28 20:34:18 +000092 return MemoryBuffer_Malloc;
93 }
Chris Lattneree2d1f12007-04-29 06:58:52 +000094};
95}
96
97/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
Chris Lattner60789262011-05-22 00:50:53 +000098/// that InputData must be a null terminated if RequiresNullTerminator is true!
Chris Lattner0e45d242010-04-05 22:42:30 +000099MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
Rafael Espindolaab959a22011-03-17 22:18:42 +0000100 StringRef BufferName,
101 bool RequiresNullTerminator) {
Michael J. Spencer2343a742013-03-12 19:28:19 +0000102 return new (NamedBufferAlloc(BufferName))
103 MemoryBufferMem(InputData, RequiresNullTerminator);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000104}
105
Chris Lattnerf5ea3862007-10-09 21:46:38 +0000106/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
107/// copying the contents and taking ownership of it. This has no requirements
108/// on EndPtr[0].
Chris Lattner0e45d242010-04-05 22:42:30 +0000109MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000110 StringRef BufferName) {
111 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
Craig Topperc10719f2014-04-07 04:17:22 +0000112 if (!Buf) return nullptr;
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000113 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
114 InputData.size());
115 return Buf;
Chris Lattnerf5ea3862007-10-09 21:46:38 +0000116}
117
Chris Lattneree2d1f12007-04-29 06:58:52 +0000118/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000119/// that is not initialized. Note that the caller should initialize the
120/// memory allocated by this method. The memory is owned by the MemoryBuffer
121/// object.
Evan Cheng86cb3182008-05-05 18:30:58 +0000122MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000123 StringRef BufferName) {
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000124 // Allocate space for the MemoryBuffer, the data and the name. It is important
125 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
Reid Kleckner86a8e1e2013-12-16 18:18:12 +0000126 // TODO: Is 16-byte alignment enough? We copy small object files with large
127 // alignment expectations into this buffer.
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000128 size_t AlignedStringLen =
Reid Kleckner86a8e1e2013-12-16 18:18:12 +0000129 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000130 size_t RealLen = AlignedStringLen + Size + 1;
131 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
Craig Topperc10719f2014-04-07 04:17:22 +0000132 if (!Mem) return nullptr;
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000133
134 // The name is stored after the class itself.
135 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
136
137 // The buffer begins after the name and must be aligned.
138 char *Buf = Mem + AlignedStringLen;
139 Buf[Size] = 0; // Null terminate buffer.
140
Rafael Espindola258a6052011-03-10 18:33:29 +0000141 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000142}
143
144/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
145/// is completely initialized to zeros. Note that the caller should
146/// initialize the memory allocated by this method. The memory is owned by
147/// the MemoryBuffer object.
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000148MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
Chris Lattneree2d1f12007-04-29 06:58:52 +0000149 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
Craig Topperc10719f2014-04-07 04:17:22 +0000150 if (!SB) return nullptr;
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000151 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000152 return SB;
153}
154
155
Chris Lattner44158472007-11-18 18:52:28 +0000156/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
157/// if the Filename is "-". If an error occurs, this returns null and fills
158/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
159/// returns an empty buffer.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000160std::error_code
161MemoryBuffer::getFileOrSTDIN(StringRef Filename,
162 std::unique_ptr<MemoryBuffer> &Result,
163 int64_t FileSize) {
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000164 if (Filename == "-")
Ahmed Charlesfba06642014-03-05 10:27:34 +0000165 return getSTDIN(Result);
166 return getFile(Filename, Result, FileSize);
Chris Lattner44158472007-11-18 18:52:28 +0000167}
168
Ahmed Charlesfba06642014-03-05 10:27:34 +0000169
Chris Lattneree2d1f12007-04-29 06:58:52 +0000170//===----------------------------------------------------------------------===//
Chris Lattneree2d1f12007-04-29 06:58:52 +0000171// MemoryBuffer::getFile implementation.
172//===----------------------------------------------------------------------===//
173
Chris Lattnera5425182008-04-01 06:05:21 +0000174namespace {
Matt Arsenault305a2252013-09-10 19:54:54 +0000175/// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
Michael J. Spencer2343a742013-03-12 19:28:19 +0000176///
177/// This handles converting the offset into a legal offset on the platform.
178class MemoryBufferMMapFile : public MemoryBuffer {
179 sys::fs::mapped_file_region MFR;
180
181 static uint64_t getLegalMapOffset(uint64_t Offset) {
182 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
183 }
184
185 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
186 return Len + (Offset - getLegalMapOffset(Offset));
187 }
188
189 const char *getStart(uint64_t Len, uint64_t Offset) {
190 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
191 }
192
Chris Lattnera5425182008-04-01 06:05:21 +0000193public:
Michael J. Spencer2343a742013-03-12 19:28:19 +0000194 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000195 uint64_t Offset, std::error_code EC)
Michael J. Spencer42ad29f2013-03-14 00:20:10 +0000196 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
Michael J. Spencer2343a742013-03-12 19:28:19 +0000197 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
198 if (!EC) {
199 const char *Start = getStart(Len, Offset);
200 init(Start, Start + Len, RequiresNullTerminator);
201 }
202 }
Benjamin Kramerce2a9222010-06-25 11:50:40 +0000203
Craig Topper6ff5aa72014-03-10 03:53:12 +0000204 const char *getBufferIdentifier() const override {
Michael J. Spencer2343a742013-03-12 19:28:19 +0000205 // The name is stored after the class itself.
206 return reinterpret_cast<const char *>(this + 1);
Chris Lattnera5425182008-04-01 06:05:21 +0000207 }
Craig Topper3186c012012-09-23 02:12:10 +0000208
Craig Topper6ff5aa72014-03-10 03:53:12 +0000209 BufferKind getBufferKind() const override {
Ted Kremeneke203bbb2011-04-28 20:34:18 +0000210 return MemoryBuffer_MMap;
211 }
Chris Lattnera5425182008-04-01 06:05:21 +0000212};
213}
214
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000215static std::error_code
216getMemoryBufferForStream(int FD, StringRef BufferName,
217 std::unique_ptr<MemoryBuffer> &Result) {
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000218 const ssize_t ChunkSize = 4096*4;
219 SmallString<ChunkSize> Buffer;
220 ssize_t ReadBytes;
221 // Read into Buffer until we hit EOF.
222 do {
223 Buffer.reserve(Buffer.size() + ChunkSize);
224 ReadBytes = read(FD, Buffer.end(), ChunkSize);
225 if (ReadBytes == -1) {
226 if (errno == EINTR) continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000227 return std::error_code(errno, std::generic_category());
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000228 }
229 Buffer.set_size(Buffer.size() + ReadBytes);
230 } while (ReadBytes != 0);
231
Ahmed Charlesfba06642014-03-05 10:27:34 +0000232 Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000233 return std::error_code();
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000234}
235
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000236static std::error_code getFileAux(const char *Filename,
237 std::unique_ptr<MemoryBuffer> &Result,
238 int64_t FileSize, bool RequiresNullTerminator,
239 bool IsVolatileSize);
Rafael Espindola1d19c8f2013-10-25 19:06:52 +0000240
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000241std::error_code MemoryBuffer::getFile(Twine Filename,
242 std::unique_ptr<MemoryBuffer> &Result,
243 int64_t FileSize,
244 bool RequiresNullTerminator,
245 bool IsVolatileSize) {
Chris Lattner6bf4e6d2010-11-23 22:20:27 +0000246 // Ensure the path is null terminated.
Rafael Espindola1d19c8f2013-10-25 19:06:52 +0000247 SmallString<256> PathBuf;
248 StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
Ahmed Charlesfba06642014-03-05 10:27:34 +0000249 return getFileAux(NullTerminatedName.data(), Result, FileSize,
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000250 RequiresNullTerminator, IsVolatileSize);
Dan Gohmanb377e282010-06-24 16:25:50 +0000251}
252
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000253static std::error_code getOpenFileImpl(int FD, const char *Filename,
254 std::unique_ptr<MemoryBuffer> &Result,
255 uint64_t FileSize, uint64_t MapSize,
256 int64_t Offset,
257 bool RequiresNullTerminator,
258 bool IsVolatileSize);
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000259
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000260static std::error_code getFileAux(const char *Filename,
261 std::unique_ptr<MemoryBuffer> &Result,
262 int64_t FileSize, bool RequiresNullTerminator,
263 bool IsVolatileSize) {
Rafael Espindola6d354812013-07-16 19:44:17 +0000264 int FD;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000265 std::error_code EC = sys::fs::openFileForRead(Filename, FD);
Rafael Espindola6d354812013-07-16 19:44:17 +0000266 if (EC)
267 return EC;
Chris Lattner60789262011-05-22 00:50:53 +0000268
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000269 std::error_code ret =
270 getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
271 RequiresNullTerminator, IsVolatileSize);
Michael J. Spencer42ad29f2013-03-14 00:20:10 +0000272 close(FD);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000273 return ret;
Chris Lattner6bf4e6d2010-11-23 22:20:27 +0000274}
Michael J. Spencer447762d2010-11-29 18:16:10 +0000275
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000276static bool shouldUseMmap(int FD,
277 size_t FileSize,
Rafael Espindola258a6052011-03-10 18:33:29 +0000278 size_t MapSize,
279 off_t Offset,
280 bool RequiresNullTerminator,
Argyrios Kyrtzidisbde59272014-05-06 00:51:45 +0000281 int PageSize,
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000282 bool IsVolatileSize) {
Argyrios Kyrtzidisbde59272014-05-06 00:51:45 +0000283 // mmap may leave the buffer without null terminator if the file size changed
284 // by the time the last page is mapped in, so avoid it if the file size is
285 // likely to change.
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000286 if (IsVolatileSize)
Argyrios Kyrtzidisbde59272014-05-06 00:51:45 +0000287 return false;
288
Rafael Espindola258a6052011-03-10 18:33:29 +0000289 // We don't use mmap for small files because this can severely fragment our
290 // address space.
NAKAMURA Takumi26c8ea62013-08-22 10:23:52 +0000291 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
Rafael Espindola258a6052011-03-10 18:33:29 +0000292 return false;
293
294 if (!RequiresNullTerminator)
295 return true;
296
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000297
298 // If we don't know the file size, use fstat to find out. fstat on an open
299 // file descriptor is cheaper than stat on a random path.
300 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
301 // RequiresNullTerminator = false and MapSize != -1.
302 if (FileSize == size_t(-1)) {
Rafael Espindola4d105872013-07-18 03:04:20 +0000303 sys::fs::file_status Status;
Alp Tokerc7bd4d22014-05-09 08:57:32 +0000304 if (sys::fs::status(FD, Status))
305 return false;
Rafael Espindola4d105872013-07-18 03:04:20 +0000306 FileSize = Status.getSize();
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000307 }
308
Rafael Espindola258a6052011-03-10 18:33:29 +0000309 // If we need a null terminator and the end of the map is inside the file,
310 // we cannot use mmap.
311 size_t End = Offset + MapSize;
312 assert(End <= FileSize);
313 if (End != FileSize)
314 return false;
315
316 // Don't try to map files that are exactly a multiple of the system page size
317 // if we need a null terminator.
318 if ((FileSize & (PageSize -1)) == 0)
319 return false;
320
321 return true;
322}
323
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000324static std::error_code getOpenFileImpl(int FD, const char *Filename,
325 std::unique_ptr<MemoryBuffer> &Result,
326 uint64_t FileSize, uint64_t MapSize,
327 int64_t Offset,
328 bool RequiresNullTerminator,
329 bool IsVolatileSize) {
Chandler Carruthacd64be2012-12-31 23:31:56 +0000330 static int PageSize = sys::process::get_self()->page_size();
Rafael Espindola258a6052011-03-10 18:33:29 +0000331
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000332 // Default is to map the full file.
Ivan Krasin639222d2011-09-15 23:13:00 +0000333 if (MapSize == uint64_t(-1)) {
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000334 // If we don't know the file size, use fstat to find out. fstat on an open
335 // file descriptor is cheaper than stat on a random path.
Ivan Krasin639222d2011-09-15 23:13:00 +0000336 if (FileSize == uint64_t(-1)) {
Rafael Espindola4d105872013-07-18 03:04:20 +0000337 sys::fs::file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000338 std::error_code EC = sys::fs::status(FD, Status);
Rafael Espindola4d105872013-07-18 03:04:20 +0000339 if (EC)
340 return EC;
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000341
Dan Gohman782609e2013-02-19 19:36:55 +0000342 // If this not a file or a block device (e.g. it's a named pipe
343 // or character device), we can't trust the size. Create the memory
344 // buffer by copying off the stream.
Rafael Espindola4d105872013-07-18 03:04:20 +0000345 sys::fs::file_type Type = Status.type();
346 if (Type != sys::fs::file_type::regular_file &&
347 Type != sys::fs::file_type::block_file)
Ahmed Charlesfba06642014-03-05 10:27:34 +0000348 return getMemoryBufferForStream(FD, Filename, Result);
Daniel Dunbar43a172d2012-11-05 21:55:40 +0000349
Rafael Espindola4d105872013-07-18 03:04:20 +0000350 FileSize = Status.getSize();
Chris Lattnera5425182008-04-01 06:05:21 +0000351 }
Rafael Espindolacbe6a1a2011-03-10 20:54:07 +0000352 MapSize = FileSize;
Chris Lattnera5425182008-04-01 06:05:21 +0000353 }
Michael J. Spencer447762d2010-11-29 18:16:10 +0000354
Argyrios Kyrtzidisbde59272014-05-06 00:51:45 +0000355 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000356 PageSize, IsVolatileSize)) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000357 std::error_code EC;
Ahmed Charlesfba06642014-03-05 10:27:34 +0000358 Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
Michael J. Spencer2343a742013-03-12 19:28:19 +0000359 RequiresNullTerminator, FD, MapSize, Offset, EC));
360 if (!EC)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000361 return std::error_code();
Chris Lattnera5425182008-04-01 06:05:21 +0000362 }
Evan Cheng333db7a2009-02-13 07:54:34 +0000363
Rafael Espindola258a6052011-03-10 18:33:29 +0000364 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
Evan Cheng333db7a2009-02-13 07:54:34 +0000365 if (!Buf) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000366 // Failed to create a buffer. The only way it can fail is if
367 // new(std::nothrow) returns 0.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000368 return make_error_code(errc::not_enough_memory);
Evan Cheng333db7a2009-02-13 07:54:34 +0000369 }
370
Ahmed Charlesfba06642014-03-05 10:27:34 +0000371 std::unique_ptr<MemoryBuffer> SB(Buf);
Chris Lattnera5425182008-04-01 06:05:21 +0000372 char *BufPtr = const_cast<char*>(SB->getBufferStart());
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000373
Rafael Espindola258a6052011-03-10 18:33:29 +0000374 size_t BytesLeft = MapSize;
Benjamin Kramere1effb02011-11-22 12:31:53 +0000375#ifndef HAVE_PREAD
Rafael Espindola258a6052011-03-10 18:33:29 +0000376 if (lseek(FD, Offset, SEEK_SET) == -1)
Rafael Espindolab4ad29b2014-06-13 02:36:09 +0000377 return std::error_code(errno, std::generic_category());
Benjamin Kramere1effb02011-11-22 12:31:53 +0000378#endif
Rafael Espindola258a6052011-03-10 18:33:29 +0000379
Chris Lattneree2d1f12007-04-29 06:58:52 +0000380 while (BytesLeft) {
Benjamin Kramere1effb02011-11-22 12:31:53 +0000381#ifdef HAVE_PREAD
382 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
383#else
Chris Lattneree2d1f12007-04-29 06:58:52 +0000384 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
Benjamin Kramere1effb02011-11-22 12:31:53 +0000385#endif
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000386 if (NumRead == -1) {
387 if (errno == EINTR)
388 continue;
389 // Error while reading.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000390 return std::error_code(errno, std::generic_category());
Chris Lattneree2d1f12007-04-29 06:58:52 +0000391 }
Argyrios Kyrtzidis3dc531e2012-03-13 20:18:42 +0000392 if (NumRead == 0) {
Argyrios Kyrtzidisbde59272014-05-06 00:51:45 +0000393 memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
Argyrios Kyrtzidis3dc531e2012-03-13 20:18:42 +0000394 break;
395 }
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000396 BytesLeft -= NumRead;
397 BufPtr += NumRead;
Chris Lattneree2d1f12007-04-29 06:58:52 +0000398 }
Benjamin Kramer10b0f3b2010-04-01 14:35:22 +0000399
Ahmed Charlesfba06642014-03-05 10:27:34 +0000400 Result.swap(SB);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000401 return std::error_code();
Chris Lattneree2d1f12007-04-29 06:58:52 +0000402}
403
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000404std::error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
405 std::unique_ptr<MemoryBuffer> &Result,
406 uint64_t FileSize,
407 bool RequiresNullTerminator,
408 bool IsVolatileSize) {
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000409 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000410 RequiresNullTerminator, IsVolatileSize);
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000411}
412
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000413std::error_code MemoryBuffer::getOpenFileSlice(
414 int FD, const char *Filename, std::unique_ptr<MemoryBuffer> &Result,
415 uint64_t MapSize, int64_t Offset, bool IsVolatileSize) {
Argyrios Kyrtzidis20a92ae2014-05-05 21:55:51 +0000416 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
Argyrios Kyrtzidis8c1eafc2014-05-06 01:03:52 +0000417 IsVolatileSize);
Ahmed Charlesfba06642014-03-05 10:27:34 +0000418}
419
Chris Lattneree2d1f12007-04-29 06:58:52 +0000420//===----------------------------------------------------------------------===//
421// MemoryBuffer::getSTDIN implementation.
422//===----------------------------------------------------------------------===//
423
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000424std::error_code MemoryBuffer::getSTDIN(std::unique_ptr<MemoryBuffer> &Result) {
Chris Lattneree2d1f12007-04-29 06:58:52 +0000425 // Read in all of the data from stdin, we cannot mmap stdin.
Daniel Dunbar124fc5e2009-11-10 00:43:58 +0000426 //
427 // FIXME: That isn't necessarily true, we should try to mmap stdin and
428 // fallback if it fails.
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000429 sys::ChangeStdinToBinary();
Benjamin Kramer58e6c2e2010-06-25 16:07:18 +0000430
Ahmed Charlesfba06642014-03-05 10:27:34 +0000431 return getMemoryBufferForStream(0, "<stdin>", Result);
432}