--- old/os/posix/os_posix.cpp 2018-06-14 13:09:49.704858331 -0700 +++ new/os/posix/os_posix.cpp 2018-06-14 13:09:49.672858330 -0700 @@ -238,24 +238,37 @@ #endif } +// Allocates space for the file on diven device +int os::allocate_file(int file_desc, size_t size) { + // allocate space for the file + if (file_desc == -1) { + vm_exit_during_initialization(err_msg("Invalid file descriptor passed (%d)", file_desc)); + } + int ret = util_posix_fallocate(file_desc, 0, (off_t)size); + if (ret != 0) { + vm_exit_during_initialization(err_msg("Could not allocate file to map Java heap on given filesystem... error(%d)", ret)); + } + return ret; +} + // Map the given address range to the provided file descriptor. -char* os::map_memory_to_file(char* base, size_t size, int fd) { +char* os::map_memory_to_file(char* base, size_t size, int fd, int offset, bool exec, bool allocate) { assert(fd != -1, "File descriptor is not valid"); // allocate space for the file - int ret = util_posix_fallocate(fd, 0, (off_t)size); - if (ret != 0) { - vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret)); - return NULL; + if (allocate == true) { + if (os::allocate_file(fd, size) != 0) { + return NULL; + } } - int prot = PROT_READ | PROT_WRITE; + int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE; int flags = MAP_SHARED; if (base != NULL) { flags |= MAP_FIXED; } - char* addr = (char*)mmap(base, size, prot, flags, fd, 0); + char* addr = (char*)mmap(base, size, prot, flags, fd, offset); if (addr == MAP_FAILED) { warning("Failed mmap to file. (%s)", os::strerror(errno)); return NULL;