如何使用C语言中的"mmap"命令分配特定的内存区域? (Android NDK) [英] How do you allocate a specific area of memory using the 'mmap' command in C? (Android NDK)

查看:304
本文介绍了如何使用C语言中的"mmap"命令分配特定的内存区域? (Android NDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中使用"mmap"分配内存的特定区域的正确方法是什么?我已阅读/proc/self/maps确定该区域可用.

What is the proper way to allocate a specific region of memory using 'mmap' in C? I've read /proc/self/maps to determine that the area is available.

我尝试了以下操作,但是在尝试写入分配的内存时会崩溃:

I've tried the following, but it crashes when trying to write to the allocated memory:

// rdram is defined in header as:  #define rdram ((unsigned int *)0x80000000)
     printf( "rdram=0x%x", (int)rdram );
     printf( "munmapping" );
     munmap ((void*)0x80000000, 0x800000);
     printf( "mmapping" );
     if(mmap ((void*)0x80000000, 0x800000,
            PROT_READ | PROT_WRITE,
            MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
            -1, 0) <= 0)
     {
         printf( "mmap(0x80000000) failed" );
     }
     else
     {
         for (i=0; i<(0x800000/4); i++)
         {
             printf( "writing a zero at 0x%x", (0x80000000 + i) );
             rdram[i]=0;    // <<---------CRASH HERE--------<<
         }
         printf( "done writing zeros" );
      }

在我之前的问题(由于未明确陈述而被关闭)上,有人建议我无法正确检测到mmap何时失败,并且我应该使用posix_typed_mem_open而不是-1作为fildes参数.我以为我会尝试以下方法(请告诉我这是否有问题):

It was suggested on my earlier question (which got closed for not being stated clearly), that I am not properly detecting when mmap failed, and that I should use posix_typed_mem_open instead of -1 for the fildes parameter. I thought I would try the following (please let me know if there is something wrong with this):

 int m;
 int p;

 printf( "rdram=0x%x", (int)rdram );
 printf( "munmapping" );
 munmap ((void*)0x80000000, 0x800000);
 printf( "posix_typed_mem_open" );
 p = posix_typed_mem_open( "/memory/ram", O_RDWR, POSIX_TYPED_MEM_ALLOCATE_CONTIG );
 printf( "mmapping" );
 m = (int) mmap ((void*)0x80000000, 0x800000,
             PROT_READ | PROT_WRITE,
             MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
             p, 0);
 if( m == (int) MAP_FAILED )
 {
     printf( "mmap failed" )
 }
 else
 {
     for (i=0; i<(0x800000/4); i++)
     {
         printf( "writing a zero at 0x%x", (0x80000000 + i) );
         rdram[i]=0;
     }
     printf( "done writing zeros" );
 }

这是在我的测试仪之一的设备上运行的(我的设备上的内存映射不合适),所以我想让他运行它以查看什么样的输出之前,这是有道理的他得到了.

This is being run on one of one of my testers' devices (the memory mapping on my device is not suitable), so I'd like to know this makes sense before I have him run it to see what kind of output he gets.

推荐答案

您是否要映射虚拟物理内存?如果是虚拟的,那么您走在正确的道路上,但是您的第一种方法可能会遇到Android中的某些问题.类型化的内存对象不是必需的,但是,内核将mmap的第一个参数用作 hint . ENOMEM建议您达到设备上某种形式的限制.

Are you trying to map virtual or physical memory? If virtual, then you're on right track but might be hitting some issue in Android with your first approach. Typed memory objects are not required, however, the kernel takes the first argument to mmap as hint only. The ENOMEM suggests that you're hitting some form of limit set on your device, though.

如果您的目标是分配物理内存,那么您需要做的是/map/mem(需要root或更改它的权限),并将偏移设置为您想要的地址.

If your goal was allocation of physical memory, what you need to do is to mmap /dev/mem (requires root, or changing permissions on it) with the offset set to address you want.

这篇关于如何使用C语言中的"mmap"命令分配特定的内存区域? (Android NDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆