c++ - Force memory allocation to allocate from higher address (>4GB) on 64-bit Linux -


here're want do: have library built 64-bit linux. created application linking library. want make sure when run application, memory allocated library in higher location (>4gb).

on windows, user can force allocations allocate higher addresses before lower addresses testing purposes, specify mem_top_down when calling virtualalloc or set following registry value 0x100000:

hkey_local_machine\system\currentcontrolset\control\session manager\memory management\allocationpreference

i wonder if there's similar strategy on linux. understand linux memory management different window, found clue such using mmap() or linker scripts. haven't been able achieve goal. provide more information?

#include <sys/mman.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>  #define handle_error(msg) \   { perror(msg); exit(exit_failure); } while (0)  int main() {   void *addr1=0, *addr2=0;   long sz = sysconf(_sc_page_size); // page size   size_t length = sz*1000*1000; // 1,000,000 pages   int fd = -1;    printf("page size = %ld\n", sz);    // find available address   int *p = (int*)malloc(sizeof(int));   long start = (long)p + sizeof(int);   free(p); // free 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);    // 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 memory still available   p = (int*)malloc(sizeof(int)*10);   printf("allocated address: 0x%tx\n", p);    return 0; } 

output:

page size = 4096 start = 0x1d77000 first map: 1d77000 second map: 0x7f5f26c2f000 allocated address: 0x1d76030 

i don't understand why want that (avoiding mmap giving address in first 4 gigabytes).

hoever, could, in program -e.g. start of main, or constructor function- call mmap(2) map_fixed , map_noreserve on several memory segments achieve goal; ensure address space below 4g "filled" -either pre-existing segments of programs, or such calls mmap.

however, library (which indirectly dlopen-ed) started after program.

once first 4 gigabytes used in address spaces, ordinary mmap calls (those done malloc example) go outside.

of course, should mmap at time before first malloc (which call mmap or sbrk); , should take care of existing memory segments (perhaps can them parsing /proc/self/maps), because need avoid them in mmap map_fixed|map_noreserve.

and define own malloc. perhaps mmap map_noreserve huge region (e.g. terabyte), , have own malloc using address inside (by mmap-ing again there).

i think trying solve wrong problem. doing suggest tricky... see no valid reason avoid addresses in first 4 gbytes.

btw, tool find memory leaks on linux valgrind.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -