8326067: Remove os::remap_memory and simplify os::map_memory

Reviewed-by: eosterlund, iklam
This commit is contained in:
Thomas Stuefe 2024-02-19 19:33:07 +00:00
parent 5c5a282f91
commit 20a25f60ea
8 changed files with 56 additions and 174 deletions

View File

@ -2608,56 +2608,6 @@ jlong os::seek_to_file_offset(int fd, jlong offset) {
return (jlong)::lseek(fd, (off_t)offset, SEEK_SET);
}
// Map a block of memory.
char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
int prot;
int flags = MAP_PRIVATE;
if (read_only) {
prot = PROT_READ;
flags = MAP_SHARED;
} else {
prot = PROT_READ | PROT_WRITE;
flags = MAP_PRIVATE;
}
if (allow_exec) {
prot |= PROT_EXEC;
}
if (addr != nullptr) {
flags |= MAP_FIXED;
}
// Allow anonymous mappings if 'fd' is -1.
if (fd == -1) {
flags |= MAP_ANONYMOUS;
}
char* mapped_address = (char*)::mmap(addr, (size_t)bytes, prot, flags,
fd, file_offset);
if (mapped_address == MAP_FAILED) {
return nullptr;
}
return mapped_address;
}
// Remap a block of memory.
char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
// same as map_memory() on this OS
return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
allow_exec);
}
// Unmap a block of memory.
bool os::pd_unmap_memory(char* addr, size_t bytes) {
return munmap(addr, bytes) == 0;
}
// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
// are used by JVM M&M and JVMTI to get user+sys or user CPU time
// of a thread.

View File

@ -2345,53 +2345,6 @@ jlong os::seek_to_file_offset(int fd, jlong offset) {
return (jlong)::lseek(fd, (off_t)offset, SEEK_SET);
}
// Map a block of memory.
char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
int prot;
int flags;
if (read_only) {
prot = PROT_READ;
flags = MAP_SHARED;
} else {
prot = PROT_READ | PROT_WRITE;
flags = MAP_PRIVATE;
}
if (allow_exec) {
prot |= PROT_EXEC;
}
if (addr != nullptr) {
flags |= MAP_FIXED;
}
char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
fd, file_offset);
if (mapped_address == MAP_FAILED) {
return nullptr;
}
return mapped_address;
}
// Remap a block of memory.
char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
// same as map_memory() on this OS
return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
allow_exec);
}
// Unmap a block of memory.
bool os::pd_unmap_memory(char* addr, size_t bytes) {
return munmap(addr, bytes) == 0;
}
// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
// are used by JVM M&M and JVMTI to get user+sys or user CPU time
// of a thread.

View File

@ -5067,51 +5067,6 @@ jlong os::seek_to_file_offset(int fd, jlong offset) {
return (jlong)::lseek(fd, (off_t)offset, SEEK_SET);
}
// Map a block of memory.
char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
int prot;
int flags = MAP_PRIVATE;
if (read_only) {
prot = PROT_READ;
} else {
prot = PROT_READ | PROT_WRITE;
}
if (allow_exec) {
prot |= PROT_EXEC;
}
if (addr != nullptr) {
flags |= MAP_FIXED;
}
char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
fd, file_offset);
if (mapped_address == MAP_FAILED) {
return nullptr;
}
return mapped_address;
}
// Remap a block of memory.
char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
// same as map_memory() on this OS
return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
allow_exec);
}
// Unmap a block of memory.
bool os::pd_unmap_memory(char* addr, size_t bytes) {
return munmap(addr, bytes) == 0;
}
static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
static jlong fast_cpu_time(Thread *thread) {

View File

@ -2031,3 +2031,53 @@ void os::die() {
const char* os::file_separator() { return "/"; }
const char* os::line_separator() { return "\n"; }
const char* os::path_separator() { return ":"; }
// Map file into memory; uses mmap().
// Notes:
// - if caller specifies addr, MAP_FIXED is used. That means existing
// mappings will be replaced.
// - The file descriptor must be valid (to create anonymous mappings, use
// os::reserve_memory()).
// Returns address to mapped memory, nullptr on error
char* os::pd_map_memory(int fd, const char* unused,
size_t file_offset, char *addr, size_t bytes,
bool read_only, bool allow_exec) {
assert(fd != -1, "Specify a valid file descriptor");
int prot;
int flags = MAP_PRIVATE;
if (read_only) {
prot = PROT_READ;
} else {
prot = PROT_READ | PROT_WRITE;
}
if (allow_exec) {
prot |= PROT_EXEC;
}
if (addr != nullptr) {
flags |= MAP_FIXED;
}
char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
fd, file_offset);
if (mapped_address == MAP_FAILED) {
return nullptr;
}
// If we did specify an address, and the mapping succeeded, it should
// have returned that address since we specify MAP_FIXED
assert(addr == nullptr || addr == mapped_address,
"mmap+MAP_FIXED returned " PTR_FORMAT ", expected " PTR_FORMAT,
p2i(mapped_address), p2i(addr));
return mapped_address;
}
// Unmap a block of memory. Uses munmap.
bool os::pd_unmap_memory(char* addr, size_t bytes) {
return munmap(addr, bytes) == 0;
}

View File

@ -5167,22 +5167,6 @@ char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
return base;
}
// Remap a block of memory.
char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
// This OS does not allow existing memory maps to be remapped so we
// would have to unmap the memory before we remap it.
// Because there is a small window between unmapping memory and mapping
// it in again with different protections, CDS archives are mapped RW
// on windows, so this function isn't called.
ShouldNotReachHere();
return nullptr;
}
// Unmap a block of memory.
// Returns true=success, otherwise false.

View File

@ -1690,9 +1690,12 @@ bool FileMapInfo::remap_shared_readonly_as_readwrite() {
return false;
}
char *addr = r->mapped_base();
char *base = os::remap_memory(_fd, _full_path, r->file_offset(),
addr, size, false /* !read_only */,
r->allow_exec());
// This path should not be reached for Windows; see JDK-8222379.
assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
// Replace old mapping with new one that is writable.
char *base = os::map_memory(_fd, _full_path, r->file_offset(),
addr, size, false /* !read_only */,
r->allow_exec());
close();
// These have to be errors because the shared region is now unmapped.
if (base == nullptr) {

View File

@ -2158,13 +2158,6 @@ char* os::map_memory(int fd, const char* file_name, size_t file_offset,
return result;
}
char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec) {
return pd_remap_memory(fd, file_name, file_offset, addr, bytes,
read_only, allow_exec);
}
bool os::unmap_memory(char *addr, size_t bytes) {
bool result;
if (MemTracker::enabled()) {

View File

@ -217,9 +217,6 @@ class os: AllStatic {
static char* pd_map_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only = false,
bool allow_exec = false);
static char* pd_remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec);
static bool pd_unmap_memory(char *addr, size_t bytes);
static void pd_free_memory(char *addr, size_t bytes, size_t alignment_hint);
static void pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint);
@ -507,9 +504,6 @@ class os: AllStatic {
static char* map_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only = false,
bool allow_exec = false, MEMFLAGS flags = mtNone);
static char* remap_memory(int fd, const char* file_name, size_t file_offset,
char *addr, size_t bytes, bool read_only,
bool allow_exec);
static bool unmap_memory(char *addr, size_t bytes);
static void free_memory(char *addr, size_t bytes, size_t alignment_hint);
static void realign_memory(char *addr, size_t bytes, size_t alignment_hint);