简体   繁体   中英

How to allocate buffer within 32-bit address space?

I would like to allocate a buffer within 32-bit address space on 64-bit ARM. In other words, I would like to ensure that my buffer is bound to the lower 32-bit address space. Do you know a nice C function which does that?

There is no C standard function to do so. However, since you tagged the question as Linux, take a look at mmap(2) and the MAP_ANONYMOUS and MAP_32BIT flags, eg:

mmap(
    0, 1,
    PROT_READ | PROT_WRITE,
    MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT,
    -1, 0
);

Another option is passing an explicit address in the lower 32-bit address space using the MAP_FIXED flag:

mmap(
    (void *)0x10000, 1,
    PROT_READ | PROT_WRITE,
    MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
    -1, 0
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM