teodron
teodron

Reputation: 1438

Memory mapping, virtual and physical memory in C++

I am trying to solve the following issue: having a custom data container that manages a generic type, I need to allow for other application components to retrieve the container's internal pointer and use it as if it were a simple T* array region (without treating it as a more intelligent array holder). The problem is that this memory is, in a very special case, moved somewhere else and erased. So there are a plethora of components that are aware of the old data pointer and will use that one to access their required information.

The setup looks, pseudo-codeish, something like this:

container<T>
{ 
  T* ptr;
  public:
  ContainerInterfaceCode..
}

Hypothesis:

T* ptr is a pseudo-address (may I call it "virtual"?) which is mapped in a physical space A.

When an event is risen, T* ptr's mapping will be set for another physical space, B.

Any component that uses T* ptr is then oblivious of the change of physical location, "thinking" its data is stored at that virtual address.

Conclusion:

I would therefore like to know whether there is a mechanism involving memory mapping (virtual to physical) that will allow to juggle with the mapping of the T* ptr, thus leaving other application components untouched. Simply put, T* ptr should point to a memory region that gets mapped in a certain part, and, upon request, that same pointer will be mapped in another place (where the underlying data is to be copied for consistency). This must allow seamless transitions.

Note: I can't use wrappers, smart pointers, handles, etc. for the simple fact that it means modifying a huge codebase just for one, rather minor, modification.

As I haven't found enough resources dealing with this scenario, can anyone, perhaps, present a short webography with some relevant reading material on the subject?

Upvotes: 1

Views: 1054

Answers (1)

cloud
cloud

Reputation: 1091

In linux you can used the shared memory.The shared memory is a mechanism which allow two process access a same area of memory, it is a kind of IPC method. You can find some more details here http://en.wikipedia.org/wiki/Shared_memory.

Upvotes: 1

Related Questions