llvm.org GIT mirror llvm / 10222d2
[Support/MemoryBuffer] Rename IsVolatile -> IsVolatileSize and add a comment about the use case for the new parameter. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208026 91177308-0d34-0410-b5e6-96231b3b80d8 Argyrios Kyrtzidis 6 years ago
2 changed file(s) with 38 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
6767 /// specified, this means that the client knows that the file exists and that
6868 /// it has the specified size.
6969 ///
70 /// \param IsVolatile true indicates that the file may be changing often.
70 /// \param IsVolatileSize Set to true to indicate that the file size may be
71 /// changing, e.g. when libclang tries to parse while the user is
72 /// editing/updating the file.
7173 static error_code getFile(Twine Filename, OwningPtr &Result,
7274 int64_t FileSize = -1,
7375 bool RequiresNullTerminator = true,
74 bool IsVolatile = false);
76 bool IsVolatileSize = false);
7577 static error_code getFile(Twine Filename,
7678 std::unique_ptr &Result,
7779 int64_t FileSize = -1,
7880 bool RequiresNullTerminator = true,
79 bool IsVolatile = false);
81 bool IsVolatileSize = false);
8082
8183 /// Given an already-open file descriptor, map some slice of it into a
8284 /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
8385 /// Since this is in the middle of a file, the buffer is not null terminated.
8486 ///
85 /// \param IsVolatile true indicates that the file may be changing often.
87 /// \param IsVolatileSize Set to true to indicate that the file size may be
88 /// changing, e.g. when libclang tries to parse while the user is
89 /// editing/updating the file.
8690 static error_code getOpenFileSlice(int FD, const char *Filename,
8791 OwningPtr &Result,
8892 uint64_t MapSize, int64_t Offset,
89 bool IsVolatile = false);
93 bool IsVolatileSize = false);
9094 static error_code getOpenFileSlice(int FD, const char *Filename,
9195 std::unique_ptr &Result,
9296 uint64_t MapSize, int64_t Offset,
93 bool IsVolatile = false);
97 bool IsVolatileSize = false);
9498
9599 /// Given an already-open file descriptor, read the file and return a
96100 /// MemoryBuffer.
97101 ///
98 /// \param IsVolatile true indicates that the file may be changing often.
102 /// \param IsVolatileSize Set to true to indicate that the file size may be
103 /// changing, e.g. when libclang tries to parse while the user is
104 /// editing/updating the file.
99105 static error_code getOpenFile(int FD, const char *Filename,
100106 OwningPtr &Result,
101107 uint64_t FileSize,
102108 bool RequiresNullTerminator = true,
103 bool IsVolatile = false);
109 bool IsVolatileSize = false);
104110 static error_code getOpenFile(int FD, const char *Filename,
105111 std::unique_ptr &Result,
106112 uint64_t FileSize,
107113 bool RequiresNullTerminator = true,
108 bool IsVolatile = false);
114 bool IsVolatileSize = false);
109115
110116 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
111117 /// that InputData must be null terminated if RequiresNullTerminator is true.
252252 std::unique_ptr &Result,
253253 int64_t FileSize,
254254 bool RequiresNullTerminator,
255 bool IsVolatile);
255 bool IsVolatileSize);
256256
257257 error_code MemoryBuffer::getFile(Twine Filename,
258258 std::unique_ptr &Result,
259259 int64_t FileSize,
260260 bool RequiresNullTerminator,
261 bool IsVolatile) {
261 bool IsVolatileSize) {
262262 // Ensure the path is null terminated.
263263 SmallString<256> PathBuf;
264264 StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
265265 return getFileAux(NullTerminatedName.data(), Result, FileSize,
266 RequiresNullTerminator, IsVolatile);
266 RequiresNullTerminator, IsVolatileSize);
267267 }
268268
269269 error_code MemoryBuffer::getFile(Twine Filename,
270270 OwningPtr &Result,
271271 int64_t FileSize,
272272 bool RequiresNullTerminator,
273 bool IsVolatile) {
273 bool IsVolatileSize) {
274274 std::unique_ptr MB;
275275 error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator,
276 IsVolatile);
276 IsVolatileSize);
277277 Result = std::move(MB);
278278 return ec;
279279 }
282282 std::unique_ptr &Result,
283283 uint64_t FileSize, uint64_t MapSize,
284284 int64_t Offset, bool RequiresNullTerminator,
285 bool IsVolatile);
285 bool IsVolatileSize);
286286
287287 static error_code getFileAux(const char *Filename,
288288 std::unique_ptr &Result, int64_t FileSize,
289289 bool RequiresNullTerminator,
290 bool IsVolatile) {
290 bool IsVolatileSize) {
291291 int FD;
292292 error_code EC = sys::fs::openFileForRead(Filename, FD);
293293 if (EC)
294294 return EC;
295295
296296 error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
297 RequiresNullTerminator, IsVolatile);
297 RequiresNullTerminator, IsVolatileSize);
298298 close(FD);
299299 return ret;
300300 }
305305 off_t Offset,
306306 bool RequiresNullTerminator,
307307 int PageSize,
308 bool IsVolatile) {
308 bool IsVolatileSize) {
309309 // mmap may leave the buffer without null terminator if the file size changed
310310 // by the time the last page is mapped in, so avoid it if the file size is
311311 // likely to change.
312 if (IsVolatile)
312 if (IsVolatileSize)
313313 return false;
314314
315315 // We don't use mmap for small files because this can severely fragment our
361361 std::unique_ptr &Result,
362362 uint64_t FileSize, uint64_t MapSize,
363363 int64_t Offset, bool RequiresNullTerminator,
364 bool IsVolatile) {
364 bool IsVolatileSize) {
365365 static int PageSize = sys::process::get_self()->page_size();
366366
367367 // Default is to map the full file.
388388 }
389389
390390 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
391 PageSize, IsVolatile)) {
391 PageSize, IsVolatileSize)) {
392392 error_code EC;
393393 Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
394394 RequiresNullTerminator, FD, MapSize, Offset, EC));
425425 return error_code(errno, posix_category());
426426 }
427427 if (NumRead == 0) {
428 assert(IsVolatile && "We got inaccurate FileSize value or fstat reported "
429 "an invalid file size.");
428 assert(IsVolatileSize &&
429 "We got inaccurate FileSize value or fstat reported an invalid "
430 "file size.");
430431 memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
431432 break;
432433 }
442443 std::unique_ptr &Result,
443444 uint64_t FileSize,
444445 bool RequiresNullTerminator,
445 bool IsVolatile) {
446 bool IsVolatileSize) {
446447 return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
447 RequiresNullTerminator, IsVolatile);
448 RequiresNullTerminator, IsVolatileSize);
448449 }
449450
450451 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
451452 OwningPtr &Result,
452453 uint64_t FileSize,
453454 bool RequiresNullTerminator,
454 bool IsVolatile) {
455 bool IsVolatileSize) {
455456 std::unique_ptr MB;
456457 error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
457 RequiresNullTerminator, IsVolatile);
458 RequiresNullTerminator, IsVolatileSize);
458459 Result = std::move(MB);
459460 return ec;
460461 }
462463 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
463464 std::unique_ptr &Result,
464465 uint64_t MapSize, int64_t Offset,
465 bool IsVolatile) {
466 bool IsVolatileSize) {
466467 return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
467 IsVolatile);
468 IsVolatileSize);
468469 }
469470
470471 error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
471472 OwningPtr &Result,
472473 uint64_t MapSize, int64_t Offset,
473 bool IsVolatile) {
474 bool IsVolatileSize) {
474475 std::unique_ptr MB;
475476 error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false,
476 IsVolatile);
477 IsVolatileSize);
477478 Result = std::move(MB);
478479 return ec;
479480 }