By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Is it possible to put a class object in share memory?

P: n/a
Greeting,

My platform is Solaris 2.8. My question is that if there is a simple way to
put a class object in share memory, so that multiple process can use this
object.

Thanks in advance!
Evan
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"music4" <mu****@163.net> wrote in message news:cg********@netnews.proxy.lucent.com...
Greeting,

My platform is Solaris 2.8. My question is that if there is a simple way to
put a class object in share memory, so that multiple process can use this
object.


Look up functions called shm* in the Solaris docs.

However, it's actually NOT easy for any but the most trivial classes
to share memories as there are pointers both explicit and hidden inside
class objects that may not be valid in different processes.

Jul 22 '05 #2

P: n/a
"music4" <mu****@163.net> wrote in message news:<cg********@netnews.proxy.lucent.com>...
Greeting,

My platform is Solaris 2.8. My question is that if there is a simple way to
put a class object in share memory, so that multiple process can use this
object.


The usual way is to allocate the shared memory, then use the placement
new operator to construct an object there. If you have a type of
object that is always shared, you can instead overload its operator
new to allocate shared memory.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3

P: n/a
"music4" <mu****@163.net> wrote in message news:<cg********@netnews.proxy.lucent.com>...
Greeting,

My platform is Solaris 2.8. My question is that if there is a simple way to
put a class object in share memory, so that multiple process can use this
object.

Thanks in advance!
Evan


The direct way is to allocate[1] a chunk of that shared memory for
your object, and then use placement new[2] to initialise the instance
in that memory.

The user-friendly-but-more-work way[3] is to write an operator new for
the class which takes as an extra argument the shared memory
'arena'[4] you want the object created in.
Footnotes:
[1] - you need some allocator for your shared memory which provides
the equivalent of new and delete functions for allocating/releasing
chunks of that shared segment. This might be entirely trivial if you
only ever want an array of one type of object, but it could be a
full-fledged heap implementation.

[2] - see the C++ FAQ or your favourite reference if you haven't used
this - it initialises an object in some raw memory.

[3] - this is only really friendlier if you're allocating these things
all over the place, and don't want to change the client code (much).
If you're using a factory function, there isn't much point doing this.

[4] - this could just be a pointer to the start of your shared memory
segment if your allocation routine is simple and well-known, or it
could be a pointer to an allocator object (see [1] again) which
provides a known allocation interface, where some allocator instance
is associated with a particular shm segment. See the C++ FAQ (again) -
section 11.14.
Caveats:
Note that if your objects aren't POD, you'll have to make sure that
all their subobjects allocate from the same shared memory segment.
For things like std::string, that means the typedef breaks, since you
need to pass std::basic_string a different allocator.

Also, none of this will work for objects having virtual functions,
because the vtable pointer will hold an address specific to the
process which created the object.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.