我们可以 memset 与 malloc 大小相同的大小吗?
ubuntu 10.04 LTS(2.6.32-38-generic) i686 - intel i7 - 16GB
我在 memset
特定内存量时遇到 map failed
错误。你有什么想法吗?附加信息是我可以 malloc
相同的大小。这是我的代码。
//Here I could malloc successfully
pdev->frame_buffer = (uint16_t *)malloc(3840000);
//Then, I got map_failed error here and message from compiler is "Invalid argument"
if((pdev->frame_buffer = (uint16_t *)mmap(0, 3840000, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0)) == MAP_FAILED){
perror("Error: cannot mmap frame buffer");
exit(1);
}
如果我 mmap
小于我上面尝试的大小,例如 mmap(0, 100,...)
,那么它会返回正确的地址。我不确定这个问题是否是因为尺寸的原因。
你知道为什么会这样吗?
ubuntu 10.04 LTS(2.6.32-38-generic) i686 - intel i7 - 16GB
I got a map failed
error to memset
certain amount of memory. Do you have any idea? Additional information is I could malloc
the same size. Here's my code.
//Here I could malloc successfully
pdev->frame_buffer = (uint16_t *)malloc(3840000);
//Then, I got map_failed error here and message from compiler is "Invalid argument"
if((pdev->frame_buffer = (uint16_t *)mmap(0, 3840000, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0)) == MAP_FAILED){
perror("Error: cannot mmap frame buffer");
exit(1);
}
If I mmap
smaller than the size I tried above like mmap(0, 100,...)
, then it returns right address. I'm not sure if this issue is because of the size.
Do you have any guess why it happend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 memset 实际上有一个相差一错误。您已 malloc'd 3,840,000 字节,但您的 meset 范围指定总共要设置 3,840,001 字节。参数应该是
Your memset actually has an off-by-one error. You've malloc'd 3,840,000 bytes, but your meset range specifies a total of 3,840,001 bytes to set. The args should be
您的代码示例根本不包含
memset
,仅包含malloc
和mmap
?我对为什么
mmap
失败的猜测是文件描述符引用了帧缓冲区(上下文表明是这样,从变量名frame_buffer
和文件描述符fb
),并且您有“分配大于帧缓冲区”、“分配驱动程序不喜欢的权限/标志组合”和“其他一些模糊原因”的组合。映射帧缓冲区或任何其他设备内存并不像映射普通内存或文件那么简单(嗯,有点是,但话又说回来,不是),可能有很多更明显和不太明显的事情这可能会失败的原因。
第一个显而易见的事情是,您当前的屏幕分辨率乘以每像素字节数加起来是否等于 3840000。如果没有,你就出界了。
Your code example does not contain
memset
at all, onlymalloc
andmmap
?My guess as to why
mmap
fails is that the file descriptor refers to the framebuffer (the context suggests so, both from the variable nameframe_buffer
and the file descriptorfb
), and you have a combination of "allocated larger than framebuffer", "allocated with a permission/flag combination that the driver does not like" and "some other obscure reason".Mapping the framebuffer or any other device memory is not as trivial as mapping normal memory or a file (well, it kind of is, but then again, not), there can be many more obvious and less obvious reasons why this might fail.
The first obvious thing to look for would be if your current screen resolution times bytes-per-pixel adds up to 3840000 at all. If it doesn't, you're out of bounds.