mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-06 06:33:24 +00:00
[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:
parent
604db310f0
commit
6bc86018d1
@ -14,14 +14,14 @@
|
|||||||
#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
||||||
#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/OwningPtr.h"
|
||||||
#include "llvm/ADT/SmallString.h"
|
#include "llvm/ADT/SmallString.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
#include "llvm/Support/DataTypes.h"
|
#include "llvm/Support/DataTypes.h"
|
||||||
|
#include "llvm/Support/FileSystem.h"
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
class error_code;
|
class error_code;
|
||||||
template<class T> class OwningPtr;
|
|
||||||
|
|
||||||
/// FileOutputBuffer - This interface provides simple way to create an in-memory
|
/// FileOutputBuffer - This interface provides simple way to create an in-memory
|
||||||
/// buffer which will be written to a file. During the lifetime of these
|
/// 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.
|
/// to the file at the specified path.
|
||||||
static error_code create(StringRef FilePath, size_t Size,
|
static error_code create(StringRef FilePath, size_t Size,
|
||||||
OwningPtr<FileOutputBuffer> &Result,
|
OwningPtr<FileOutputBuffer> &Result,
|
||||||
unsigned Flags=0);
|
unsigned Flags = 0);
|
||||||
|
|
||||||
|
|
||||||
/// Returns a pointer to the start of the buffer.
|
/// Returns a pointer to the start of the buffer.
|
||||||
uint8_t *getBufferStart() const {
|
uint8_t *getBufferStart() {
|
||||||
return BufferStart;
|
return (uint8_t*)Region->data();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a pointer to the end of the buffer.
|
/// Returns a pointer to the end of the buffer.
|
||||||
uint8_t *getBufferEnd() const {
|
uint8_t *getBufferEnd() {
|
||||||
return BufferEnd;
|
return (uint8_t*)Region->data() + Region->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns size of the buffer.
|
/// Returns size of the buffer.
|
||||||
size_t getBufferSize() const {
|
size_t getBufferSize() const {
|
||||||
return BufferEnd - BufferStart;
|
return Region->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns path where file will show up if buffer is committed.
|
/// 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.
|
/// deallocates the buffer and the target file is never written.
|
||||||
~FileOutputBuffer();
|
~FileOutputBuffer();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
||||||
FileOutputBuffer &operator=(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);
|
StringRef Path, StringRef TempPath);
|
||||||
|
|
||||||
uint8_t *BufferStart;
|
OwningPtr<llvm::sys::fs::mapped_file_region> Region;
|
||||||
uint8_t *BufferEnd;
|
|
||||||
SmallString<128> FinalPath;
|
SmallString<128> FinalPath;
|
||||||
SmallString<128> TempPath;
|
SmallString<128> TempPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // end namespace llvm
|
} // end namespace llvm
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -14,32 +14,24 @@
|
|||||||
#include "llvm/Support/FileOutputBuffer.h"
|
#include "llvm/Support/FileOutputBuffer.h"
|
||||||
#include "llvm/ADT/OwningPtr.h"
|
#include "llvm/ADT/OwningPtr.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/Support/FileSystem.h"
|
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
#include "llvm/Support/system_error.h"
|
#include "llvm/Support/system_error.h"
|
||||||
|
|
||||||
|
using llvm::sys::fs::mapped_file_region;
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
|
||||||
|
|
||||||
FileOutputBuffer::FileOutputBuffer(uint8_t *Start, uint8_t *End,
|
|
||||||
StringRef Path, StringRef TmpPath)
|
StringRef Path, StringRef TmpPath)
|
||||||
: BufferStart(Start), BufferEnd(End) {
|
: Region(R)
|
||||||
FinalPath.assign(Path);
|
, FinalPath(Path)
|
||||||
TempPath.assign(TmpPath);
|
, TempPath(TmpPath) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
FileOutputBuffer::~FileOutputBuffer() {
|
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;
|
bool Existed;
|
||||||
sys::fs::remove(Twine(TempPath), Existed);
|
sys::fs::remove(Twine(TempPath), Existed);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
error_code FileOutputBuffer::create(StringRef FilePath,
|
error_code FileOutputBuffer::create(StringRef FilePath,
|
||||||
size_t Size,
|
size_t Size,
|
||||||
OwningPtr<FileOutputBuffer> &Result,
|
OwningPtr<FileOutputBuffer> &Result,
|
||||||
@ -78,15 +70,8 @@ error_code FileOutputBuffer::create(StringRef FilePath,
|
|||||||
if (EC)
|
if (EC)
|
||||||
return EC;
|
return EC;
|
||||||
|
|
||||||
// The unique_file() interface leaks lower layers and returns a file
|
OwningPtr<mapped_file_region> MappedFile(
|
||||||
// descriptor. There is no way to directly close it, so use this hack
|
new mapped_file_region(FD, mapped_file_region::readwrite, Size, 0, EC));
|
||||||
// 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);
|
|
||||||
if (EC)
|
if (EC)
|
||||||
return EC;
|
return EC;
|
||||||
|
|
||||||
@ -110,30 +95,20 @@ error_code FileOutputBuffer::create(StringRef FilePath,
|
|||||||
return EC;
|
return EC;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory map new file.
|
Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
|
||||||
void *Base;
|
if (Result)
|
||||||
EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base);
|
MappedFile.take();
|
||||||
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();
|
return error_code::success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
|
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
|
||||||
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
||||||
void *Start = reinterpret_cast<void*>(BufferStart);
|
Region.reset(0);
|
||||||
error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize());
|
|
||||||
if (EC)
|
|
||||||
return EC;
|
|
||||||
|
|
||||||
// If requested, resize file as part of commit.
|
// If requested, resize file as part of commit.
|
||||||
if ( NewSmallerSize != -1 ) {
|
if ( NewSmallerSize != -1 ) {
|
||||||
EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
|
error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
|
||||||
if (EC)
|
if (EC)
|
||||||
return EC;
|
return EC;
|
||||||
}
|
}
|
||||||
@ -141,7 +116,4 @@ error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
|
|||||||
// Rename file to final name.
|
// Rename file to final name.
|
||||||
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -7,9 +7,10 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/Support/FileOutputBuffer.h"
|
||||||
|
|
||||||
#include "llvm/ADT/OwningPtr.h"
|
#include "llvm/ADT/OwningPtr.h"
|
||||||
#include "llvm/Support/ErrorHandling.h"
|
#include "llvm/Support/ErrorHandling.h"
|
||||||
#include "llvm/Support/FileOutputBuffer.h"
|
|
||||||
#include "llvm/Support/FileSystem.h"
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include "llvm/Support/PathV2.h"
|
#include "llvm/Support/PathV2.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
@ -27,13 +28,6 @@ using namespace llvm::sys;
|
|||||||
} else {}
|
} else {}
|
||||||
|
|
||||||
namespace {
|
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) {
|
TEST(FileOutputBuffer, Test) {
|
||||||
// Create unique temporary directory for these tests
|
// Create unique temporary directory for these tests
|
||||||
SmallString<128> TestDirectory;
|
SmallString<128> TestDirectory;
|
||||||
@ -84,7 +78,6 @@ TEST(FileOutputBuffer, Test) {
|
|||||||
ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
|
ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
|
||||||
EXPECT_FALSE(Exists);
|
EXPECT_FALSE(Exists);
|
||||||
|
|
||||||
|
|
||||||
// TEST 3: Verify sizing down case.
|
// TEST 3: Verify sizing down case.
|
||||||
SmallString<128> File3(TestDirectory);
|
SmallString<128> File3(TestDirectory);
|
||||||
File3.append("/file3");
|
File3.append("/file3");
|
||||||
@ -108,7 +101,6 @@ TEST(FileOutputBuffer, Test) {
|
|||||||
ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
|
ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
|
||||||
ASSERT_EQ(File3Size, 5000ULL);
|
ASSERT_EQ(File3Size, 5000ULL);
|
||||||
|
|
||||||
|
|
||||||
// TEST 4: Verify file can be made executable.
|
// TEST 4: Verify file can be made executable.
|
||||||
SmallString<128> File4(TestDirectory);
|
SmallString<128> File4(TestDirectory);
|
||||||
File4.append("/file4");
|
File4.append("/file4");
|
||||||
@ -131,7 +123,4 @@ TEST(FileOutputBuffer, Test) {
|
|||||||
uint32_t RemovedCount;
|
uint32_t RemovedCount;
|
||||||
ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
|
ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // LLVM_ON_UNIX
|
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user