//pszFilename must have room for at least MaxPath+1 characters static inline bool get_file_name_from_handle_function (void * hFile, wchar_t *pszFilename, std::size_t length, std::size_t &out_length) { if(length <= MaxPath) { return false; } void *hiPSAPI = load_library("PSAPI.DLL"); if (0 == hiPSAPI) return 0; class library_unloader { void *lib_; public: library_unloader(void *module) : lib_(module) {} ~library_unloader() { free_library(lib_); } } unloader(hiPSAPI); // Pointer to function getMappedFileName() in PSAPI.DLL GetMappedFileName_t pfGMFN = (GetMappedFileName_t)get_proc_address(hiPSAPI, "GetMappedFileNameW"); if (! pfGMFN) { return 0; // Failed: unexpected error } bool bSuccess = false; // Create a file mapping object. void * hFileMap = create_file_mapping(hFile, page_readonly, 0, 1, 0); if(hFileMap) { // Create a file mapping to get the file name. void* pMem = map_view_of_file_ex(hFileMap, file_map_read, 0, 0, 1, 0); if (pMem) { out_length = pfGMFN(get_current_process(), pMem, pszFilename, MaxPath); if(out_length) { bSuccess = true; } unmap_view_of_file(pMem); } close_handle(hFileMap); } return(bSuccess); }
inline void osmium::util::MemoryMapping::resize(size_t new_size) { unmap(); m_size = new_size; resize_fd(m_fd); m_handle = create_file_mapping(); if (!m_handle) { throw std::system_error(GetLastError(), std::system_category(), "CreateFileMapping failed"); } m_addr = map_view_of_file(); if (!is_valid()) { throw std::system_error(GetLastError(), std::system_category(), "MapViewOfFile failed"); } }
inline osmium::util::MemoryMapping::MemoryMapping(size_t size, MemoryMapping::mapping_mode mode, int fd, off_t offset) : m_size(check_size(size)), m_offset(offset), m_fd(resize_fd(fd)), m_mapping_mode(mode), m_handle(create_file_mapping()), m_addr(nullptr) { if (!m_handle) { throw std::system_error(GetLastError(), std::system_category(), "CreateFileMapping failed"); } m_addr = map_view_of_file(); if (!is_valid()) { throw std::system_error(GetLastError(), std::system_category(), "MapViewOfFile failed"); } }