Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating specific address in Linux

I would like to allocate a memory in Linux in process at a specific address. Actually I would like to do something like : I will have number of process. Each process will call an initialization function in a library (written by me) which will allocate some memory in address space of the process (which will store process related information). This will be done by each process

Once this memory is allocated, latter the program will call other function in the library. Now these function would like to access the memory allocated (containing process related information) by the first function.

The problem is that i cannot store the address of the memory allocated in the process address space in library (not even in static pointer as there are number of process) and i don't even want user program to store that address. I just don't want user program to know that there is memory allocated by library in their address space. Library function will be abstraction for them and they have to just use them.

Is it possible to to over come this problem. I was thinking like, whenever any process calls initialization function of library which allocates memory , the memory always gets allocated at same address(say 10000) in all the process irrespective of all other things .

So that any library function which wants to access that memory can easily do by : char *p=10000;

and then access, which will be access into the address space of the process which called the library function.

like image 731
app Avatar asked Dec 21 '25 07:12

app


1 Answers

Not 100% I got what you are aiming for, but if you want to map memory into a specific set address you can use the MAP_FIXED flag to mmap():

"When MAP_FIXED is set in the flags argument, the implementation is informed that the value of pa shall be addr, exactly. If MAP_FIXED is set, mmap() may return MAP_FAILED and set errno to [EINVAL]. If a MAP_FIXED request is successful, the mapping established by mmap() replaces any previous mappings for the process' pages in the range [pa,pa+len)."

See mmap man page: http://linux.die.net/man/3/mmap

like image 126
gby Avatar answered Dec 23 '25 21:12

gby