diff --git a/rocclr/os/os.hpp b/rocclr/os/os.hpp index 82879dee67..d7b958e847 100644 --- a/rocclr/os/os.hpp +++ b/rocclr/os/os.hpp @@ -120,6 +120,10 @@ class Os : AllStatic { const void** mmap_ptr); // Given a valid file name, returns mmapped memory with the mapped size. static bool MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size); + + // Given a valid file name amd mapped size, returns ftruncated mmaped memory + static bool MemoryMapFileTruncated(const char* fname, const void** mmap_ptr, size_t mmap_size); + // Given a valid mmaped ptr with correct size, unmaps the ptr from memory static bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size); diff --git a/rocclr/os/os_posix.cpp b/rocclr/os/os_posix.cpp index 91d08bc3d0..1210e291f1 100644 --- a/rocclr/os/os_posix.cpp +++ b/rocclr/os/os_posix.cpp @@ -813,6 +813,31 @@ bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_si return true; } +bool Os::MemoryMapFileTruncated(const char* fname, const void** mmap_ptr, size_t mmap_size) { + if ((mmap_ptr == nullptr)) { + return false; + } + + struct stat stat_buf; + int fd = shm_open(fname, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); + if (fd < 0 ) { + return false; + } + + if (ftruncate(fd, mmap_size) != 0) { + return false; + } + *mmap_ptr = mmap(NULL, mmap_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + + close(fd); + + if (*mmap_ptr == nullptr) { + return false; + } + + return true; +} + } // namespace amd #endif // !defined(_WIN32) && !defined(__CYGWIN__) diff --git a/rocclr/os/os_win32.cpp b/rocclr/os/os_win32.cpp index 53abbb2d89..f176231bc0 100644 --- a/rocclr/os/os_win32.cpp +++ b/rocclr/os/os_win32.cpp @@ -911,6 +911,10 @@ bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_si return true; } +bool Os::MemoryMapFileTruncated(const char* fname, const void** mmap_ptr, size_t mmap_size) { + //TODO: fix with proper implementation + return false; +} } // namespace amd #endif // _WIN32 || __CYGWIN__