1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-05 17:17:40 +03:00
This commit is contained in:
Rui Ueyama 2024-06-22 13:07:46 +09:00
parent 5535e7a6b7
commit 4cc61361f3
3 changed files with 30 additions and 27 deletions

View File

@ -963,6 +963,8 @@ class MappedFile {
public:
~MappedFile() { unmap(); }
void unmap();
void close_fd();
void reopen_fd(const std::string &path);
template <typename Context>
MappedFile *slice(Context &ctx, std::string name, u64 start, u64 size) {
@ -1001,32 +1003,6 @@ public:
return name;
}
void close_fd() {
#ifdef WIN32
if (fd != INVALID_HANDLE_VALUE) {
CloseHandle(fd);
fd = INVALID_HANDLE_VALUE;
}
#else
if (fd != -1) {
close(fd);
fd = -1;
}
#endif
}
void reopen_fd(const char *path) {
#ifdef WIN32
if (fd != INVALID_HANDLE_VALUE)
fd = CreateFileA(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
#else
if (fd == -1)
fd = open(path, O_RDONLY);
#endif
}
std::string name;
u8 *data = nullptr;
i64 size = 0;

View File

@ -32,9 +32,21 @@ MappedFile *open_file_impl(const std::string &path, std::string &error) {
void MappedFile::unmap() {
if (size == 0 || parent || !data)
return;
munmap(data, size);
data = nullptr;
}
void MappedFile::close_fd() {
if (fd == -1)
return;
close(fd);
fd = -1;
}
void MappedFile::reopen_fd(const std::string &path) {
if (fd != -1)
close(fd);
fd = open(path.c_str(), O_RDONLY);
}
} // namespace mold

View File

@ -64,4 +64,19 @@ void MappedFile::unmap() {
data = nullptr;
}
void MappedFile::close_fd() {
if (fd == INVALID_HANDLE_VALUE)
return;
CloseHandle(fd);
fd = INVALID_HANDLE_VALUE;
}
void MappedFile::reopen_fd(const std::string &path) {
if (fd != INVALID_HANDLE_VALUE)
CloseHandle(fd);
fd = CreateFileA(path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
}
} // namespace mold