Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you allocate a specific area of memory using the 'mmap' command in C? (Android NDK)
    primarykey
    data
    text
    <p>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.</p> <p>I've tried the following, but it crashes when trying to write to the allocated memory:</p> <pre><code>// 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) &lt;= 0) { printf( "mmap(0x80000000) failed" ); } else { for (i=0; i&lt;(0x800000/4); i++) { printf( "writing a zero at 0x%x", (0x80000000 + i) ); rdram[i]=0; // &lt;&lt;---------CRASH HERE--------&lt;&lt; } printf( "done writing zeros" ); } </code></pre> <p>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):</p> <pre><code> 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&lt;(0x800000/4); i++) { printf( "writing a zero at 0x%x", (0x80000000 + i) ); rdram[i]=0; } printf( "done writing zeros" ); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload