blob: ffbb4beccfab891fd1f76982ee23ade58a87913a [file] [log] [blame]
Chris Lattneree2d1f12007-04-29 06:58:52 +00001//===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MemoryBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/MemoryBuffer.h"
15#include "llvm/System/MappedFile.h"
16#include "llvm/System/Process.h"
Jeff Cohen50b2d2c62007-04-29 14:21:44 +000017#include <cassert>
Chris Lattneree2d1f12007-04-29 06:58:52 +000018#include <cstdio>
19#include <cstring>
20#include <cerrno>
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// MemoryBuffer implementation itself.
25//===----------------------------------------------------------------------===//
26
27MemoryBuffer::~MemoryBuffer() {
28 if (MustDeleteBuffer)
29 delete [] BufferStart;
30}
31
32/// initCopyOf - Initialize this source buffer with a copy of the specified
33/// memory range. We make the copy so that we can null terminate it
34/// successfully.
35void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
36 size_t Size = BufEnd-BufStart;
37 BufferStart = new char[Size+1];
38 BufferEnd = BufferStart+Size;
39 memcpy(const_cast<char*>(BufferStart), BufStart, Size);
40 *const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
41 MustDeleteBuffer = false;
42}
43
44/// init - Initialize this MemoryBuffer as a reference to externally allocated
45/// memory, memory that we know is already null terminated.
46void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
47 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
48 BufferStart = BufStart;
49 BufferEnd = BufEnd;
50 MustDeleteBuffer = false;
51}
52
53//===----------------------------------------------------------------------===//
54// MemoryBufferMem implementation.
55//===----------------------------------------------------------------------===//
56
57namespace {
58class MemoryBufferMem : public MemoryBuffer {
59 std::string FileID;
60public:
61 MemoryBufferMem(const char *Start, const char *End, const char *FID)
62 : FileID(FID) {
63 init(Start, End);
64 }
65
66 virtual const char *getBufferIdentifier() const {
67 return FileID.c_str();
68 }
69};
70}
71
72/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
73/// that EndPtr[0] must be a null byte and be accessible!
74MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
75 const char *EndPtr,
76 const char *BufferName) {
77 return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
78}
79
80/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
81/// that is completely initialized to zeros. Note that the caller should
82/// initialize the memory allocated by this method. The memory is owned by
83/// the MemoryBuffer object.
84MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size,
85 const char *BufferName) {
86 char *Buf = new char[Size+1];
87 Buf[Size] = 0;
88 MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
89 // The memory for this buffer is owned by the MemoryBuffer.
90 SB->MustDeleteBuffer = true;
91 return SB;
92}
93
94/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
95/// is completely initialized to zeros. Note that the caller should
96/// initialize the memory allocated by this method. The memory is owned by
97/// the MemoryBuffer object.
98MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
99 const char *BufferName) {
100 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
101 memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1);
102 return SB;
103}
104
105
106//===----------------------------------------------------------------------===//
107// MemoryBufferMMapFile implementation.
108//===----------------------------------------------------------------------===//
109
110namespace {
111class MemoryBufferMMapFile : public MemoryBuffer {
112 sys::MappedFile File;
113public:
Chris Lattner5db36d32007-05-06 07:24:46 +0000114 MemoryBufferMMapFile() {}
115
116 bool open(const sys::Path &Filename);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000117
118 virtual const char *getBufferIdentifier() const {
119 return File.path().c_str();
120 }
121
122 ~MemoryBufferMMapFile();
123};
124}
125
Chris Lattner5db36d32007-05-06 07:24:46 +0000126bool MemoryBufferMMapFile::open(const sys::Path &Filename) {
Chris Lattneree2d1f12007-04-29 06:58:52 +0000127 // FIXME: This does an extra stat syscall to figure out the size, but we
128 // already know the size!
129 bool Failure = File.open(Filename);
Chris Lattner5db36d32007-05-06 07:24:46 +0000130 if (Failure) return true;
Chris Lattneree2d1f12007-04-29 06:58:52 +0000131
132 File.map();
133
134 size_t Size = File.size();
135
136 static unsigned PageSize = sys::Process::GetPageSize();
137 assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
138 "Page size is not a power of 2!");
139
140 // If this file is not an exact multiple of the system page size (common
141 // case), then the OS has zero terminated the buffer for us.
142 if ((Size & (PageSize-1))) {
143 init(File.charBase(), File.charBase()+Size);
144 } else {
145 // Otherwise, we allocate a new memory buffer and copy the data over
146 initCopyOf(File.charBase(), File.charBase()+Size);
147
148 // No need to keep the file mapped any longer.
149 File.unmap();
150 }
Chris Lattner5db36d32007-05-06 07:24:46 +0000151 return false;
Chris Lattneree2d1f12007-04-29 06:58:52 +0000152}
153
154MemoryBufferMMapFile::~MemoryBufferMMapFile() {
Chris Lattner5db36d32007-05-06 07:24:46 +0000155 if (File.isMapped())
156 File.unmap();
Chris Lattneree2d1f12007-04-29 06:58:52 +0000157}
158
159//===----------------------------------------------------------------------===//
160// MemoryBuffer::getFile implementation.
161//===----------------------------------------------------------------------===//
162
163MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
164 int64_t FileSize) {
Jeff Cohen990a58f2007-04-29 14:43:31 +0000165 sys::PathWithStatus P(FilenameStart, FnSize);
Chris Lattneree2d1f12007-04-29 06:58:52 +0000166#if 1
Chris Lattner5db36d32007-05-06 07:24:46 +0000167 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
168 if (!M->open(P))
169 return M;
170 delete M;
171 return 0;
Chris Lattneree2d1f12007-04-29 06:58:52 +0000172#else
173 // FIXME: We need an efficient and portable method to open a file and then use
174 // 'read' to copy the bits out. The unix implementation is below. This is
175 // an important optimization for clients that want to open large numbers of
176 // small files (using mmap on everything can easily exhaust address space!).
177
178 // If the user didn't specify a filesize, do a stat to find it.
179 if (FileSize == -1) {
180 const sys::FileStatus *FS = P.getFileStatus();
181 if (FS == 0) return 0; // Error stat'ing file.
182
183 FileSize = FS->fileSize;
184 }
185
186 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
Chris Lattner5db36d32007-05-06 07:24:46 +0000187 if (FileSize >= 4096*4) {
188 MemoryBufferMMapFile *M = new MemoryBufferMMapFile();
189 if (!M->open(P))
190 return M;
191 delete M;
192 return 0;
193 }
Chris Lattneree2d1f12007-04-29 06:58:52 +0000194
195 MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
196 char *BufPtr = const_cast<char*>(SB->getBufferStart());
197
198 int FD = ::open(FilenameStart, O_RDONLY);
199 if (FD == -1) {
200 delete SB;
201 return 0;
202 }
203
204 unsigned BytesLeft = FileSize;
205 while (BytesLeft) {
206 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
207 if (NumRead != -1) {
208 BytesLeft -= NumRead;
209 BufPtr += NumRead;
210 } else if (errno == EINTR) {
211 // try again
212 } else {
213 // error reading.
214 close(FD);
215 delete SB;
216 return 0;
217 }
218 }
219 close(FD);
220
221 return SB;
222#endif
223}
224
225
226//===----------------------------------------------------------------------===//
227// MemoryBuffer::getSTDIN implementation.
228//===----------------------------------------------------------------------===//
229
230namespace {
231class STDINBufferFile : public MemoryBuffer {
232public:
233 virtual const char *getBufferIdentifier() const {
234 return "<stdin>";
235 }
236};
237}
238
239MemoryBuffer *MemoryBuffer::getSTDIN() {
240 char Buffer[4096*4];
241
242 std::vector<char> FileData;
243
244 // Read in all of the data from stdin, we cannot mmap stdin.
245 while (size_t ReadBytes = fread(Buffer, 1, 4096*4, stdin))
246 FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
247
248 size_t Size = FileData.size();
249 MemoryBuffer *B = new STDINBufferFile();
250 B->initCopyOf(&FileData[0], &FileData[Size]);
251 return B;
252}