2012-08-01 04:29:50 +02:00
|
|
|
//=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Utility for creating a in-memory buffer that will be written to a file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
|
|
|
#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
|
|
|
|
2012-12-03 23:09:52 +01:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2012-08-01 04:29:50 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-12-03 23:09:52 +01:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-08-01 04:29:50 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class error_code;
|
|
|
|
|
|
|
|
/// FileOutputBuffer - This interface provides simple way to create an in-memory
|
2012-12-03 23:09:52 +01:00
|
|
|
/// buffer which will be written to a file. During the lifetime of these
|
2012-08-01 04:29:50 +02:00
|
|
|
/// objects, the content or existence of the specified file is undefined. That
|
|
|
|
/// is, creating an OutputBuffer for a file may immediately remove the file.
|
2012-12-03 23:09:52 +01:00
|
|
|
/// If the FileOutputBuffer is committed, the target file's content will become
|
|
|
|
/// the buffer content at the time of the commit. If the FileOutputBuffer is
|
2012-08-01 04:29:50 +02:00
|
|
|
/// not committed, the file will be deleted in the FileOutputBuffer destructor.
|
|
|
|
class FileOutputBuffer {
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum {
|
|
|
|
F_executable = 1 /// set the 'x' bit on the resulting file
|
2012-12-03 23:09:52 +01:00
|
|
|
};
|
2012-08-01 04:29:50 +02:00
|
|
|
|
|
|
|
/// Factory method to create an OutputBuffer object which manages a read/write
|
|
|
|
/// buffer of the specified size. When committed, the buffer will be written
|
2012-12-03 23:09:52 +01:00
|
|
|
/// to the file at the specified path.
|
|
|
|
static error_code create(StringRef FilePath, size_t Size,
|
|
|
|
OwningPtr<FileOutputBuffer> &Result,
|
|
|
|
unsigned Flags = 0);
|
2012-08-01 04:29:50 +02:00
|
|
|
|
|
|
|
/// Returns a pointer to the start of the buffer.
|
2012-12-03 23:09:52 +01:00
|
|
|
uint8_t *getBufferStart() {
|
|
|
|
return (uint8_t*)Region->data();
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
/// Returns a pointer to the end of the buffer.
|
2012-12-03 23:09:52 +01:00
|
|
|
uint8_t *getBufferEnd() {
|
|
|
|
return (uint8_t*)Region->data() + Region->size();
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
/// Returns size of the buffer.
|
|
|
|
size_t getBufferSize() const {
|
2012-12-03 23:09:52 +01:00
|
|
|
return Region->size();
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
/// Returns path where file will show up if buffer is committed.
|
|
|
|
StringRef getPath() const {
|
|
|
|
return FinalPath;
|
|
|
|
}
|
2012-12-03 23:09:52 +01:00
|
|
|
|
|
|
|
/// Flushes the content of the buffer to its file and deallocates the
|
2012-08-01 04:29:50 +02:00
|
|
|
/// buffer. If commit() is not called before this object's destructor
|
|
|
|
/// is called, the file is deleted in the destructor. The optional parameter
|
|
|
|
/// is used if it turns out you want the file size to be smaller than
|
|
|
|
/// initially requested.
|
|
|
|
error_code commit(int64_t NewSmallerSize = -1);
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
/// If this object was previously committed, the destructor just deletes
|
|
|
|
/// this object. If this object was not committed, the destructor
|
|
|
|
/// deallocates the buffer and the target file is never written.
|
|
|
|
~FileOutputBuffer();
|
|
|
|
|
2012-09-17 08:31:17 +02:00
|
|
|
private:
|
|
|
|
FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
|
|
|
FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
2012-12-03 23:09:52 +01:00
|
|
|
|
|
|
|
FileOutputBuffer(llvm::sys::fs::mapped_file_region *R,
|
|
|
|
StringRef Path, StringRef TempPath);
|
|
|
|
|
|
|
|
OwningPtr<llvm::sys::fs::mapped_file_region> Region;
|
2012-08-01 04:29:50 +02:00
|
|
|
SmallString<128> FinalPath;
|
|
|
|
SmallString<128> TempPath;
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|