diff options
author | Derek Schuff <dschuff@chromium.org> | 2013-01-09 16:55:43 -0800 |
---|---|---|
committer | Derek Schuff <dschuff@chromium.org> | 2013-01-11 13:47:37 -0800 |
commit | b770d0e0636a4b5ad61b1ca661caee67576c05fc (patch) | |
tree | c486ce032d41f97313c50629bd5b879f53e6ccbf /lib/Support/FileOutputBuffer.cpp | |
parent | b835840cf112a6178506d834b58aa625f59a8994 (diff) | |
parent | 1ad9253c9d34ccbce3e7e4ea5d87c266cbf93410 (diff) |
Merge commit '1ad9253c9d34ccbce3e7e4ea5d87c266cbf93410'
deplib features commented out due to removal upstream;
will add back as a localmod
Conflicts:
include/llvm/ADT/Triple.h
include/llvm/MC/MCAssembler.h
include/llvm/Target/TargetFrameLowering.h
lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.h
lib/CodeGen/BranchFolding.cpp
lib/LLVMBuild.txt
lib/Linker/LinkArchives.cpp
lib/MC/MCAssembler.cpp
lib/MC/MCELFStreamer.cpp
lib/Makefile
lib/Target/ARM/ARMExpandPseudoInsts.cpp
lib/Target/ARM/ARMFrameLowering.cpp
lib/Target/ARM/ARMISelLowering.cpp
lib/Target/ARM/ARMSubtarget.h
lib/Target/ARM/ARMTargetObjectFile.cpp
lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
lib/Target/Mips/MipsInstrFPU.td
lib/Target/Mips/MipsInstrInfo.td
lib/Target/X86/X86CodeEmitter.cpp
lib/Target/X86/X86Subtarget.h
lib/VMCore/Module.cpp
test/MC/MachO/ARM/nop-armv4-padding.s
tools/Makefile
tools/llc/llc.cpp
tools/lto/LTOModule.cpp
tools/lto/lto.cpp
Diffstat (limited to 'lib/Support/FileOutputBuffer.cpp')
-rw-r--r-- | lib/Support/FileOutputBuffer.cpp | 83 |
1 files changed, 27 insertions, 56 deletions
diff --git a/lib/Support/FileOutputBuffer.cpp b/lib/Support/FileOutputBuffer.cpp index 7dc9587caa..cd430f218b 100644 --- a/lib/Support/FileOutputBuffer.cpp +++ b/lib/Support/FileOutputBuffer.cpp @@ -12,37 +12,28 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/FileOutputBuffer.h" - #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/Support/FileSystem.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/system_error.h" +using llvm::sys::fs::mapped_file_region; namespace llvm { - - -FileOutputBuffer::FileOutputBuffer(uint8_t *Start, uint8_t *End, - StringRef Path, StringRef TmpPath) - : BufferStart(Start), BufferEnd(End) { - FinalPath.assign(Path); - TempPath.assign(TmpPath); +FileOutputBuffer::FileOutputBuffer(mapped_file_region * R, + StringRef Path, StringRef TmpPath) + : Region(R) + , FinalPath(Path) + , TempPath(TmpPath) { } - FileOutputBuffer::~FileOutputBuffer() { - // If not already commited, delete buffer and remove temp file. - if ( BufferStart != NULL ) { - sys::fs::unmap_file_pages((void*)BufferStart, getBufferSize()); - bool Existed; - sys::fs::remove(Twine(TempPath), Existed); - } + bool Existed; + sys::fs::remove(Twine(TempPath), Existed); } - -error_code FileOutputBuffer::create(StringRef FilePath, - size_t Size, +error_code FileOutputBuffer::create(StringRef FilePath, + size_t Size, OwningPtr<FileOutputBuffer> &Result, unsigned Flags) { // If file already exists, it must be a regular file (to be mappable). @@ -70,34 +61,27 @@ error_code FileOutputBuffer::create(StringRef FilePath, EC = sys::fs::remove(FilePath, Existed); if (EC) return EC; - + // Create new file in same directory but with random name. SmallString<128> TempFilePath; int FD; - EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%", - FD, TempFilePath, false, 0644); + EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%", + FD, TempFilePath, false, 0644); if (EC) return EC; - - // The unique_file() interface leaks lower layers and returns a file - // descriptor. There is no way to directly close it, so use this hack - // to hand it off to raw_fd_ostream to close for us. - { - raw_fd_ostream Dummy(FD, /*shouldClose=*/true); - } - - // Resize file to requested initial size - EC = sys::fs::resize_file(Twine(TempFilePath), Size); + + OwningPtr<mapped_file_region> MappedFile( + new mapped_file_region(FD, mapped_file_region::readwrite, Size, 0, EC)); if (EC) return EC; - + // If requested, make the output file executable. if ( Flags & F_executable ) { sys::fs::file_status Stat2; EC = sys::fs::status(Twine(TempFilePath), Stat2); if (EC) return EC; - + sys::fs::perms new_perms = Stat2.permissions(); if ( new_perms & sys::fs::owner_read ) new_perms |= sys::fs::owner_exe; @@ -111,38 +95,25 @@ error_code FileOutputBuffer::create(StringRef FilePath, return EC; } - // Memory map new file. - void *Base; - EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base); - if (EC) - return EC; - - // Create FileOutputBuffer object to own mapped range. - uint8_t *Start = reinterpret_cast<uint8_t*>(Base); - Result.reset(new FileOutputBuffer(Start, Start+Size, FilePath, TempFilePath)); - - return error_code::success(); -} + Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath)); + if (Result) + MappedFile.take(); + return error_code::success(); +} error_code FileOutputBuffer::commit(int64_t NewSmallerSize) { // Unmap buffer, letting OS flush dirty pages to file on disk. - void *Start = reinterpret_cast<void*>(BufferStart); - error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize()); - if (EC) - return EC; - + Region.reset(0); + // If requested, resize file as part of commit. if ( NewSmallerSize != -1 ) { - EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); + error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize); if (EC) return EC; } - + // Rename file to final name. return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); } - - } // namespace - |