我们可以 memset 与 malloc 大小相同的大小吗?

发布于 2025-01-04 22:39:35 字数 714 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2025-01-11 22:39:35

您的 memset 实际上有一个相差一错误。您已 malloc'd 3,840,000 字节,但您的 meset 范围指定总共要设置 3,840,001 字节。参数应该是

if((pdev->frame_buffer = (uint16_t *)mmap(0, 3839999, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0)) == MAP_FAILED){
                                             ^^^^^^^---note the change.

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

if((pdev->frame_buffer = (uint16_t *)mmap(0, 3839999, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0)) == MAP_FAILED){
                                             ^^^^^^^---note the change.
給妳壹絲溫柔 2025-01-11 22:39:35

您的代码示例根本不包含 memset,仅包含 mallocmmap

我对为什么 mmap 失败的猜测是文件描述符引用了帧缓冲区(上下文表明是这样,从变量名 frame_buffer 和文件描述符fb),并且您有“分配大于帧缓冲区”、“分配驱动程序不喜欢的权限/标志组合”和“其他一些模糊原因”的组合。

映射帧缓冲区或任何其他设备内存并不像映射普通内存或文件那么简单(嗯,有点是,但话又说回来,不是),可能有很多更明显和不太明显的事情这可能会失败的原因。

第一个显而易见的事情是,您当前的屏幕分辨率乘以每像素字节数加起来是否等于 3840000。如果没有,你就出界了。

Your code example does not contain memset at all, only malloc and mmap?

My guess as to why mmap fails is that the file descriptor refers to the framebuffer (the context suggests so, both from the variable name frame_buffer and the file descriptor fb), 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文