I was looking for a method of allocating memories on Linux which similar VirtualAlloc on Windows. Requirements are:
On Windows because lower limit of application address (lpMinimumApplicationAddress) we have (2) obvious right. From (1), (2) and system rules we also achieved (3).
Thanks for helping.
You want posix_memalign()
:
void *ptr;
int memalign_err = posix_memalign(&ptr, 1UL << 16, 1UL << 16);
if (memalign_err) {
fprintf(stderr, "posix_memalign: %s\n", strerror(memalign_err));
} else {
/* ptr is valid */
}
The first 1UL << 16
is the alignment, and the second is the size.
When you're done with the block you pass it to free()
.