Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Win32) Heap shared between processes

Is it possible to create a memory pool shared between processes (for example from CreateFileMapping with INVALID_HANDLE_VALUE in Win32) and then use that chunk of memory as a heap ?

I would like the new/delete operators (or malloc/free) to allocate/free memory from that chunk (and do their usual heap management on it).

Thanks.

EDIT: My real problem is simple: for now I use the new operator to allocate various large series of structures normally, but I want these structures to persist after the process closes. So I would like to create another process whose almost only function is to hold the memory when the main process closes.

For that, I would create a chunk of shared memory with CreateFileMapping in the auxiliary process, and, in the main process, tell those new/delete used to allocate the structures to use that chunk instead of the process's heap.

To recover the structures after the main processes is relaunched, I would add a fixed amount to all the pointers, reflecting the different origins of the mapped view returned by MapViewOfFile.

like image 757
SemMike Avatar asked Dec 08 '25 13:12

SemMike


1 Answers

Thanks to the people who commented.

Answering my own question:

I gave up on making the shared memory a heap, I simply save a fixed snapshot of the structures to the shared memory allocated by the other process with CreateFileMapping. I just do the opposite when the process is relaunched, using OpenFileMapping and MapViewOfFile.

like image 57
SemMike Avatar answered Dec 10 '25 19:12

SemMike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!