placement new and further

D

Dima Stopel

Dear comp.lang.c++ members,

I need to write an application that shares some object via shared memory
with other applications.

I use Memory-Mapped Files technique in order to share memory.

Thus, in each process I have the *same* region of memory that is shared.

All I need now is to put my object into this region.

I use 'placement new' technique in order to put the object and all objects
it creates in its constructor into this region.

However, let's say that the object creates a hash_map in the constuctor. The
hash_map is placed inside the region as needed, however if hash_map also
creates some objects in *it's* constructor they won't be placed into the
region and I have no control over it. Thus, the other applications which
will try to call methods of my object will fail because those objects that
hash_map creates are not inside the shared region.

How can I ensure that the *whole* object with all it's sub-objects will be
inside the shared region? Is it possible?

Thank you in advance,
 
A

Alf P. Steinbach

* Dima Stopel:
Dear comp.lang.c++ members,

I need to write an application that shares some object via shared memory
with other applications.

I use Memory-Mapped Files technique in order to share memory.

Thus, in each process I have the *same* region of memory that is shared.

All I need now is to put my object into this region.

I use 'placement new' technique in order to put the object and all
objects it creates in its constructor into this region.

However, let's say that the object creates a hash_map in the constuctor.
The hash_map is placed inside the region as needed, however if hash_map
also creates some objects in *it's* constructor they won't be placed
into the region and I have no control over it. Thus, the other
applications which will try to call methods of my object will fail
because those objects that hash_map creates are not inside the shared
region.

How can I ensure that the *whole* object with all it's sub-objects will
be inside the shared region? Is it possible?

In general no.

You can take control over dynamic allocation.

However, you can't take control over static allocation.

Instead of using shared memory directly, consider other means
inter-process communication.

In general those are abstractions on top of shared memory, but limited
to more useful and safe functionality, such as Windows mailslots, files,
RPC, Windows message queues, Windows DCOM/Automation, SOAP, etc.; it's
very very rare that raw shared memory is really called for, unless
you're implementing such an abstraction yourself.
 
V

Victor Bazarov

Dima said:
I need to write an application that shares some object via shared
memory with other applications.

I use Memory-Mapped Files technique in order to share memory.

Thus, in each process I have the *same* region of memory that is
shared.
All I need now is to put my object into this region.

I use 'placement new' technique in order to put the object and all
objects it creates in its constructor into this region.

However, let's say that the object creates a hash_map in the
constuctor. The hash_map is placed inside the region as needed,
however if hash_map also creates some objects in *it's* constructor
they won't be placed into the region and I have no control over it.
Thus, the other applications which will try to call methods of my
object will fail because those objects that hash_map creates are not
inside the shared region.
How can I ensure that the *whole* object with all it's sub-objects
will be inside the shared region? Is it possible?

I guess you're using the terms "whole object" and "sub-objects" too
freely. Whatever a hash_map creates are not its "sub-objects". That
said, I believe you need to look into the Allocator template argument
any standard container has. It is unclear from your posting whether
you already know about those.

V
 
D

Dima Stopel

You are right.

I'm not very familiar with the allocator specification.

However, does the structure (as hash_map) requests the memory from the
allocator for *any* object it creates (such as different helper objects it
creates during the initialization), or does it requests memory *only* for
inserting pairs into the structure ?

If it is the latter then it is not very suitable for me.

Thanks,
 
J

James Kanze

Be very, very careful about this. In most cases, if you need a
constructor, you can't put the object in shared memory.
I'm not very familiar with the allocator specification.
However, does the structure (as hash_map) requests the memory
from the allocator for *any* object it creates (such as
different helper objects it creates during the
initialization), or does it requests memory *only* for
inserting pairs into the structure ?

The hash_map object requests all dynamic memory it uses using
the allocator. Some of the objects it creates, however, may
have their own constructors, which request memory using their
allocators. Thus, a hash_map< std::string, ...,
SharedMemAllocator > will not work, because the strings used as
keys will not use the shared allocator.

As a general rule, I'd go the other route, and try to design the
objects in shared memory so that they are POD's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top