llvm.org GIT mirror llvm / 70c7e48
Split getOpenFile into getOpenFile and getOpenFileSlice. The main observation is that we never need both the filesize and the map size. When mapping a slice of a file, it doesn't make sense to request a null terminator and that would be the only case where the filesize would be used. There are other cleanups that should be done in this area: * A client should not have to pass the size (even an explicit -1) to say if it wants a null terminator or not, so we should probably swap the argument order. * The default should be to not require a null terminator. Very few clients require this, but many end up asking for it just because it is the default. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186984 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
8 changed file(s) with 48 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
7373 int64_t FileSize = -1,
7474 bool RequiresNullTerminator = true);
7575
76 /// getOpenFile - Given an already-open file descriptor, read the file and
77 /// return a MemoryBuffer.
76 // Get a MemoryBuffer of part of a file. Since this is in the middle of a
77 // file, the buffer is not null terminated.
78 static error_code getOpenFileSlice(int FD, const char *Filename,
79 OwningPtr &Result,
80 uint64_t MapSize, int64_t Offset);
81
82 /// Given an already-open file descriptor, read the file and return a
83 /// MemoryBuffer.
7884 static error_code getOpenFile(int FD, const char *Filename,
79 OwningPtr &result,
80 uint64_t FileSize = -1,
81 uint64_t MapSize = -1,
82 int64_t Offset = 0,
85 OwningPtr &Result,
86 uint64_t FileSize,
8387 bool RequiresNullTerminator = true);
8488
8589 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
247247 RequiresNullTerminator);
248248 }
249249
250 static error_code getOpenFileImpl(int FD, const char *Filename,
251 OwningPtr &Result,
252 uint64_t FileSize, uint64_t MapSize,
253 int64_t Offset, bool RequiresNullTerminator);
254
250255 error_code MemoryBuffer::getFile(const char *Filename,
251256 OwningPtr &result,
252257 int64_t FileSize,
256261 if (EC)
257262 return EC;
258263
259 error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize,
260 0, RequiresNullTerminator);
264 error_code ret = getOpenFileImpl(FD, Filename, result, FileSize, FileSize, 0,
265 RequiresNullTerminator);
261266 close(FD);
262267 return ret;
263268 }
304309 return true;
305310 }
306311
307 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
308 OwningPtr &result,
309 uint64_t FileSize, uint64_t MapSize,
310 int64_t Offset,
311 bool RequiresNullTerminator) {
312 static error_code getOpenFileImpl(int FD, const char *Filename,
313 OwningPtr &result,
314 uint64_t FileSize, uint64_t MapSize,
315 int64_t Offset, bool RequiresNullTerminator) {
312316 static int PageSize = sys::process::get_self()->page_size();
313317
314318 // Default is to map the full file.
385389 return error_code::success();
386390 }
387391
392 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
393 OwningPtr &Result,
394 uint64_t FileSize,
395 bool RequiresNullTerminator) {
396 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
397 RequiresNullTerminator);
398 }
399
400 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
401 OwningPtr &Result,
402 uint64_t MapSize, int64_t Offset) {
403 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
404 }
405
388406 //===----------------------------------------------------------------------===//
389407 // MemoryBuffer::getSTDIN implementation.
390408 //===----------------------------------------------------------------------===//
251251 if (file->offset) {
252252 offset = file->offset;
253253 }
254 if (error_code ec =
255 MemoryBuffer::getOpenFile(file->fd, file->name, buffer, -1,
256 file->filesize, offset, false)) {
254 if (error_code ec = MemoryBuffer::getOpenFileSlice(
255 file->fd, file->name, buffer, file->filesize, offset)) {
257256 (*message)(LDPL_ERROR, ec.message().c_str());
258257 return LDPS_ERR;
259258 }
769769 failIfError(sys::fs::status(FD, Status), FileName);
770770
771771 OwningPtr File;
772 failIfError(
773 MemoryBuffer::getOpenFile(FD, FileName, File, Status.getSize()),
774 FileName);
772 failIfError(MemoryBuffer::getOpenFile(FD, FileName, File,
773 Status.getSize(), false),
774 FileName);
775775
776776 StringRef Name = sys::path::filename(FileName);
777777 if (Name.size() < 16)
208208
209209 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
210210 size_t size, std::string &errMsg) {
211 return makeLTOModule(fd, path, size, size, 0, errMsg);
211 return makeLTOModule(fd, path, size, 0, errMsg);
212212 }
213213
214214 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
215 size_t file_size,
216215 size_t map_size,
217216 off_t offset,
218217 std::string &errMsg) {
219218 OwningPtr buffer;
220 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
221 map_size, offset, false)) {
219 if (error_code ec =
220 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
222221 errMsg = ec.message();
223222 return NULL;
224223 }
8181 static LTOModule *makeLTOModule(int fd, const char *path,
8282 size_t size, std::string &errMsg);
8383 static LTOModule *makeLTOModule(int fd, const char *path,
84 size_t file_size,
8584 size_t map_size,
8685 off_t offset,
8786 std::string& errMsg);
7777 size_t file_size,
7878 size_t map_size,
7979 off_t offset) {
80 return LTOModule::makeLTOModule(fd, path, file_size, map_size,
81 offset, sLastErrorString);
80 return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
8281 }
8382
8483 /// lto_module_create_from_memory - Loads an object file from memory. Returns
112112 }
113113
114114 OwningBuffer Buf;
115 error_code EC = MemoryBuffer::getOpenFile(TestFD,
116 TestPath.c_str(),
117 Buf,
118 40000, // Size
119 -1,
120 8000, // Offset
121 false);
115 error_code EC = MemoryBuffer::getOpenFileSlice(TestFD, TestPath.c_str(), Buf,
116 40000, // Size
117 8000 // Offset
118 );
122119 EXPECT_FALSE(EC);
123120
124121 StringRef BufData = Buf->getBuffer();