Add MemoryMapFileTruncated function

Change-Id: I26d2a9fbac668cbac284bdfad0d7912db521dc70
Этот коммит содержится в:
Rahul Garg
2020-10-10 00:20:11 +00:00
родитель be66e29e94
Коммит 5ff3010e1b
3 изменённых файлов: 33 добавлений и 0 удалений
+4
Просмотреть файл
@@ -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);
+25
Просмотреть файл
@@ -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__)
+4
Просмотреть файл
@@ -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__