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 to allow files to be read with
39 #define S_ISREG(x) (1)
42 #define S_ISBLK(x) (0)
48 //===----------------------------------------------------------------------===//
49 // MemoryBuffer implementation itself.
50 //===----------------------------------------------------------------------===//
52 MemoryBuffer::~MemoryBuffer() { }
54 /// init - Initialize this MemoryBuffer as a reference to externally allocated
55 /// memory, memory that we know is already null terminated.
56 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
57 bool RequiresNullTerminator) {
58 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
59 "Buffer is not null terminated!");
60 BufferStart = BufStart;
64 //===----------------------------------------------------------------------===//
65 // MemoryBufferMem implementation.
66 //===----------------------------------------------------------------------===//
68 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
69 /// null-terminates it.
70 static void CopyStringRef(char *Memory, StringRef Data) {
71 memcpy(Memory, Data.data(), Data.size());
72 Memory[Data.size()] = 0; // Null terminate string.
75 struct NamedBufferAlloc {
77 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 LLVM_OVERRIDE {
95 // The name is stored after the class itself.
96 return reinterpret_cast<const char*>(this + 1);
99 virtual BufferKind getBufferKind() const LLVM_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 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));
141 // The name is stored after the class itself.
142 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
144 // The buffer begins after the name and must be aligned.
145 char *Buf = Mem + AlignedStringLen;
146 Buf[Size] = 0; // Null terminate buffer.
148 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
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.
155 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
156 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
158 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
163 /// 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.
167 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
168 OwningPtr<MemoryBuffer> &result,
171 return getSTDIN(result);
172 return getFile(Filename, result, FileSize);
175 error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
176 OwningPtr<MemoryBuffer> &result,
178 if (strcmp(Filename, "-") == 0)
179 return getSTDIN(result);
180 return getFile(Filename, result, FileSize);
183 //===----------------------------------------------------------------------===//
184 // MemoryBuffer::getFile implementation.
185 //===----------------------------------------------------------------------===//
188 /// \brief Memorry maps a file descriptor using sys::fs::mapped_file_region.
190 /// This handles converting the offset into a legal offset on the platform.
191 class MemoryBufferMMapFile : public MemoryBuffer {
192 sys::fs::mapped_file_region MFR;
194 static uint64_t getLegalMapOffset(uint64_t Offset) {
195 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
198 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
199 return Len + (Offset - getLegalMapOffset(Offset));
202 const char *getStart(uint64_t Len, uint64_t Offset) {
203 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
207 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
208 uint64_t Offset, error_code EC)
209 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
210 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
212 const char *Start = getStart(Len, Offset);
213 init(Start, Start + Len, RequiresNullTerminator);
217 virtual const char *getBufferIdentifier() const LLVM_OVERRIDE {
218 // The name is stored after the class itself.
219 return reinterpret_cast<const char *>(this + 1);
222 virtual BufferKind getBufferKind() const LLVM_OVERRIDE {
223 return MemoryBuffer_MMap;
228 static error_code getMemoryBufferForStream(int FD,
229 StringRef BufferName,
230 OwningPtr<MemoryBuffer> &result) {
231 const ssize_t ChunkSize = 4096*4;
232 SmallString<ChunkSize> Buffer;
234 // Read into Buffer until we hit EOF.
236 Buffer.reserve(Buffer.size() + ChunkSize);
237 ReadBytes = read(FD, Buffer.end(), ChunkSize);
238 if (ReadBytes == -1) {
239 if (errno == EINTR) continue;
240 return error_code(errno, posix_category());
242 Buffer.set_size(Buffer.size() + ReadBytes);
243 } while (ReadBytes != 0);
245 result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
246 return error_code::success();
249 error_code MemoryBuffer::getFile(StringRef Filename,
250 OwningPtr<MemoryBuffer> &result,
252 bool RequiresNullTerminator) {
253 // Ensure the path is null terminated.
254 SmallString<256> PathBuf(Filename.begin(), Filename.end());
255 return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize,
256 RequiresNullTerminator);
259 error_code MemoryBuffer::getFile(const char *Filename,
260 OwningPtr<MemoryBuffer> &result,
262 bool RequiresNullTerminator) {
263 // FIXME: Review if this check is unnecessary on windows as well.
265 // First check that the "file" is not a directory
267 error_code err = sys::fs::is_directory(Filename, is_dir);
271 return make_error_code(errc::is_a_directory);
274 int OpenFlags = O_RDONLY;
276 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
278 int FD = ::open(Filename, OpenFlags);
280 return error_code(errno, posix_category());
282 error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize,
283 0, RequiresNullTerminator);
288 static bool shouldUseMmap(int FD,
292 bool RequiresNullTerminator,
294 // We don't use mmap for small files because this can severely fragment our
296 if (MapSize < 4096*4)
299 if (!RequiresNullTerminator)
303 // If we don't know the file size, use fstat to find out. fstat on an open
304 // file descriptor is cheaper than stat on a random path.
305 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
306 // RequiresNullTerminator = false and MapSize != -1.
307 if (FileSize == size_t(-1)) {
308 struct stat FileInfo;
309 // TODO: This should use fstat64 when available.
310 if (fstat(FD, &FileInfo) == -1) {
311 return error_code(errno, posix_category());
313 FileSize = FileInfo.st_size;
316 // If we need a null terminator and the end of the map is inside the file,
317 // we cannot use mmap.
318 size_t End = Offset + MapSize;
319 assert(End <= FileSize);
323 // Don't try to map files that are exactly a multiple of the system page size
324 // if we need a null terminator.
325 if ((FileSize & (PageSize -1)) == 0)
331 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
332 OwningPtr<MemoryBuffer> &result,
333 uint64_t FileSize, uint64_t MapSize,
335 bool RequiresNullTerminator) {
336 static int PageSize = sys::process::get_self()->page_size();
338 // Default is to map the full file.
339 if (MapSize == uint64_t(-1)) {
340 // If we don't know the file size, use fstat to find out. fstat on an open
341 // file descriptor is cheaper than stat on a random path.
342 if (FileSize == uint64_t(-1)) {
343 struct stat FileInfo;
344 // TODO: This should use fstat64 when available.
345 if (fstat(FD, &FileInfo) == -1) {
346 return error_code(errno, posix_category());
349 // If this not a file or a block device (e.g. it's a named pipe
350 // or character device), we can't trust the size. Create the memory
351 // buffer by copying off the stream.
352 if (!S_ISREG(FileInfo.st_mode) && !S_ISBLK(FileInfo.st_mode)) {
353 return getMemoryBufferForStream(FD, Filename, result);
356 FileSize = FileInfo.st_size;
361 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
364 result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
365 RequiresNullTerminator, FD, MapSize, Offset, EC));
367 return error_code::success();
370 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
372 // Failed to create a buffer. The only way it can fail is if
373 // new(std::nothrow) returns 0.
374 return make_error_code(errc::not_enough_memory);
377 OwningPtr<MemoryBuffer> SB(Buf);
378 char *BufPtr = const_cast<char*>(SB->getBufferStart());
380 size_t BytesLeft = MapSize;
382 if (lseek(FD, Offset, SEEK_SET) == -1)
383 return error_code(errno, posix_category());
388 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
390 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
395 // Error while reading.
396 return error_code(errno, posix_category());
399 assert(0 && "We got inaccurate FileSize value or fstat reported an "
400 "invalid file size.");
401 *BufPtr = '\0'; // null-terminate at the actual size.
404 BytesLeft -= NumRead;
409 return error_code::success();
412 //===----------------------------------------------------------------------===//
413 // MemoryBuffer::getSTDIN implementation.
414 //===----------------------------------------------------------------------===//
416 error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
417 // Read in all of the data from stdin, we cannot mmap stdin.
419 // FIXME: That isn't necessarily true, we should try to mmap stdin and
420 // fallback if it fails.
421 sys::Program::ChangeStdinToBinary();
423 return getMemoryBufferForStream(0, "<stdin>", result);