2011-07-03 30 views
2

我設法通過將數據塊讀入內存並使用下面顯示的函數交換big-endian整數來解析大型二進制文件(〜8Gb)。但是,我試圖通過使用Boost Memory-Mapped files來獲得更多性能,但我無法使用endian_swap函數,因爲該文件是以只讀模式打開的。有沒有有效的方法來交換字節而不寫入原始文件?如果不是,性能會受I/O開銷的影響?在內存映射文件中交換字節的有效方法

inline void endian_swap(unsigned short int& x) 
{ 
    x = (x>>8) | 
    (x<<8); 
} 
inline void endian_swap(unsigned int& x) 
{ 
    x = (x>>24) | 
    ((x<<8) & 0x00FF0000) | 
    ((x>>8) & 0x0000FF00) | 
    (x<<24); 
} 
inline void endian_swap(unsigned long long int& x) 
{ 
    x = (((unsigned long long int)(x) << 56) | \ 
     (((unsigned long long int)(x) << 40) & 0xff000000000000ULL) | \ 
     (((unsigned long long int)(x) << 24) & 0xff0000000000ULL) | \ 
     (((unsigned long long int)(x) << 8) & 0xff00000000ULL) | \ 
     (((unsigned long long int)(x) >> 8) & 0xff000000ULL) | \ 
     (((unsigned long long int)(x) >> 24) & 0xff0000ULL) | \ 
     (((unsigned long long int)(x) >> 40) & 0xff00ULL) | \ 
     ((unsigned long long int)(x) >> 56)); 
} 

該代碼被發現在這個article。 非常感謝您的寶貴時間

回答

2

至少底層操作系統的支持您需要的行爲:

MAP_PRIVATE 
       Create a private copy-on-write mapping. Updates 
       to the mapping are not visible to other processes 
       mapping the same file, and are not carried through 
       to the underlying file. It is unspecified whether 
       changes made to the file after the mmap() call are 
       visible in the mapped region. 

priv標記看起來轉化爲MAP_PRIVATE

void* data = 
    ::BOOST_IOSTREAMS_FD_MMAP( 
     const_cast<char*>(p.hint), 
     size_, 
     readonly ? PROT_READ : (PROT_READ | PROT_WRITE), 
     priv ? MAP_PRIVATE : MAP_SHARED, 
     handle_, 
     p.offset); 
if (data == MAP_FAILED) 
    cleanup_and_throw("failed mapping file"); 
+0

我沒有」實現「openmode標誌」功能,因爲我在1.44版本中使用1.42版本。謝謝! – Emer

+0

@Emer,啊!這些小細節讓世界變得更加重要。當我一起來看網站的源代碼時,很明顯它不是一個更新的功能。 :) – sarnold