lovehell
lovehell

Reputation: 169

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?

Upvotes: 4

Views: 980

Answers (1)

Acorn
Acorn

Reputation: 26066

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, e.g.:

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
);

Upvotes: 5

Related Questions