Moving an STL Object Between Processes

I know this is weird, but I just have fun.

I am trying to pass std::map (created using the placement of a new one in a fixed area of ​​memory) between two processes through a socket between two machines: Master and Slave . The map I use has this typedef :

  // A vector of Page objects typedef std::vector<Page*, PageTableAllocator<Page*> > PageVectorType; // A mapping of binary 'ip address' to a PageVector typedef std::map<uint32_t, PageVectorType*, std::less<uint32_t>, PageTableAllocator<std::pair<uint32_t, PageVectorType*> > > PageTableType; 

The PageTableAllocator<T> class is responsible for allocating any memory that may / need STL containers to a fixed location in memory. For example, all Page objects and internal STL structures are created in this fixed memory area. This ensures that both the std::map object and the allocator are placed in a fixed area of ​​memory. I used GDB to make sure that the card and allocator behave correctly (all used memory is in a fixed area, nothing happens in the normal application heap).

Assuming Master starts up, initializes all its STL structures and a special memory area, the following happens. Slave launches, prints its version of the page table, then searches for Master . Slave finds the master, deletes its version of the page table, copies the Master version of the page table (and the special memory area), and successfully prints its version of the Master page table. From what I did in GDB, I can perform many read-only operations.

When trying to add errors to the newly copied PageTableType Slave object in the allocator void construct (pointer p, const T& value) method. The value passed as p indicates the already allocated memory area (according to the version of Master std::map ).

I don’t know anything about the structure of C ++ objects, but I assume that the state of the object from the Slave version of PageTableType should be freezing even after I replaced all the memory with PageTableType and its allocator is used. My question is that this is a serious problem. Does C ++ support some kind of state of the object outside of the memory area that the object instantiates din?

All objects used on the map are not PODs. The same is true for the dispenser.

+4
source share
1 answer

To answer your specific question:

Does C ++ support any state of the object outside the memory area in which the object was created?

The answer is no. There are no other data structures configured to β€œtrack” objects or the like. C ++ uses an explicit memory allocation model, so if you decide to be responsible for allocation and release, then you have full control.

I suspect that something is wrong in your code, but since you think the code is correct, you are inventing another reason why your code may fail, and following this path. I would step back and carefully study everything how your code works right now, and see if you can solve the problem. Although the STL classes are complex (especially std::map ), they are ultimately just code, and there is no hidden magic.

+3
source

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


All Articles