我們有許多高速緩存建立在32位機器上,我們現在必須在64位環境中讀取這些高速緩存。 當我們想要打開讀取緩存文件時,會出現分段錯誤。在64位環境中讀取32位高速緩存
這將需要幾個星期來重現緩存,所以我想知道如何仍然可以在64位機器上處理我們的32位緩存文件。
下面是我們用來讀取和寫入我們的緩存代碼:
bool IntArray::fload(const char* fname, long offset, long _size){
long size = _size * sizeof(long);
long fd = open(fname, O_RDONLY);
if (fd >0 ){
struct stat file_status;
if (stat(fname, &file_status) == 0){
if (offset < 0 || offset > file_status.st_size){
std::__throw_out_of_range("offset out of range");
return false;
}
if (size + offset > file_status.st_size){
std::__throw_out_of_range("read size out of range");
return false;
}
void *map = mmap(NULL, file_status.st_size, PROT_READ, MAP_SHARED, fd, offset);
if (map == MAP_FAILED) {
close(fd);
std::__throw_runtime_error("Error mmapping the file");
return false;
}
this->resize(_size);
memcpy(this->values, map, size);
if (munmap(map, file_status.st_size) == -1) {
close(fd);
std::__throw_runtime_error("Error un-mmapping the file");
return false;
/* Decide here whether to close(fd) and exit() or not. Depends... */
}
close(fd);
return true;
}
}
return false;
}
bool IntArray::fsave(const char* fname){
long fd = open(fname, O_WRONLY | O_CREAT, 0644); //O_TRUNC
if (fd >0 ){
long size = this->_size * sizeof(long);
long r = write(fd,this->values,size);
close(fd);
if (r != size){
std::__throw_runtime_error("Error writing the file");
}
return true;
}
return false;
}
這些緩存包含什麼? – DarkDust 2011-03-11 08:37:17
如果您提供了正在讀取的實際數據類型以及您正在處理的平臺,那將會很有趣。在大約64個平臺中,「long」是32位,而在其他平臺中則是64位,這可以解釋這個問題。作爲一個方面說明,你不應該直接調用以'__'開頭的方法,因爲這些方法是爲實現保留的,並且可以隨時更改。如果你想拋出一個'std :: runtime_error',就這樣做:'throw std :: runtime_error(「my_error」)' – 2011-03-11 09:56:22