Linux specific address allocation

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

Once this memory is allocated, the last program will call another function in the library. Now this function would like to access the allocated memory (containing information about the process) by the first function.

The problem is that I can’t save the address of the memory allocated in the address space of the process in the library (not even in the static pointer, because there are a number of processes), and I don’t even want the user program to save this address. I just don’t want the user program to know that the library allocated by the library is allocated in their address space. The library function will be an abstraction for them, and they should just use them.

Is it possible to overcome this problem. I thought that whenever a process calls a library initialization function that allocates memory, memory is always allocated to the same address (say, 10000) throughout the process, regardless of all the others.

Thus, any library function that wants to access this memory can easily execute: char *p=10000;

and then access, which will have access to the address space of a process called a library function.

0
source share
2 answers

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

"When the MAP_FIXED parameter is set in the flags argument, the implementation says that pa must be equal to addr. If MAP_FIXED is set, mmap () can return MAP_FAILED and set errno to [EINVAL]. If the MAP_FIXED request is successful, the mapping set by mmap () , replaces any previous mappings for process pages in the range [pa, pa + len). "

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

+4
source

Your question does not make sense. Since you have formulated your question, the global variable in your library will work fine.

Perhaps you say: "one process can load / unload your library, and then load the library again and get the address on the second load." Maybe you say: "There are 2 libraries, and each library needs one address." Just. Use setenv () and getenv (). They will store / retrieve everything that can be represented as a string in a variable having PROCESS WIDE SCOPE .... ie all libraries can see the same environment variables. Just convert your address to a string (itoa), use setenv () to save it in an environment variable named "__SuperSecretGlobalAddress__", and then use getenv () to retrieve the value.

When your program starts, a copy of the shell environment is created for your process. getenv and setenv and modify this copy. You cannot change the shell environment using these functions.

See this post .

0
source

Source: https://habr.com/ru/post/988234/


All Articles