]>
git.proxmox.com Git - rustc.git/blob - src/llvm/lib/Support/MemoryBuffer.cpp
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/MathExtras.h"
19 #include "llvm/Support/Errno.h"
20 #include "llvm/Support/FileSystem.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"
30 #include <sys/types.h>
32 #if !defined(_MSC_VER) && !defined(__MINGW32__)
40 //===----------------------------------------------------------------------===//
41 // MemoryBuffer implementation itself.
42 //===----------------------------------------------------------------------===//
44 MemoryBuffer::~MemoryBuffer() { }
46 /// init - Initialize this MemoryBuffer as a reference to externally allocated
47 /// memory, memory that we know is already null terminated.
48 void MemoryBuffer::init(const char *BufStart
, const char *BufEnd
,
49 bool RequiresNullTerminator
) {
50 assert((!RequiresNullTerminator
|| BufEnd
[0] == 0) &&
51 "Buffer is not null terminated!");
52 BufferStart
= BufStart
;
56 //===----------------------------------------------------------------------===//
57 // MemoryBufferMem implementation.
58 //===----------------------------------------------------------------------===//
60 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
61 /// null-terminates it.
62 static void CopyStringRef(char *Memory
, StringRef Data
) {
63 memcpy(Memory
, Data
.data(), Data
.size());
64 Memory
[Data
.size()] = 0; // Null terminate string.
67 /// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it.
69 static T
*GetNamedBuffer(StringRef Buffer
, StringRef Name
,
70 bool RequiresNullTerminator
) {
71 char *Mem
= static_cast<char*>(operator new(sizeof(T
) + Name
.size() + 1));
72 CopyStringRef(Mem
+ sizeof(T
), Name
);
73 return new (Mem
) T(Buffer
, RequiresNullTerminator
);
77 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
78 class MemoryBufferMem
: public MemoryBuffer
{
80 MemoryBufferMem(StringRef InputData
, bool RequiresNullTerminator
) {
81 init(InputData
.begin(), InputData
.end(), RequiresNullTerminator
);
84 virtual const char *getBufferIdentifier() const LLVM_OVERRIDE
{
85 // The name is stored after the class itself.
86 return reinterpret_cast<const char*>(this + 1);
89 virtual BufferKind
getBufferKind() const LLVM_OVERRIDE
{
90 return MemoryBuffer_Malloc
;
95 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
96 /// that InputData must be a null terminated if RequiresNullTerminator is true!
97 MemoryBuffer
*MemoryBuffer::getMemBuffer(StringRef InputData
,
99 bool RequiresNullTerminator
) {
100 return GetNamedBuffer
<MemoryBufferMem
>(InputData
, BufferName
,
101 RequiresNullTerminator
);
104 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
105 /// copying the contents and taking ownership of it. This has no requirements
107 MemoryBuffer
*MemoryBuffer::getMemBufferCopy(StringRef InputData
,
108 StringRef BufferName
) {
109 MemoryBuffer
*Buf
= getNewUninitMemBuffer(InputData
.size(), BufferName
);
111 memcpy(const_cast<char*>(Buf
->getBufferStart()), InputData
.data(),
116 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
117 /// that is not initialized. Note that the caller should initialize the
118 /// memory allocated by this method. The memory is owned by the MemoryBuffer
120 MemoryBuffer
*MemoryBuffer::getNewUninitMemBuffer(size_t Size
,
121 StringRef BufferName
) {
122 // Allocate space for the MemoryBuffer, the data and the name. It is important
123 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
124 size_t AlignedStringLen
=
125 RoundUpToAlignment(sizeof(MemoryBufferMem
) + BufferName
.size() + 1,
126 sizeof(void*)); // TODO: Is sizeof(void*) enough?
127 size_t RealLen
= AlignedStringLen
+ Size
+ 1;
128 char *Mem
= static_cast<char*>(operator new(RealLen
, std::nothrow
));
131 // The name is stored after the class itself.
132 CopyStringRef(Mem
+ sizeof(MemoryBufferMem
), BufferName
);
134 // The buffer begins after the name and must be aligned.
135 char *Buf
= Mem
+ AlignedStringLen
;
136 Buf
[Size
] = 0; // Null terminate buffer.
138 return new (Mem
) MemoryBufferMem(StringRef(Buf
, Size
), true);
141 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
142 /// is completely initialized to zeros. Note that the caller should
143 /// initialize the memory allocated by this method. The memory is owned by
144 /// the MemoryBuffer object.
145 MemoryBuffer
*MemoryBuffer::getNewMemBuffer(size_t Size
, StringRef BufferName
) {
146 MemoryBuffer
*SB
= getNewUninitMemBuffer(Size
, BufferName
);
148 memset(const_cast<char*>(SB
->getBufferStart()), 0, Size
);
153 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
154 /// if the Filename is "-". If an error occurs, this returns null and fills
155 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
156 /// returns an empty buffer.
157 error_code
MemoryBuffer::getFileOrSTDIN(StringRef Filename
,
158 OwningPtr
<MemoryBuffer
> &result
,
161 return getSTDIN(result
);
162 return getFile(Filename
, result
, FileSize
);
165 error_code
MemoryBuffer::getFileOrSTDIN(const char *Filename
,
166 OwningPtr
<MemoryBuffer
> &result
,
168 if (strcmp(Filename
, "-") == 0)
169 return getSTDIN(result
);
170 return getFile(Filename
, result
, FileSize
);
173 //===----------------------------------------------------------------------===//
174 // MemoryBuffer::getFile implementation.
175 //===----------------------------------------------------------------------===//
178 /// MemoryBufferMMapFile - This represents a file that was mapped in with the
179 /// sys::Path::MapInFilePages method. When destroyed, it calls the
180 /// sys::Path::UnMapFilePages method.
181 class MemoryBufferMMapFile
: public MemoryBufferMem
{
183 MemoryBufferMMapFile(StringRef Buffer
, bool RequiresNullTerminator
)
184 : MemoryBufferMem(Buffer
, RequiresNullTerminator
) { }
186 ~MemoryBufferMMapFile() {
187 static int PageSize
= sys::Process::GetPageSize();
189 uintptr_t Start
= reinterpret_cast<uintptr_t>(getBufferStart());
190 size_t Size
= getBufferSize();
191 uintptr_t RealStart
= Start
& ~(PageSize
- 1);
192 size_t RealSize
= Size
+ (Start
- RealStart
);
194 sys::Path::UnMapFilePages(reinterpret_cast<const char*>(RealStart
),
198 virtual BufferKind
getBufferKind() const LLVM_OVERRIDE
{
199 return MemoryBuffer_MMap
;
204 error_code
MemoryBuffer::getFile(StringRef Filename
,
205 OwningPtr
<MemoryBuffer
> &result
,
207 bool RequiresNullTerminator
) {
208 // Ensure the path is null terminated.
209 SmallString
<256> PathBuf(Filename
.begin(), Filename
.end());
210 return MemoryBuffer::getFile(PathBuf
.c_str(), result
, FileSize
,
211 RequiresNullTerminator
);
214 error_code
MemoryBuffer::getFile(const char *Filename
,
215 OwningPtr
<MemoryBuffer
> &result
,
217 bool RequiresNullTerminator
) {
218 // First check that the "file" is not a directory
220 error_code err
= sys::fs::is_directory(Filename
, is_dir
);
224 return make_error_code(errc::is_a_directory
);
226 int OpenFlags
= O_RDONLY
;
228 OpenFlags
|= O_BINARY
; // Open input file in binary mode on win32.
230 int FD
= ::open(Filename
, OpenFlags
);
232 return error_code(errno
, posix_category());
234 error_code ret
= getOpenFile(FD
, Filename
, result
, FileSize
, FileSize
,
235 0, RequiresNullTerminator
);
240 static bool shouldUseMmap(int FD
,
244 bool RequiresNullTerminator
,
246 // We don't use mmap for small files because this can severely fragment our
248 if (MapSize
< 4096*4)
251 if (!RequiresNullTerminator
)
255 // If we don't know the file size, use fstat to find out. fstat on an open
256 // file descriptor is cheaper than stat on a random path.
257 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
258 // RequiresNullTerminator = false and MapSize != -1.
259 if (FileSize
== size_t(-1)) {
260 struct stat FileInfo
;
261 // TODO: This should use fstat64 when available.
262 if (fstat(FD
, &FileInfo
) == -1) {
263 return error_code(errno
, posix_category());
265 FileSize
= FileInfo
.st_size
;
268 // If we need a null terminator and the end of the map is inside the file,
269 // we cannot use mmap.
270 size_t End
= Offset
+ MapSize
;
271 assert(End
<= FileSize
);
275 // Don't try to map files that are exactly a multiple of the system page size
276 // if we need a null terminator.
277 if ((FileSize
& (PageSize
-1)) == 0)
283 error_code
MemoryBuffer::getOpenFile(int FD
, const char *Filename
,
284 OwningPtr
<MemoryBuffer
> &result
,
285 uint64_t FileSize
, uint64_t MapSize
,
287 bool RequiresNullTerminator
) {
288 static int PageSize
= sys::Process::GetPageSize();
290 // Default is to map the full file.
291 if (MapSize
== uint64_t(-1)) {
292 // If we don't know the file size, use fstat to find out. fstat on an open
293 // file descriptor is cheaper than stat on a random path.
294 if (FileSize
== uint64_t(-1)) {
295 struct stat FileInfo
;
296 // TODO: This should use fstat64 when available.
297 if (fstat(FD
, &FileInfo
) == -1) {
298 return error_code(errno
, posix_category());
300 FileSize
= FileInfo
.st_size
;
305 if (shouldUseMmap(FD
, FileSize
, MapSize
, Offset
, RequiresNullTerminator
,
307 off_t RealMapOffset
= Offset
& ~(PageSize
- 1);
308 off_t Delta
= Offset
- RealMapOffset
;
309 size_t RealMapSize
= MapSize
+ Delta
;
311 if (const char *Pages
= sys::Path::MapInFilePages(FD
,
314 result
.reset(GetNamedBuffer
<MemoryBufferMMapFile
>(
315 StringRef(Pages
+ Delta
, MapSize
), Filename
, RequiresNullTerminator
));
316 return error_code::success();
320 MemoryBuffer
*Buf
= MemoryBuffer::getNewUninitMemBuffer(MapSize
, Filename
);
322 // Failed to create a buffer. The only way it can fail is if
323 // new(std::nothrow) returns 0.
324 return make_error_code(errc::not_enough_memory
);
327 OwningPtr
<MemoryBuffer
> SB(Buf
);
328 char *BufPtr
= const_cast<char*>(SB
->getBufferStart());
330 size_t BytesLeft
= MapSize
;
332 if (lseek(FD
, Offset
, SEEK_SET
) == -1)
333 return error_code(errno
, posix_category());
338 ssize_t NumRead
= ::pread(FD
, BufPtr
, BytesLeft
, MapSize
-BytesLeft
+Offset
);
340 ssize_t NumRead
= ::read(FD
, BufPtr
, BytesLeft
);
345 // Error while reading.
346 return error_code(errno
, posix_category());
349 assert(0 && "We got inaccurate FileSize value or fstat reported an "
350 "invalid file size.");
351 *BufPtr
= '\0'; // null-terminate at the actual size.
354 BytesLeft
-= NumRead
;
359 return error_code::success();
362 //===----------------------------------------------------------------------===//
363 // MemoryBuffer::getSTDIN implementation.
364 //===----------------------------------------------------------------------===//
366 error_code
MemoryBuffer::getSTDIN(OwningPtr
<MemoryBuffer
> &result
) {
367 // Read in all of the data from stdin, we cannot mmap stdin.
369 // FIXME: That isn't necessarily true, we should try to mmap stdin and
370 // fallback if it fails.
371 sys::Program::ChangeStdinToBinary();
373 const ssize_t ChunkSize
= 4096*4;
374 SmallString
<ChunkSize
> Buffer
;
376 // Read into Buffer until we hit EOF.
378 Buffer
.reserve(Buffer
.size() + ChunkSize
);
379 ReadBytes
= read(0, Buffer
.end(), ChunkSize
);
380 if (ReadBytes
== -1) {
381 if (errno
== EINTR
) continue;
382 return error_code(errno
, posix_category());
384 Buffer
.set_size(Buffer
.size() + ReadBytes
);
385 } while (ReadBytes
!= 0);
387 result
.reset(getMemBufferCopy(Buffer
, "<stdin>"));
388 return error_code::success();