这就是我想要做的:我有一个为 64 位 Linux 构建的库。然后我创建了一个链接该库的应用程序。我想确保在运行应用程序时,从库分配的内存始终位于较高的位置(>4GB)。
在 Windows 上,出于测试目的,用户可以强制分配先从高地址分配到低地址,在调用 VirtualAlloc 时指定 MEM_TOP_DOWN 或将以下注册表值设置为 0x100000:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management\AllocationPreference
我想知道Linux上是否有类似的策略。我了解 Linux 内存管理与 Window 不同,但我发现了一些线索,例如使用 mmap() 或链接器脚本。但我一直未能实现目标。有人可以提供更多信息吗?
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main()
{
void *addr1=0, *addr2=0;
long sz = sysconf(_SC_PAGE_SIZE); // get page size
size_t length = sz*1000*1000; // 1,000,000 pages
int fd = -1;
printf("page size = %ld\n", sz);
// find some available address
int *p = (int*)malloc(sizeof(int));
long start = (long)p + sizeof(int);
free(p); // free it anyway
start += (sz-(start % sz)); // page alignment
printf("start = 0x%lx\n", start);
// mmap fixed
addr1 = mmap((void*)start, length, PROT_NONE,
MAP_PRIVATE|MAP_NORESERVE|MAP_ANONYMOUS|MAP_FIXED, fd, 0);
if (addr1 == MAP_FAILED)
handle_error("mmap");
printf("first map: %tx\n", addr1);
//msync(addr1, length, 0);
// another mmap
addr2 = mmap(NULL, sz*10, PROT_NONE,
MAP_ANONYMOUS|MAP_PRIVATE, fd, 0);
if (addr2 == MAP_FAILED)
handle_error("mmap");
printf("second map: 0x%tx\n", addr2);
// test whether the memory is still available
p = (int*)malloc(sizeof(int)*10);
printf("allocated address: 0x%tx\n", p);
return 0;
}
输出:
page size = 4096
start = 0x1d77000
first map: 1d77000
second map: 0x7f5f26c2f000
allocated address: 0x1d76030