我编写了一个简单的Android本机函数,该函数获取文件名和更多参数,并通过映射(mmap)内存来读取文件.
因为它是mmap,所以我真的不需要调用“ read()”,所以我只是从mmap()返回的地址使用memcpy().
但是,在某个地方我得到了SIGSEGV,可能是因为我试图访问不允许的内存.但是我不明白为什么,我已经要求映射所有文件的内存!
我正在附上我的代码和出现的错误:
编辑
我修复了无限循环,但在读取了25001984个字节后仍然得到SIGSEGV.
该函数适用于这些参数:
jn_bytes = 100,000,000
jbuffer_size = 8192
jshared = jpopulate = jadvice = 0
void Java_com_def_benchmark_Benchmark_testMmapRead(JNIEnv* env, jobject javaThis,
jstring jfile_name, unsigned int jn_bytes, unsigned int jbuffer_size, jboolean jshared, jboolean jpopulate, jint jadvice) {
const char *file_name = env->GetStringUTFChars(jfile_name, 0);
/* *** start count *** */
int fd = open(file_name, O_RDONLY);
//get the size of the file
size_t length = lseek(fd, 0L, SEEK_END);
lseek(fd, 0L, SEEK_SET);
length = length>jn_bytes?jn_bytes:length;
// man 2 mmap: MAP_POPULATE is only supported for private mappings since Linux 2.6.23
int flags = 0;
if (jshared) flags |= MAP_SHARED; else flags |= MAP_PRIVATE;
if(jpopulate) flags |= MAP_POPULATE;
//int flags = MAP_PRIVATE;
int * addr = reinterpret_cast<int *>(mmap(NULL, length , PROT_READ, flags , fd, 0));
if (addr == MAP_FAILED) {
__android_log_write(ANDROID_LOG_ERROR, "NDK_FOO_TAG", strerror(errno));
return;
}
int * initaddr = addr;
if(jadvice > 0)
madvise(addr,length,jadvice==1?(MADV_SEQUENTIAL|MADV_WILLNEED):(MADV_DONTNEED));
close(fd);
char buffer[jbuffer_size];
void *ret_val = buffer;
int read_length = length;
while(ret_val == buffer || read_length<jbuffer_size) {
/*****GETTING SIGSEGV SOMWHERE HERE IN THE WHILE************/
ret_val = memcpy(buffer, addr,jbuffer_size);
addr+=jbuffer_size;
read_length -= jbuffer_size;
}
munmap(initaddr,length);
/* stop count */
env->ReleaseStringUTFChars(jfile_name, file_name);
}
和错误日志:
15736^done
(gdb)
15737 info signal SIGSEGV
&"info signal SIGSEGV\n"
~"Signal Stop\tPrint\tPass to program\tDescription\n"
~"SIGSEGV Yes\tYes\tYes\t\tSegmentation fault\n"
15737^done
(gdb)
15738-stack-list-arguments 0 0 0
15738^done,stack-args=[frame={level="0",args=[]}]
(gdb)
15739-stack-list-locals 0
15739^done,locals=[]
(gdb)
解决方法:
这里有个大问题:
addr+=jbuffer_size;
您通过sizeof(int)* jbuffer_size个字节增加了addr,而您只想将其增加jbuffer_size个字节.
我的猜测是系统上的sizeof(int)为4,因此您在整个循环的大约25%时间内崩溃,因为每次迭代时addr的增量都增加了4倍.