mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-26 23:24:34 +00:00
Remove MappedFile support for mapping files for write and exec
and shared. This complicates the design, is not used, and probably doesn't even work. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49022 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -52,7 +52,7 @@ namespace llvm {
|
|||||||
SourceFile(const std::string &fn, const GlobalVariable *Desc)
|
SourceFile(const std::string &fn, const GlobalVariable *Desc)
|
||||||
: Filename(fn), Descriptor(Desc), File() {
|
: Filename(fn), Descriptor(Desc), File() {
|
||||||
std::string ErrMsg;
|
std::string ErrMsg;
|
||||||
if (File.open(Filename, sys::MappedFile::READ_ACCESS, &ErrMsg))
|
if (File.open(Filename, &ErrMsg))
|
||||||
throw ErrMsg;
|
throw ErrMsg;
|
||||||
readFile();
|
readFile();
|
||||||
}
|
}
|
||||||
|
@ -25,26 +25,16 @@ namespace sys {
|
|||||||
|
|
||||||
/// This class provides an abstraction for a memory mapped file in the
|
/// This class provides an abstraction for a memory mapped file in the
|
||||||
/// operating system's filesystem. It provides platform independent operations
|
/// operating system's filesystem. It provides platform independent operations
|
||||||
/// for mapping a file into memory for both read and write access. This class
|
/// for mapping a file into memory for both read access.
|
||||||
/// does not provide facilities for finding the file or operating on paths to
|
|
||||||
/// files. The sys::Path class is used for that.
|
|
||||||
class MappedFile {
|
class MappedFile {
|
||||||
sys::PathWithStatus Path; ///< Path to the file.
|
sys::PathWithStatus Path; ///< Path to the file.
|
||||||
unsigned Options; ///< Options used to create the mapping
|
|
||||||
void *BasePtr; ///< Pointer to the base memory address
|
void *BasePtr; ///< Pointer to the base memory address
|
||||||
mutable MappedFileInfo *MapInfo; ///< Platform specific info for the mapping
|
mutable MappedFileInfo *MapInfo; ///< Platform specific info for the mapping
|
||||||
|
|
||||||
MappedFile& operator=(const MappedFile &that); // DO NOT IMPLEMENT
|
MappedFile& operator=(const MappedFile &that); // DO NOT IMPLEMENT
|
||||||
MappedFile(const MappedFile &that); // DO NOT IMPLEMENT
|
MappedFile(const MappedFile &that); // DO NOT IMPLEMENT
|
||||||
public:
|
public:
|
||||||
enum MappingOptions {
|
MappedFile() : BasePtr(0), MapInfo(0) {}
|
||||||
READ_ACCESS = 0x0001, ///< Map the file for reading
|
|
||||||
WRITE_ACCESS = 0x0002, ///< Map the file for write access
|
|
||||||
EXEC_ACCESS = 0x0004, ///< Map the file for execution access
|
|
||||||
SHARED_MAPPING = 0x0008 ///< Map the file shared with other processes
|
|
||||||
};
|
|
||||||
|
|
||||||
MappedFile() : Options(READ_ACCESS), BasePtr(0), MapInfo(0) {}
|
|
||||||
|
|
||||||
/// Destruct a MappedFile and release all memory associated with it.
|
/// Destruct a MappedFile and release all memory associated with it.
|
||||||
~MappedFile() { close(); }
|
~MappedFile() { close(); }
|
||||||
@ -71,7 +61,7 @@ namespace sys {
|
|||||||
/// This function returns a reference to the sys::Path object kept by the
|
/// This function returns a reference to the sys::Path object kept by the
|
||||||
/// MappedFile object. This contains the path to the file that is or
|
/// MappedFile object. This contains the path to the file that is or
|
||||||
/// will be mapped.
|
/// will be mapped.
|
||||||
const sys::Path& path() const { return Path; }
|
const sys::PathWithStatus &path() const { return Path; }
|
||||||
|
|
||||||
/// This function returns the number of bytes in the file.
|
/// This function returns the number of bytes in the file.
|
||||||
size_t size() const;
|
size_t size() const;
|
||||||
@ -80,10 +70,8 @@ namespace sys {
|
|||||||
|
|
||||||
/// Open a file to be mapped and get its size but don't map it yet. Return
|
/// Open a file to be mapped and get its size but don't map it yet. Return
|
||||||
/// true on error.
|
/// true on error.
|
||||||
bool open(const sys::Path &P, int options = READ_ACCESS,
|
bool open(const sys::Path &P, std::string *ErrMsg = 0) {
|
||||||
std::string *ErrMsg = 0) {
|
|
||||||
Path = P;
|
Path = P;
|
||||||
Options = options;
|
|
||||||
return initialize(ErrMsg);
|
return initialize(ErrMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,14 +85,6 @@ namespace sys {
|
|||||||
/// or 0 if an error occurred.
|
/// or 0 if an error occurred.
|
||||||
void *map(std::string* ErrMsg = 0);
|
void *map(std::string* ErrMsg = 0);
|
||||||
|
|
||||||
/// resize - This method causes the size of the file, and consequently the
|
|
||||||
/// size of the mapping to be set. This is logically the same as unmap(),
|
|
||||||
/// adjust size of the file, map(). Consequently, when calling this
|
|
||||||
/// function, the caller should not rely on previous results of the
|
|
||||||
/// map(), base(), or baseChar() members as they may point to invalid
|
|
||||||
/// areas of memory after this call.
|
|
||||||
bool resize(size_t new_size, std::string *ErrMsg = 0);
|
|
||||||
|
|
||||||
void close() { if (MapInfo) terminate(); }
|
void close() { if (MapInfo) terminate(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -148,7 +148,7 @@ bool
|
|||||||
Archive::mapToMemory(std::string* ErrMsg)
|
Archive::mapToMemory(std::string* ErrMsg)
|
||||||
{
|
{
|
||||||
mapfile = new sys::MappedFile();
|
mapfile = new sys::MappedFile();
|
||||||
if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
|
if (mapfile->open(archPath, ErrMsg))
|
||||||
return true;
|
return true;
|
||||||
if (!(base = (char*) mapfile->map(ErrMsg)))
|
if (!(base = (char*) mapfile->map(ErrMsg)))
|
||||||
return true;
|
return true;
|
||||||
|
@ -212,7 +212,7 @@ Archive::writeMember(
|
|||||||
sys::MappedFile* mFile = 0;
|
sys::MappedFile* mFile = 0;
|
||||||
if (!data) {
|
if (!data) {
|
||||||
mFile = new sys::MappedFile();
|
mFile = new sys::MappedFile();
|
||||||
if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
|
if (mFile->open(member.getPath(), ErrMsg))
|
||||||
return true;
|
return true;
|
||||||
if (!(data = (const char*) mFile->map(ErrMsg)))
|
if (!(data = (const char*) mFile->map(ErrMsg)))
|
||||||
return true;
|
return true;
|
||||||
@ -411,7 +411,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
|
|||||||
|
|
||||||
// Map in the archive we just wrote.
|
// Map in the archive we just wrote.
|
||||||
sys::MappedFile arch;
|
sys::MappedFile arch;
|
||||||
if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
|
if (arch.open(TmpArchive, ErrMsg))
|
||||||
return true;
|
return true;
|
||||||
const char* base;
|
const char* base;
|
||||||
if (!(base = (const char*) arch.map(ErrMsg)))
|
if (!(base = (const char*) arch.map(ErrMsg)))
|
||||||
|
@ -192,10 +192,10 @@ int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
|
|||||||
// Now its safe to mmap the files into memory becasue both files
|
// Now its safe to mmap the files into memory becasue both files
|
||||||
// have a non-zero size.
|
// have a non-zero size.
|
||||||
sys::MappedFile F1;
|
sys::MappedFile F1;
|
||||||
if (F1.open(FileA, sys::MappedFile::READ_ACCESS, Error))
|
if (F1.open(FileA, Error))
|
||||||
return 2;
|
return 2;
|
||||||
sys::MappedFile F2;
|
sys::MappedFile F2;
|
||||||
if (F2.open(FileB, sys::MappedFile::READ_ACCESS, Error))
|
if (F2.open(FileB, Error))
|
||||||
return 2;
|
return 2;
|
||||||
if (!F1.map(Error))
|
if (!F1.map(Error))
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -159,7 +159,7 @@ bool MemoryBufferMMapFile::open(const sys::Path &Filename,
|
|||||||
std::string *ErrStr) {
|
std::string *ErrStr) {
|
||||||
// FIXME: This does an extra stat syscall to figure out the size, but we
|
// FIXME: This does an extra stat syscall to figure out the size, but we
|
||||||
// already know the size!
|
// already know the size!
|
||||||
bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
|
bool Failure = File.open(Filename, ErrStr);
|
||||||
if (Failure) return true;
|
if (Failure) return true;
|
||||||
|
|
||||||
if (!File.map(ErrStr))
|
if (!File.map(ErrStr))
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
|
//===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===//
|
||||||
//
|
//
|
||||||
// The LLVM Compiler Infrastructure
|
// The LLVM Compiler Infrastructure
|
||||||
//
|
//
|
||||||
@ -11,11 +11,6 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
||||||
//=== is guaranteed to work on *all* UNIX variants.
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
#include "Unix.h"
|
#include "Unix.h"
|
||||||
#include "llvm/System/Process.h"
|
#include "llvm/System/Process.h"
|
||||||
|
|
||||||
@ -44,16 +39,7 @@ namespace llvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool MappedFile::initialize(std::string* ErrMsg) {
|
bool MappedFile::initialize(std::string* ErrMsg) {
|
||||||
int mode = 0;
|
int FD = ::open(Path.c_str(), O_RDONLY);
|
||||||
if (Options & READ_ACCESS)
|
|
||||||
if (Options & WRITE_ACCESS)
|
|
||||||
mode = O_RDWR;
|
|
||||||
else
|
|
||||||
mode = O_RDONLY;
|
|
||||||
else if (Options & WRITE_ACCESS)
|
|
||||||
mode = O_WRONLY;
|
|
||||||
|
|
||||||
int FD = ::open(Path.c_str(), mode);
|
|
||||||
if (FD < 0) {
|
if (FD < 0) {
|
||||||
MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
|
MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
|
||||||
return true;
|
return true;
|
||||||
@ -80,8 +66,6 @@ void MappedFile::unmap() {
|
|||||||
assert(MapInfo && "MappedFile not initialized");
|
assert(MapInfo && "MappedFile not initialized");
|
||||||
if (!isMapped()) return;
|
if (!isMapped()) return;
|
||||||
|
|
||||||
if (Options & WRITE_ACCESS)
|
|
||||||
::msync(BasePtr, MapInfo->Size, MS_SYNC);
|
|
||||||
::munmap(BasePtr, MapInfo->Size);
|
::munmap(BasePtr, MapInfo->Size);
|
||||||
BasePtr = 0; // Mark this as non-mapped.
|
BasePtr = 0; // Mark this as non-mapped.
|
||||||
}
|
}
|
||||||
@ -90,28 +74,13 @@ void* MappedFile::map(std::string* ErrMsg) {
|
|||||||
assert(MapInfo && "MappedFile not initialized");
|
assert(MapInfo && "MappedFile not initialized");
|
||||||
if (isMapped()) return BasePtr;
|
if (isMapped()) return BasePtr;
|
||||||
|
|
||||||
int prot = PROT_NONE;
|
int prot = PROT_READ;
|
||||||
int flags = 0;
|
int flags = MAP_PRIVATE;
|
||||||
#ifdef MAP_FILE
|
#ifdef MAP_FILE
|
||||||
flags |= MAP_FILE;
|
flags |= MAP_FILE;
|
||||||
#endif
|
#endif
|
||||||
if (Options == 0) {
|
size_t PageSize = Process::GetPageSize();
|
||||||
prot = PROT_READ;
|
size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize;
|
||||||
flags = MAP_PRIVATE;
|
|
||||||
} else {
|
|
||||||
if (Options & READ_ACCESS)
|
|
||||||
prot |= PROT_READ;
|
|
||||||
if (Options & WRITE_ACCESS)
|
|
||||||
prot |= PROT_WRITE;
|
|
||||||
if (Options & EXEC_ACCESS)
|
|
||||||
prot |= PROT_EXEC;
|
|
||||||
if (Options & SHARED_MAPPING)
|
|
||||||
flags |= MAP_SHARED;
|
|
||||||
else
|
|
||||||
flags |= MAP_PRIVATE;
|
|
||||||
}
|
|
||||||
size_t map_size = ((MapInfo->Size / Process::GetPageSize())+1) *
|
|
||||||
Process::GetPageSize();
|
|
||||||
|
|
||||||
BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0);
|
BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0);
|
||||||
if (BasePtr == MAP_FAILED) {
|
if (BasePtr == MAP_FAILED) {
|
||||||
@ -126,30 +95,3 @@ size_t MappedFile::size() const {
|
|||||||
return MapInfo->Size;
|
return MapInfo->Size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MappedFile::resize(size_t new_size, std::string* ErrMsg) {
|
|
||||||
assert(MapInfo && "MappedFile not initialized");
|
|
||||||
|
|
||||||
// Take the mapping out of memory
|
|
||||||
unmap();
|
|
||||||
|
|
||||||
// Adjust the current size to a page boundary
|
|
||||||
size_t cur_size = ((MapInfo->Size / Process::GetPageSize())+1) *
|
|
||||||
Process::GetPageSize();
|
|
||||||
|
|
||||||
// Adjust the new_size to a page boundary
|
|
||||||
new_size = ((new_size / Process::GetPageSize())+1) *
|
|
||||||
Process::GetPageSize();
|
|
||||||
|
|
||||||
// If the file needs to be extended
|
|
||||||
if (new_size > cur_size) {
|
|
||||||
// Ensure we can allocate at least the idodes necessary to handle the
|
|
||||||
// file size requested.
|
|
||||||
if ((off_t)-1 == ::lseek(MapInfo->FD, new_size, SEEK_SET))
|
|
||||||
return MakeErrMsg(ErrMsg, "Can't lseek: ");
|
|
||||||
if (-1 == ::write(MapInfo->FD, "\0", 1))
|
|
||||||
return MakeErrMsg(ErrMsg, "Can't write: ");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put the mapping back into memory.
|
|
||||||
return map(ErrMsg);
|
|
||||||
}
|
|
||||||
|
@ -33,12 +33,8 @@ bool MappedFile::initialize(std::string* ErrMsg) {
|
|||||||
MapInfo->hFile = INVALID_HANDLE_VALUE;
|
MapInfo->hFile = INVALID_HANDLE_VALUE;
|
||||||
MapInfo->hMapping = NULL;
|
MapInfo->hMapping = NULL;
|
||||||
|
|
||||||
DWORD mode = Options & WRITE_ACCESS ? GENERIC_WRITE : GENERIC_READ;
|
MapInfo->hFile = CreateFile(Path.c_str(), GENERIC_READ, 0, NULL,
|
||||||
DWORD disposition = Options & WRITE_ACCESS ? OPEN_ALWAYS : OPEN_EXISTING;
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
DWORD share = Options & WRITE_ACCESS ? FILE_SHARE_WRITE : FILE_SHARE_READ;
|
|
||||||
share = Options & SHARED_MAPPING ? share : 0;
|
|
||||||
MapInfo->hFile = CreateFile(Path.c_str(), mode, share, NULL, disposition,
|
|
||||||
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
||||||
if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
|
if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
|
||||||
delete MapInfo;
|
delete MapInfo;
|
||||||
MapInfo = NULL;
|
MapInfo = NULL;
|
||||||
@ -81,19 +77,14 @@ void MappedFile::unmap() {
|
|||||||
|
|
||||||
void* MappedFile::map(std::string* ErrMsg) {
|
void* MappedFile::map(std::string* ErrMsg) {
|
||||||
if (!isMapped()) {
|
if (!isMapped()) {
|
||||||
DWORD prot = PAGE_READONLY;
|
MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, PAGE_READONLY,
|
||||||
if (Options & EXEC_ACCESS)
|
0, 0, NULL);
|
||||||
prot = SEC_IMAGE;
|
|
||||||
else if (Options & WRITE_ACCESS)
|
|
||||||
prot = PAGE_READWRITE;
|
|
||||||
MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, prot, 0, 0, NULL);
|
|
||||||
if (MapInfo->hMapping == NULL) {
|
if (MapInfo->hMapping == NULL) {
|
||||||
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
|
MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
prot = (Options & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ;
|
BasePtr = MapViewOfFileEx(MapInfo->hMapping, FILE_MAP_READ, 0, 0, 0, NULL);
|
||||||
BasePtr = MapViewOfFileEx(MapInfo->hMapping, prot, 0, 0, 0, NULL);
|
|
||||||
if (BasePtr == NULL) {
|
if (BasePtr == NULL) {
|
||||||
CloseHandle(MapInfo->hMapping);
|
CloseHandle(MapInfo->hMapping);
|
||||||
MapInfo->hMapping = NULL;
|
MapInfo->hMapping = NULL;
|
||||||
@ -109,32 +100,5 @@ size_t MappedFile::size() const {
|
|||||||
return MapInfo->size;
|
return MapInfo->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MappedFile::resize(size_t new_size, std::string* ErrMsg) {
|
|
||||||
assert(MapInfo && "MappedFile not initialized");
|
|
||||||
|
|
||||||
// Take the mapping out of memory.
|
|
||||||
unmap();
|
|
||||||
|
|
||||||
// Adjust the new_size to a page boundary.
|
|
||||||
size_t pagesizem1 = Process::GetPageSize() - 1;
|
|
||||||
new_size = (new_size + pagesizem1) & ~pagesizem1;
|
|
||||||
|
|
||||||
// If the file needs to be extended, do so.
|
|
||||||
if (new_size > MapInfo->size) {
|
|
||||||
LARGE_INTEGER eof;
|
|
||||||
eof.QuadPart = new_size;
|
|
||||||
if (!SetFilePointerEx(MapInfo->hFile, eof, NULL, FILE_BEGIN))
|
|
||||||
return MakeErrMsg(ErrMsg,
|
|
||||||
std::string("Can't set end of file: ") + Path.toString());
|
|
||||||
if (!SetEndOfFile(MapInfo->hFile))
|
|
||||||
return MakeErrMsg(ErrMsg,
|
|
||||||
std::string("Can't set end of file: ") + Path.toString());
|
|
||||||
MapInfo->size = new_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remap the file.
|
|
||||||
return map(ErrMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user