llvm.org GIT mirror llvm / 333ffd4
Add a new memorybuffer class, to unify all the file reading code in the system git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36553 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 322 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
14 #define LLVM_SUPPORT_MEMORYBUFFER_H
15
16 #include "llvm/Support/DataTypes.h"
17
18 namespace llvm {
19
20 /// MemoryBuffer - This interface provides simple read-only access to a block
21 /// of memory, and provides simple methods for reading files and standard input
22 /// into a memory buffer. In addition to basic access to the characters in the
23 /// file, this interface guarantees you can read one character past the end of
24 /// the file, and that this character will read as '\0'.
25 class MemoryBuffer {
26 const char *BufferStart; // Start of the buffer.
27 const char *BufferEnd; // End of the buffer.
28
29 /// MustDeleteBuffer - True if we allocated this buffer. If so, the
30 /// destructor must know the delete[] it.
31 bool MustDeleteBuffer;
32 protected:
33 MemoryBuffer() : MustDeleteBuffer(false) {}
34 void init(const char *BufStart, const char *BufEnd);
35 void initCopyOf(const char *BufStart, const char *BufEnd);
36 public:
37 virtual ~MemoryBuffer();
38
39 const char *getBufferStart() const { return BufferStart; }
40 const char *getBufferEnd() const { return BufferEnd; }
41 unsigned getBufferSize() const { return BufferEnd-BufferStart; }
42
43 /// getBufferIdentifier - Return an identifier for this buffer, typically the
44 /// filename it was read from.
45 virtual const char *getBufferIdentifier() const {
46 return "Unknown buffer";
47 }
48
49 /// getFile - Open the specified file as a MemoryBuffer, returning a new
50 /// MemoryBuffer if successful, otherwise returning null. If FileSize is
51 /// specified, this means that the client knows that the file exists and that
52 /// it has the specified size.
53 static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
54 int64_t FileSize = -1);
55
56 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
57 /// that EndPtr[0] must be a null byte and be accessible!
58 static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
59 const char *BufferName = "");
60
61 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
62 /// is completely initialized to zeros. Note that the caller should
63 /// initialize the memory allocated by this method. The memory is owned by
64 /// the MemoryBuffer object.
65 static MemoryBuffer *getNewMemBuffer(unsigned Size,
66 const char *BufferName = "");
67
68 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
69 /// that is not initialized. Note that the caller should initialize the
70 /// memory allocated by this method. The memory is owned by the MemoryBuffer
71 /// object.
72 static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
73 const char *BufferName = "");
74
75 /// getSTDIN - Read all of stdin into a file buffer, and return it. This
76 /// fails if stdin is empty.
77 static MemoryBuffer *getSTDIN();
78 };
79
80 } // end namespace llvm
81
82 #endif
0 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chris Lattner and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/System/MappedFile.h"
15 #include "llvm/System/Process.h"
16 #include
17 #include
18 #include
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 // MemoryBuffer implementation itself.
23 //===----------------------------------------------------------------------===//
24
25 MemoryBuffer::~MemoryBuffer() {
26 if (MustDeleteBuffer)
27 delete [] BufferStart;
28 }
29
30 /// initCopyOf - Initialize this source buffer with a copy of the specified
31 /// memory range. We make the copy so that we can null terminate it
32 /// successfully.
33 void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
34 size_t Size = BufEnd-BufStart;
35 BufferStart = new char[Size+1];
36 BufferEnd = BufferStart+Size;
37 memcpy(const_cast(BufferStart), BufStart, Size);
38 *const_cast(BufferEnd) = 0; // Null terminate buffer.
39 MustDeleteBuffer = false;
40 }
41
42 /// init - Initialize this MemoryBuffer as a reference to externally allocated
43 /// memory, memory that we know is already null terminated.
44 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
45 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
46 BufferStart = BufStart;
47 BufferEnd = BufEnd;
48 MustDeleteBuffer = false;
49 }
50
51 //===----------------------------------------------------------------------===//
52 // MemoryBufferMem implementation.
53 //===----------------------------------------------------------------------===//
54
55 namespace {
56 class MemoryBufferMem : public MemoryBuffer {
57 std::string FileID;
58 public:
59 MemoryBufferMem(const char *Start, const char *End, const char *FID)
60 : FileID(FID) {
61 init(Start, End);
62 }
63
64 virtual const char *getBufferIdentifier() const {
65 return FileID.c_str();
66 }
67 };
68 }
69
70 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
71 /// that EndPtr[0] must be a null byte and be accessible!
72 MemoryBuffer *MemoryBuffer::getMemBuffer(const char *StartPtr,
73 const char *EndPtr,
74 const char *BufferName) {
75 return new MemoryBufferMem(StartPtr, EndPtr, BufferName);
76 }
77
78 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
79 /// that is completely initialized to zeros. Note that the caller should
80 /// initialize the memory allocated by this method. The memory is owned by
81 /// the MemoryBuffer object.
82 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(unsigned Size,
83 const char *BufferName) {
84 char *Buf = new char[Size+1];
85 Buf[Size] = 0;
86 MemoryBufferMem *SB = new MemoryBufferMem(Buf, Buf+Size, BufferName);
87 // The memory for this buffer is owned by the MemoryBuffer.
88 SB->MustDeleteBuffer = true;
89 return SB;
90 }
91
92 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
93 /// is completely initialized to zeros. Note that the caller should
94 /// initialize the memory allocated by this method. The memory is owned by
95 /// the MemoryBuffer object.
96 MemoryBuffer *MemoryBuffer::getNewMemBuffer(unsigned Size,
97 const char *BufferName) {
98 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
99 memset(const_cast(SB->getBufferStart()), 0, Size+1);
100 return SB;
101 }
102
103
104 //===----------------------------------------------------------------------===//
105 // MemoryBufferMMapFile implementation.
106 //===----------------------------------------------------------------------===//
107
108 namespace {
109 class MemoryBufferMMapFile : public MemoryBuffer {
110 sys::MappedFile File;
111 public:
112 MemoryBufferMMapFile(const sys::Path &Filename);
113
114 virtual const char *getBufferIdentifier() const {
115 return File.path().c_str();
116 }
117
118 ~MemoryBufferMMapFile();
119 };
120 }
121
122 MemoryBufferMMapFile::MemoryBufferMMapFile(const sys::Path &Filename) {
123 // FIXME: This does an extra stat syscall to figure out the size, but we
124 // already know the size!
125 bool Failure = File.open(Filename);
126 Failure = Failure; // Silence warning in no-asserts mode.
127 assert(!Failure && "Can't open file??");
128
129 File.map();
130
131 size_t Size = File.size();
132
133 static unsigned PageSize = sys::Process::GetPageSize();
134 assert(((PageSize & (PageSize-1)) == 0) && PageSize &&
135 "Page size is not a power of 2!");
136
137 // If this file is not an exact multiple of the system page size (common
138 // case), then the OS has zero terminated the buffer for us.
139 if ((Size & (PageSize-1))) {
140 init(File.charBase(), File.charBase()+Size);
141 } else {
142 // Otherwise, we allocate a new memory buffer and copy the data over
143 initCopyOf(File.charBase(), File.charBase()+Size);
144
145 // No need to keep the file mapped any longer.
146 File.unmap();
147 }
148 }
149
150 MemoryBufferMMapFile::~MemoryBufferMMapFile() {
151 File.unmap();
152 }
153
154 //===----------------------------------------------------------------------===//
155 // MemoryBuffer::getFile implementation.
156 //===----------------------------------------------------------------------===//
157
158 MemoryBuffer *MemoryBuffer::getFile(const char *FilenameStart, unsigned FnSize,
159 int64_t FileSize) {
160 sys::PathWithStatus P(FilenameStart, FnSize);
161 #if 1
162 return new MemoryBufferMMapFile(P);
163 #else
164 // FIXME: We need an efficient and portable method to open a file and then use
165 // 'read' to copy the bits out. The unix implementation is below. This is
166 // an important optimization for clients that want to open large numbers of
167 // small files (using mmap on everything can easily exhaust address space!).
168
169 // If the user didn't specify a filesize, do a stat to find it.
170 if (FileSize == -1) {
171 const sys::FileStatus *FS = P.getFileStatus();
172 if (FS == 0) return 0; // Error stat'ing file.
173
174 FileSize = FS->fileSize;
175 }
176
177 // If the file is larger than some threshold, use mmap, otherwise use 'read'.
178 if (FileSize >= 4096*4)
179 return new MemoryBufferMMapFile(P);
180
181 MemoryBuffer *SB = getNewUninitMemBuffer(FileSize, FilenameStart);
182 char *BufPtr = const_cast(SB->getBufferStart());
183
184 int FD = ::open(FilenameStart, O_RDONLY);
185 if (FD == -1) {
186 delete SB;
187 return 0;
188 }
189
190 unsigned BytesLeft = FileSize;
191 while (BytesLeft) {
192 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
193 if (NumRead != -1) {
194 BytesLeft -= NumRead;
195 BufPtr += NumRead;
196 } else if (errno == EINTR) {
197 // try again
198 } else {
199 // error reading.
200 close(FD);
201 delete SB;
202 return 0;
203 }
204 }
205 close(FD);
206
207 return SB;
208 #endif
209 }
210
211
212 //===----------------------------------------------------------------------===//
213 // MemoryBuffer::getSTDIN implementation.
214 //===----------------------------------------------------------------------===//
215
216 namespace {
217 class STDINBufferFile : public MemoryBuffer {
218 public:
219 virtual const char *getBufferIdentifier() const {
220 return "";
221 }
222 };
223 }
224
225 MemoryBuffer *MemoryBuffer::getSTDIN() {
226 char Buffer[4096*4];
227
228 std::vector FileData;
229
230 // Read in all of the data from stdin, we cannot mmap stdin.
231 while (size_t ReadBytes = fread(Buffer, 1, 4096*4, stdin))
232 FileData.insert(FileData.end(), Buffer, Buffer+ReadBytes);
233
234 size_t Size = FileData.size();
235 MemoryBuffer *B = new STDINBufferFile();
236 B->initCopyOf(&FileData[0], &FileData[Size]);
237 return B;
238 }