[Support] Make FileOutputBuffer work on Windows.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169167 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael J. Spencer 2012-12-03 22:09:52 +00:00
parent 604db310f0
commit 6bc86018d1
3 changed files with 60 additions and 105 deletions

View File

@ -14,14 +14,14 @@
#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/FileSystem.h"
namespace llvm {
class error_code;
template<class T> class OwningPtr;
/// FileOutputBuffer - This interface provides simple way to create an in-memory
/// buffer which will be written to a file. During the lifetime of these
@ -42,22 +42,21 @@ public:
/// to the file at the specified path.
static error_code create(StringRef FilePath, size_t Size,
OwningPtr<FileOutputBuffer> &Result,
unsigned Flags=0);
unsigned Flags = 0);
/// Returns a pointer to the start of the buffer.
uint8_t *getBufferStart() const {
return BufferStart;
uint8_t *getBufferStart() {
return (uint8_t*)Region->data();
}
/// Returns a pointer to the end of the buffer.
uint8_t *getBufferEnd() const {
return BufferEnd;
uint8_t *getBufferEnd() {
return (uint8_t*)Region->data() + Region->size();
}
/// Returns size of the buffer.
size_t getBufferSize() const {
return BufferEnd - BufferStart;
return Region->size();
}
/// Returns path where file will show up if buffer is committed.
@ -77,22 +76,17 @@ public:
/// deallocates the buffer and the target file is never written.
~FileOutputBuffer();
private:
FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
protected:
FileOutputBuffer(uint8_t *Start, uint8_t *End,
FileOutputBuffer(llvm::sys::fs::mapped_file_region *R,
StringRef Path, StringRef TempPath);
uint8_t *BufferStart;
uint8_t *BufferEnd;
OwningPtr<llvm::sys::fs::mapped_file_region> Region;
SmallString<128> FinalPath;
SmallString<128> TempPath;
};
} // end namespace llvm
#endif

View File

@ -14,32 +14,24 @@
#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,
FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
StringRef Path, StringRef TmpPath)
: BufferStart(Start), BufferEnd(End) {
FinalPath.assign(Path);
TempPath.assign(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);
}
}
error_code FileOutputBuffer::create(StringRef FilePath,
size_t Size,
OwningPtr<FileOutputBuffer> &Result,
@ -78,15 +70,8 @@ error_code FileOutputBuffer::create(StringRef FilePath,
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;
@ -110,30 +95,20 @@ 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));
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;
}
@ -141,7 +116,4 @@ error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
// Rename file to final name.
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
}
} // namespace

View File

@ -7,9 +7,10 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/PathV2.h"
#include "llvm/Support/raw_ostream.h"
@ -27,13 +28,6 @@ using namespace llvm::sys;
} else {}
namespace {
// NOTE: Temporarily run this test on unix only. Once the file mapping
// routines are ported to Windows, this conditional can be removed.
#if LLVM_ON_UNIX
TEST(FileOutputBuffer, Test) {
// Create unique temporary directory for these tests
SmallString<128> TestDirectory;
@ -84,7 +78,6 @@ TEST(FileOutputBuffer, Test) {
ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
EXPECT_FALSE(Exists);
// TEST 3: Verify sizing down case.
SmallString<128> File3(TestDirectory);
File3.append("/file3");
@ -108,7 +101,6 @@ TEST(FileOutputBuffer, Test) {
ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
ASSERT_EQ(File3Size, 5000ULL);
// TEST 4: Verify file can be made executable.
SmallString<128> File4(TestDirectory);
File4.append("/file4");
@ -131,7 +123,4 @@ TEST(FileOutputBuffer, Test) {
uint32_t RemovedCount;
ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
}
#endif // LLVM_ON_UNIX
} // anonymous namespace