我们有许多高速缓存建立在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