What is Linux or POSIX equivalent of VirtualAlloc with MEM_TOP_DOWN, if there's any?
Asked
Active
Viewed 1,981 times
1 Answers
3
POSIX does not seem to have anything when it comes to mmap
.
The GNU C Library version of mmap
(BSD libc also has these flags) extends its functionality though, with a MAP_FIXED
(allocate at specific address) and MAP_GROWSDOWN
(which is named similarly but MEM_TOP_DOWN
actually has nothing to do with this option). Both GNU and BSD's manpages discourage use of these functions, because of portability issues (you're bound to the specific C library, and not fully OS independent anymore).
You'd need to determine a way to find the topmost address. I suggest trying to allocate at the top, and moving the passed address down until it does succeed. The step size and "search" algorithm will depend on your need of precision and performance.

rubenvb
- 74,642
- 33
- 187
- 332
-
@frp Why? The functionality is there. You can use it. The warning is just that: a warning, telling you this flag is'nt POSIX. – rubenvb Mar 11 '16 at 12:33
-
It does basically the same, right. My task is solved. By "sad" I meant that just emulating VirtualAlloc this way would be quite slow. But taking in account the specific details of my task, it can easily be optimized quite a lot, so that's not such a big deal. – frp Mar 11 '16 at 13:08
-
@frp have you measured your "quite slow" claim? Seeing how most of these things are by default a lot zippier on non-Windows, I'd not be surprised that it was `VirtualAlloc` that was slow all along. But that's just a hunch. – rubenvb Mar 11 '16 at 13:30
-
Not really. I'll do some benchmarks. This assumption is based on another assumption, that VirtualAlloc is implemented in the efficient way: only one context switch, and using the knowledge of process' memory layout. Since there's no source code available, I can't say if that's how it works. – frp Mar 11 '16 at 13:41